Propagate context in reactive HTTP server and client

Prior to this commit, the ServerHttpObservationFilter would not add
the current observation as a key in the Reactor context, preventing
from being used or propagated during the HTTP exchange handling.

Also, the client instrumentation in `DefaultWebClient` would start
the observation once the request is fully formed and immutable,
preventing the context from being propagated through HTTP request
headers.

This commit fixes both uses cases now by:

* adding the current observation as a key in the reactor context
  on the server side
* using the `ClientRequest.Builder` as a Carrier on the client side

Closes gh-29388
This commit is contained in:
Marcin Grzejszczak
2022-10-26 20:22:47 +02:00
committed by Brian Clozel
parent 396336fdcc
commit c03ccb2e6c
7 changed files with 111 additions and 25 deletions

View File

@@ -59,6 +59,11 @@ public class ServerHttpObservationFilter implements WebFilter {
private static final Set<String> DISCONNECTED_CLIENT_EXCEPTIONS = Set.of("AbortedException",
"ClientAbortException", "EOFException", "EofException");
/**
* Aligned with ObservationThreadLocalAccessor#KEY from micrometer-core.
*/
private static final String MICROMETER_OBSERVATION_KEY = "micrometer.observation";
private final ObservationRegistry observationRegistry;
private final ServerRequestObservationConvention observationConvention;
@@ -117,7 +122,8 @@ public class ServerHttpObservationFilter implements WebFilter {
.doOnCancel(() -> {
observationContext.setConnectionAborted(true);
observation.stop();
});
})
.contextWrite(context -> context.put(MICROMETER_OBSERVATION_KEY, observation));
}
private void onTerminalSignal(Observation observation, ServerWebExchange exchange) {