viewpoint_cdp/protocol/tracing/
mod.rs

1//! Tracing domain types.
2//!
3//! The Tracing domain allows recording a trace of the browser activity.
4
5use serde::{Deserialize, Serialize};
6
7/// Transfer mode for tracing data.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10#[derive(Default)]
11pub enum TransferMode {
12    /// Report trace events via `Tracing.dataCollected` events.
13    #[default]
14    ReportEvents,
15    /// Return trace data as a stream.
16    ReturnAsStream,
17}
18
19/// Stream format for trace data.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
21#[serde(rename_all = "lowercase")]
22#[derive(Default)]
23pub enum StreamFormat {
24    /// JSON format.
25    #[default]
26    Json,
27    /// Protocol buffer format.
28    Proto,
29}
30
31/// Stream compression type.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
33#[serde(rename_all = "lowercase")]
34#[derive(Default)]
35pub enum StreamCompression {
36    /// No compression.
37    #[default]
38    None,
39    /// Gzip compression.
40    Gzip,
41}
42
43/// Tracing backend type.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(rename_all = "lowercase")]
46#[derive(Default)]
47pub enum TracingBackend {
48    /// Auto-select backend.
49    #[default]
50    Auto,
51    /// Use Chrome tracing.
52    Chrome,
53    /// Use system tracing.
54    System,
55}
56
57// ============================================================================
58// Tracing.start
59// ============================================================================
60
61/// Parameters for Tracing.start.
62#[derive(Debug, Clone, Serialize, Default)]
63#[serde(rename_all = "camelCase")]
64pub struct StartParams {
65    /// Category/tag filter as a string.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub categories: Option<String>,
68    /// Tracing options.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub options: Option<String>,
71    /// Buffer size in kilobytes.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub buffer_usage_reporting_interval: Option<f64>,
74    /// Transfer mode.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub transfer_mode: Option<TransferMode>,
77    /// Stream format.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub stream_format: Option<StreamFormat>,
80    /// Stream compression.
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub stream_compression: Option<StreamCompression>,
83    /// Trace config for Chromium's tracing backend.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub trace_config: Option<TraceConfig>,
86    /// Base64-encoded serialized perfetto.protos.TraceConfig.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub perfetto_config: Option<String>,
89    /// Backend type.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub tracing_backend: Option<TracingBackend>,
92}
93
94impl StartParams {
95    /// Create new start parameters.
96    pub fn new() -> Self {
97        Self::default()
98    }
99
100    /// Set the categories filter.
101    #[must_use]
102    pub fn categories(mut self, categories: impl Into<String>) -> Self {
103        self.categories = Some(categories.into());
104        self
105    }
106
107    /// Set the transfer mode.
108    #[must_use]
109    pub fn transfer_mode(mut self, mode: TransferMode) -> Self {
110        self.transfer_mode = Some(mode);
111        self
112    }
113
114    /// Set the stream format.
115    #[must_use]
116    pub fn stream_format(mut self, format: StreamFormat) -> Self {
117        self.stream_format = Some(format);
118        self
119    }
120
121    /// Set the trace config.
122    #[must_use]
123    pub fn trace_config(mut self, config: TraceConfig) -> Self {
124        self.trace_config = Some(config);
125        self
126    }
127}
128
129/// Trace configuration.
130#[derive(Debug, Clone, Serialize, Default)]
131#[serde(rename_all = "camelCase")]
132pub struct TraceConfig {
133    /// Recording mode: recordUntilFull, recordContinuously, recordAsMuchAsPossible.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub record_mode: Option<String>,
136    /// Trace buffer size in kilobytes.
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub trace_buffer_size_in_kb: Option<i32>,
139    /// Whether to enable sampling.
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub enable_sampling: Option<bool>,
142    /// Whether to enable systrace.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub enable_systrace: Option<bool>,
145    /// Whether to enable argument filter.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub enable_argument_filter: Option<bool>,
148    /// Included categories.
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub included_categories: Option<Vec<String>>,
151    /// Excluded categories.
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub excluded_categories: Option<Vec<String>>,
154    /// Synthetic delays configuration.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub synthetic_delays: Option<Vec<String>>,
157    /// Memory dump configuration.
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub memory_dump_config: Option<serde_json::Value>,
160}
161
162impl TraceConfig {
163    /// Create a new trace config.
164    pub fn new() -> Self {
165        Self::default()
166    }
167
168    /// Set included categories.
169    #[must_use]
170    pub fn included_categories(mut self, categories: Vec<String>) -> Self {
171        self.included_categories = Some(categories);
172        self
173    }
174
175    /// Add an included category.
176    #[must_use]
177    pub fn include_category(mut self, category: impl Into<String>) -> Self {
178        let categories = self.included_categories.get_or_insert_with(Vec::new);
179        categories.push(category.into());
180        self
181    }
182
183    /// Set excluded categories.
184    #[must_use]
185    pub fn excluded_categories(mut self, categories: Vec<String>) -> Self {
186        self.excluded_categories = Some(categories);
187        self
188    }
189
190    /// Set record mode.
191    #[must_use]
192    pub fn record_mode(mut self, mode: impl Into<String>) -> Self {
193        self.record_mode = Some(mode.into());
194        self
195    }
196}
197
198// ============================================================================
199// Tracing.end
200// ============================================================================
201
202/// Parameters for Tracing.end (empty).
203#[derive(Debug, Clone, Serialize, Default)]
204pub struct EndParams {}
205
206/// Result of Tracing.end when using stream transfer mode.
207#[derive(Debug, Clone, Deserialize)]
208#[serde(rename_all = "camelCase")]
209pub struct EndResult {
210    /// Stream handle for the trace data (when using stream transfer mode).
211    pub stream: Option<String>,
212    /// Compression type of the trace data in the stream.
213    pub stream_compression: Option<StreamCompression>,
214}
215
216// ============================================================================
217// Tracing.getCategories
218// ============================================================================
219
220/// Parameters for Tracing.getCategories (empty).
221#[derive(Debug, Clone, Serialize, Default)]
222pub struct GetCategoriesParams {}
223
224/// Result of Tracing.getCategories.
225#[derive(Debug, Clone, Deserialize)]
226pub struct GetCategoriesResult {
227    /// List of supported tracing categories.
228    pub categories: Vec<String>,
229}
230
231// ============================================================================
232// Tracing.recordClockSyncMarker
233// ============================================================================
234
235/// Parameters for Tracing.recordClockSyncMarker.
236#[derive(Debug, Clone, Serialize)]
237#[serde(rename_all = "camelCase")]
238pub struct RecordClockSyncMarkerParams {
239    /// Sync marker ID.
240    pub sync_id: String,
241}
242
243// ============================================================================
244// Tracing.requestMemoryDump
245// ============================================================================
246
247/// Parameters for Tracing.requestMemoryDump.
248#[derive(Debug, Clone, Serialize, Default)]
249#[serde(rename_all = "camelCase")]
250pub struct RequestMemoryDumpParams {
251    /// Whether to dump memory from all processes.
252    #[serde(skip_serializing_if = "Option::is_none")]
253    pub deterministic: Option<bool>,
254    /// Request a memory-infra dump for all processes.
255    #[serde(skip_serializing_if = "Option::is_none")]
256    pub level_of_detail: Option<String>,
257}
258
259/// Result of Tracing.requestMemoryDump.
260#[derive(Debug, Clone, Deserialize)]
261#[serde(rename_all = "camelCase")]
262pub struct RequestMemoryDumpResult {
263    /// GUID of the resulting global memory dump.
264    pub dump_guid: String,
265    /// True if the global memory dump succeeded.
266    pub success: bool,
267}
268
269// ============================================================================
270// Events
271// ============================================================================
272
273/// Event: Tracing.dataCollected
274///
275/// Contains a bucket of collected trace events.
276#[derive(Debug, Clone, Deserialize)]
277pub struct DataCollectedEvent {
278    /// Collected trace events.
279    pub value: Vec<serde_json::Value>,
280}
281
282/// Event: Tracing.tracingComplete
283///
284/// Signals that tracing is stopped and there is no more data to collect.
285#[derive(Debug, Clone, Deserialize)]
286#[serde(rename_all = "camelCase")]
287pub struct TracingCompleteEvent {
288    /// Stream handle for trace data (when using stream transfer mode).
289    pub stream: Option<String>,
290    /// Compression type of the trace data in the stream.
291    pub stream_compression: Option<StreamCompression>,
292    /// Stream format of the trace data in the stream.
293    pub stream_format: Option<StreamFormat>,
294}
295
296/// Event: Tracing.bufferUsage
297///
298/// Signals that tracing is approaching its buffer limit.
299#[derive(Debug, Clone, Deserialize)]
300#[serde(rename_all = "camelCase")]
301pub struct BufferUsageEvent {
302    /// A number in range [0..1] that indicates the used size of event buffer.
303    pub percent_full: Option<f64>,
304    /// An approximate number of events in the trace log.
305    pub event_count: Option<f64>,
306    /// A number in range [0..1] that indicates the used size of event buffer.
307    pub value: Option<f64>,
308}