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

Allow skipping arguments in #[instrument]. #359

Merged
merged 6 commits into from
Sep 23, 2019
Merged

Allow skipping arguments in #[instrument]. #359

merged 6 commits into from
Sep 23, 2019

Conversation

hdevalence
Copy link
Contributor

Motivation

This adds a skip directive to the #[instrument] macro that allows
specifying one or more arguments to the function which will not appear
in the generated span. In particular, this means that it's possible to
use #[instrument] on functions with a non-Debug parameter.

Solution

The interior of a skip(arg1, arg2) argument is scanned for identifiers and these are filtered out of the identifier list used to generate the span.

This hopefully closes #11.

This adds a `skip` directive to the `#[instrument]` macro that allows
specifying one or more arguments to the function which will not appear
in the generated span.  In particular, this means that it's possible to
use `#[instrument]` on functions with a non-`Debug` parameter.
lit.span() => compile_error!("expected only a single `skip` argument!")
};
*/
panic!("not sure how to make this return an error -- the examples below return impl ToTokens and get stuck into the output.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed before merging but I'm not sure what the appropriate way to return an error in a proc macro is.

Copy link
Member

@hawkw hawkw Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented-out code above is correct with regards to how you'd display the error to the user — you want to return a quoteed block that expands to the compile_error! macro.

However, unlike the other functions that return impl ToTokens, this function isn't actually returning anything that expands to tokens; it's returning a HashSet. I'd probably make this return a Result<HashSet<Ident>, impl ToTokens> or something? Then, at the call site, you could match the result and insert the compiler error if it's an Err.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hdevalence this is great, thanks so much!

I'd be very happy to merge this once the error handling thing is resolved --- I tried to answer your question in my review. I also commented on a couple of nits that aren't really blockers.

lit.span() => compile_error!("expected only a single `skip` argument!")
};
*/
panic!("not sure how to make this return an error -- the examples below return impl ToTokens and get stuck into the output.");
Copy link
Member

@hawkw hawkw Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented-out code above is correct with regards to how you'd display the error to the user — you want to return a quoteed block that expands to the compile_error! macro.

However, unlike the other functions that return impl ToTokens, this function isn't actually returning anything that expands to tokens; it's returning a HashSet. I'd probably make this return a Result<HashSet<Ident>, impl ToTokens> or something? Then, at the call site, you could match the result and insert the compiler error if it's an Err.

@@ -80,8 +82,8 @@ fn fields() {
.run_with_handle();

with_default(subscriber, || {
my_fn(2, false);
my_fn(3, true);
my_fn(2, false, UnDebug(0), UnDebug(1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add a .only() to the mock fields above, so that we assert no other fields are also added to the generated span? like:

            span.clone().with_field(
                field::mock("arg1")
                    .with_value(&format_args!("2"))
                    .and(field::mock("arg2").with_value(&format_args!("false")))
                    .only(),
            ),

fn my_fn(arg1: usize, arg2: bool) {}
struct UnDebug(pub u32);

#[instrument(target = "my_target", level = "debug", skip(_arg3, _arg4))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take it or leave it, but: rather than adding the skip behavior to this test, I might add a new test for the skip attribute? That way, if there's a regression that breaks only the skip attribute, we could see what behavior is broken based on which test fails. Not a blocker though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to add a new test, but I didn't do it mostly because I don't really understand how the span and subscriber mocking works, so adding into the existing test seemed easier.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple minor nits if you're interested in addressing them as well. Otherwise, this looks great, thanks!

tracing-attributes/src/lib.rs Show resolved Hide resolved
tracing-attributes/tests/instrument.rs Outdated Show resolved Hide resolved
@hawkw
Copy link
Member

hawkw commented Sep 23, 2019

@hdevalence I think the CI build for this is failing because rustfmt needs to be run. If you don't mind running cargo fmt --all & pushing a commit with the result, I'll merge this as soon as it lands.

@hdevalence
Copy link
Contributor Author

Fixed!

@hdevalence
Copy link
Contributor Author

Hmm, I ran cargo fmt --all but the CI is still upset about formatting. Perhaps there's a version mismatch between my local version and the one in CI, or..?

@hdevalence
Copy link
Contributor Author

Oh, maybe it was that I ran cargo fmt before merging changes from suggestions on Github.

@hawkw hawkw merged commit 3552c6c into tokio-rs:master Sep 23, 2019
@hawkw
Copy link
Member

hawkw commented Sep 23, 2019

And merged! Thanks @hdevalence!

@hdevalence hdevalence deleted the skip branch September 23, 2019 23:29
hawkw added a commit that referenced this pull request Sep 25, 2019
Added:

- Optional `skip` argument to `#[instrument]` for excluding function
  parameters from generated spans (#359)

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Sep 25, 2019
Added:

- Optional `skip` argument to `#[instrument]` for excluding function
  parameters from generated spans (#359)

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Sep 25, 2019
Added:

- Optional `skip` argument to `#[instrument]` for excluding function
  parameters from generated spans (#359)

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Sep 26, 2019
Added:

- Optional `skip` argument to `#[instrument]` for excluding function
  parameters from generated spans (#359)

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Sep 26, 2019
Added:

- Optional `skip` argument to `#[instrument]` for excluding function
  parameters from generated spans (#359)

Signed-off-by: Eliza Weisman <[email protected]>
@hawkw
Copy link
Member

hawkw commented Sep 26, 2019

@hdevalence just wanted to let you know that this has now been published in tracing-attributes version 0.1.4!

@hdevalence
Copy link
Contributor Author

Thanks!

hawkw added a commit that referenced this pull request Oct 23, 2019
Added

- Support for destructuring in arguments to `#[instrument]`ed functions
  (#397)
- Generated field for `self` parameters when `#[instrument]`ing methods
  (#397)
- Optional `skip` argument to `#[instrument]` for excluding function
  parametersfrom generated spans (#359)
- Added `dispatcher::set_default` and `subscriber::set_default` APIs,
  which return a drop guard (#388)

Fixed

- Some minor documentation errors (#356, #370)

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Oct 23, 2019
* tracing: bump attrs and core versions

Signed-off-by: Eliza Weisman <[email protected]>

* tracing: prepare to release 0.1.10

Added

- Support for destructuring in arguments to `#[instrument]`ed functions
  (#397)
- Generated field for `self` parameters when `#[instrument]`ing methods
  (#397)
- Optional `skip` argument to `#[instrument]` for excluding function
  parametersfrom generated spans (#359)
- Added `dispatcher::set_default` and `subscriber::set_default` APIs,
  which return a drop guard (#388)

Fixed

- Some minor documentation errors (#356, #370)

Signed-off-by: Eliza Weisman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ignored parameters with #[trace_ignore]
2 participants