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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import org.springframework.util.StringUtils;
* {@link KeyValue} and {@link KeyName}. * {@link KeyValue} and {@link KeyName}.
* *
* @author Mark Paluch * @author Mark Paluch
* @since 4.4.9
*/ */
record MongoKeyName<C>(String name, boolean required, Function<C, @Nullable Object> valueFunction) implements KeyName { 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
* @return * @return
* @param <C> * @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); return required(name, valueFunction, Objects::nonNull);
} }

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

@ -15,16 +15,15 @@
*/ */
package org.springframework.data.mongodb.observability; 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.common.docs.KeyName;
import io.micrometer.observation.docs.ObservationDocumentation; import io.micrometer.observation.docs.ObservationDocumentation;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.mongodb.ConnectionString;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription; import com.mongodb.connection.ConnectionDescription;
import com.mongodb.event.CommandEvent; import com.mongodb.event.CommandEvent;
@ -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_NAME = MongoKeyName.required("net.peer.name", ServerAddress::getHost);
static MongoKeyName<ServerAddress> NET_PEER_PORT = MongoKeyName.required("net.peer.port", ServerAddress::getPort); 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}. * 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. * @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. * @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 -> { return Observer.fromContext(context, it -> {
it.contribute(DB_SYSTEM).contribute(MONGODB_COMMAND, DB_NAME, MONGODB_COLLECTION); 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) // it.nested(MongoHandlerContext::getCommandStartedEvent) //
.nested(CommandEvent::getConnectionDescription).contribute(MONGODB_CLUSTER_ID) // .nested(CommandEvent::getConnectionDescription).contribute(MONGODB_CLUSTER_ID) //
.nested(ConnectionDescription::getServerAddress) // .nested(ConnectionDescription::getServerAddress) //
@ -116,9 +109,8 @@ enum MongoObservation implements ObservationDocumentation {
* *
* @return the key names for low cardinality keys. * @return the key names for low cardinality keys.
*/ */
public static KeyName[] getKeyNames() { static KeyName[] getKeyNames() {
return observe(null).toKeyNames(); 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;
* observability systems. * observability systems.
* *
* @author Mark Paluch * @author Mark Paluch
* @since 4.4.9
*/ */
class Observer { class Observer {
@ -40,7 +41,7 @@ class Observer {
* *
* @return a new {@link Observer}. * @return a new {@link Observer}.
*/ */
public static Observer create() { static Observer create() {
return new Observer(); return new Observer();
} }
@ -53,7 +54,7 @@ class Observer {
* @return the stateful {@link Observer}. * @return the stateful {@link Observer}.
* @param <C> context type. * @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(); Observer contributor = create();
@ -68,7 +69,7 @@ class Observer {
* @param keyValue * @param keyValue
* @return * @return
*/ */
public Observer contribute(MongoKeyName.MongoKeyValue keyValue) { Observer contribute(MongoKeyName.MongoKeyValue keyValue) {
keyValues.add(keyValue); keyValues.add(keyValue);
@ -83,7 +84,7 @@ class Observer {
* @return the nested contextual {@link ContextualObserver} that can contribute key-value tuples. * @return the nested contextual {@link ContextualObserver} that can contribute key-value tuples.
* @param <C> * @param <C>
*/ */
public <C> ContextualObserver<C> contextual(@Nullable C context) { <C> ContextualObserver<C> contextual(@Nullable C context) {
if (context == null) { if (context == null) {
return new EmptyContextualObserver<>(keyValues); return new EmptyContextualObserver<>(keyValues);
@ -92,15 +93,11 @@ class Observer {
return new DefaultContextualObserver<>(context, keyValues); return new DefaultContextualObserver<>(context, keyValues);
} }
public <T> ContextualObserver<T> empty(Class<T> targetType) { KeyValues toKeyValues() {
return new EmptyContextualObserver<>(this.keyValues);
}
public KeyValues toKeyValues() {
return KeyValues.of(keyValues); return KeyValues.of(keyValues);
} }
public KeyName[] toKeyNames() { KeyName[] toKeyNames() {
KeyName[] keyNames = new KeyName[keyValues.size()]; 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 {
for (FinishedSpan span : tracer.getFinishedSpans()) { 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()) { 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"); "db.mongodb.collection", "net.peer.name", "net.peer.port");
} else { } 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"); "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
[cols="a,a"] [cols="a,a"]
|=== |===
|Name | Description |Name | Description
|`db.connection_string` _(required)_|MongoDB connection string.
|`db.mongodb.collection` _(required)_|MongoDB collection name. |`db.mongodb.collection` _(required)_|MongoDB collection name.
|`db.name` _(required)_|MongoDB database name. |`db.name` _(required)_|MongoDB database name.
|`db.operation` _(required)_|MongoDB command value. |`db.operation` _(required)_|MongoDB command value.
|`db.system` _(required)_|MongoDB database system. |`db.system` _(required)_|MongoDB database system.
|`db.user` _(required)_|MongoDB user.
|`net.peer.name` _(required)_|Name of the database host. |`net.peer.name` _(required)_|Name of the database host.
|`net.peer.port` _(required)_|Logical remote port number. |`net.peer.port` _(required)_|Logical remote port number.
|`net.sock.peer.addr` _(required)_|Mongo peer address. |`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
.Tag Keys .Tag Keys
|=== |===
|Name | Description |Name | Description
|`db.connection_string` _(required)_|MongoDB connection string.
|`db.mongodb.collection` _(required)_|MongoDB collection name. |`db.mongodb.collection` _(required)_|MongoDB collection name.
|`db.name` _(required)_|MongoDB database name. |`db.name` _(required)_|MongoDB database name.
|`db.operation` _(required)_|MongoDB command value. |`db.operation` _(required)_|MongoDB command value.
|`db.system` _(required)_|MongoDB database system. |`db.system` _(required)_|MongoDB database system.
|`db.user` _(required)_|MongoDB user.
|`net.peer.name` _(required)_|Name of the database host. |`net.peer.name` _(required)_|Name of the database host.
|`net.peer.port` _(required)_|Logical remote port number. |`net.peer.port` _(required)_|Logical remote port number.
|`net.sock.peer.addr` _(required)_|Mongo peer address. |`net.sock.peer.addr` _(required)_|Mongo peer address.

Loading…
Cancel
Save