Skip to main content

docspec_http/
tracing_init.rs

1//! Tracing subscriber initialization.
2
3use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _, Layer as _};
4
5/// Installs a hardcoded tracing subscriber writing to stderr at INFO level with pretty format.
6///
7/// # Panics
8///
9/// Panics if a global subscriber has already been installed. In tests, use [`try_init`] instead.
10#[inline]
11pub fn init() {
12    let fmt = tracing_subscriber::fmt::layer()
13        .with_writer(std::io::stderr)
14        .pretty()
15        .with_filter(tracing::level_filters::LevelFilter::INFO);
16
17    #[cfg(feature = "sentry")]
18    tracing_subscriber::registry()
19        .with(fmt)
20        .with(crate::telemetry::tracing_layer())
21        .init();
22
23    #[cfg(not(feature = "sentry"))]
24    tracing_subscriber::registry().with(fmt).init();
25}
26
27/// Installs the tracing subscriber, returning an error if one is already set.
28///
29/// Use this in tests to avoid panics from double-initialization.
30///
31/// # Errors
32///
33/// Returns [`Err`] if a global subscriber has already been installed.
34#[inline]
35pub fn try_init() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
36    let fmt = tracing_subscriber::fmt::layer()
37        .with_writer(std::io::stderr)
38        .pretty()
39        .with_filter(tracing::level_filters::LevelFilter::INFO);
40
41    #[cfg(feature = "sentry")]
42    return tracing_subscriber::registry()
43        .with(fmt)
44        .with(crate::telemetry::tracing_layer())
45        .try_init()
46        .map_err(Into::into);
47
48    #[cfg(not(feature = "sentry"))]
49    tracing_subscriber::registry()
50        .with(fmt)
51        .try_init()
52        .map_err(Into::into)
53}