I want to add key-value pairs to my Tokio tracing at runtime. How can I manage this?
I tried the following:
tracing = "0.1.40"
tracing-logfmt = "0.3.4"
tracing-subscriber = { version = "0.3.18", features = [
"fmt",
], default-features = false }
use tracing::{info, info_span, level_filters::LevelFilter};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
fn main() {
tracing_subscriber::registry()
.with(LevelFilter::INFO) .with(tracing_logfmt::layer())
.init();
info!(key1 = "value1", "message");
let k = "key2";
let v = "val2";
let _span1 = info_span!("span1", k = v);
let _guard1 = _span1.enter();
info!(key1 = "value1", "message");
}
$ cargo run --bin test
Compiling rkm v0.1.0 (/home/vagrant/Code/atc-github.azure.cloud.bmw/rkm)
warning: unused variable: `k`
--> src/bin/test.rs:12:9
|
12 | let k = "key2";
| ^ help: if this is intentional, prefix it with an underscore: `_k`
|
= note: `#[warn(unused_variables)]` on by default
warning: `rkm` (bin "test") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.35s
Running `target/debug/test`
ts=2024-06-06T14:00:30.154669291Z level=info target=test message=message key1=value1
ts=2024-06-06T14:00:30.155327356Z level=info target=test span=span1 span_path=span1 message=message key1=value1 k=val2
Unfortunately, I'm not able to set the name of the new key to key2
. Furthermore, I do not want to have the additional pairs span
and span_path
.
How can I achive that?
Thanks for your support!