[−][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 fn
s may also be
instrumented:
ⓘThis example deliberately fails to compile
// this currently only compiles on nightly. #![feature(async-await)] #[instrument] pub async fn my_function() -> Result<(), ()> { // ... }
Notes
- All argument types must implement
fmt::Debug
- When using
#[instrument]
on anasync fn
, thetracing_futures
must also be specified as a dependency inCargo.toml
.