Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support of tracing::Span::record operation for sentry-tracing #364

Merged
merged 4 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn extract_span_data(attrs: &span::Attributes) -> (Option<String>, BTreeMap<

#[derive(Default)]
/// Records all fields of [`tracing_core::Event`] for easy access
struct BTreeMapRecorder(pub BTreeMap<String, Value>);
pub(crate) struct BTreeMapRecorder(pub BTreeMap<String, Value>);

impl BTreeMapRecorder {
fn record<T: Into<Value>>(&mut self, field: &Field, value: T) {
Expand Down
21 changes: 21 additions & 0 deletions sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,27 @@ where
client.send_envelope(envelope);
});
}

/// Implement the writing of extra data to span
fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) {
let span = match ctx.span(span) {
Some(s) => s,
_ => return,
};

let mut extensions_holder = span.extensions_mut();
let trace = match extensions_holder.get_mut::<Trace>() {
Some(t) => t,
_ => return,
};

let mut data = BTreeMapRecorder::default();
values.record(&mut data);

for (key, value) in data.0 {
trace.span.data.insert(key, value);
}
}
}

/// Timing informations for a given Span
Expand Down
43 changes: 40 additions & 3 deletions sentry/tests/test_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use tracing_subscriber::prelude::*;
#[test]
fn test_tracing() {
// Don't configure the fmt layer to avoid logging to test output
tracing_subscriber::registry()
let _dispatcher = tracing_subscriber::registry()
.with(sentry_tracing::layer())
.try_init()
.unwrap();
.set_default();

let events = sentry::test::with_captured_events(|| {
sentry::configure_scope(|scope| {
Expand Down Expand Up @@ -54,3 +53,41 @@ fn test_tracing() {
Some("Hello Logging World!".into())
);
}

#[tracing::instrument(fields(span_field))]
fn function() {
tracing::Span::current().record("span_field", &"some data");
}

#[test]
fn test_span_record() {
let _dispatcher = tracing_subscriber::registry()
.with(sentry_tracing::layer())
.set_default();

let options = sentry::ClientOptions {
traces_sample_rate: 1.0,
..Default::default()
};

let envelopes = sentry::test::with_captured_envelopes_options(
|| {
let _span = tracing::span!(tracing::Level::INFO, "span").entered();
function();
},
options,
);

assert_eq!(envelopes.len(), 1);

let envelope_item = envelopes[0].items().next().unwrap();
let transaction = match envelope_item {
sentry::protocol::EnvelopeItem::Transaction(t) => t,
_ => panic!("expected only a transaction item"),
};

assert_eq!(
transaction.spans[0].data["span_field"].as_str().unwrap(),
"some data"
);
}