Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions objectstore-log/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,20 @@ include!(concat!(env!("OUT_DIR"), "/constants.gen.rs"));
/// by `config.format`, defaulting to pretty when a terminal is attached.
///
/// When built with the `sentry` feature, also attaches Sentry tracing layers if the Sentry client
/// has been initialized. `ERROR`..`DEBUG` spans become Sentry spans and `ERROR`..`DEBUG` events
/// become Sentry logs. `ERROR` and `WARN` events also become Sentry error events. Logs and error
/// events include the attributes of their parent spans. `TRACE` spans and events are ignored.
/// has been initialized:
///
/// - `ERROR`..`DEBUG` spans become Sentry spans.
/// - `TRACE` spans are ignored.
/// - `ERROR`..`DEBUG` events become Sentry logs.
/// - `ERROR` and `WARN` events also become Sentry error events.
/// - `TRACE` events are ignored.
/// - Logs and error events include the attributes of their parent spans.
pub fn init(config: &LoggingConfig) {
#[cfg(feature = "sentry")]
let sentry_layer = sentry::Hub::current()
.client()
.filter(|c| c.is_enabled())
.map(|_| {
use sentry::integrations::tracing::{ErrorLayer, LogLayer, SpanLayer};
use tracing_subscriber::filter::{FilterExt, LevelFilter, filter_fn};

SpanLayer::new()
.with_filter(LevelFilter::DEBUG)
.and_then(
ErrorLayer::new()
.enable_span_attributes()
// Span attributes come only from spans that pass both the span layer's
// and the event layer's filter, so the error layer accepts all spans.
.with_filter(LevelFilter::WARN.or(filter_fn(|m| m.is_span()))),
)
.and_then(
LogLayer::new()
.enable_span_attributes()
.with_filter(LevelFilter::DEBUG),
)
});
.map(|_| sentry_layers());

let format = tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
Expand Down Expand Up @@ -82,6 +69,34 @@ pub fn init(config: &LoggingConfig) {
.init();
}

/// Builds the Sentry tracing layers that [`init`] attaches, with the filtering described there.
#[cfg(feature = "sentry")]
fn sentry_layers<S>() -> impl tracing_subscriber::Layer<S>
where
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a> + 'static,
{
use sentry::integrations::tracing::{ErrorLayer, LogLayer, SpanLayer};
use tracing_subscriber::filter::{FilterExt, LevelFilter, filter_fn};

// Span attributes come only from spans that pass both the SpanLayer's and the ErrorLayer's
// filter, so the ErrorLayer accepts all spans. The LogLayer's filter already admits the
// same spans as the SpanLayer's.
let error_filter = LevelFilter::WARN.or(filter_fn(|metadata| metadata.is_span()));

SpanLayer::new()
.with_filter(LevelFilter::DEBUG)
.and_then(
ErrorLayer::new()
.enable_span_attributes()
.with_filter(error_filter),
)
.and_then(
LogLayer::new()
.enable_span_attributes()
.with_filter(LevelFilter::DEBUG),
)
}

/// Builds the default [`EnvFilter`] when `RUST_LOG` is not set.
///
/// Uses `INFO` as the base level. Selected dependencies are raised to `DEBUG` where their debug
Expand Down
Loading