|
15 | 15 | */ |
16 | 16 | package org.springframework.data.mongodb.observability; |
17 | 17 |
|
18 | | -import org.springframework.data.mongodb.observability.MongoObservation.HighCardinalityCommandKeyNames; |
| 18 | +import java.net.InetSocketAddress; |
| 19 | + |
19 | 20 | import org.springframework.data.mongodb.observability.MongoObservation.LowCardinalityCommandKeyNames; |
20 | | -import org.springframework.lang.Nullable; |
21 | 21 | import org.springframework.util.ObjectUtils; |
22 | 22 |
|
| 23 | +import com.mongodb.ConnectionString; |
| 24 | +import com.mongodb.ServerAddress; |
23 | 25 | import com.mongodb.connection.ConnectionDescription; |
24 | 26 | import com.mongodb.connection.ConnectionId; |
25 | 27 | import com.mongodb.event.CommandStartedEvent; |
26 | 28 |
|
27 | | -import io.micrometer.common.KeyValue; |
28 | 29 | import io.micrometer.common.KeyValues; |
29 | 30 |
|
30 | 31 | /** |
31 | 32 | * Default {@link MongoHandlerObservationConvention} implementation. |
32 | 33 | * |
33 | 34 | * @author Greg Turnquist |
34 | | - * @since 4 |
| 35 | + * @author Mark Paluch |
| 36 | + * @since 4.0 |
35 | 37 | */ |
36 | 38 | class DefaultMongoHandlerObservationConvention implements MongoHandlerObservationConvention { |
37 | 39 |
|
38 | 40 | @Override |
39 | 41 | public KeyValues getLowCardinalityKeyValues(MongoHandlerContext context) { |
40 | 42 |
|
41 | | - KeyValues keyValues = KeyValues.empty(); |
| 43 | + KeyValues keyValues = KeyValues.of(LowCardinalityCommandKeyNames.DB_SYSTEM.withValue("mongodb"), |
| 44 | + LowCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandName())); |
| 45 | + |
| 46 | + ConnectionString connectionString = context.getConnectionString(); |
| 47 | + if (connectionString != null) { |
| 48 | + |
| 49 | + keyValues = keyValues |
| 50 | + .and(LowCardinalityCommandKeyNames.DB_CONNECTION_STRING.withValue(connectionString.getConnectionString())); |
| 51 | + |
| 52 | + String user = connectionString.getUsername(); |
| 53 | + |
| 54 | + if (!ObjectUtils.isEmpty(user)) { |
| 55 | + keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_USER.withValue(user)); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + if (!ObjectUtils.isEmpty(context.getDatabaseName())) { |
| 61 | + keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_NAME.withValue(context.getDatabaseName())); |
| 62 | + } |
42 | 63 |
|
43 | 64 | if (!ObjectUtils.isEmpty(context.getCollectionName())) { |
44 | 65 | keyValues = keyValues |
45 | 66 | .and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName())); |
46 | 67 | } |
47 | 68 |
|
48 | | - KeyValue connectionTag = connectionTag(context.getCommandStartedEvent()); |
49 | | - if (connectionTag != null) { |
50 | | - keyValues = keyValues.and(connectionTag); |
| 69 | + ConnectionDescription connectionDescription = context.getCommandStartedEvent().getConnectionDescription(); |
| 70 | + |
| 71 | + if (connectionDescription != null) { |
| 72 | + |
| 73 | + ServerAddress serverAddress = connectionDescription.getServerAddress(); |
| 74 | + |
| 75 | + 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 = serverAddress.getSocketAddress(); |
| 82 | + |
| 83 | + if (socketAddress != null) { |
| 84 | + |
| 85 | + keyValues = keyValues.and( |
| 86 | + LowCardinalityCommandKeyNames.NET_SOCK_PEER_ADDR.withValue(socketAddress.getHostName()), |
| 87 | + LowCardinalityCommandKeyNames.NET_SOCK_PEER_PORT.withValue("" + socketAddress.getPort())); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + ConnectionId connectionId = connectionDescription.getConnectionId(); |
| 92 | + if (connectionId != null) { |
| 93 | + keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID |
| 94 | + .withValue(connectionId.getServerId().getClusterId().getValue())); |
| 95 | + } |
51 | 96 | } |
52 | 97 |
|
53 | 98 | return keyValues; |
54 | 99 | } |
55 | 100 |
|
56 | 101 | @Override |
57 | 102 | public KeyValues getHighCardinalityKeyValues(MongoHandlerContext context) { |
58 | | - |
59 | | - return KeyValues.of( |
60 | | - HighCardinalityCommandKeyNames.MONGODB_COMMAND.withValue(context.getCommandStartedEvent().getCommandName())); |
| 103 | + return KeyValues.empty(); |
61 | 104 | } |
62 | 105 |
|
63 | 106 | @Override |
64 | 107 | public String getContextualName(MongoHandlerContext context) { |
65 | | - return context.getContextualName(); |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * Extract connection details for a MongoDB connection into a {@link KeyValue}. |
70 | | - * |
71 | | - * @param event |
72 | | - * @return |
73 | | - */ |
74 | | - @Nullable |
75 | | - private static KeyValue connectionTag(CommandStartedEvent event) { |
76 | 108 |
|
77 | | - ConnectionDescription connectionDescription = event.getConnectionDescription(); |
78 | | - |
79 | | - if (connectionDescription != null) { |
| 109 | + String collectionName = context.getCollectionName(); |
| 110 | + CommandStartedEvent commandStartedEvent = context.getCommandStartedEvent(); |
80 | 111 |
|
81 | | - ConnectionId connectionId = connectionDescription.getConnectionId(); |
82 | | - if (connectionId != null) { |
83 | | - return LowCardinalityCommandKeyNames.MONGODB_CLUSTER_ID |
84 | | - .withValue(connectionId.getServerId().getClusterId().getValue()); |
85 | | - } |
| 112 | + if (ObjectUtils.isEmpty(collectionName)) { |
| 113 | + return commandStartedEvent.getCommandName(); |
86 | 114 | } |
87 | 115 |
|
88 | | - return null; |
| 116 | + return collectionName + "." + commandStartedEvent.getCommandName(); |
89 | 117 | } |
| 118 | + |
90 | 119 | } |
0 commit comments