Skip to content

Commit 104cb1f

Browse files
jonatan-ivanovchristophstrobl
authored andcommitted
Provide fallback for Observation KeyValues
Closes: #5020
1 parent d1847d6 commit 104cb1f

File tree

2 files changed

+41
-48
lines changed

2 files changed

+41
-48
lines changed

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

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@
1515
*/
1616
package org.springframework.data.mongodb.observability;
1717

18-
import io.micrometer.common.KeyValue;
19-
import io.micrometer.common.KeyValues;
20-
21-
import java.net.InetSocketAddress;
22-
23-
import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames;
24-
import org.springframework.data.mongodb.util.MongoCompatibilityAdapter;
25-
import org.springframework.util.ObjectUtils;
26-
2718
import com.mongodb.ConnectionString;
2819
import com.mongodb.ServerAddress;
2920
import com.mongodb.connection.ConnectionDescription;
3021
import com.mongodb.connection.ConnectionId;
3122
import com.mongodb.event.CommandStartedEvent;
23+
import io.micrometer.common.KeyValues;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.ObjectUtils;
26+
27+
import static org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames.*;
3228

3329
/**
3430
* Default {@link MongoHandlerObservationConvention} implementation.
@@ -43,60 +39,43 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio
4339
@Override
4440
public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) {
4541

46-
KeyValues keyValues = KeyValues.of(LowCardinalityCommandKeyNames.DB_SYSTEM.withValue("mongodb"),
47-
LowCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandName()));
48-
49-
ConnectionString connectionString = context.getConnectionString();
50-
if (connectionString != null) {
51-
52-
keyValues = keyValues
53-
.and(LowCardinalityCommandKeyNames.DB_CONNECTION_STRING.withValue(connectionString.getConnectionString()));
54-
55-
String user = connectionString.getUsername();
56-
57-
if (!ObjectUtils.isEmpty(user)) {
58-
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_USER.withValue(user));
59-
}
60-
}
61-
62-
if (!ObjectUtils.isEmpty(context.getDatabaseName())) {
63-
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_NAME.withValue(context.getDatabaseName()));
42+
if (context.getCommandStartedEvent() == null) {
43+
throw new IllegalStateException("not command started event present");
6444
}
6545

66-
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(
67-
ObjectUtils.isEmpty(context.getCollectionName()) ? KeyValue.NONE_VALUE : context.getCollectionName()));
46+
ConnectionString connectionString = context.getConnectionString();
47+
String connectionStringValue = connectionString != null ? connectionString.getConnectionString() : null;
48+
String username = connectionString != null ? connectionString.getUsername() : null;
6849

50+
String transport = null, peerName = null, peerPort =null, clusterId = null;
6951
ConnectionDescription connectionDescription = context.getCommandStartedEvent().getConnectionDescription();
70-
7152
if (connectionDescription != null) {
72-
7353
ServerAddress serverAddress = connectionDescription.getServerAddress();
7454

7555
if (serverAddress != null) {
76-
77-
keyValues = keyValues.and(LowCardinalityCommandKeyNames.NET_TRANSPORT.withValue("IP.TCP"),
78-
LowCardinalityCommandKeyNames.NET_PEER_NAME.withValue(serverAddress.getHost()),
79-
LowCardinalityCommandKeyNames.NET_PEER_PORT.withValue("" + serverAddress.getPort()));
80-
81-
InetSocketAddress socketAddress = MongoCompatibilityAdapter.serverAddressAdapter(serverAddress)
82-
.getSocketAddress();
83-
84-
if (socketAddress != null) {
85-
86-
keyValues = keyValues.and(
87-
LowCardinalityCommandKeyNames.NET_SOCK_PEER_ADDR.withValue(socketAddress.getHostName()),
88-
LowCardinalityCommandKeyNames.NET_SOCK_PEER_PORT.withValue("" + socketAddress.getPort()));
89-
}
56+
transport = "IP.TCP";
57+
peerName = serverAddress.getHost();
58+
peerPort = String.valueOf(serverAddress.getPort());
9059
}
9160

9261
ConnectionId connectionId = connectionDescription.getConnectionId();
9362
if (connectionId != null) {
94-
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID
95-
.withValue(connectionId.getServerId().getClusterId().getValue()));
63+
clusterId = connectionId.getServerId().getClusterId().getValue();
9664
}
9765
}
9866

99-
return keyValues;
67+
return KeyValues.of(
68+
DB_SYSTEM.withValue("mongodb"),
69+
MONGODB_COMMAND.withValue(context.getCommandName()),
70+
DB_CONNECTION_STRING.withOptionalValue(connectionStringValue),
71+
DB_USER.withOptionalValue(username),
72+
DB_NAME.withOptionalValue(context.getDatabaseName()),
73+
MONGODB_COLLECTION.withOptionalValue(context.getCollectionName()),
74+
NET_TRANSPORT.withOptionalValue(transport),
75+
NET_PEER_NAME.withOptionalValue(peerName),
76+
NET_PEER_PORT.withOptionalValue(peerPort),
77+
MONGODB_CLUSTER_ID.withOptionalValue(clusterId)
78+
);
10079
}
10180

10281
@Override
@@ -110,6 +89,8 @@ public String getContextualName(MongoHandlerContext context) {
11089
String collectionName = context.getCollectionName();
11190
CommandStartedEvent commandStartedEvent = context.getCommandStartedEvent();
11291

92+
Assert.notNull(commandStartedEvent, "CommandStartedEvent must not be null");
93+
11394
if (ObjectUtils.isEmpty(collectionName)) {
11495
return commandStartedEvent.getCommandName();
11596
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.observability;
1717

18+
import io.micrometer.common.KeyValue;
1819
import io.micrometer.common.docs.KeyName;
1920
import io.micrometer.observation.docs.ObservationDocumentation;
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.util.ObjectUtils;
2023

2124
/**
2225
* A MongoDB-based {@link io.micrometer.observation.Observation}.
@@ -172,6 +175,15 @@ public String asString() {
172175
public String asString() {
173176
return "db.operation";
174177
}
178+
};
179+
180+
/**
181+
* Creates a key value for the given key name.
182+
* @param value value for key, if value is null or empty {@link KeyValue#NONE_VALUE} will be used
183+
* @return key value
184+
*/
185+
public KeyValue withOptionalValue(@Nullable String value) {
186+
return withValue(ObjectUtils.isEmpty(value) ? KeyValue.NONE_VALUE : value);
175187
}
176188
}
177189

0 commit comments

Comments
 (0)