To confirm what you're after, is it dynamic naming of your spans, ie. the name is decided at runtime? or do you just want to be able to declare the name of your spans in constants and use those when you create your spans?
If it's the former, then I don't believe you're able to, because the macro explicitly only accepts literals
. However if its the latter, you can work around it via basic macros.
Playground
macro_rules! prefix_name {
($name:literal) => {
concat!("prefix::", $name)
};
}
macro_rules! span_name {
() => {
"foo::bar"
};
}
Then you can use it below
fn main() {
let span = tracing::span!(tracing::Level::TRACE, prefix_name!("foo bar"));
let span_handle = span.enter();
tracing::info!("Hello, world!");
drop(span_handle);
let span = tracing::span!(tracing::Level::TRACE, span_name!());
let span_handle = span.enter();
tracing::info!("Hello, world!");
drop(span_handle);
}
To be honest I can only take a guess at why this works (so anyone who knows for sure, please feel free to correct me). But I would assume that macro expansion occurs prior to actually trying to compile the code. So by the time the compiler is trying to compile that span, the macro has already been expanded to the underlying literal.