tracing_modality/async/
mod.rs

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