Skip to content

Commit

Permalink
Align conventions with OpenTelemetry spec.
Browse files Browse the repository at this point in the history
See: #4216
  • Loading branch information
mp911de committed Oct 21, 2022
1 parent 5007e68 commit 2d63d60
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 261 deletions.
Expand Up @@ -31,7 +31,6 @@

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import reactor.core.CoreSubscriber;

/**
Expand Down Expand Up @@ -105,9 +104,6 @@ public RequestContext getContext(Subscriber<?> subscriber) {

Map<Object, Object> map = cs.currentContext().stream()
.collect(Collectors.toConcurrentMap(Entry::getKey, Entry::getValue));
if (map.containsKey(ObservationThreadLocalAccessor.KEY)) {
map.put(Observation.class, map.get(ObservationThreadLocalAccessor.KEY));
}

return new MapRequestContext(map);
}
Expand Down
Expand Up @@ -15,76 +15,105 @@
*/
package org.springframework.data.mongodb.observability;

import org.springframework.data.mongodb.observability.MongoObservation.HighCardinalityCommandKeyNames;
import java.net.InetSocketAddress;

import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
import org.springframework.lang.Nullable;
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.KeyValue;
import io.micrometer.common.KeyValues;

/**
* Default {@link MongoHandlerObservationConvention} implementation.
*
* @author Greg Turnquist
* @since 4
* @author Mark Paluch
* @since 4.0
*/
class DefaultMongoHandlerObservationConvention implements MongoHandlerObservationConvention {

@Override
public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {

KeyValues keyValues = KeyValues.empty();
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()));
}

if (!ObjectUtils.isEmpty(context.getCollectionName())) {
keyValues = keyValues
.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName()));
}

KeyValue connectionTag = connectionTag(context.getCommandStartedEvent());
if (connectionTag != null) {
keyValues = keyValues.and(connectionTag);
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()));

InetSocketAddress socketAddress = serverAddress.getSocketAddress();

if (socketAddress != null) {

keyValues = keyValues.and(
LowCardinalityCommandKeyNames.NET_SOCK_PEER_ADDR.withValue(socketAddress.getHostName()),
LowCardinalityCommandKeyNames.NET_SOCK_PEER_PORT.withValue("" + socketAddress.getPort()));
}
}

ConnectionId connectionId = connectionDescription.getConnectionId();
if (connectionId != null) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID
.withValue(connectionId.getServerId().getClusterId().getValue()));
}
}

return keyValues;
}

@Override
public KeyValues getHighCardinalityKeyValues(MongoHandlerContext context) {

return KeyValues.of(
HighCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandStartedEvent().getCommandName()));
return KeyValues.empty();
}

@Override
public String getContextualName(MongoHandlerContext context) {
return context.getContextualName();
}

/**
* Extract connection details for a MongoDB connection into a {@link KeyValue}.
*
* @param event
* @return
*/
@Nullable
private static KeyValue connectionTag(CommandStartedEvent event) {

ConnectionDescription connectionDescription = event.getConnectionDescription();

if (connectionDescription != null) {
String collectionName = context.getCollectionName();
CommandStartedEvent commandStartedEvent = context.getCommandStartedEvent();

ConnectionId connectionId = connectionDescription.getConnectionId();
if (connectionId != null) {
return LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID
.withValue(connectionId.getServerId().getClusterId().getValue());
}
if (ObjectUtils.isEmpty(collectionName)) {
return commandStartedEvent.getCommandName();
}

return null;
return collectionName + "." + commandStartedEvent.getCommandName();
}

}
Expand Up @@ -23,6 +23,7 @@
import org.bson.BsonValue;
import org.springframework.lang.Nullable;

import com.mongodb.ConnectionString;
import com.mongodb.RequestContext;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandStartedEvent;
Expand All @@ -37,9 +38,10 @@
*
* @author Marcin Grzejszczak
* @author Greg Turnquist
* @since 4.0.0
* @author Mark Paluch
* @since 4.0
*/
public class MongoHandlerContext extends SenderContext<Object> {
class MongoHandlerContext extends SenderContext<Object> {

/**
* @see <a href=
Expand All @@ -51,15 +53,19 @@ public class MongoHandlerContext extends SenderContext<Object> {
"insert", "update", "collMod", "compact", "convertToCapped", "create", "createIndexes", "drop", "dropIndexes",
"killCursors", "listIndexes", "reIndex"));

private final @Nullable ConnectionString connectionString;
private final CommandStartedEvent commandStartedEvent;
private final RequestContext requestContext;
private final String collectionName;

private CommandSucceededEvent commandSucceededEvent;
private CommandFailedEvent commandFailedEvent;

public MongoHandlerContext(CommandStartedEvent commandStartedEvent, RequestContext requestContext) {
public MongoHandlerContext(@Nullable ConnectionString connectionString, CommandStartedEvent commandStartedEvent,
RequestContext requestContext) {

super((carrier, key, value) -> {}, Kind.CLIENT);
this.connectionString = connectionString;
this.commandStartedEvent = commandStartedEvent;
this.requestContext = requestContext;
this.collectionName = getCollectionName(commandStartedEvent);
Expand All @@ -73,17 +79,21 @@ public RequestContext getRequestContext() {
return this.requestContext;
}

public String getDatabaseName() {
return commandStartedEvent.getDatabaseName();
}

public String getCollectionName() {
return this.collectionName;
}

public String getContextualName() {

if (this.collectionName == null) {
return this.commandStartedEvent.getCommandName();
}
public String getCommandName() {
return commandStartedEvent.getCommandName();
}

return this.commandStartedEvent.getCommandName() + " " + this.collectionName;
@Nullable
public ConnectionString getConnectionString() {
return connectionString;
}

public void setCommandSucceededEvent(CommandSucceededEvent commandSucceededEvent) {
Expand Down Expand Up @@ -116,7 +126,7 @@ private static String getCollectionName(CommandStartedEvent event) {
}

// Some other commands, like getMore, have a field like {"collection": collectionName}.
return getNonEmptyBsonString(command.get("collection"));
return command == null ? "" : getNonEmptyBsonString(command.get("collection"));
}

/**
Expand All @@ -125,7 +135,7 @@ private static String getCollectionName(CommandStartedEvent event) {
* @return trimmed string from {@code bsonValue} or null if the trimmed string was empty or the value wasn't a string
*/
@Nullable
private static String getNonEmptyBsonString(BsonValue bsonValue) {
private static String getNonEmptyBsonString(@Nullable BsonValue bsonValue) {

if (bsonValue == null || !bsonValue.isString()) {
return null;
Expand All @@ -135,4 +145,5 @@ private static String getNonEmptyBsonString(BsonValue bsonValue) {

return stringValue.isEmpty() ? null : stringValue;
}

}

This file was deleted.

0 comments on commit 2d63d60

Please sign in to comment.