Browse Source

Polishing.

Update javadoc and remove deprecated observation keys.

Original Pull Request: #5020
pull/5044/head
Christoph Strobl 4 months ago
parent
commit
94845e0fb2
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoKeyName.java
  2. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservation.java
  3. 17
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/Observer.java
  4. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/ImperativeIntegrationTests.java
  5. 2
      src/main/antora/modules/ROOT/pages/observability/metrics.adoc
  6. 2
      src/main/antora/modules/ROOT/pages/observability/spans.adoc

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2025-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import org.springframework.util.StringUtils; @@ -31,6 +31,7 @@ import org.springframework.util.StringUtils;
* {@link KeyValue} and {@link KeyName}.
*
* @author Mark Paluch
* @since 4.4.9
*/
record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Object> valueFunction) implements KeyName {
@ -44,7 +45,7 @@ record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Obje @@ -44,7 +45,7 @@ record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Obje
* @return
* @param <C>
*/
public static <C> MongoKeyName<C> required(String name, Function<C, @Nullable Object> valueFunction) {
static <C> MongoKeyName<C> required(String name, Function<C, @Nullable Object> valueFunction) {
return required(name, valueFunction, Objects::nonNull);
}

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

@ -15,16 +15,15 @@ @@ -15,16 +15,15 @@
*/
package org.springframework.data.mongodb.observability;
import static org.springframework.data.mongodb.observability.MongoKeyName.*;
import static org.springframework.data.mongodb.observability.MongoKeyName.MongoKeyValue;
import static org.springframework.data.mongodb.observability.MongoKeyName.just;
import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.docs.ObservationDocumentation;
import org.jspecify.annotations.Nullable;
import org.springframework.util.StringUtils;
import com.mongodb.ConnectionString;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.event.CommandEvent;
@ -86,24 +85,18 @@ enum MongoObservation implements ObservationDocumentation { @@ -86,24 +85,18 @@ enum MongoObservation implements ObservationDocumentation {
static MongoKeyName<ServerAddress> NET_PEER_NAME = MongoKeyName.required("net.peer.name", ServerAddress::getHost);
static MongoKeyName<ServerAddress> NET_PEER_PORT = MongoKeyName.required("net.peer.port", ServerAddress::getPort);
static MongoKeyName<ConnectionString> DB_CONNECTION_STRING = MongoKeyName.requiredString("db.connection_string",
Object::toString);
static MongoKeyName<ConnectionString> DB_USER = MongoKeyName.requiredString("db.user",
ConnectionString::getUsername);
/**
* Observe low cardinality key values for the given {@link MongoHandlerContext}.
*
* @param context the context to contribute from, can be {@literal null} if no context is available.
* @return the key value contributor providing low cardinality key names.
*/
public static Observer observe(@Nullable MongoHandlerContext context) {
static Observer observe(@Nullable MongoHandlerContext context) {
return Observer.fromContext(context, it -> {
it.contribute(DB_SYSTEM).contribute(MONGODB_COMMAND, DB_NAME, MONGODB_COLLECTION);
it.nested(MongoHandlerContext::getConnectionString).contribute(DB_CONNECTION_STRING, DB_USER);
it.nested(MongoHandlerContext::getCommandStartedEvent) //
.nested(CommandEvent::getConnectionDescription).contribute(MONGODB_CLUSTER_ID) //
.nested(ConnectionDescription::getServerAddress) //
@ -116,9 +109,8 @@ enum MongoObservation implements ObservationDocumentation { @@ -116,9 +109,8 @@ enum MongoObservation implements ObservationDocumentation {
*
* @return the key names for low cardinality keys.
*/
public static KeyName[] getKeyNames() {
static KeyName[] getKeyNames() {
return observe(null).toKeyNames();
}
}
}

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

@ -30,6 +30,7 @@ import org.jspecify.annotations.Nullable; @@ -30,6 +30,7 @@ import org.jspecify.annotations.Nullable;
* observability systems.
*
* @author Mark Paluch
* @since 4.4.9
*/
class Observer {
@ -40,7 +41,7 @@ class Observer { @@ -40,7 +41,7 @@ class Observer {
*
* @return a new {@link Observer}.
*/
public static Observer create() {
static Observer create() {
return new Observer();
}
@ -53,7 +54,7 @@ class Observer { @@ -53,7 +54,7 @@ class Observer {
* @return the stateful {@link Observer}.
* @param <C> context type.
*/
public static <C> Observer fromContext(@Nullable C context, Consumer<? super ContextualObserver<C>> consumer) {
static <C> Observer fromContext(@Nullable C context, Consumer<? super ContextualObserver<C>> consumer) {
Observer contributor = create();
@ -68,7 +69,7 @@ class Observer { @@ -68,7 +69,7 @@ class Observer {
* @param keyValue
* @return
*/
public Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
keyValues.add(keyValue);
@ -83,7 +84,7 @@ class Observer { @@ -83,7 +84,7 @@ class Observer {
* @return the nested contextual {@link ContextualObserver} that can contribute key-value tuples.
* @param <C>
*/
public <C> ContextualObserver<C> contextual(@Nullable C context) {
<C> ContextualObserver<C> contextual(@Nullable C context) {
if (context == null) {
return new EmptyContextualObserver<>(keyValues);
@ -92,15 +93,11 @@ class Observer { @@ -92,15 +93,11 @@ class Observer {
return new DefaultContextualObserver<>(context, keyValues);
}
public <T> ContextualObserver<T> empty(Class<T> targetType) {
return new EmptyContextualObserver<>(this.keyValues);
}
public KeyValues toKeyValues() {
KeyValues toKeyValues() {
return KeyValues.of(keyValues);
}
public KeyName[] toKeyNames() {
KeyName[] toKeyNames() {
KeyName[] keyNames = new KeyName[keyValues.size()];

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

@ -81,13 +81,17 @@ public class ImperativeIntegrationTests extends SampleTestRunner { @@ -81,13 +81,17 @@ public class ImperativeIntegrationTests extends SampleTestRunner {
for (FinishedSpan span : tracer.getFinishedSpans()) {
assertThat(span.getTags()).containsEntry("db.system", "mongodb").containsEntry("net.transport", "IP.TCP");
assertThat(span.getTags()) //
.containsEntry("db.system", "mongodb") //
.containsEntry("net.transport", "IP.TCP") //
.doesNotContainKey("db.connection_string") //
.doesNotContainKey("db.user");
if (MongoClientVersion.isVersion5orNewer()) {
assertThat(span.getTags()).containsKeys("db.connection_string", "db.name", "db.operation",
assertThat(span.getTags()).containsKeys("db.name", "db.operation",
"db.mongodb.collection", "net.peer.name", "net.peer.port");
} else {
assertThat(span.getTags()).containsKeys("db.connection_string", "db.name", "db.operation",
assertThat(span.getTags()).containsKeys("db.name", "db.operation",
"db.mongodb.collection", "net.peer.name", "net.peer.port", "net.sock.peer.addr", "net.sock.peer.port");
}
}

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

@ -24,12 +24,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob @@ -24,12 +24,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob
[cols="a,a"]
|===
|Name | Description
|`db.connection_string` _(required)_|MongoDB connection string.
|`db.mongodb.collection` _(required)_|MongoDB collection name.
|`db.name` _(required)_|MongoDB database name.
|`db.operation` _(required)_|MongoDB command value.
|`db.system` _(required)_|MongoDB database system.
|`db.user` _(required)_|MongoDB user.
|`net.peer.name` _(required)_|Name of the database host.
|`net.peer.port` _(required)_|Logical remote port number.
|`net.sock.peer.addr` _(required)_|Mongo peer address.

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

@ -15,12 +15,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob @@ -15,12 +15,10 @@ Fully qualified name of the enclosing class `org.springframework.data.mongodb.ob
.Tag Keys
|===
|Name | Description
|`db.connection_string` _(required)_|MongoDB connection string.
|`db.mongodb.collection` _(required)_|MongoDB collection name.
|`db.name` _(required)_|MongoDB database name.
|`db.operation` _(required)_|MongoDB command value.
|`db.system` _(required)_|MongoDB database system.
|`db.user` _(required)_|MongoDB user.
|`net.peer.name` _(required)_|Name of the database host.
|`net.peer.port` _(required)_|Logical remote port number.
|`net.sock.peer.addr` _(required)_|Mongo peer address.

Loading…
Cancel
Save