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(tracing): Add line information from tracing #430

Merged
merged 3 commits into from
Feb 4, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- Remove unused `serde_json` feature from `curl` dependency. ([#420](http://github.com/getsentry/sentry-rust/pull/420))
- `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))
- `sentry-tracing`: Add line numbers and tags into custom Contexts sections. ([#430](http://github.com/getsentry/sentry-rust/pull/430))

**Thank you**:

Expand Down
36 changes: 34 additions & 2 deletions sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,38 @@ pub fn breadcrumb_from_event(event: &tracing_core::Event) -> Breadcrumb {
}
}

fn contexts_from_event(
event: &tracing_core::Event,
event_tags: BTreeMap<String, Value>,
) -> BTreeMap<String, sentry_core::protocol::Context> {
let event_meta = event.metadata();
let mut location_map = BTreeMap::new();
if let Some(module_path) = event_meta.module_path() {
location_map.insert("module_path".to_string(), module_path.into());
}
if let Some(file) = event_meta.file() {
location_map.insert("file".to_string(), file.into());
}
if let Some(line) = event_meta.line() {
location_map.insert("line".to_string(), line.into());
}

let mut context = BTreeMap::new();
if !event_tags.is_empty() {
context.insert(
"Rust Tracing Tags".to_string(),
sentry_core::protocol::Context::Other(event_tags),
);
}
if !location_map.is_empty() {
context.insert(
"Rust Tracing Location".to_string(),
flub marked this conversation as resolved.
Show resolved Hide resolved
sentry_core::protocol::Context::Other(location_map),
);
}
context
}

/// Creates an [`Event`] from a given [`tracing_core::Event`]
pub fn event_from_event<S>(event: &tracing_core::Event, _ctx: Context<S>) -> Event<'static>
where
Expand All @@ -114,7 +146,7 @@ where
logger: Some(event.metadata().target().to_owned()),
level: convert_tracing_level(event.metadata().level()),
message,
extra: visitor.json_values,
contexts: contexts_from_event(event, visitor.json_values),
..Default::default()
}
}
Expand All @@ -133,8 +165,8 @@ where
logger: Some(event.metadata().target().to_owned()),
level: convert_tracing_level(event.metadata().level()),
message,
extra: visitor.json_values,
exception: visitor.exceptions.into(),
contexts: contexts_from_event(event, visitor.json_values),
..Default::default()
}
}
35 changes: 33 additions & 2 deletions sentry/tests/test_tracing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "test")]

use log_ as log;
use sentry::protocol::{Context, Value};
use tracing_ as tracing;
use tracing_subscriber::prelude::*;

Expand All @@ -17,14 +18,14 @@ fn test_tracing() {
});

tracing::info!("Hello Tracing World!");
tracing::error!("Shit's on fire yo");
tracing::error!(tagname = "tagvalue", "Shit's on fire yo");

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

let err = "NaN".parse::<usize>().unwrap_err();
let err: &dyn std::error::Error = &err;
tracing::error!(err);
tracing::error!(err, tagname = "tagvalue");
});

assert_eq!(events.len(), 3);
Expand All @@ -40,6 +41,21 @@ fn test_tracing() {
event.breadcrumbs[0].message,
Some("Hello Tracing World!".into())
);
match event.contexts.get("Rust Tracing Tags").unwrap() {
Context::Other(tags) => {
let value = Value::String("tagvalue".to_string());
assert_eq!(*tags.get("tagname").unwrap(), value);
}
_ => panic!("Wrong context type"),
}
match event.contexts.get("Rust Tracing Location").unwrap() {
Context::Other(tags) => {
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
}
_ => panic!("Wrong context type"),
}

let event = events.next().unwrap();
assert_eq!(event.tags["worker"], "worker1");
Expand All @@ -64,6 +80,21 @@ fn test_tracing() {
event.exception[0].value,
Some("invalid digit found in string".into())
);
match event.contexts.get("Rust Tracing Tags").unwrap() {
Context::Other(tags) => {
let value = Value::String("tagvalue".to_string());
assert_eq!(*tags.get("tagname").unwrap(), value);
}
_ => panic!("Wrong context type"),
}
match event.contexts.get("Rust Tracing Location").unwrap() {
Context::Other(tags) => {
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
flub marked this conversation as resolved.
Show resolved Hide resolved
}
_ => panic!("Wrong context type"),
}
}

#[tracing::instrument(fields(span_field))]
Expand Down