18
18
import static io .micrometer .core .tck .MeterRegistryAssert .*;
19
19
import static org .mockito .Mockito .*;
20
20
21
+ import com .mongodb .ConnectionString ;
21
22
import io .micrometer .common .KeyValues ;
22
23
import io .micrometer .core .instrument .MeterRegistry ;
23
24
import io .micrometer .core .instrument .observation .DefaultMeterObservationHandler ;
48
49
* @author Marcin Grzejszczak
49
50
* @author Greg Turnquist
50
51
* @author Mark Paluch
52
+ * @author François Kha
51
53
*/
52
54
class MongoObservationCommandListenerTests {
53
55
@@ -108,8 +110,7 @@ void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestConte
108
110
new ConnectionDescription ( //
109
111
new ServerId ( //
110
112
new ClusterId ("description" ), //
111
- new ServerAddress ("localhost" , 1234 ))),
112
- "database" , "insert" , //
113
+ new ServerAddress ("localhost" , 1234 ))), "database" , "insert" , //
113
114
new BsonDocument ("collection" , new BsonString ("user" ))));
114
115
listener .commandSucceeded (new CommandSucceededEvent (traceRequestContext , 0 , null , "insert" , null , 0 ));
115
116
@@ -179,7 +180,8 @@ void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
179
180
assertThatTimerRegisteredWithTags ();
180
181
}
181
182
182
- @ Test // GH-4481
183
+ @ Test
184
+ // GH-4481
183
185
void completionShouldIgnoreIncompatibleObservationContext () {
184
186
185
187
// given
@@ -195,7 +197,8 @@ void completionShouldIgnoreIncompatibleObservationContext() {
195
197
verifyNoMoreInteractions (observation );
196
198
}
197
199
198
- @ Test // GH-4481
200
+ @ Test
201
+ // GH-4481
199
202
void failureShouldIgnoreIncompatibleObservationContext () {
200
203
201
204
// given
@@ -211,6 +214,31 @@ void failureShouldIgnoreIncompatibleObservationContext() {
211
214
verifyNoMoreInteractions (observation );
212
215
}
213
216
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
+
214
242
private RequestContext getContext () {
215
243
return ((SynchronousContextProvider ) ContextProviderFactory .create (observationRegistry )).getContext ();
216
244
}
0 commit comments