I need to instrument spans with different names dynamically. How can I create a tracing span with dynamic naming?
use tracing; // 0.1.22
fn main() {
let name: &'static str = "foo bar";
let span = tracing::span!(tracing::Level::TRACE, name);
let span_handle = span.enter();
tracing::info!("Hello, world!");
drop(span_handle);
}
error[E0435]: attempt to use a non-constant value in a constant
--> src/main.rs:5:54
|
5 | let span = tracing::span!(tracing::Level::TRACE, name);
| ^^^^ non-constant value
Instead of attempting to set the span's name dynamically, add a field:
let value = "forty-two";
let span = tracing::span!(tracing::Level::TRACE, "foo bar", value = value);
From the docs:
A span consists of fields, user-defined key-value pairs of arbitrary data that describe the context the span represents