Align the context propagation entries with the rest of the portfolio.

Closes #4218
This commit is contained in:
Marcin Grzejszczak
2022-10-21 17:52:49 +02:00
committed by Mark Paluch
parent 2d63d6006d
commit d4daa305a8
3 changed files with 15 additions and 10 deletions

View File

@@ -85,7 +85,8 @@ public class ContextProviderFactory {
Observation currentObservation = observationRegistry.getCurrentObservation(); Observation currentObservation = observationRegistry.getCurrentObservation();
if (currentObservation != null) { if (currentObservation != null) {
requestContext.put(Observation.class, currentObservation); // Aligned with ObservationThreadLocalAccessor.KEY
requestContext.put("micrometer.observation", currentObservation);
} }
return requestContext; return requestContext;

View File

@@ -42,6 +42,11 @@ public class MongoObservationCommandListener implements CommandListener {
private static final Log log = LogFactory.getLog(MongoObservationCommandListener.class); private static final Log log = LogFactory.getLog(MongoObservationCommandListener.class);
/**
* Aligns with ObservationThreadLocalAccessor.KEY.
*/
private static final String MICROMETER_OBSERVATION_KEY = "micrometer.observation";
private final ObservationRegistry observationRegistry; private final ObservationRegistry observationRegistry;
private final @Nullable ConnectionString connectionString; private final @Nullable ConnectionString connectionString;
@@ -114,8 +119,7 @@ public class MongoObservationCommandListener implements CommandListener {
observation.start(); observation.start();
requestContext.put(Observation.class, observation); requestContext.put(MICROMETER_OBSERVATION_KEY, observation);
requestContext.put(MongoHandlerContext.class, observationContext);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug(
@@ -132,12 +136,12 @@ public class MongoObservationCommandListener implements CommandListener {
return; return;
} }
Observation observation = requestContext.getOrDefault(Observation.class, null); Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
if (observation == null) { if (observation == null) {
return; return;
} }
MongoHandlerContext context = requestContext.get(MongoHandlerContext.class); MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
context.setCommandSucceededEvent(event); context.setCommandSucceededEvent(event);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@@ -156,12 +160,12 @@ public class MongoObservationCommandListener implements CommandListener {
return; return;
} }
Observation observation = requestContext.getOrDefault(Observation.class, null); Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
if (observation == null) { if (observation == null) {
return; return;
} }
MongoHandlerContext context = requestContext.get(MongoHandlerContext.class); MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
context.setCommandFailedEvent(event); context.setCommandFailedEvent(event);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@@ -181,7 +185,7 @@ public class MongoObservationCommandListener implements CommandListener {
@Nullable @Nullable
private static Observation observationFromContext(RequestContext context) { private static Observation observationFromContext(RequestContext context) {
Observation observation = context.getOrDefault(Observation.class, null); Observation observation = context.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
if (observation != null) { if (observation != null) {
@@ -192,7 +196,7 @@ public class MongoObservationCommandListener implements CommandListener {
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No observation was found - will not create any child spans"); log.debug("No observation was found - will not create any child observations");
} }
return null; return null;

View File

@@ -73,7 +73,7 @@ public class ReactiveIntegrationTests extends SampleTestRunner {
.verifyComplete(); .verifyComplete();
repository.findByLastname("Matthews") // repository.findByLastname("Matthews") //
.contextWrite(Context.of(Observation.class, intermediate)) // .contextWrite(Context.of("micrometer.observation", intermediate)) //
.as(StepVerifier::create).assertNext(actual -> { .as(StepVerifier::create).assertNext(actual -> {
assertThat(actual).extracting("firstname", "lastname").containsExactly("Dave", "Matthews"); assertThat(actual).extracting("firstname", "lastname").containsExactly("Dave", "Matthews");