Skip to content

Commit 52efa25

Browse files
committed
Use custom MongodbContext instead of generic SenderContext
Observations were created with Micrometer's generic SenderContext, preventing users from filtering or customizing MongoDB observations by context type. This blocks the ObservationConvention pattern that Spring Boot needs for tag alignment. Introduced MongodbContext extending SenderContext<Object> with Kind.CLIENT, giving users a MongoDB-specific type to register ObservationHandler<MongodbContext> or ObservationConvention<MongodbContext> instances scoped to only MongoDB observations.
1 parent 27b3a97 commit 52efa25

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

driver-core/src/main/com/mongodb/internal/observability/micrometer/MicrometerTracer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import io.micrometer.common.KeyValues;
2323
import io.micrometer.observation.Observation;
2424
import io.micrometer.observation.ObservationRegistry;
25-
import io.micrometer.observation.transport.Kind;
26-
import io.micrometer.observation.transport.SenderContext;
2725
import org.bson.BsonDocument;
2826
import org.bson.BsonReader;
2927
import org.bson.json.JsonMode;
@@ -96,8 +94,7 @@ public boolean includeCommandPayload() {
9694
}
9795

9896
private Observation getObservation(final MongodbObservation observationType, final String name) {
99-
return observationType.observation(observationRegistry,
100-
() -> new SenderContext<>((carrier, key, value) -> {}, Kind.CLIENT))
97+
return observationType.observation(observationRegistry, MongodbContext::new)
10198
.contextualName(name);
10299
}
103100
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.internal.observability.micrometer;
18+
19+
import io.micrometer.observation.transport.Kind;
20+
import io.micrometer.observation.transport.SenderContext;
21+
22+
/**
23+
* A MongoDB-specific {@link SenderContext} for Micrometer observations.
24+
* <p>
25+
* Extends {@link SenderContext} with {@link Kind#CLIENT} to preserve the client span kind
26+
* in the tracing bridge. Provides a MongoDB-specific type that users can filter on
27+
* when registering {@code ObservationHandler} or {@code ObservationConvention} instances.
28+
* </p>
29+
*
30+
* @since 5.7
31+
*/
32+
public class MongodbContext extends SenderContext<Object> {
33+
public MongodbContext() {
34+
super((carrier, key, value) -> { }, Kind.CLIENT);
35+
}
36+
}

0 commit comments

Comments
 (0)