Skip to content

Commit 94845e0

Browse files
Polishing.
Update javadoc and remove deprecated observation keys. Original Pull Request: #5020
1 parent 75f07cd commit 94845e0

File tree

6 files changed

+21
-31
lines changed

6 files changed

+21
-31
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 the original author or authors.
2+
* Copyright 2025-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* {@link KeyValue} and {@link KeyName}.
3232
*
3333
* @author Mark Paluch
34+
* @since 4.4.9
3435
*/
3536
record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Object> valueFunction) implements KeyName {
3637

@@ -44,7 +45,7 @@ record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Obje
4445
* @return
4546
* @param <C>
4647
*/
47-
public static <C> MongoKeyName<C> required(String name, Function<C, @Nullable Object> valueFunction) {
48+
static <C> MongoKeyName<C> required(String name, Function<C, @Nullable Object> valueFunction) {
4849
return required(name, valueFunction, Objects::nonNull);
4950
}
5051

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

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

18-
import static org.springframework.data.mongodb.observability.MongoKeyName.*;
18+
import static org.springframework.data.mongodb.observability.MongoKeyName.MongoKeyValue;
19+
import static org.springframework.data.mongodb.observability.MongoKeyName.just;
1920

2021
import io.micrometer.common.docs.KeyName;
2122
import io.micrometer.observation.docs.ObservationDocumentation;
2223

2324
import org.jspecify.annotations.Nullable;
24-
2525
import org.springframework.util.StringUtils;
2626

27-
import com.mongodb.ConnectionString;
2827
import com.mongodb.ServerAddress;
2928
import com.mongodb.connection.ConnectionDescription;
3029
import com.mongodb.event.CommandEvent;
@@ -86,24 +85,18 @@ static class LowCardinality {
8685
static MongoKeyName<ServerAddress> NET_PEER_NAME = MongoKeyName.required("net.peer.name", ServerAddress::getHost);
8786
static MongoKeyName<ServerAddress> NET_PEER_PORT = MongoKeyName.required("net.peer.port", ServerAddress::getPort);
8887

89-
static MongoKeyName<ConnectionString> DB_CONNECTION_STRING = MongoKeyName.requiredString("db.connection_string",
90-
Object::toString);
91-
static MongoKeyName<ConnectionString> DB_USER = MongoKeyName.requiredString("db.user",
92-
ConnectionString::getUsername);
93-
9488
/**
9589
* Observe low cardinality key values for the given {@link MongoHandlerContext}.
9690
*
9791
* @param context the context to contribute from, can be {@literal null} if no context is available.
9892
* @return the key value contributor providing low cardinality key names.
9993
*/
100-
public static Observer observe(@Nullable MongoHandlerContext context) {
94+
static Observer observe(@Nullable MongoHandlerContext context) {
10195

10296
return Observer.fromContext(context, it -> {
10397

10498
it.contribute(DB_SYSTEM).contribute(MONGODB_COMMAND, DB_NAME, MONGODB_COLLECTION);
10599

106-
it.nested(MongoHandlerContext::getConnectionString).contribute(DB_CONNECTION_STRING, DB_USER);
107100
it.nested(MongoHandlerContext::getCommandStartedEvent) //
108101
.nested(CommandEvent::getConnectionDescription).contribute(MONGODB_CLUSTER_ID) //
109102
.nested(ConnectionDescription::getServerAddress) //
@@ -116,9 +109,8 @@ public static Observer observe(@Nullable MongoHandlerContext context) {
116109
*
117110
* @return the key names for low cardinality keys.
118111
*/
119-
public static KeyName[] getKeyNames() {
112+
static KeyName[] getKeyNames() {
120113
return observe(null).toKeyNames();
121114
}
122115
}
123-
124116
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* observability systems.
3131
*
3232
* @author Mark Paluch
33+
* @since 4.4.9
3334
*/
3435
class Observer {
3536

@@ -40,7 +41,7 @@ class Observer {
4041
*
4142
* @return a new {@link Observer}.
4243
*/
43-
public static Observer create() {
44+
static Observer create() {
4445
return new Observer();
4546
}
4647

@@ -53,7 +54,7 @@ public static Observer create() {
5354
* @return the stateful {@link Observer}.
5455
* @param <C> context type.
5556
*/
56-
public static <C> Observer fromContext(@Nullable C context, Consumer<? super ContextualObserver<C>> consumer) {
57+
static <C> Observer fromContext(@Nullable C context, Consumer<? super ContextualObserver<C>> consumer) {
5758

5859
Observer contributor = create();
5960

@@ -68,7 +69,7 @@ public static <C> Observer fromContext(@Nullable C context, Consumer<? super Con
6869
* @param keyValue
6970
* @return
7071
*/
71-
public Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
72+
Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
7273

7374
keyValues.add(keyValue);
7475

@@ -83,7 +84,7 @@ public Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
8384
* @return the nested contextual {@link ContextualObserver} that can contribute key-value tuples.
8485
* @param <C>
8586
*/
86-
public <C> ContextualObserver<C> contextual(@Nullable C context) {
87+
<C> ContextualObserver<C> contextual(@Nullable C context) {
8788

8889
if (context == null) {
8990
return new EmptyContextualObserver<>(keyValues);
@@ -92,15 +93,11 @@ public <C> ContextualObserver<C> contextual(@Nullable C context) {
9293
return new DefaultContextualObserver<>(context, keyValues);
9394
}
9495

95-
public <T> ContextualObserver<T> empty(Class<T> targetType) {
96-
return new EmptyContextualObserver<>(this.keyValues);
97-
}
98-
99-
public KeyValues toKeyValues() {
96+
KeyValues toKeyValues() {
10097
return KeyValues.of(keyValues);
10198
}
10299

103-
public KeyName[] toKeyNames() {
100+
KeyName[] toKeyNames() {
104101

105102
KeyName[] keyNames = new KeyName[keyValues.size()];
106103

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,17 @@ public SampleTestRunnerConsumer yourCode() {
8181

8282
for (FinishedSpan span : tracer.getFinishedSpans()) {
8383

84-
assertThat(span.getTags()).containsEntry("db.system", "mongodb").containsEntry("net.transport", "IP.TCP");
84+
assertThat(span.getTags()) //
85+
.containsEntry("db.system", "mongodb") //
86+
.containsEntry("net.transport", "IP.TCP") //
87+
.doesNotContainKey("db.connection_string") //
88+
.doesNotContainKey("db.user");
8589

8690
if (MongoClientVersion.isVersion5orNewer()) {
87-
assertThat(span.getTags()).containsKeys("db.connection_string", "db.name", "db.operation",
91+
assertThat(span.getTags()).containsKeys("db.name", "db.operation",
8892
"db.mongodb.collection", "net.peer.name", "net.peer.port");
8993
} else {
90-
assertThat(span.getTags()).containsKeys("db.connection_string", "db.name", "db.operation",
94+
assertThat(span.getTags()).containsKeys("db.name", "db.operation",
9195
"db.mongodb.collection", "net.peer.name", "net.peer.port", "net.sock.peer.addr", "net.sock.peer.port");
9296
}
9397
}

src/main/antora/modules/ROOT/pages/observability/metrics.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob
2424
[cols="a,a"]
2525
|===
2626
|Name | Description
27-
|`db.connection_string` _(required)_|MongoDB connection string.
2827
|`db.mongodb.collection` _(required)_|MongoDB collection name.
2928
|`db.name` _(required)_|MongoDB database name.
3029
|`db.operation` _(required)_|MongoDB command value.
3130
|`db.system` _(required)_|MongoDB database system.
32-
|`db.user` _(required)_|MongoDB user.
3331
|`net.peer.name` _(required)_|Name of the database host.
3432
|`net.peer.port` _(required)_|Logical remote port number.
3533
|`net.sock.peer.addr` _(required)_|Mongo peer address.

src/main/antora/modules/ROOT/pages/observability/spans.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob
1515
.Tag Keys
1616
|===
1717
|Name | Description
18-
|`db.connection_string` _(required)_|MongoDB connection string.
1918
|`db.mongodb.collection` _(required)_|MongoDB collection name.
2019
|`db.name` _(required)_|MongoDB database name.
2120
|`db.operation` _(required)_|MongoDB command value.
2221
|`db.system` _(required)_|MongoDB database system.
23-
|`db.user` _(required)_|MongoDB user.
2422
|`net.peer.name` _(required)_|Name of the database host.
2523
|`net.peer.port` _(required)_|Logical remote port number.
2624
|`net.sock.peer.addr` _(required)_|Mongo peer address.

0 commit comments

Comments
 (0)