Skip to content

Commit

Permalink
fix: Handle #[instrument(err)] by extracting the error message (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvnvelasco authored Apr 11, 2022
1 parent 5c338e7 commit 799f211
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 799f211

Please sign in to comment.