Skip to content

Commit

Permalink
feat(sentry-tracing): add span fields to breadcrumbs (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger authored Nov 29, 2024
1 parent 05fc7de commit eb902e0
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sentry-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sentry-backtrace = { version = "0.34.0", path = "../sentry-backtrace", optional
[dev-dependencies]
log = "0.4"
sentry = { path = "../sentry", default-features = false, features = ["test"] }
serde_json = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3.1", features = ["fmt", "registry"] }
tokio = { version = "1.8", features = ["rt-multi-thread", "macros", "time"] }
10 changes: 8 additions & 2 deletions sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ impl Visit for FieldVisitor {
}

/// Creates a [`Breadcrumb`] from a given [`tracing_core::Event`]
pub fn breadcrumb_from_event(event: &tracing_core::Event) -> Breadcrumb {
let (message, visitor) = extract_event_data(event);
pub fn breadcrumb_from_event<'context, S>(
event: &tracing_core::Event,
ctx: impl Into<Option<Context<'context, S>>>,
) -> Breadcrumb
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
let (message, visitor) = extract_event_data_with_context(event, ctx.into());
Breadcrumb {
category: Some(event.metadata().target().to_owned()),
ty: "log".into(),
Expand Down
2 changes: 1 addition & 1 deletion sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ where
match (self.event_filter)(event.metadata()) {
EventFilter::Ignore => EventMapping::Ignore,
EventFilter::Breadcrumb => {
EventMapping::Breadcrumb(breadcrumb_from_event(event))
EventMapping::Breadcrumb(breadcrumb_from_event(event, span_ctx))
}
EventFilter::Event => EventMapping::Event(event_from_event(event, span_ctx)),
EventFilter::Exception => {
Expand Down
3 changes: 3 additions & 0 deletions sentry-tracing/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Integration tests for `sentry-tracing`

These tests are split up into 1 file per test to ensure they don't run concurrently.
34 changes: 34 additions & 0 deletions sentry-tracing/tests/breadcrumbs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mod shared;

#[test]
fn breadcrumbs_should_capture_span_fields() {
let transport = shared::init_sentry();

foo();

let data = transport.fetch_and_clear_envelopes();
assert_eq!(data.len(), 2);

let event = data.first().expect("should have 1 event");
let event = match event.items().next().unwrap() {
sentry::protocol::EnvelopeItem::Event(event) => event,
unexpected => panic!("Expected event, but got {:#?}", unexpected),
};

assert_eq!(event.breadcrumbs.len(), 1);
assert_eq!(
event.breadcrumbs[0].data["foo:contextual_value"],
serde_json::Value::from(42)
);
assert_eq!(
event.breadcrumbs[0].message,
Some("executing foo".to_owned())
);
}

#[tracing::instrument(fields(contextual_value = 42))]
fn foo() {
tracing::info!("executing foo");

tracing::error!("boom!");
}
24 changes: 24 additions & 0 deletions sentry-tracing/tests/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use sentry::{ClientOptions, Hub};
use sentry_core::test::TestTransport;

use std::sync::Arc;

pub fn init_sentry() -> Arc<TestTransport> {
use tracing_subscriber::prelude::*;

let transport = TestTransport::new();
let options = ClientOptions {
dsn: Some("https://[email protected]/test".parse().unwrap()),
transport: Some(Arc::new(transport.clone())),
sample_rate: 1.0,
traces_sample_rate: 1.0,
..ClientOptions::default()
};
Hub::current().bind_client(Some(Arc::new(options.into())));

let _ = tracing_subscriber::registry()
.with(sentry_tracing::layer().enable_span_attributes())
.try_init();

transport
}
27 changes: 2 additions & 25 deletions sentry-tracing/tests/tracing.rs → sentry-tracing/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
use sentry::{ClientOptions, Hub};
use sentry_core::test::TestTransport;

use std::sync::Arc;

fn init_sentry() -> Arc<TestTransport> {
use tracing_subscriber::prelude::*;

let transport = TestTransport::new();
let options = ClientOptions {
dsn: Some("https://[email protected]/test".parse().unwrap()),
transport: Some(Arc::new(transport.clone())),
sample_rate: 1.0,
traces_sample_rate: 1.0,
..ClientOptions::default()
};
Hub::current().bind_client(Some(Arc::new(options.into())));

let _ = tracing_subscriber::registry()
.with(sentry_tracing::layer().enable_span_attributes())
.try_init();

transport
}
mod shared;

#[tracing::instrument(fields(tags.tag = "key", not_tag = "value"))]
fn function_with_tags(value: i32) {
Expand All @@ -30,7 +7,7 @@ fn function_with_tags(value: i32) {

#[test]
fn should_instrument_function_with_event() {
let transport = init_sentry();
let transport = shared::init_sentry();

function_with_tags(1);

Expand Down

0 comments on commit eb902e0

Please sign in to comment.