Skip to content

Commit f052256

Browse files
Polishing.
Delegate constructor calls and fix formatting in tests. Original Pull Request: #4607
1 parent 5db4132 commit f052256

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

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

+12-21
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,35 @@ public class MongoObservationCommandListener implements CommandListener {
5757
* @param observationRegistry must not be {@literal null}
5858
*/
5959
public MongoObservationCommandListener(ObservationRegistry observationRegistry) {
60-
61-
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
62-
63-
this.observationRegistry = observationRegistry;
64-
this.connectionString = null;
65-
this.observationConvention = new DefaultMongoHandlerObservationConvention();
60+
this(observationRegistry, null);
6661
}
6762

6863
/**
6964
* Create a new {@link MongoObservationCommandListener} to record {@link Observation}s. This constructor attaches the
7065
* {@link ConnectionString} to every {@link Observation}.
7166
*
7267
* @param observationRegistry must not be {@literal null}
73-
* @param connectionString must not be {@literal null}
68+
* @param connectionString can be {@literal null}
7469
*/
75-
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString) {
76-
77-
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
78-
Assert.notNull(connectionString, "ConnectionString must not be null");
79-
80-
this.observationRegistry = observationRegistry;
81-
this.connectionString = connectionString;
82-
this.observationConvention = new DefaultMongoHandlerObservationConvention();
70+
public MongoObservationCommandListener(ObservationRegistry observationRegistry,
71+
@Nullable ConnectionString connectionString) {
72+
this(observationRegistry, connectionString, new DefaultMongoHandlerObservationConvention());
8373
}
8474

8575
/**
8676
* 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}
77+
* {@link ConnectionString} to every {@link Observation} and uses the given {@link MongoHandlerObservationConvention}.
8878
*
8979
* @param observationRegistry must not be {@literal null}
90-
* @param connectionString must not be {@literal null}
80+
* @param connectionString can be {@literal null}
9181
* @param observationConvention must not be {@literal null}
82+
* @since 4.3
9283
*/
93-
public MongoObservationCommandListener(ObservationRegistry observationRegistry, ConnectionString connectionString, MongoHandlerObservationConvention observationConvention) {
84+
public MongoObservationCommandListener(ObservationRegistry observationRegistry,
85+
@Nullable ConnectionString connectionString, MongoHandlerObservationConvention observationConvention) {
9486

9587
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");
88+
Assert.notNull(observationConvention, "ObservationConvention must not be null");
9889

9990
this.observationRegistry = observationRegistry;
10091
this.connectionString = connectionString;
@@ -193,7 +184,7 @@ private void doInObservation(@Nullable RequestContext requestContext,
193184
}
194185

195186
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
196-
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) {
187+
if (observation == null || !(observation.getContext() instanceof MongoHandlerContext context)) {
197188
return;
198189
}
199190

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

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

21-
import com.mongodb.ConnectionString;
2221
import io.micrometer.common.KeyValues;
2322
import io.micrometer.core.instrument.MeterRegistry;
2423
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
@@ -31,9 +30,9 @@
3130
import org.bson.BsonString;
3231
import org.junit.jupiter.api.BeforeEach;
3332
import org.junit.jupiter.api.Test;
34-
3533
import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
3634

35+
import com.mongodb.ConnectionString;
3736
import com.mongodb.RequestContext;
3837
import com.mongodb.ServerAddress;
3938
import com.mongodb.client.SynchronousContextProvider;
@@ -181,8 +180,7 @@ void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
181180
assertThatTimerRegisteredWithTags();
182181
}
183182

184-
@Test
185-
// GH-4481
183+
@Test // GH-4481
186184
void completionShouldIgnoreIncompatibleObservationContext() {
187185

188186
// given
@@ -198,8 +196,7 @@ void completionShouldIgnoreIncompatibleObservationContext() {
198196
verifyNoMoreInteractions(observation);
199197
}
200198

201-
@Test
202-
// GH-4481
199+
@Test // GH-4481
203200
void failureShouldIgnoreIncompatibleObservationContext() {
204201

205202
// given
@@ -215,10 +212,10 @@ void failureShouldIgnoreIncompatibleObservationContext() {
215212
verifyNoMoreInteractions(observation);
216213
}
217214

218-
@Test
219-
// GH-4321
215+
@Test // GH-4321
220216
void shouldUseObservationConvention() {
221-
//given
217+
218+
// given
222219
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
223220
@Override
224221
public boolean supportsContext(Observation.Context context) {
@@ -234,7 +231,7 @@ public String getName() {
234231
customObservationConvention);
235232

236233
// when
237-
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0,null, "some name", "", null));
234+
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
238235

239236
// then
240237
assertThat(meterRegistry).hasMeterWithName("custom.name.active");

0 commit comments

Comments
 (0)