tauri-plugin-tracing 0.3.3

Use the tracing crate in your Tauri app
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! CPU profiling integration via `tauri-plugin-profiling`.
//!
//! This module provides tracing-aware CPU profiling that automatically creates
//! spans and logs profiling events, with optional span timing correlation.
//!
//! ## Basic Usage
//!
//! ```rust,no_run
//! use tauri::Manager;
//! use tauri_plugin_tracing::{Builder, LevelFilter, TracedProfilingExt, init_profiling};
//!
//! tauri::Builder::default()
//!     .plugin(Builder::new().with_max_level(LevelFilter::DEBUG).build())
//!     .plugin(init_profiling())
//!     .setup(|app| {
//!         app.start_cpu_profile_traced()?;
//!         // ... do work ...
//!         let result = app.stop_cpu_profile_traced()?;
//!         Ok(())
//!     })
//!     .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//!     .expect("error while running tauri application");
//! ```
//!
//! ## Span-Correlated Profiling
//!
//! Use [`SpanTimingLayer`] to correlate CPU samples with active tracing spans:
//!
//! ```rust,no_run
//! use tauri::Manager;
//! use tauri_plugin_tracing::{
//!     Builder, LevelFilter, SpanTimingLayer, SpanAwareProfilingExt, init_profiling
//! };
//! use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt};
//!
//! // Create the span timing layer
//! let (span_layer, span_capture) = SpanTimingLayer::new();
//!
//! tauri::Builder::default()
//!     .plugin(Builder::new().build())
//!     .plugin(init_profiling())
//!     .setup(move |app| {
//!         // Register the span capture for later correlation
//!         app.manage(span_capture);
//!
//!         // Initialize subscriber with span timing layer
//!         Registry::default()
//!             .with(span_layer)
//!             .with(tracing_subscriber::fmt::layer())
//!             .init();
//!
//!         // Profile with span correlation
//!         app.start_span_aware_profile()?;
//!         // ... do work with tracing spans ...
//!         let report = app.stop_span_aware_profile()?;
//!         println!("{}", report);
//!         Ok(())
//!     })
//!     .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//!     .expect("error while running tauri application");
//! ```

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

use tauri::{AppHandle, Manager, Runtime};
use tauri_plugin_profiling::ProfilingExt;
use tracing::span::Attributes;
use tracing::{Id, Span, Subscriber};
use tracing_subscriber::Layer;
use tracing_subscriber::layer::Context;
use tracing_subscriber::registry::LookupSpan;

// Re-export all types from tauri-plugin-profiling
pub use tauri_plugin_profiling::{
    Error as ProfilingError, ProfileResult, ProfilingConfig, ProfilingExt as ProfilingExtBase,
    Result as ProfilingResult, StartOptions, init as init_profiling,
    init_with_config as init_profiling_with_config,
};

// ============================================================================
// Span Timing Layer
// ============================================================================

/// A recorded span timing event.
#[derive(Debug, Clone)]
pub struct SpanEvent {
    /// Span name (target::name format)
    pub name: String,
    /// Span ID
    pub span_id: u64,
    /// Parent span ID (0 if none)
    pub parent_id: u64,
    /// Event type
    pub event_type: SpanEventType,
    /// Timestamp relative to capture start (microseconds)
    pub timestamp_us: u64,
    /// Thread ID where the event occurred
    pub thread_id: u64,
}

/// Type of span event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpanEventType {
    /// Span was entered
    Enter,
    /// Span was exited
    Exit,
    /// Span was closed (dropped)
    Close,
}

/// Shared state for span timing capture.
#[derive(Debug)]
pub struct SpanTimingCapture {
    events: Mutex<Vec<SpanEvent>>,
    start_time: Mutex<Option<Instant>>,
    capturing: AtomicBool,
    next_id: AtomicU64,
}

impl SpanTimingCapture {
    /// Creates a new span timing capture.
    fn new() -> Self {
        Self {
            events: Mutex::new(Vec::new()),
            start_time: Mutex::new(None),
            capturing: AtomicBool::new(false),
            next_id: AtomicU64::new(1),
        }
    }

    /// Starts capturing span timing events.
    pub fn start_capture(&self) {
        if let Ok(mut events) = self.events.lock() {
            events.clear();
        }
        if let Ok(mut start) = self.start_time.lock() {
            *start = Some(Instant::now());
        }
        self.capturing.store(true, Ordering::SeqCst);
    }

    /// Stops capturing and returns all recorded events.
    pub fn stop_capture(&self) -> Vec<SpanEvent> {
        self.capturing.store(false, Ordering::SeqCst);
        if let Ok(mut events) = self.events.lock() {
            std::mem::take(&mut *events)
        } else {
            Vec::new()
        }
    }

    /// Returns whether capture is currently active.
    pub fn is_capturing(&self) -> bool {
        self.capturing.load(Ordering::SeqCst)
    }

    fn record_event(&self, name: String, span_id: u64, parent_id: u64, event_type: SpanEventType) {
        if !self.capturing.load(Ordering::SeqCst) {
            return;
        }

        let timestamp_us = if let Ok(start) = self.start_time.lock() {
            start.map(|s| s.elapsed().as_micros() as u64).unwrap_or(0)
        } else {
            0
        };

        // Use hash of thread ID since as_u64() is unstable
        let thread_id = {
            use std::hash::{Hash, Hasher};
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            std::thread::current().id().hash(&mut hasher);
            hasher.finish()
        };

        let event = SpanEvent {
            name,
            span_id,
            parent_id,
            event_type,
            timestamp_us,
            thread_id,
        };

        if let Ok(mut events) = self.events.lock() {
            events.push(event);
        }
    }

    fn next_id(&self) -> u64 {
        self.next_id.fetch_add(1, Ordering::SeqCst)
    }
}

/// A tracing layer that captures span timing for correlation with CPU profiles.
///
/// Create with [`SpanTimingLayer::new()`], which returns both the layer and
/// a [`SpanTimingCapture`] handle for controlling capture and retrieving events.
pub struct SpanTimingLayer {
    capture: Arc<SpanTimingCapture>,
}

impl SpanTimingLayer {
    /// Creates a new span timing layer and its capture handle.
    ///
    /// The returned [`SpanTimingCapture`] should be stored (e.g., in Tauri state)
    /// and used to start/stop capture and retrieve events.
    pub fn new() -> (Self, Arc<SpanTimingCapture>) {
        let capture = Arc::new(SpanTimingCapture::new());
        (
            Self {
                capture: capture.clone(),
            },
            capture,
        )
    }
}

impl Default for SpanTimingLayer {
    fn default() -> Self {
        Self::new().0
    }
}

// Store our internal ID on spans
struct SpanTimingId(u64);

impl<S> Layer<S> for SpanTimingLayer
where
    S: Subscriber + for<'a> LookupSpan<'a>,
{
    fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
        let internal_id = self.capture.next_id();

        // Store our internal ID on the span
        if let Some(span) = ctx.span(id) {
            span.extensions_mut().insert(SpanTimingId(internal_id));
        }

        // Get parent ID
        let parent_id = attrs
            .parent()
            .and_then(|pid| ctx.span(pid))
            .and_then(|span| span.extensions().get::<SpanTimingId>().map(|id| id.0))
            .or_else(|| {
                ctx.lookup_current()
                    .and_then(|span| span.extensions().get::<SpanTimingId>().map(|id| id.0))
            })
            .unwrap_or(0);

        let name = format!("{}::{}", attrs.metadata().target(), attrs.metadata().name());
        self.capture
            .record_event(name, internal_id, parent_id, SpanEventType::Enter);
    }

    fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
        if let Some(span) = ctx.span(id)
            && let Some(timing_id) = span.extensions().get::<SpanTimingId>()
        {
            let name = format!("{}::{}", span.metadata().target(), span.metadata().name());
            self.capture
                .record_event(name, timing_id.0, 0, SpanEventType::Enter);
        }
    }

    fn on_exit(&self, id: &Id, ctx: Context<'_, S>) {
        if let Some(span) = ctx.span(id)
            && let Some(timing_id) = span.extensions().get::<SpanTimingId>()
        {
            let name = format!("{}::{}", span.metadata().target(), span.metadata().name());
            self.capture
                .record_event(name, timing_id.0, 0, SpanEventType::Exit);
        }
    }

    fn on_close(&self, id: Id, ctx: Context<'_, S>) {
        if let Some(span) = ctx.span(&id)
            && let Some(timing_id) = span.extensions().get::<SpanTimingId>()
        {
            let name = format!("{}::{}", span.metadata().target(), span.metadata().name());
            self.capture
                .record_event(name, timing_id.0, 0, SpanEventType::Close);
        }
    }
}

// ============================================================================
// Span Correlation Report
// ============================================================================

/// A span that was active during CPU profiling.
#[derive(Debug, Clone)]
pub struct ActiveSpan {
    /// Span name
    pub name: String,
    /// Total time the span was active (microseconds)
    pub total_time_us: u64,
    /// Number of times the span was entered
    pub enter_count: u64,
    /// Percentage of profile duration this span was active
    pub percentage: f64,
}

/// Report correlating CPU profile with span timing.
#[derive(Debug)]
pub struct SpanCorrelationReport {
    /// CPU profile result
    pub profile: ProfileResult,
    /// Total profile duration in microseconds
    pub duration_us: u64,
    /// Spans that were active during profiling, sorted by total time
    pub active_spans: Vec<ActiveSpan>,
    /// Raw span events (for advanced analysis)
    pub events: Vec<SpanEvent>,
}

impl std::fmt::Display for SpanCorrelationReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "=== CPU Profile with Span Correlation ===")?;
        writeln!(f, "Duration: {:.2}ms", self.duration_us as f64 / 1000.0)?;
        writeln!(f, "Samples: {}", self.profile.sample_count)?;
        writeln!(f, "Flamegraph: {}", self.profile.flamegraph_path.display())?;
        writeln!(f)?;
        writeln!(f, "Active Spans (by time):")?;
        writeln!(f, "{:-<60}", "")?;

        for span in &self.active_spans {
            writeln!(
                f,
                "{:50} {:>6.1}% ({:.2}ms, {} entries)",
                truncate_span_name(&span.name, 50),
                span.percentage,
                span.total_time_us as f64 / 1000.0,
                span.enter_count
            )?;
        }

        if self.active_spans.is_empty() {
            writeln!(f, "(no spans recorded during profiling)")?;
        }

        Ok(())
    }
}

fn truncate_span_name(name: &str, max_len: usize) -> String {
    if name.len() <= max_len {
        name.to_string()
    } else {
        format!("...{}", &name[name.len() - max_len + 3..])
    }
}

/// Analyzes span events to compute active span statistics.
fn analyze_span_events(events: &[SpanEvent], duration_us: u64) -> Vec<ActiveSpan> {
    use std::collections::HashMap;

    // Track active time per span name
    #[derive(Default)]
    struct SpanStats {
        total_time_us: u64,
        enter_count: u64,
        last_enter_time: Option<u64>,
    }

    let mut stats: HashMap<String, SpanStats> = HashMap::new();

    for event in events {
        let entry = stats.entry(event.name.clone()).or_default();

        match event.event_type {
            SpanEventType::Enter => {
                entry.enter_count += 1;
                entry.last_enter_time = Some(event.timestamp_us);
            }
            SpanEventType::Exit | SpanEventType::Close => {
                if let Some(enter_time) = entry.last_enter_time.take() {
                    entry.total_time_us += event.timestamp_us.saturating_sub(enter_time);
                }
            }
        }
    }

    // Handle spans that were still active at the end
    for entry in stats.values_mut() {
        if let Some(enter_time) = entry.last_enter_time.take() {
            entry.total_time_us += duration_us.saturating_sub(enter_time);
        }
    }

    let mut active_spans: Vec<_> = stats
        .into_iter()
        .filter(|(_, s)| s.total_time_us > 0 || s.enter_count > 0)
        .map(|(name, s)| ActiveSpan {
            name,
            total_time_us: s.total_time_us,
            enter_count: s.enter_count,
            percentage: if duration_us > 0 {
                (s.total_time_us as f64 / duration_us as f64) * 100.0
            } else {
                0.0
            },
        })
        .collect();

    // Sort by total time descending
    active_spans.sort_by_key(|s| std::cmp::Reverse(s.total_time_us));

    active_spans
}

// ============================================================================
// Basic Traced Profiling (existing functionality)
// ============================================================================

/// Holds the active profiling span guard.
struct ProfilingSpanGuard {
    span: Mutex<Option<Span>>,
}

/// Extension trait for tracing-aware CPU profiling.
///
/// This trait wraps [`ProfilingExt`](tauri_plugin_profiling::ProfilingExt) methods
/// with automatic span creation and logging.
pub trait TracedProfilingExt<R: Runtime> {
    /// Starts CPU profiling with automatic span creation and logging.
    fn start_cpu_profile_traced(&self) -> ProfilingResult<()>;

    /// Starts CPU profiling with options, automatic span creation, and logging.
    fn start_cpu_profile_traced_with_options(&self, options: StartOptions) -> ProfilingResult<()>;

    /// Stops CPU profiling, closes the span, and logs results.
    fn stop_cpu_profile_traced(&self) -> ProfilingResult<ProfileResult>;
}

impl<R: Runtime, T: Manager<R>> TracedProfilingExt<R> for T {
    fn start_cpu_profile_traced(&self) -> ProfilingResult<()> {
        start_traced_impl(self.app_handle(), None)
    }

    fn start_cpu_profile_traced_with_options(&self, options: StartOptions) -> ProfilingResult<()> {
        start_traced_impl(self.app_handle(), Some(options))
    }

    fn stop_cpu_profile_traced(&self) -> ProfilingResult<ProfileResult> {
        let result = self.app_handle().stop_cpu_profile()?;

        tracing::info!(
            samples = result.sample_count,
            duration_ms = result.duration_ms,
            flamegraph = %result.flamegraph_path.display(),
            "CPU profiling stopped"
        );

        if let Some(state) = self.app_handle().try_state::<ProfilingSpanGuard>()
            && let Ok(mut guard) = state.span.lock()
            && let Some(span) = guard.take()
        {
            drop(span);
        }

        Ok(result)
    }
}

fn start_traced_impl<R: Runtime>(
    app: &AppHandle<R>,
    options: Option<StartOptions>,
) -> ProfilingResult<()> {
    let frequency = options.as_ref().and_then(|o| o.frequency).unwrap_or(100);

    let span = tracing::info_span!("cpu_profile", frequency = frequency);
    tracing::info!(frequency = frequency, "CPU profiling started");

    match options {
        Some(opts) => app.start_cpu_profile_with_options(opts)?,
        None => app.start_cpu_profile()?,
    }

    match app.try_state::<ProfilingSpanGuard>() {
        Some(state) => {
            if let Ok(mut guard) = state.span.lock() {
                *guard = Some(span);
            }
        }
        None => {
            app.manage(ProfilingSpanGuard {
                span: Mutex::new(Some(span)),
            });
        }
    }

    Ok(())
}

// ============================================================================
// Span-Aware Profiling (with correlation)
// ============================================================================

/// Extension trait for CPU profiling with span correlation.
///
/// Requires a [`SpanTimingCapture`] to be registered in Tauri state.
pub trait SpanAwareProfilingExt<R: Runtime> {
    /// Starts CPU profiling and span timing capture.
    fn start_span_aware_profile(&self) -> ProfilingResult<()>;

    /// Starts CPU profiling with options and span timing capture.
    fn start_span_aware_profile_with_options(&self, options: StartOptions) -> ProfilingResult<()>;

    /// Stops profiling and returns a correlation report.
    fn stop_span_aware_profile(&self) -> ProfilingResult<SpanCorrelationReport>;
}

impl<R: Runtime, T: Manager<R>> SpanAwareProfilingExt<R> for T {
    fn start_span_aware_profile(&self) -> ProfilingResult<()> {
        start_span_aware_impl(self.app_handle(), None)
    }

    fn start_span_aware_profile_with_options(&self, options: StartOptions) -> ProfilingResult<()> {
        start_span_aware_impl(self.app_handle(), Some(options))
    }

    fn stop_span_aware_profile(&self) -> ProfilingResult<SpanCorrelationReport> {
        // Stop CPU profiling
        let profile = self.app_handle().stop_cpu_profile()?;

        // Stop span capture and get events
        let events = if let Some(capture) = self.app_handle().try_state::<Arc<SpanTimingCapture>>()
        {
            capture.stop_capture()
        } else {
            Vec::new()
        };

        let duration_us = profile.duration_ms * 1000;
        let active_spans = analyze_span_events(&events, duration_us);

        tracing::info!(
            samples = profile.sample_count,
            duration_ms = profile.duration_ms,
            spans_recorded = events.len(),
            active_spans = active_spans.len(),
            flamegraph = %profile.flamegraph_path.display(),
            "Span-aware CPU profiling stopped"
        );

        Ok(SpanCorrelationReport {
            profile,
            duration_us,
            active_spans,
            events,
        })
    }
}

fn start_span_aware_impl<R: Runtime>(
    app: &AppHandle<R>,
    options: Option<StartOptions>,
) -> ProfilingResult<()> {
    // Start span capture if available
    if let Some(capture) = app.try_state::<Arc<SpanTimingCapture>>() {
        capture.start_capture();
    } else {
        tracing::warn!(
            "SpanTimingCapture not found in state - span correlation will be unavailable"
        );
    }

    let frequency = options.as_ref().and_then(|o| o.frequency).unwrap_or(100);
    tracing::info!(frequency = frequency, "Span-aware CPU profiling started");

    match options {
        Some(opts) => app.start_cpu_profile_with_options(opts)?,
        None => app.start_cpu_profile()?,
    }

    Ok(())
}