Browse Source

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
pull/4517/head
francoiskha 2 years ago committed by Christoph Strobl
parent
commit
ebdffb249f
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservationCommandListener.java
  2. 36
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/MongoObservationCommandListenerTests.java

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

@ -39,6 +39,7 @@ import com.mongodb.event.CommandSucceededEvent; @@ -39,6 +39,7 @@ import com.mongodb.event.CommandSucceededEvent;
* @author OpenZipkin Brave Authors
* @author Marcin Grzejszczak
* @author Greg Turnquist
* @author François Kha
* @since 4.0
*/
public class MongoObservationCommandListener implements CommandListener {
@ -48,7 +49,7 @@ public class MongoObservationCommandListener implements CommandListener { @@ -48,7 +49,7 @@ public class MongoObservationCommandListener implements CommandListener {
private final ObservationRegistry observationRegistry;
private final @Nullable ConnectionString connectionString;
private final MongoHandlerObservationConvention observationConvention = new DefaultMongoHandlerObservationConvention();
private final MongoHandlerObservationConvention observationConvention;
/**
* Create a new {@link MongoObservationCommandListener} to record {@link Observation}s.
@ -61,6 +62,7 @@ public class MongoObservationCommandListener implements CommandListener { @@ -61,6 +62,7 @@ public class MongoObservationCommandListener implements CommandListener {
this.observationRegistry = observationRegistry;
this.connectionString = null;
this.observationConvention = new DefaultMongoHandlerObservationConvention();
}
/**
@ -77,6 +79,26 @@ public class MongoObservationCommandListener implements CommandListener { @@ -77,6 +79,26 @@ public class MongoObservationCommandListener implements CommandListener {
this.observationRegistry = observationRegistry;
this.connectionString = connectionString;
this.observationConvention = new DefaultMongoHandlerObservationConvention();
}
/**
* Create a new {@link MongoObservationCommandListener} to record {@link Observation}s. This constructor attaches the
* {@link ConnectionString} to every {@link Observation} and uses the given {@link MongoHandlerObservationConvention}
*
* @param observationRegistry must not be {@literal null}
* @param connectionString must not be {@literal null}
* @param observationConvention must not be {@literal null}
*/
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString, MongoHandlerObservationConvention observationConvention) {
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
Assert.notNull(connectionString, "ConnectionString must not be null");
Assert.notNull(observationConvention, "MongoHandlerObservationConvention must not be null");
this.observationRegistry = observationRegistry;
this.connectionString = connectionString;
this.observationConvention = observationConvention;
}
@Override

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

@ -18,6 +18,7 @@ package org.springframework.data.mongodb.observability; @@ -18,6 +18,7 @@ package org.springframework.data.mongodb.observability;
import static io.micrometer.core.tck.MeterRegistryAssert.*;
import static org.mockito.Mockito.*;
import com.mongodb.ConnectionString;
import io.micrometer.common.KeyValues;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
@ -49,6 +50,7 @@ import com.mongodb.event.CommandSucceededEvent; @@ -49,6 +50,7 @@ import com.mongodb.event.CommandSucceededEvent;
* @author Marcin Grzejszczak
* @author Greg Turnquist
* @author Mark Paluch
* @author François Kha
*/
class MongoObservationCommandListenerTests {
@ -109,8 +111,7 @@ class MongoObservationCommandListenerTests { @@ -109,8 +111,7 @@ class MongoObservationCommandListenerTests {
new ConnectionDescription( //
new ServerId( //
new ClusterId("description"), //
new ServerAddress("localhost", 1234))),
"database", "insert", //
new ServerAddress("localhost", 1234))), "database", "insert", //
new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
@ -180,7 +181,8 @@ class MongoObservationCommandListenerTests { @@ -180,7 +181,8 @@ class MongoObservationCommandListenerTests {
assertThatTimerRegisteredWithTags();
}
@Test // GH-4481
@Test
// GH-4481
void completionShouldIgnoreIncompatibleObservationContext() {
// given
@ -196,7 +198,8 @@ class MongoObservationCommandListenerTests { @@ -196,7 +198,8 @@ class MongoObservationCommandListenerTests {
verifyNoMoreInteractions(observation);
}
@Test // GH-4481
@Test
// GH-4481
void failureShouldIgnoreIncompatibleObservationContext() {
// given
@ -212,6 +215,31 @@ class MongoObservationCommandListenerTests { @@ -212,6 +215,31 @@ class MongoObservationCommandListenerTests {
verifyNoMoreInteractions(observation);
}
@Test
// GH-4321
void shouldUseObservationConvention() {
//given
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
@Override
public boolean supportsContext(Observation.Context context) {
return MongoHandlerObservationConvention.super.supportsContext(context);
}
@Override
public String getName() {
return "custom.name";
}
};
this.listener = new MongoObservationCommandListener(observationRegistry, mock(ConnectionString.class),
customObservationConvention);
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("custom.name.active");
}
private RequestContext getContext() {
return ((SynchronousContextProvider) ContextProviderFactory.create(observationRegistry)).getContext();
}

Loading…
Cancel
Save