Skip to content

Commit aa5601f

Browse files
committed
Allow custom MongoHandlerObservationConvention
Closes spring-projects#4321 add author
1 parent 4877756 commit aa5601f

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;
@@ -48,6 +49,7 @@
4849
* @author Marcin Grzejszczak
4950
* @author Greg Turnquist
5051
* @author Mark Paluch
52+
* @author François Kha
5153
*/
5254
class MongoObservationCommandListenerTests {
5355

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

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

182-
@Test // GH-4481
183+
@Test
184+
// GH-4481
183185
void completionShouldIgnoreIncompatibleObservationContext() {
184186

185187
// given
@@ -195,7 +197,8 @@ void completionShouldIgnoreIncompatibleObservationContext() {
195197
verifyNoMoreInteractions(observation);
196198
}
197199

198-
@Test // GH-4481
200+
@Test
201+
// GH-4481
199202
void failureShouldIgnoreIncompatibleObservationContext() {
200203

201204
// given
@@ -211,6 +214,31 @@ void failureShouldIgnoreIncompatibleObservationContext() {
211214
verifyNoMoreInteractions(observation);
212215
}
213216

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

0 commit comments

Comments
 (0)