Browse Source

Use ObservationThreadLocalAccessor.KEY where possible

Closes gh-29439
pull/29443/head
Johnny Lim 2 years ago committed by Brian Clozel
parent
commit
710cf1f486
  1. 8
      spring-web/src/main/java/org/springframework/web/filter/reactive/ServerHttpObservationFilter.java
  2. 8
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

8
spring-web/src/main/java/org/springframework/web/filter/reactive/ServerHttpObservationFilter.java

@ -21,6 +21,7 @@ import java.util.Set;
import io.micrometer.observation.Observation; import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -59,11 +60,6 @@ public class ServerHttpObservationFilter implements WebFilter {
private static final Set<String> DISCONNECTED_CLIENT_EXCEPTIONS = Set.of("AbortedException", private static final Set<String> DISCONNECTED_CLIENT_EXCEPTIONS = Set.of("AbortedException",
"ClientAbortException", "EOFException", "EofException"); "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 ObservationRegistry observationRegistry;
private final ServerRequestObservationConvention observationConvention; private final ServerRequestObservationConvention observationConvention;
@ -123,7 +119,7 @@ public class ServerHttpObservationFilter implements WebFilter {
observationContext.setConnectionAborted(true); observationContext.setConnectionAborted(true);
observation.stop(); observation.stop();
}) })
.contextWrite(context -> context.put(MICROMETER_OBSERVATION_KEY, observation)); .contextWrite(context -> context.put(ObservationThreadLocalAccessor.KEY, observation));
} }
private void onTerminalSignal(Observation observation, ServerWebExchange exchange) { private void onTerminalSignal(Observation observation, ServerWebExchange exchange) {

8
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -33,6 +33,7 @@ import java.util.function.Supplier;
import io.micrometer.observation.Observation; import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -75,11 +76,6 @@ class DefaultWebClient implements WebClient {
private static final DefaultClientRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultClientRequestObservationConvention(); private static final DefaultClientRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultClientRequestObservationConvention();
/**
* Aligned with ObservationThreadLocalAccessor#KEY from micrometer-core.
*/
private static final String MICROMETER_OBSERVATION = "micrometer.observation";
private final ExchangeFunction exchangeFunction; private final ExchangeFunction exchangeFunction;
private final UriBuilderFactory uriBuilderFactory; private final UriBuilderFactory uriBuilderFactory;
@ -463,7 +459,7 @@ class DefaultWebClient implements WebClient {
DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, observationRegistry); DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, observationRegistry);
observationContext.setCarrier(requestBuilder); observationContext.setCarrier(requestBuilder);
observation observation
.parentObservation(contextView.getOrDefault(MICROMETER_OBSERVATION, null)) .parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null))
.start(); .start();
ClientRequest request = requestBuilder.build(); ClientRequest request = requestBuilder.build();
observationContext.setUriTemplate((String) request.attribute(URI_TEMPLATE_ATTRIBUTE).orElse(null)); observationContext.setUriTemplate((String) request.attribute(URI_TEMPLATE_ATTRIBUTE).orElse(null));

Loading…
Cancel
Save