Skip to content

Commit 63e176e

Browse files
michaldomp911de
michaldo
authored andcommitted
Always include Collection tag in MongoDB observations.
Use default "none" collection name when command doesn't define a collection. Signed-off-by: michaldo <[email protected]> Closes: #4994
1 parent 8fceaae commit 63e176e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.observability;
1717

18+
import io.micrometer.common.KeyValue;
1819
import io.micrometer.common.KeyValues;
1920

2021
import java.net.InetSocketAddress;
@@ -65,6 +66,8 @@ public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {
6566
if (!ObjectUtils.isEmpty(context.getCollectionName())) {
6667
keyValues = keyValues
6768
.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName()));
69+
} else {
70+
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(KeyValue.NONE_VALUE));
6871
}
6972

7073
ConnectionDescription connectionDescription = context.getCommandStartedEvent().getConnectionDescription();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.micrometer.common.KeyValues;
2222
import io.micrometer.core.instrument.MeterRegistry;
23+
import io.micrometer.core.instrument.Tags;
2324
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
2425
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2526
import io.micrometer.observation.Observation;
@@ -98,6 +99,20 @@ void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() {
9899
assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active");
99100
}
100101

102+
@Test
103+
void commandStartedShouldIncludeCollectionIfMissing() {
104+
105+
// when
106+
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
107+
108+
// then
109+
// although command 'hello' is collection-less, metric must have tag "db.mongodb.collection"
110+
assertThat(meterRegistry).hasMeterWithNameAndTags(
111+
"spring.data.mongodb.command.active",
112+
Tags.of("db.mongodb.collection", "none"));
113+
114+
}
115+
101116
@Test
102117
void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() {
103118

0 commit comments

Comments
 (0)