Skip to content

Commit d765eaa

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

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
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

0 commit comments

Comments
 (0)