Skip to content

How to report errors on the current spans with '?' operator #2459

Answered by hawkw
wcarmon asked this question in Q&A
Discussion options

You must be logged in to vote

The #[instrument] attribute macro has the ability to automatically record an event when the annotated function returns an error, like this:

#[tracing::instrument(err)]
fn do_something() -> anyhow::Result<()> {
   // ...
}

If you're using #[instrument] to generate spans, you can use this attribute to automatically emit events for errors returned by instrumented functions.

However, this will only work if you want to have a span generated for that function as well. If you're recording spans using the span! macros, or if the functions which you want to record error events from do not have their own spans but are instead inside the span of a calling function, you may not want to use #[instrume…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@wcarmon
Comment options

@hawkw
Comment options

hawkw Feb 8, 2023
Maintainer

@hawkw
Comment options

hawkw Feb 8, 2023
Maintainer

Answer selected by wcarmon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2456 on February 07, 2023 17:32.