-
This is the simplified version of the code I am facing the problem with. The scopes are correct, but the rest of the code is more complex (calling of use tracing::{metadata::LevelFilter, Span};
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*};
fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_span_events(FmtSpan::FULL)
.with_filter(LevelFilter::INFO),
)
.init();
tracing::info_span!("parent", my_field = tracing::field::Empty).in_scope(|| {
tracing::info_span!("child").in_scope(|| {
Span::current().record("my_field", 42);
});
});
} Logs
Is there any way I can somehow |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
After some thinking I got to this structure: tracing::info_span!("parent", my_field = tracing::field::Empty).in_scope(|| {
Span::current().record("my_field", 42);
tracing::info_span!("child").in_scope(|| {
// rest of the code
});
}); And in my code it looks like this: // convert from this...
#[tracing::instrument(name = "child")]
async fn do_thing() {
Span::current().record("my_field", 42);
// rest of the code
}
// ...to this
async fn do_thing() {
Span::current().record("my_field", 42);
async {
// rest of the code
}
.instrument(tracing::info_span!("child"))
.await
} Essentially, I'm just extracting the Still, interesting if there's a better way of doing this. It's unfortunate I can't use |
Beta Was this translation helpful? Give feedback.
-
For a solution, we implemented and released the crate just for that:
|
Beta Was this translation helpful? Give feedback.
For a solution, we implemented and released the crate just for that:
tracing-record-hierarchical
It allows you to
record_hierarchical
ly fields to the parentSpan
from anywhere in its children.