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

fix: Handle #[instrument(err)] by extracting the error message #453

Merged
merged 3 commits into from Apr 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions sentry-tracing/src/converters.rs
Expand Up @@ -26,6 +26,9 @@ fn extract_event_data(event: &tracing_core::Event) -> (Option<String>, FieldVisi
let message = visitor
.json_values
.remove("message")
// When #[instrument(err)] is used the event does not have a message attached to it.
// the error message is attached to the field "error".
.or_else(|| visitor.json_values.remove("error"))
.and_then(|v| v.as_str().map(|s| s.to_owned()));

(message, visitor)
Expand Down
11 changes: 10 additions & 1 deletion sentry/tests/test_tracing.rs
Expand Up @@ -12,6 +12,11 @@ fn test_tracing() {
.with(sentry_tracing::layer())
.set_default();

#[tracing::instrument(err)]
fn fn_errors() -> Result<(), Box<dyn std::error::Error>> {
Err("I'm broken!".into())
}

let events = sentry::test::with_captured_events(|| {
sentry::configure_scope(|scope| {
scope.set_tag("worker", "worker1");
Expand All @@ -26,9 +31,10 @@ fn test_tracing() {
let err = "NaN".parse::<usize>().unwrap_err();
let err: &dyn std::error::Error = &err;
tracing::error!(err, tagname = "tagvalue");
let _ = fn_errors();
});

assert_eq!(events.len(), 3);
assert_eq!(events.len(), 4);
let mut events = events.into_iter();

let event = events.next().unwrap();
Expand Down Expand Up @@ -95,6 +101,9 @@ fn test_tracing() {
}
_ => panic!("Wrong context type"),
}

let event = events.next().unwrap();
assert_eq!(event.message, Some("I'm broken!".to_string()));
}

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