Skip to content

Commit

Permalink
ref: Rewrite tracing integration in terms of manual API (NATIVE-312)
Browse files Browse the repository at this point in the history
Instead of hand-rolling all the transaction/span manipulation, rather
implement it on top of the manual performance monitoring API.
Currently lacks the ability to attach additional `data` to a span.
  • Loading branch information
Swatinem committed Jan 5, 2022
1 parent 9ab46da commit 14a7238
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 355 deletions.
41 changes: 5 additions & 36 deletions sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::collections::BTreeMap;

use sentry_core::protocol::{self, Event, TraceContext, Value};
use sentry_core::protocol::{Event, Value};
use sentry_core::{Breadcrumb, Level};
use tracing_core::{
field::{Field, Visit},
span, Subscriber,
};
use tracing_core::field::{Field, Visit};
use tracing_core::{span, Subscriber};
use tracing_subscriber::layer::Context;
use tracing_subscriber::registry::LookupSpan;

use crate::Trace;

/// Converts a [`tracing_core::Level`] to a Sentry [`Level`]
pub fn convert_tracing_level(level: &tracing_core::Level) -> Level {
match level {
Expand Down Expand Up @@ -94,46 +90,19 @@ pub fn breadcrumb_from_event(event: &tracing_core::Event) -> Breadcrumb {
}

/// Creates an [`Event`] from a given [`tracing_core::Event`]
pub fn event_from_event<S>(event: &tracing_core::Event, ctx: Context<S>) -> Event<'static>
pub fn event_from_event<S>(event: &tracing_core::Event, _ctx: Context<S>) -> Event<'static>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
let (message, extra) = extract_event_data(event);

let mut result = Event {
Event {
logger: Some(event.metadata().target().to_owned()),
level: convert_tracing_level(event.metadata().level()),
message,
extra,
..Default::default()
};

let parent = event
.parent()
.and_then(|id| ctx.span(id))
.or_else(|| ctx.lookup_current());

if let Some(parent) = parent {
let extensions = parent.extensions();
if let Some(trace) = extensions.get::<Trace>() {
let context = protocol::Context::from(TraceContext {
span_id: trace.span.span_id,
trace_id: trace.span.trace_id,
..TraceContext::default()
});

result.contexts.insert(String::from("trace"), context);

result.transaction = parent
.parent()
.into_iter()
.flat_map(|span| span.scope())
.last()
.map(|root| root.name().into());
}
}

result
}

/// Creates an exception [`Event`] from a given [`tracing_core::Event`]
Expand Down
Loading

0 comments on commit 14a7238

Please sign in to comment.