Affects: \v6.0.10 - Spring Boot v3.1.1
Hi, I made a post about this in the Micrometer Slack channel and was referred here.
I'm currently in the process of migrating one of our MVC APIs to SB3/SF6, and we have hooked into an interceptor for RestTemplate
to be able to grab a specific header off of the request and set that header to the current Zipkin span name (though I'm told this is not recommended).
During my testing, I found that I am unable to access the current span or observation within the interceptor as it is null.
Here is a sample project highlighting this issue: https://github.com/JustinJNEAL/micrometer-testing
Comment From: bclozel
Thanks for the minimal and complete repro @JustinJNEAL.
Previously, the instrumentation was indeed done at the interceptor level and this was causing a lot of issues. This is now done deeper within RestTemplate
itself: when the interceptors are executed, the observation has not yet started. This is especially useful when interceptors are changing the request or performing I/O themselves for authentication purposes.
In this case, since setting the contextual name seems to be one of the goals. You can achieve that by implementing a custom ClientRequestObservationConvention
(or here, extending the DefaultClientRequestObservationConvention
and overriding the relevant methods).
If this is not enough for your use case, maybe an ObservationFilter
(getting the context from the observation, which contains the request) would work? Spring Framework only depends on the Observation API and doesn't bring in the Tracing dependency so I'm afraid we don't have a dedicated contract for that.
Comment From: JustinJNEAL
Thank you, this is super helpful!
Comment From: bclozel
Thanks for the update! Let's keep this issue closed for now - but let me know if something's missing and we can reopen it.
Comment From: JustinJNEAL
@bclozel I've updated my example to include a dummy header sent along with the GET request. I've also extended DefaultClientRequestObservationConvention
and overrode getLowCardinalityKeyValues
to also grab the header from the context.
In my testing, sometimes I'm able to get the headers, but the majority of the time the headers are empty. When the headers are empty, it seems that no attributes are attached to the context such as name. I can't quite figure out why this is happening.
Would this be an issue with my config or is there a bug causing this?
Comment From: bclozel
The ObservationConvention
is really called twice per observation: once when the tracing is being set up and the request is not created yet (it will add outgoing headers with the tracing information), and once again when the observation is done to record the metric.
Does this explain the behavior you're seeing?
Comment From: JustinJNEAL
Thanks for the quick response. Yes, that's what I'm seeing