Skip to content

Commit 55378ae

Browse files
author
michaldo
committed
Do not instrument "hello"
When MongoObservationCommandListener is enabled according to documentation, there is a race in metrics registration: > The meter (MeterId{name='spring.data.mongodb.command.active', tags=[tag(db.name=test),tag(db.operation=hello),tag(db.system=mongodb),tag(net.peer.name=localhost),tag(net.peer.port=27017),tag(net.transport=IP.TCP),tag(spring.data.mongodb.cluster_id=6840b93dabcd29b2f0526362)]}) registration has failed: ... Prometheus does not allow register metric with different tags. The problem is that healthcheck calls Mongo command "hello" which does not refer to any collection. Consequently, tag "db.mongodb.collection" is absent for "hello" but present in regular commands. Event if regular commands always wins the race (and metrics always has tag "collection"), the race must be removed. My proposal is skip "hello" instrumentation, like "admin" commands Signed-off-by: michaldo <[email protected]>
1 parent 96c2e71 commit 55378ae

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public void commandStarted(CommandStartedEvent event) {
105105
return; // don't instrument commands like "endSessions"
106106
}
107107

108+
if ("hello".equals(event.getCommandName())) {
109+
return; // don't instrument healthcheck
110+
}
111+
108112
RequestContext requestContext = event.getRequestContext();
109113

110114
if (requestContext == null) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() {
9898
assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active");
9999
}
100100

101+
@Test
102+
void commandStartedShouldNotInstrumentWhenHello() {
103+
104+
// when
105+
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
106+
107+
// then
108+
assertThat(meterRegistry).hasNoMetrics();
109+
}
110+
101111
@Test
102112
void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() {
103113

0 commit comments

Comments
 (0)