Skip to content

Commit 8947917

Browse files
author
Floris Bruynooghe
authored
feat(tracing): Add line information from tracing (#430)
This adds the line information that tracing provides back into the event. It also moves around the tags into a custom context instead of the "extra" grab-bag as this is less likely to step on the user's ad-hoc toes.
1 parent 5b20cdd commit 8947917

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- Remove unused `serde_json` feature from `curl` dependency. ([#420](http://github.com/getsentry/sentry-rust/pull/420))
1313
- `sentry-tracing`: When converting a `tracing` event to a `sentry` event, don't create an exception if the original event doesn't have one ([#423](https://github.com/getsentry/sentry-rust/pull/423))
14+
- `sentry-tracing`: Add line numbers and tags into custom Contexts sections. ([#430](http://github.com/getsentry/sentry-rust/pull/430))
1415

1516
**Thank you**:
1617

sentry-tracing/src/converters.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,38 @@ pub fn breadcrumb_from_event(event: &tracing_core::Event) -> Breadcrumb {
103103
}
104104
}
105105

106+
fn contexts_from_event(
107+
event: &tracing_core::Event,
108+
event_tags: BTreeMap<String, Value>,
109+
) -> BTreeMap<String, sentry_core::protocol::Context> {
110+
let event_meta = event.metadata();
111+
let mut location_map = BTreeMap::new();
112+
if let Some(module_path) = event_meta.module_path() {
113+
location_map.insert("module_path".to_string(), module_path.into());
114+
}
115+
if let Some(file) = event_meta.file() {
116+
location_map.insert("file".to_string(), file.into());
117+
}
118+
if let Some(line) = event_meta.line() {
119+
location_map.insert("line".to_string(), line.into());
120+
}
121+
122+
let mut context = BTreeMap::new();
123+
if !event_tags.is_empty() {
124+
context.insert(
125+
"Rust Tracing Tags".to_string(),
126+
sentry_core::protocol::Context::Other(event_tags),
127+
);
128+
}
129+
if !location_map.is_empty() {
130+
context.insert(
131+
"Rust Tracing Location".to_string(),
132+
sentry_core::protocol::Context::Other(location_map),
133+
);
134+
}
135+
context
136+
}
137+
106138
/// Creates an [`Event`] from a given [`tracing_core::Event`]
107139
pub fn event_from_event<S>(event: &tracing_core::Event, _ctx: Context<S>) -> Event<'static>
108140
where
@@ -114,7 +146,7 @@ where
114146
logger: Some(event.metadata().target().to_owned()),
115147
level: convert_tracing_level(event.metadata().level()),
116148
message,
117-
extra: visitor.json_values,
149+
contexts: contexts_from_event(event, visitor.json_values),
118150
..Default::default()
119151
}
120152
}
@@ -133,8 +165,8 @@ where
133165
logger: Some(event.metadata().target().to_owned()),
134166
level: convert_tracing_level(event.metadata().level()),
135167
message,
136-
extra: visitor.json_values,
137168
exception: visitor.exceptions.into(),
169+
contexts: contexts_from_event(event, visitor.json_values),
138170
..Default::default()
139171
}
140172
}

sentry/tests/test_tracing.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![cfg(feature = "test")]
22

33
use log_ as log;
4+
use sentry::protocol::{Context, Value};
45
use tracing_ as tracing;
56
use tracing_subscriber::prelude::*;
67

@@ -17,14 +18,14 @@ fn test_tracing() {
1718
});
1819

1920
tracing::info!("Hello Tracing World!");
20-
tracing::error!("Shit's on fire yo");
21+
tracing::error!(tagname = "tagvalue", "Shit's on fire yo");
2122

2223
log::info!("Hello Logging World!");
2324
log::error!("Shit's on fire yo");
2425

2526
let err = "NaN".parse::<usize>().unwrap_err();
2627
let err: &dyn std::error::Error = &err;
27-
tracing::error!(err);
28+
tracing::error!(err, tagname = "tagvalue");
2829
});
2930

3031
assert_eq!(events.len(), 3);
@@ -40,6 +41,21 @@ fn test_tracing() {
4041
event.breadcrumbs[0].message,
4142
Some("Hello Tracing World!".into())
4243
);
44+
match event.contexts.get("Rust Tracing Tags").unwrap() {
45+
Context::Other(tags) => {
46+
let value = Value::String("tagvalue".to_string());
47+
assert_eq!(*tags.get("tagname").unwrap(), value);
48+
}
49+
_ => panic!("Wrong context type"),
50+
}
51+
match event.contexts.get("Rust Tracing Location").unwrap() {
52+
Context::Other(tags) => {
53+
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
54+
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
55+
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
56+
}
57+
_ => panic!("Wrong context type"),
58+
}
4359

4460
let event = events.next().unwrap();
4561
assert_eq!(event.tags["worker"], "worker1");
@@ -64,6 +80,21 @@ fn test_tracing() {
6480
event.exception[0].value,
6581
Some("invalid digit found in string".into())
6682
);
83+
match event.contexts.get("Rust Tracing Tags").unwrap() {
84+
Context::Other(tags) => {
85+
let value = Value::String("tagvalue".to_string());
86+
assert_eq!(*tags.get("tagname").unwrap(), value);
87+
}
88+
_ => panic!("Wrong context type"),
89+
}
90+
match event.contexts.get("Rust Tracing Location").unwrap() {
91+
Context::Other(tags) => {
92+
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
93+
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
94+
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
95+
}
96+
_ => panic!("Wrong context type"),
97+
}
6798
}
6899

69100
#[tracing::instrument(fields(span_field))]

0 commit comments

Comments
 (0)