use sentry_core::protocol::Event as SentryEvent;
use tracing_core::{Event, Subscriber};
use tracing_subscriber::layer::{Context, Layer};
use tracing_subscriber::registry::LookupSpan;
use crate::converters::event_from_event_impl;
#[must_use]
#[derive(Default)]
pub struct ErrorLayer {
with_span_attributes: bool,
}
impl ErrorLayer {
pub fn new() -> Self {
Self::default()
}
pub fn enable_span_attributes(mut self) -> Self {
self.with_span_attributes = true;
self
}
pub(super) fn capture(&self, event: SentryEvent<'static>) {
sentry_core::capture_event(event);
}
pub(super) fn capture_tracing_event<S>(&self, event: &Event, ctx: Context<'_, S>)
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
let sentry_event = event_from_event_impl(event, self.with_span_attributes.then_some(&ctx));
self.capture(sentry_event);
}
}
impl<S> Layer<S> for ErrorLayer
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
fn on_event(&self, event: &Event, ctx: Context<'_, S>) {
self.capture_tracing_event(event, ctx);
}
}