Skip to content

Commit 64c9416

Browse files
authored
Add database tag to DefaultMongoCommandTagsProvider (#4298)
1 parent e38f5e8 commit 64c9416

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/mongodb/DefaultMongoCommandTagsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class DefaultMongoCommandTagsProvider implements MongoCommandTagsProvider
5454

5555
@Override
5656
public Iterable<Tag> commandTags(CommandEvent event) {
57-
return Tags.of(Tag.of("command", event.getCommandName()),
57+
return Tags.of(Tag.of("command", event.getCommandName()), Tag.of("database", event.getDatabaseName()),
5858
Tag.of("collection", getAndRemoveCollectionNameForCommand(event)),
5959
Tag.of("cluster.id",
6060
event.getConnectionDescription().getConnectionId().getServerId().getClusterId().getValue()),

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/mongodb/DefaultMongoCommandTagsProviderTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class DefaultMongoCommandTagsProviderTest {
4949
void defaultCommandTags() {
5050
CommandSucceededEvent event = commandSucceededEvent(5150);
5151
Iterable<Tag> tags = tagsProvider.commandTags(event);
52-
assertThat(tags).containsExactlyInAnyOrder(Tag.of("command", "find"), Tag.of("collection", "unknown"),
52+
assertThat(tags).containsExactlyInAnyOrder(Tag.of("command", "find"), Tag.of("database", "db1"),
53+
Tag.of("collection", "unknown"),
5354
Tag.of("cluster.id", connectionDesc.getConnectionId().getServerId().getClusterId().getValue()),
5455
Tag.of("server.address", "localhost:5150"), Tag.of("status", "SUCCESS"));
5556
}
@@ -84,12 +85,12 @@ void handlesCommandsOverLimitGracefully() {
8485
}
8586

8687
private CommandStartedEvent commandStartedEvent(int requestId) {
87-
return new CommandStartedEvent(requestId, connectionDesc, "db1", "find",
88+
return new CommandStartedEvent(null, -1, requestId, connectionDesc, "db1", "find",
8889
new BsonDocument("find", new BsonString("collection-" + requestId)));
8990
}
9091

9192
private CommandSucceededEvent commandSucceededEvent(int requestId) {
92-
return new CommandSucceededEvent(requestId, connectionDesc, "find", new BsonDocument(), 1200L);
93+
return new CommandSucceededEvent(null, -1, requestId, connectionDesc, "db1", "find", new BsonDocument(), 1200L);
9394
}
9495

9596
@Nested

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/mongodb/MongoMetricsCommandListenerTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void shouldCreateSuccessCommandMetric() {
7676
mongo.getDatabase("test").getCollection("testCol").insertOne(new Document("testDoc", new Date()));
7777

7878
Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
79-
"command", "insert", "collection", "testCol", "status", "SUCCESS");
79+
"command", "insert", "database", "test", "collection", "testCol", "status", "SUCCESS");
8080
assertThat(registry.get("mongodb.driver.commands").tags(tags).timer().count()).isEqualTo(1);
8181
}
8282

@@ -85,7 +85,7 @@ void shouldCreateFailedCommandMetric() {
8585
mongo.getDatabase("test").getCollection("testCol").dropIndex("nonExistentIndex");
8686

8787
Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
88-
"command", "dropIndexes", "collection", "testCol", "status", "FAILED");
88+
"command", "dropIndexes", "database", "test", "collection", "testCol", "status", "FAILED");
8989
assertThat(registry.get("mongodb.driver.commands").tags(tags).timer().count()).isEqualTo(1);
9090
}
9191

@@ -110,7 +110,8 @@ public void clusterOpening(ClusterOpeningEvent event) {
110110
try (MongoClient mongo = MongoClients.create(settings)) {
111111
mongo.getDatabase("test").getCollection("testCol").insertOne(new Document("testDoc", new Date()));
112112
Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
113-
"command", "insert", "collection", "testCol", "status", "SUCCESS", "mongoz", "5150");
113+
"command", "insert", "database", "test", "collection", "testCol", "status", "SUCCESS", "mongoz",
114+
"5150");
114115
assertThat(registry.get("mongodb.driver.commands").tags(tags).timer().count()).isEqualTo(1);
115116
}
116117
}
@@ -136,7 +137,8 @@ public void clusterOpening(ClusterOpeningEvent event) {
136137
try (MongoClient mongo = MongoClients.create(settings)) {
137138
mongo.getDatabase("test").getCollection("testCol").dropIndex("nonExistentIndex");
138139
Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
139-
"command", "dropIndexes", "collection", "testCol", "status", "FAILED", "mongoz", "5150");
140+
"command", "dropIndexes", "database", "test", "collection", "testCol", "status", "FAILED", "mongoz",
141+
"5150");
140142
assertThat(registry.get("mongodb.driver.commands").tags(tags).timer().count()).isEqualTo(1);
141143
}
142144
}

0 commit comments

Comments
 (0)