Skip to content

Provide fallback for Observation KeyValues #5020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your approach and I like it more than concatenating KeyValues. With a refined approach regarding value extraction, we might get a chance for improvement.

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -172,6 +175,15 @@ public String asString() {
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if "UNKNOWN" is a better default value for most of these rather than none.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have a bit more encapsulated API for a more declarative approach to reduce the likelihood for using the API wrong. Something along the lines of:

// TODO: it would be nice to have a getKeyName() method returning KeyName from KeyValue for usage in e.g. KeyName[] getLowCardinalityKeyNames() 
static KeyValue DB_SYSTEM = KeyValue.of("db.system", "mongodb");


static SmartKeyName<ConnectionString> DB_CONNECTION_STRING = SmartKeyName.required("db.connection_string",
		ConnectionString::getConnectionString);

later:

@Nullable ConnectionString connectionString = context.getConnectionString();

KeyValues keyValues = KeyValues.of(MongoObservation.DB_SYSTEM,
		MongoObservation.DB_CONNECTION_STRING.valueOf(connectionString));

You can find SmartKeyName (needs probably a better name) at
https://gist.github.com/mp911de/6729ca327326d072820a1a0eb225c4a6

}
}

Expand Down