Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Use existing constants.

See #4218
  • Loading branch information
mp911de committed Oct 24, 2022
1 parent d4daa30 commit fd0a554
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import reactor.core.CoreSubscriber;

/**
Expand Down Expand Up @@ -85,8 +86,7 @@ public RequestContext getContext() {

Observation currentObservation = observationRegistry.getCurrentObservation();
if (currentObservation != null) {
// Aligned with ObservationThreadLocalAccessor.KEY
requestContext.put("micrometer.observation", currentObservation);
requestContext.put(ObservationThreadLocalAccessor.KEY, currentObservation);
}

return requestContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;

/**
* Implement MongoDB's {@link CommandListener} using Micrometer's {@link Observation} API.
Expand All @@ -42,11 +43,6 @@ public class MongoObservationCommandListener implements CommandListener {

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 @Nullable ConnectionString connectionString;

Expand Down Expand Up @@ -119,7 +115,7 @@ public void commandStarted(CommandStartedEvent event) {

observation.start();

requestContext.put(MICROMETER_OBSERVATION_KEY, observation);
requestContext.put(ObservationThreadLocalAccessor.KEY, observation);

if (log.isDebugEnabled()) {
log.debug(
Expand All @@ -136,7 +132,7 @@ public void commandSucceeded(CommandSucceededEvent event) {
return;
}

Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
if (observation == null) {
return;
}
Expand All @@ -160,7 +156,7 @@ public void commandFailed(CommandFailedEvent event) {
return;
}

Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
if (observation == null) {
return;
}
Expand All @@ -185,7 +181,7 @@ public void commandFailed(CommandFailedEvent event) {
@Nullable
private static Observation observationFromContext(RequestContext context) {

Observation observation = context.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
Observation observation = context.getOrDefault(ObservationThreadLocalAccessor.KEY, null);

if (observation != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public SampleTestRunnerConsumer yourCode() {
.verifyComplete();

repository.findByLastname("Matthews") //
.contextWrite(Context.of("micrometer.observation", intermediate)) //
.contextWrite(Context.of(ObservationThreadLocalAccessor.KEY, intermediate)) //
.as(StepVerifier::create).assertNext(actual -> {

assertThat(actual).extracting("firstname", "lastname").containsExactly("Dave", "Matthews");
Expand Down

0 comments on commit fd0a554

Please sign in to comment.