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