Skip to content

Commit ebdffb2

Browse files
francoiskhachristophstrobl
authored andcommitted
Allow to provide custom MongoHandlerObservationConvention.
This commit adds an additional constructor to MongoObservationCommandListener that allows to set a custom MongoHandlerObservationConvention. Closes: #4321 Original Pull Request: #4607
1 parent e70a662 commit ebdffb2

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author OpenZipkin Brave Authors
4040
* @author Marcin Grzejszczak
4141
* @author Greg Turnquist
42+
* @author François Kha
4243
* @since 4.0
4344
*/
4445
public class MongoObservationCommandListener implements CommandListener {
@@ -48,7 +49,7 @@ public class MongoObservationCommandListener implements CommandListener {
4849
private final ObservationRegistry observationRegistry;
4950
private final @Nullable ConnectionString connectionString;
5051

51-
private final MongoHandlerObservationConvention observationConvention = new DefaultMongoHandlerObservationConvention();
52+
private final MongoHandlerObservationConvention observationConvention;
5253

5354
/**
5455
* Create a new {@link MongoObservationCommandListener} to record {@link Observation}s.
@@ -61,6 +62,7 @@ public MongoObservationCommandListener(ObservationRegistry observationRegistry)
6162

6263
this.observationRegistry = observationRegistry;
6364
this.connectionString = null;
65+
this.observationConvention = new DefaultMongoHandlerObservationConvention();
6466
}
6567

6668
/**
@@ -77,6 +79,26 @@ public MongoObservationCommandListener(ObservationRegistry observationRegistry,
7779

7880
this.observationRegistry = observationRegistry;
7981
this.connectionString = connectionString;
82+
this.observationConvention = new DefaultMongoHandlerObservationConvention();
83+
}
84+
85+
/**
86+
* Create a new {@link MongoObservationCommandListener} to record {@link Observation}s. This constructor attaches the
87+
* {@link ConnectionString} to every {@link Observation} and uses the given {@link MongoHandlerObservationConvention}
88+
*
89+
* @param observationRegistry must not be {@literal null}
90+
* @param connectionString must not be {@literal null}
91+
* @param observationConvention must not be {@literal null}
92+
*/
93+
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString, MongoHandlerObservationConvention observationConvention) {
94+
95+
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
96+
Assert.notNull(connectionString, "ConnectionString must not be null");
97+
Assert.notNull(observationConvention, "MongoHandlerObservationConvention must not be null");
98+
99+
this.observationRegistry = observationRegistry;
100+
this.connectionString = connectionString;
101+
this.observationConvention = observationConvention;
80102
}
81103

82104
@Override

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static io.micrometer.core.tck.MeterRegistryAssert.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import com.mongodb.ConnectionString;
2122
import io.micrometer.common.KeyValues;
2223
import io.micrometer.core.instrument.MeterRegistry;
2324
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
@@ -49,6 +50,7 @@
4950
* @author Marcin Grzejszczak
5051
* @author Greg Turnquist
5152
* @author Mark Paluch
53+
* @author François Kha
5254
*/
5355
class MongoObservationCommandListenerTests {
5456

@@ -109,8 +111,7 @@ void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestConte
109111
new ConnectionDescription( //
110112
new ServerId( //
111113
new ClusterId("description"), //
112-
new ServerAddress("localhost", 1234))),
113-
"database", "insert", //
114+
new ServerAddress("localhost", 1234))), "database", "insert", //
114115
new BsonDocument("collection", new BsonString("user"))));
115116
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
116117

@@ -180,7 +181,8 @@ void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
180181
assertThatTimerRegisteredWithTags();
181182
}
182183

183-
@Test // GH-4481
184+
@Test
185+
// GH-4481
184186
void completionShouldIgnoreIncompatibleObservationContext() {
185187

186188
// given
@@ -196,7 +198,8 @@ void completionShouldIgnoreIncompatibleObservationContext() {
196198
verifyNoMoreInteractions(observation);
197199
}
198200

199-
@Test // GH-4481
201+
@Test
202+
// GH-4481
200203
void failureShouldIgnoreIncompatibleObservationContext() {
201204

202205
// given
@@ -212,6 +215,31 @@ void failureShouldIgnoreIncompatibleObservationContext() {
212215
verifyNoMoreInteractions(observation);
213216
}
214217

218+
@Test
219+
// GH-4321
220+
void shouldUseObservationConvention() {
221+
//given
222+
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
223+
@Override
224+
public boolean supportsContext(Observation.Context context) {
225+
return MongoHandlerObservationConvention.super.supportsContext(context);
226+
}
227+
228+
@Override
229+
public String getName() {
230+
return "custom.name";
231+
}
232+
};
233+
this.listener = new MongoObservationCommandListener(observationRegistry, mock(ConnectionString.class),
234+
customObservationConvention);
235+
236+
// when
237+
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, null, "some name", "", null));
238+
239+
// then
240+
assertThat(meterRegistry).hasMeterWithName("custom.name.active");
241+
}
242+
215243
private RequestContext getContext() {
216244
return ((SynchronousContextProvider) ContextProviderFactory.create(observationRegistry)).getContext();
217245
}

0 commit comments

Comments
 (0)