Skip to content

Commit fc3ab4c

Browse files
authored
tracing-attributes: document skip in #[instrument] (#468)
Authored-By: David Barsky <[email protected]>
1 parent 364ce2d commit fc3ab4c

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

tracing-attributes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ quote = "1"
4444

4545
[dev-dependencies]
4646
tracing = "0.1"
47+
tracing-futures = "0.2"
4748

4849
[badges]
4950
azure-devops = { project = "tracing/tracing", pipeline = "tokio-rs.tracing", build = "1" }

tracing-attributes/src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ use syn::{
7575
/// Instruments a function to create and enter a `tracing` [span] every time
7676
/// the function is called.
7777
///
78-
/// The generated span's name will be the name of the function, and any
79-
/// arguments to that function will be recorded as fields using `fmt::Debug`.
78+
/// The generated span's name will be the name of the function. Any arguments
79+
/// to that function will be recorded as fields using [`fmt::Debug`]. To skip
80+
/// recording a function's or method's argument, pass the argument's name
81+
/// to the `skip` argument on the `#[instrument]` macro. For example,
82+
/// `skip` can be used when an argument to an instrumented function does
83+
/// not implement [`fmt::Debug`], or to exclude an argument with a verbose
84+
/// or costly Debug implementation. Note that:
85+
/// - multiple argument names can be passed to `skip`.
86+
/// - arguments passed to `skip` do _not_ need to implement `fmt::Debug`.
8087
///
8188
/// # Examples
8289
/// Instrumenting a function:
@@ -110,14 +117,24 @@ use syn::{
110117
/// # fn main() {}
111118
/// ```
112119
///
113-
/// When the `async-await` feature flag is enabled, `async fn`s may also be
114-
/// instrumented:
120+
/// To skip recording an argument, pass the argument's name to the `skip`:
115121
///
116-
/// ```compile_fail
117-
/// // this currently only compiles on nightly.
118-
/// #![feature(async-await)]
122+
/// ```
119123
/// # use tracing_attributes::instrument;
124+
/// struct NonDebug;
125+
///
126+
/// #[instrument(skip(non_debug))]
127+
/// fn my_function(arg: usize, non_debug: NonDebug) {
128+
/// // ...
129+
/// }
130+
/// # fn main() {}
131+
/// ```
132+
///
133+
/// If `tracing_futures` is specified as a dependency in `Cargo.toml`,
134+
/// `async fn`s may also be instrumented:
120135
///
136+
/// ```
137+
/// # use tracing_attributes::instrument;
121138
/// #[instrument]
122139
/// pub async fn my_function() -> Result<(), ()> {
123140
/// // ...
@@ -126,13 +143,9 @@ use syn::{
126143
/// # fn main() {}
127144
/// ```
128145
///
129-
/// # Notes
130-
/// - All argument types must implement `fmt::Debug`
131-
/// - When using `#[instrument]` on an `async fn`, the `tracing_futures` must
132-
/// also be specified as a dependency in `Cargo.toml`.
133-
///
134146
/// [span]: https://docs.rs/tracing/0.1.5/tracing/span/index.html
135147
/// [`tracing`]: https://github.com/tokio-rs/tracing
148+
/// [`fmt::Debug`]: https://doc.rust-lang.org/std/fmt/trait.Debug.html
136149
#[proc_macro_attribute]
137150
pub fn instrument(args: TokenStream, item: TokenStream) -> TokenStream {
138151
let input: ItemFn = syn::parse_macro_input!(item as ItemFn);

0 commit comments

Comments
 (0)