Skip to content

Commit fd0a554

Browse files
committed
Polishing.
Use existing constants. See #4218
1 parent d4daa30 commit fd0a554

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import io.micrometer.observation.Observation;
3333
import io.micrometer.observation.ObservationRegistry;
34+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
3435
import reactor.core.CoreSubscriber;
3536

3637
/**
@@ -85,8 +86,7 @@ public RequestContext getContext() {
8586

8687
Observation currentObservation = observationRegistry.getCurrentObservation();
8788
if (currentObservation != null) {
88-
// Aligned with ObservationThreadLocalAccessor.KEY
89-
requestContext.put("micrometer.observation", currentObservation);
89+
requestContext.put(ObservationThreadLocalAccessor.KEY, currentObservation);
9090
}
9191

9292
return requestContext;

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import io.micrometer.observation.Observation;
3131
import io.micrometer.observation.ObservationRegistry;
32+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
3233

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

4344
private static final Log log = LogFactory.getLog(MongoObservationCommandListener.class);
4445

45-
/**
46-
* Aligns with ObservationThreadLocalAccessor.KEY.
47-
*/
48-
private static final String MICROMETER_OBSERVATION_KEY = "micrometer.observation";
49-
5046
private final ObservationRegistry observationRegistry;
5147
private final @Nullable ConnectionString connectionString;
5248

@@ -119,7 +115,7 @@ public void commandStarted(CommandStartedEvent event) {
119115

120116
observation.start();
121117

122-
requestContext.put(MICROMETER_OBSERVATION_KEY, observation);
118+
requestContext.put(ObservationThreadLocalAccessor.KEY, observation);
123119

124120
if (log.isDebugEnabled()) {
125121
log.debug(
@@ -136,7 +132,7 @@ public void commandSucceeded(CommandSucceededEvent event) {
136132
return;
137133
}
138134

139-
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
135+
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
140136
if (observation == null) {
141137
return;
142138
}
@@ -160,7 +156,7 @@ public void commandFailed(CommandFailedEvent event) {
160156
return;
161157
}
162158

163-
Observation observation = requestContext.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
159+
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
164160
if (observation == null) {
165161
return;
166162
}
@@ -185,7 +181,7 @@ public void commandFailed(CommandFailedEvent event) {
185181
@Nullable
186182
private static Observation observationFromContext(RequestContext context) {
187183

188-
Observation observation = context.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
184+
Observation observation = context.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
189185

190186
if (observation != null) {
191187

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

Lines changed: 1 addition & 1 deletion
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("micrometer.observation", intermediate)) //
76+
.contextWrite(Context.of(ObservationThreadLocalAccessor.KEY, intermediate)) //
7777
.as(StepVerifier::create).assertNext(actual -> {
7878

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

0 commit comments

Comments
 (0)