Skip to content

Commit d4daa30

Browse files
marcingrzejszczakmp911de
authored andcommitted
Align the context propagation entries with the rest of the portfolio.
Closes spring-projects#4218
1 parent 2d63d60 commit d4daa30

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/ContextProviderFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public RequestContext getContext() {
8585

8686
Observation currentObservation = observationRegistry.getCurrentObservation();
8787
if (currentObservation != null) {
88-
requestContext.put(Observation.class, currentObservation);
88+
// Aligned with ObservationThreadLocalAccessor.KEY
89+
requestContext.put("micrometer.observation", currentObservation);
8990
}
9091

9192
return requestContext;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservationCommandListener.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class MongoObservationCommandListener implements CommandListener {
4242

4343
private static final Log log = LogFactory.getLog(MongoObservationCommandListener.class);
4444

45+
/**
46+
* Aligns with ObservationThreadLocalAccessor.KEY.
47+
*/
48+
private static final String MICROMETER_OBSERVATION_KEY = "micrometer.observation";
49+
4550
private final ObservationRegistry observationRegistry;
4651
private final @Nullable ConnectionString connectionString;
4752

@@ -114,8 +119,7 @@ public void commandStarted(CommandStartedEvent event) {
114119

115120
observation.start();
116121

117-
requestContext.put(Observation.class, observation);
118-
requestContext.put(MongoHandlerContext.class, observationContext);
122+
requestContext.put(MICROMETER_OBSERVATION_KEY, observation);
119123

120124
if (log.isDebugEnabled()) {
121125
log.debug(
@@ -132,12 +136,12 @@ public void commandSucceeded(CommandSucceededEvent event) {
132136
return;
133137
}
134138

135-
Observation observation = requestContext.getOrDefault(Observation.class, null);
139+
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
136140
if (observation == null) {
137141
return;
138142
}
139143

140-
MongoHandlerContext context = requestContext.get(MongoHandlerContext.class);
144+
MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
141145
context.setCommandSucceededEvent(event);
142146

143147
if (log.isDebugEnabled()) {
@@ -156,12 +160,12 @@ public void commandFailed(CommandFailedEvent event) {
156160
return;
157161
}
158162

159-
Observation observation = requestContext.getOrDefault(Observation.class, null);
163+
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
160164
if (observation == null) {
161165
return;
162166
}
163167

164-
MongoHandlerContext context = requestContext.get(MongoHandlerContext.class);
168+
MongoHandlerContext context = (MongoHandlerContext) observation.getContext();
165169
context.setCommandFailedEvent(event);
166170

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

184-
Observation observation = context.getOrDefault(Observation.class, null);
188+
Observation observation = context.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
185189

186190
if (observation != null) {
187191

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

194198
if (log.isDebugEnabled()) {
195-
log.debug("No observation was found - will not create any child spans");
199+
log.debug("No observation was found - will not create any child observations");
196200
}
197201

198202
return null;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/ReactiveIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public SampleTestRunnerConsumer yourCode() {
7373
.verifyComplete();
7474

7575
repository.findByLastname("Matthews") //
76-
.contextWrite(Context.of(Observation.class, intermediate)) //
76+
.contextWrite(Context.of("micrometer.observation", intermediate)) //
7777
.as(StepVerifier::create).assertNext(actual -> {
7878

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

0 commit comments

Comments
 (0)