Browse Source

Provide fallback for Observation KeyValues

Closes: #5020
pull/5044/head
Jonatan Ivanov 5 months ago committed by Christoph Strobl
parent
commit
767077a162
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 65
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/DefaultMongoHandlerObservationConvention.java
  2. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservation.java

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

@ -15,18 +15,16 @@ @@ -15,18 +15,16 @@
*/
package org.springframework.data.mongodb.observability;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import com.mongodb.ConnectionString;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ConnectionId;
import com.mongodb.event.CommandStartedEvent;
import io.micrometer.common.KeyValues;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import static org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames.*;
/**
* Default {@link MongoHandlerObservationConvention} implementation.
@ -41,54 +39,43 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio @@ -41,54 +39,43 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio
@Override
public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {
KeyValues keyValues = KeyValues.of(LowCardinalityCommandKeyNames.DB_SYSTEM.withValue("mongodb"),
LowCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandName()));
ConnectionString connectionString = context.getConnectionString();
if (connectionString != null) {
keyValues = keyValues
.and(LowCardinalityCommandKeyNames.DB_CONNECTION_STRING.withValue(connectionString.getConnectionString()));
String user = connectionString.getUsername();
if (!ObjectUtils.isEmpty(user)) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_USER.withValue(user));
}
}
if (!ObjectUtils.isEmpty(context.getDatabaseName())) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_NAME.withValue(context.getDatabaseName()));
}
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(
ObjectUtils.isEmpty(context.getCollectionName()) ? KeyValue.NONE_VALUE : context.getCollectionName()));
if (context.getCommandStartedEvent() == null) {
throw new IllegalStateException("not command started event present");
}
ConnectionDescription connectionDescription = context.getCommandStartedEvent().getConnectionDescription();
ConnectionString connectionString = context.getConnectionString();
String connectionStringValue = connectionString != null ? connectionString.getConnectionString() : null;
String username = connectionString != null ? connectionString.getUsername() : null;
String transport = null, peerName = null, peerPort =null, clusterId = null;
ConnectionDescription connectionDescription = context.getCommandStartedEvent().getConnectionDescription();
if (connectionDescription != null) {
ServerAddress serverAddress = connectionDescription.getServerAddress();
if (serverAddress != null) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.NET_TRANSPORT.withValue("IP.TCP"),
LowCardinalityCommandKeyNames.NET_PEER_NAME.withValue(serverAddress.getHost()),
LowCardinalityCommandKeyNames.NET_PEER_PORT.withValue("" + serverAddress.getPort()));
transport = "IP.TCP";
peerName = serverAddress.getHost();
peerPort = String.valueOf(serverAddress.getPort());
}
ConnectionId connectionId = connectionDescription.getConnectionId();
if (connectionId != null) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID
.withValue(connectionId.getServerId().getClusterId().getValue()));
clusterId = connectionId.getServerId().getClusterId().getValue();
}
}
return keyValues;
return KeyValues.of(
DB_SYSTEM.withValue("mongodb"),
MONGODB_COMMAND.withValue(context.getCommandName()),
DB_CONNECTION_STRING.withOptionalValue(connectionStringValue),
DB_USER.withOptionalValue(username),
DB_NAME.withOptionalValue(context.getDatabaseName()),
MONGODB_COLLECTION.withOptionalValue(context.getCollectionName()),
NET_TRANSPORT.withOptionalValue(transport),
NET_PEER_NAME.withOptionalValue(peerName),
NET_PEER_PORT.withOptionalValue(peerPort),
MONGODB_CLUSTER_ID.withOptionalValue(clusterId)
);
}
@Override

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

@ -15,8 +15,11 @@ @@ -15,8 +15,11 @@
*/
package org.springframework.data.mongodb.observability;
import io.micrometer.common.KeyValue;
import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.docs.ObservationDocumentation;
import org.jspecify.annotations.Nullable;
import org.springframework.util.ObjectUtils;
/**
* A MongoDB-based {@link io.micrometer.observation.Observation}.
@ -172,6 +175,15 @@ enum MongoObservation implements ObservationDocumentation { @@ -172,6 +175,15 @@ enum MongoObservation implements ObservationDocumentation {
public String asString() {
return "db.operation";
}
};
/**
* Creates a key value for the given key name.
* @param value value for key, if value is null or empty {@link KeyValue.NONE_VALUE} will be used
* @return key value
*/
public KeyValue withOptionalValue(@Nullable String value) {
return withValue(ObjectUtils.isEmpty(value) ? KeyValue.NONE_VALUE : value);
}
}

Loading…
Cancel
Save