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

want way to ask #[instrument] to trace the return value #1281

Closed
ijackson opened this issue Mar 4, 2021 · 5 comments · Fixed by #1716
Closed

want way to ask #[instrument] to trace the return value #1281

ijackson opened this issue Mar 4, 2021 · 5 comments · Fixed by #1716
Labels
crate/attributes Related to the `tracing-attributes` crate kind/feature New feature or request

Comments

@ijackson
Copy link

ijackson commented Mar 4, 2021

Feature Request

Crates

tracing-attributes

Motivation

Tracing's #[instrument] is very nice for printf-style debugging as well as post-hoc analysis. But as far as I can tell it does not print the return value of the annotated function (although you can use err to print the Err of a Result).

Proposal

I suggest #[instrument(return)] to make it print the return value on function exit.

Alternatives

Adding a dbg! call to the call sites seems the most obvious workaround but is far less convenient.

@hawkw hawkw added the kind/feature New feature or request label Mar 6, 2021
@hawkw
Copy link
Member

hawkw commented Mar 11, 2021

Something like this would definitely be nice, and should be doable (using the same techniques we use for #[instrument(err)]). Are you interested in writing a PR? I'm happy to give some pointers.

@hawkw hawkw added the crate/attributes Related to the `tracing-attributes` crate label Mar 11, 2021
@ijackson
Copy link
Author

Something like this would definitely be nice, and should be doable (using the same techniques we use for #[instrument(err)]). Are you interested in writing a PR? I'm happy to give some pointers.

Thanks. I've put it on my list but it might be a while before it gets to the top.

@ciuncan
Copy link
Contributor

ciuncan commented Oct 10, 2021

hi @hawkw , @ijackson , I recently opened an issue #1630 and a PR #1631 for adding err_dbg to print errors with Debug impls. Maybe this could also benefit from the same (Display vs. Debug dichotomy), maybe via return_dbg? I am willing to open a PR for the issue described here as well as the return_dbg addition, if no one else is working on this.

@hkmatsumoto
Copy link
Contributor

I want this feature very much so will work on this next week.

In order to implement #[instrument(return)] I reckon I need to add return field to InstrumentArgs and some code for parsing the arguments. Am I on the right track @hawkw ?

pub(crate) struct InstrumentArgs {
level: Option<Level>,
pub(crate) name: Option<LitStr>,
target: Option<LitStr>,
pub(crate) skips: HashSet<Ident>,
pub(crate) fields: Option<Fields>,
pub(crate) err_mode: Option<ErrorMode>,
/// Errors describing any unrecognized parse inputs that we skipped.
parse_warnings: Vec<syn::Error>,
}

@ciuncan
Copy link
Contributor

ciuncan commented Nov 15, 2021

I want this feature very much so will work on this next week.

In order to implement #[instrument(return)] I reckon I need to add return field to InstrumentArgs and some code for parsing the arguments. Am I on the right track @hawkw ?

pub(crate) struct InstrumentArgs {
level: Option<Level>,
pub(crate) name: Option<LitStr>,
target: Option<LitStr>,
pub(crate) skips: HashSet<Ident>,
pub(crate) fields: Option<Fields>,
pub(crate) err_mode: Option<ErrorMode>,
/// Errors describing any unrecognized parse inputs that we skipped.
parse_warnings: Vec<syn::Error>,
}

Hi @hkmatsumoto , allow me to jump in. Yes you are on right track. That struct is the representation of the parsed arguments of instrument macro. So having a return or rather return_mode field makes sense. It may be a bool representing its presence but I suggest you make it similar to err_mode field, since you may want to support emitting events with return value presented via either Display or Debug implementation. In my recent PR, as per @hawkw 's suggestion, we made it err(Debug), err(Display) or just err (existing way, prior to my PR, which is the same as err(Display)). You can follow the same here for consistency.

Feel free to check the changes and discussion in #1631, there will be lots of inspiration points. Basically you'll need to determine presence and mode of return keyword in macro invocation (at Parse impl of InsturmentArgs), save in InstrumentArgs, then at gen_block you should evaluate this field and emit event if necessary. You also need to handle async and non-async case.

To debug your implementation, I suggest using VS Code's "expand macro recursively" feature (after a code change you may need to close and reopen VSCode due to some limitation, though), possibly on a test example function you'll add https://github.com/tokio-rs/tracing/pull/1631/files#diff-74a47b15914675cd4c2a0c370bbec4a3d2d6e50d939dda795497ec67de050e8fR178-R181 and examine the expansion output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/attributes Related to the `tracing-attributes` crate kind/feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants