[][src]Attribute Macro tracing::instrument

#[instrument]

Instruments a function to create and enter a tracing span every time the function is called.

The generated span's name will be the name of the function. Any arguments to that function will be recorded as fields using fmt::Debug. To skip recording a function's or method's argument, pass the argument's name to the skip argument on the #[instrument] macro. For example, skip can be used when an argument to an instrumented function does not implement fmt::Debug, or to exclude an argument with a verbose or costly Debug implementation. Note that:

  • multiple argument names can be passed to skip.
  • arguments passed to skip do not need to implement fmt::Debug.

Examples

Instrumenting a function:

#[instrument]
pub fn my_function(my_arg: usize) {
    // This event will be recorded inside a span named `my_function` with the
    // field `my_arg`.
    tracing::info!("inside my_function!");
    // ...
}

Setting the level for the generated span:

#[instrument(level = "debug")]
pub fn my_function() {
    // ...
}

Overriding the generated span's target:

#[instrument(target = "my_target")]
pub fn my_function() {
    // ...
}

To skip recording an argument, pass the argument's name to the skip:

struct NonDebug;

#[instrument(skip(non_debug))]
fn my_function(arg: usize, non_debug: NonDebug) {
    // ...
}

If tracing_futures is specified as a dependency in Cargo.toml, async fns may also be instrumented:

#[instrument]
pub async fn my_function() -> Result<(), ()> {
    // ...
}