tracing_modality/blocking/
mod.rs

1mod layer;
2
3pub use crate::ingest::ModalityIngestThreadHandle;
4pub use crate::{timeline_id, InitError, Options, TimelineId};
5pub use layer::ModalityLayer;
6
7use anyhow::Context as _;
8use tracing::Dispatch;
9
10/// A global tracer instance for [tracing.rs](https://tracing.rs/) that sends traces via a network
11/// socket to [Modality](https://auxon.io/).
12///
13/// This is the synchronous version of `TracingModality`, it must not be initialized or `finish`ed
14/// from within a tokio runtime. See [`crate::TracingModality`] for a version that can be
15/// initialized inside a tokio runtime. Both versions support tracing from within and outside of a
16/// tokio runtime.
17pub struct TracingModality {
18    ingest_handle: ModalityIngestThreadHandle,
19}
20
21impl TracingModality {
22    /// Initialize with default options and set as the global default tracer.
23    pub fn init() -> Result<Self, InitError> {
24        Self::init_with_options(Default::default())
25    }
26
27    /// Initialize with the provided options and set as the global default tracer.
28    pub fn init_with_options(opts: Options) -> Result<Self, InitError> {
29        let (layer, ingest_handle) =
30            ModalityLayer::init_with_options(opts).context("initialize ModalityLayer")?;
31
32        let disp = Dispatch::new(layer.into_subscriber());
33        tracing::dispatcher::set_global_default(disp).unwrap();
34
35        Ok(Self { ingest_handle })
36    }
37
38    /// Stop accepting new trace events, flush all existing events, and stop ingest thread.
39    pub fn finish(self) {
40        self.ingest_handle.finish();
41    }
42}