Skip to content

Aligns the context entries with the rest of the portfolio #4218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public RequestContext getContext() {

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

return requestContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ 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 @@ -114,8 +119,7 @@ public void commandStarted(CommandStartedEvent event) {

observation.start();

requestContext.put(Observation.class, observation);
requestContext.put(MongoHandlerContext.class, observationContext);
requestContext.put(MICROMETER_OBSERVATION_KEY, observation);

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

Observation observation = requestContext.getOrDefault(Observation.class, null);
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
if (observation == null) {
return;
}

MongoHandlerContext context = requestContext.get(MongoHandlerContext.class);
MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
context.setCommandSucceededEvent(event);

if (log.isDebugEnabled()) {
Expand All @@ -156,12 +160,12 @@ public void commandFailed(CommandFailedEvent event) {
return;
}

Observation observation = requestContext.getOrDefault(Observation.class, null);
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
if (observation == null) {
return;
}

MongoHandlerContext context = requestContext.get(MongoHandlerContext.class);
MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
context.setCommandFailedEvent(event);

if (log.isDebugEnabled()) {
Expand All @@ -181,7 +185,7 @@ public void commandFailed(CommandFailedEvent event) {
@Nullable
private static Observation observationFromContext(RequestContext context) {

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

if (observation != null) {

Expand All @@ -192,7 +196,7 @@ private static Observation observationFromContext(RequestContext context) {
}

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;
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(Observation.class, intermediate)) //
.contextWrite(Context.of("micrometer.observation", intermediate)) //
.as(StepVerifier::create).assertNext(actual -> {

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