[][src]Attribute Macro tracing_attributes::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, and any arguments to that function will be recorded as fields using 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() {
    // ...
}

When the async-await feature flag is enabled, async fns may also be instrumented:

This example deliberately fails to compile
// this compiles only with the `async-await` feature flag enabled

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

Notes

  • All argument types must implement fmt::Debug
  • When using #[instrument] on an async fn, the tracing_futures must also be specified as a dependency in Cargo.toml.