devtools_core/
lib.rs

1#![allow(clippy::doc_markdown, clippy::needless_doctest_main)]
2
3//! Instrumentation code that will make your Tauri app compatible with CrabNebula Devtools.
4//!
5//! CrabNebula Devtools offers seamless and intuitive debugging, and monitoring of Tauri applications.
6//!
7//! The instrumentation is compatible with both the [`log`](https://docs.rs/log/latest/log/)
8//! and [`tracing`](https://docs.rs/tracing/latest/tracing/) ecosystems out-of-the-box.
9
10pub mod aggregator;
11pub mod bridge_layer;
12mod error;
13pub mod layer;
14pub mod server;
15mod visitors;
16
17use devtools_wire_format::{instrument, Field};
18pub use error::Error;
19use std::sync::atomic::AtomicUsize;
20use std::time::Instant;
21use tokio::sync::{mpsc, Notify};
22
23const EVENT_BUFFER_CAPACITY: usize = 512;
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27/// Shared data between the [`Layer`] and the [`Aggregator`]
28#[derive(Debug, Default)]
29pub struct Shared {
30    dropped_log_events: AtomicUsize,
31    dropped_span_events: AtomicUsize,
32    flush: Notify,
33}
34
35/// Data sent from the `Layer` to the `Aggregator`
36///
37/// This is designed to be as cheap to create as possible so the `Layer` impl remains lightweight.
38#[derive(Debug)]
39pub enum Event {
40    /// The tracing system registered new span or event metadata
41    /// Metadata is the portion of a data point that remains static.
42    Metadata(&'static tracing_core::Metadata<'static>),
43    /// A new event was emitted.
44    /// This usually corresponds to a log event.
45    Event {
46        at: Instant,
47        metadata: &'static tracing_core::Metadata<'static>,
48        message: String,
49        fields: Vec<Field>,
50        maybe_parent: Option<tracing_core::span::Id>,
51    },
52    /// A new span was created.
53    NewSpan {
54        at: Instant,
55        id: tracing_core::span::Id,
56        metadata: &'static tracing_core::Metadata<'static>,
57        fields: Vec<Field>,
58        maybe_parent: Option<tracing_core::span::Id>,
59    },
60    /// A previously created span was entered.
61    EnterSpan {
62        at: Instant,
63        thread_id: u64,
64        span_id: tracing_core::span::Id,
65    },
66    /// A previously created and entered span was exited.
67    ExitSpan {
68        at: Instant,
69        thread_id: u64,
70        span_id: tracing_core::span::Id,
71    },
72    /// A previously created span has been closed.
73    /// No new events regarding this particular span will be emitted.
74    /// NOTE: The span ID that corresponded to this span might be reused!
75    CloseSpan {
76        at: Instant,
77        span_id: tracing_core::span::Id,
78    },
79    /// Span recorded a new value.
80    SpanRecorded {
81        span_id: tracing_core::span::Id,
82        fields: Vec<Field>,
83    },
84}
85
86/// Commands send from the `Server` to the `Aggregator`
87pub enum Command {
88    Instrument(Watcher),
89}
90
91pub struct Watcher {
92    tx: mpsc::Sender<Result<instrument::Update>>,
93}