Polishing.

Delegate constructor calls and fix formatting in tests.

Original Pull Request: #4607
This commit is contained in:
Christoph Strobl
2024-02-08 13:07:41 +01:00
parent 5db4132658
commit f052256837
2 changed files with 19 additions and 31 deletions

View File

@@ -57,12 +57,7 @@ public class MongoObservationCommandListener implements CommandListener {
* @param observationRegistry must not be {@literal null}
*/
public MongoObservationCommandListener(ObservationRegistry observationRegistry) {
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
this.observationRegistry = observationRegistry;
this.connectionString = null;
this.observationConvention = new DefaultMongoHandlerObservationConvention();
this(observationRegistry, null);
}
/**
@@ -70,31 +65,27 @@ public class MongoObservationCommandListener implements CommandListener {
* {@link ConnectionString} to every {@link Observation}.
*
* @param observationRegistry must not be {@literal null}
* @param connectionString must not be {@literal null}
* @param connectionString can be {@literal null}
*/
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString) {
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
Assert.notNull(connectionString, "ConnectionString must not be null");
this.observationRegistry = observationRegistry;
this.connectionString = connectionString;
this.observationConvention = new DefaultMongoHandlerObservationConvention();
public MongoObservationCommandListener(ObservationRegistry observationRegistry,
@Nullable ConnectionString connectionString) {
this(observationRegistry, connectionString, 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}
* {@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 connectionString can be {@literal null}
* @param observationConvention must not be {@literal null}
* @since 4.3
*/
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString, MongoHandlerObservationConvention observationConvention) {
public MongoObservationCommandListener(ObservationRegistry observationRegistry,
@Nullable 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");
Assert.notNull(observationConvention, "ObservationConvention must not be null");
this.observationRegistry = observationRegistry;
this.connectionString = connectionString;
@@ -193,7 +184,7 @@ public class MongoObservationCommandListener implements CommandListener {
}
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) {
if (observation == null || !(observation.getContext() instanceof MongoHandlerContext context)) {
return;
}

View File

@@ -18,7 +18,6 @@ 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;
@@ -31,9 +30,9 @@ import org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
import com.mongodb.ConnectionString;
import com.mongodb.RequestContext;
import com.mongodb.ServerAddress;
import com.mongodb.client.SynchronousContextProvider;
@@ -181,8 +180,7 @@ class MongoObservationCommandListenerTests {
assertThatTimerRegisteredWithTags();
}
@Test
// GH-4481
@Test // GH-4481
void completionShouldIgnoreIncompatibleObservationContext() {
// given
@@ -198,8 +196,7 @@ class MongoObservationCommandListenerTests {
verifyNoMoreInteractions(observation);
}
@Test
// GH-4481
@Test // GH-4481
void failureShouldIgnoreIncompatibleObservationContext() {
// given
@@ -215,10 +212,10 @@ class MongoObservationCommandListenerTests {
verifyNoMoreInteractions(observation);
}
@Test
// GH-4321
@Test // GH-4321
void shouldUseObservationConvention() {
//given
// given
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
@Override
public boolean supportsContext(Observation.Context context) {
@@ -234,7 +231,7 @@ class MongoObservationCommandListenerTests {
customObservationConvention);
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0,null, "some name", "", null));
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("custom.name.active");