Skip to content

Commit a6bc117

Browse files
author
michaldo
committed
Use default collection name is missing
Signed-off-by: michaldo <[email protected]>
1 parent 55378ae commit a6bc117

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
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 org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
@@ -63,6 +64,8 @@ public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {
6364
if (!ObjectUtils.isEmpty(context.getCollectionName())) {
6465
keyValues = keyValues
6566
.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName()));
67+
} else {
68+
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(KeyValue.NONE_VALUE));
6669
}
6770

6871
if(context.getCommandStartedEvent() == null) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ 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-
112108
RequestContext requestContext = event.getRequestContext();
113109

114110
if (requestContext == null) {

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

Lines changed: 7 additions & 2 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;
@@ -99,13 +100,17 @@ void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() {
99100
}
100101

101102
@Test
102-
void commandStartedShouldNotInstrumentWhenHello() {
103+
void commandStartedShouldIncludeCollectionIfMissing() {
103104

104105
// when
105106
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
106107

107108
// then
108-
assertThat(meterRegistry).hasNoMetrics();
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+
109114
}
110115

111116
@Test

0 commit comments

Comments
 (0)