Skip to content

Commit

Permalink
tracing-attributes: implement skip_all
Browse files Browse the repository at this point in the history
Using `#[instrument(skip_all)]` is equivalent to `#[instrument(skip(foo,
bar..))]` for all the parameters.
  • Loading branch information
jsgf committed Sep 10, 2021
1 parent 3bfddc6 commit f28a62a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 26 additions & 3 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,13 @@ use syn::{
/// - multiple argument names can be passed to `skip`.
/// - arguments passed to `skip` do _not_ need to implement `fmt::Debug`.
///
/// You can also use `skip_all` to skip all arguments.
///
/// ## Examples
///
/// ```
/// # use tracing_attributes::instrument;
/// # use std::collections::HashMap;
/// // This type doesn't implement `fmt::Debug`!
/// struct NonDebug;
///
Expand All @@ -178,6 +181,12 @@ use syn::{
/// fn my_function(arg: usize, non_debug: NonDebug) {
/// // ...
/// }
///
/// // These arguments are huge
/// #[instrument(skip_all)]
/// fn my_big_data_function(large: Vec<u8>, also_large: HashMap<String, String>) {
/// // ...
/// }
/// ```
///
/// Skipping the `self` parameter:
Expand Down Expand Up @@ -257,8 +266,8 @@ use syn::{
/// }
/// ```
///
/// This can be used in conjunction with `skip` to record only some fields of a
/// struct:
/// This can be used in conjunction with `skip` or `skip_all` to record only
/// some fields of a struct:
/// ```
/// # use tracing_attributes::instrument;
/// // Remember the struct with the very large `data` field from the earlier
Expand Down Expand Up @@ -644,7 +653,7 @@ fn gen_block(
let quoted_fields: Vec<_> = param_names
.iter()
.filter(|(param, _)| {
if args.skips.contains(param) {
if args.skip_all || args.skips.contains(param) {
return false;
}

Expand Down Expand Up @@ -754,6 +763,7 @@ struct InstrumentArgs {
name: Option<LitStr>,
target: Option<LitStr>,
skips: HashSet<Ident>,
skip_all: bool,
fields: Option<Fields>,
err: bool,
/// Errors describing any unrecognized parse inputs that we skipped.
Expand Down Expand Up @@ -872,8 +882,20 @@ impl Parse for InstrumentArgs {
if !args.skips.is_empty() {
return Err(input.error("expected only a single `skip` argument"));
}
if args.skip_all {
return Err(input.error("expected either `skip` or `skip_all` argument"));
}
let Skips(skips) = input.parse()?;
args.skips = skips;
} else if lookahead.peek(kw::skip_all) {
if args.skip_all {
return Err(input.error("expected only a single `skip_all` argument"));
}
if !args.skips.is_empty() {
return Err(input.error("expected either `skip` or `skip_all` argument"));
}
let _ = input.parse::<kw::skip_all>()?;
args.skip_all = true;
} else if lookahead.peek(kw::fields) {
if args.fields.is_some() {
return Err(input.error("expected only a single `fields` argument"));
Expand Down Expand Up @@ -1158,6 +1180,7 @@ fn param_names(pat: Pat, record_type: RecordType) -> Box<dyn Iterator<Item = (Id
mod kw {
syn::custom_keyword!(fields);
syn::custom_keyword!(skip);
syn::custom_keyword!(skip_all);
syn::custom_keyword!(level);
syn::custom_keyword!(target);
syn::custom_keyword!(name);
Expand Down
3 changes: 3 additions & 0 deletions tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ fn skip() {
#[instrument(target = "my_target", level = "debug", skip(_arg2, _arg3))]
fn my_fn(arg1: usize, _arg2: UnDebug, _arg3: UnDebug) {}

#[instrument(target = "my_target", level = "debug", skip_all)]
fn my_fn2(_arg1: usize, _arg2: UnDebug, _arg3: UnDebug) {}

let span = span::mock()
.named("my_fn")
.at_level(Level::DEBUG)
Expand Down

0 comments on commit f28a62a

Please sign in to comment.