Im using Tracing in my project and there is one thing Im not able to figure out: Access value (that I set in my span when I create it) of span's key in one of my Layer
My layer looks like
impl<S> Layer<S> for CustomLayer where S: Subscriber {
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
Interest::sometimes() //hardcoding so enabled() will be called everytime a span is created
}
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool {
if metadata.is_span() {
// How do I access value of key here?
}
true
}
}
I don’t think that’s possible from enabled. From the docs for enabled
Note: This method (and Layer::register_callsite) determine whether a span or event is globally enabled, not whether the individual layer will be notified about that span or event. This is intended to be used by layers that implement filtering for the entire stack.
I think you need to define the new_span method for your layer which gives you access to Attributes which should include the values.
(I’ve never actually used tracing so take this with a grain of salt)
This is intended to be used by layers that implement filtering for the entire stack.
This statement from what you quoted is important for me. I want my Layer to act like a filter. CustomLayer is the outermost layer and when enabled() returns false, the entire layer chain will short-circuit and span will be ignored.
Looks like Attributes has the stuff I want, so if I impl new_span() I can get to it. But then how do I "connect" enabled() and new_span()?
If value in Attributes is say X, then I want enabled() to return false.
Maybe I can store state (in previous comment that would be X) in CustomLayer and when enabled() gets called, I can do some checks and return true/false.
Not sure if this can be done. I will try it anyways.
Yea I think that's right skimming the docs a bit more. The layer holds some state that if I had to guess its keyed by the span id in your case and then on the enabled call you can get that data from ctx.current_span().id() it looks like.