encore-dev 2.0.0-beta.21

Rust SDK for Encore applications
Documentation
//! Encore's tracing subscriber layers.
//!
//! Compose [`layer`] with a subscriber and install it explicitly. To forward
//! `log` records to the subscriber, optionally install `tracing_log::LogTracer`
//! once in the application:
//!
//! ```no_run
//! use tracing_subscriber::prelude::*;
//!
//! let subscriber = tracing_subscriber::registry()
//!     .with(encore::telemetry::tracing::layer());
//! tracing::subscriber::set_global_default(subscriber)?;
//! tracing_log::LogTracer::init()?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! [`logs_layer`] prints Encore-format events for the supervisor to collect;
//! [`traces_layer`] records spans into Encore's collector. Neither installs a
//! subscriber or global logger.

/// Console events and collector spans. The application installs the subscriber.
pub fn layer<S>() -> impl tracing_subscriber::Layer<S>
where
    S: ::tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    use tracing_subscriber::Layer as _;
    logs_layer().and_then(traces_layer())
}

/// Console events in Encore's log format, without initializing the runtime.
/// Info and above are enabled, as are runtime events selected by `RUST_LOG`
/// or `ENCORE_RUNTIME_LOG` (default debug). Spans are not exported.
/// Without an Encore runtime, call [`crate::telemetry::log::flush`] before exiting.
///
/// ```no_run
/// use tracing_subscriber::prelude::*;
/// tracing::subscriber::set_global_default(
///     tracing_subscriber::registry().with(encore::telemetry::tracing::logs_layer()),
/// )?;
/// tracing::info!("finished");
/// encore::telemetry::log::flush();
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn logs_layer<S>() -> impl tracing_subscriber::Layer<S>
where
    S: ::tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    crate::console_log::layer()
}

/// Encore collector spans with per-entry OpenTelemetry context activation.
/// Events are excluded from this layer, not from sibling console layers.
pub fn traces_layer<S>() -> impl tracing_subscriber::Layer<S>
where
    S: ::tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    use tracing_subscriber::Layer as _;
    tracing_opentelemetry::layer()
        .with_tracer(crate::telemetry::tracer(
            encore_sdk_core::telemetry::scope::SDK,
        ))
        .with_context_activation(true)
        .with_threads(false)
        .with_tracked_inactivity(false)
        .with_filter(tracing_subscriber::filter::filter_fn(|meta| meta.is_span()))
}