15
15
*/
16
16
package org .springframework .data .mongodb .observability ;
17
17
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
-
27
18
import com .mongodb .ConnectionString ;
28
19
import com .mongodb .ServerAddress ;
29
20
import com .mongodb .connection .ConnectionDescription ;
30
21
import com .mongodb .connection .ConnectionId ;
31
22
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 .*;
32
28
33
29
/**
34
30
* Default {@link MongoHandlerObservationConvention} implementation.
@@ -43,60 +39,43 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio
43
39
@ Override
44
40
public KeyValues getLowCardinalityKeyValues (MongoHandlerContext context ) {
45
41
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" );
64
44
}
65
45
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 ;
68
49
50
+ String transport = null , peerName = null , peerPort =null , clusterId = null ;
69
51
ConnectionDescription connectionDescription = context .getCommandStartedEvent ().getConnectionDescription ();
70
-
71
52
if (connectionDescription != null ) {
72
-
73
53
ServerAddress serverAddress = connectionDescription .getServerAddress ();
74
54
75
55
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 ());
90
59
}
91
60
92
61
ConnectionId connectionId = connectionDescription .getConnectionId ();
93
62
if (connectionId != null ) {
94
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .MONGODB_CLUSTER_ID
95
- .withValue (connectionId .getServerId ().getClusterId ().getValue ()));
63
+ clusterId = connectionId .getServerId ().getClusterId ().getValue ();
96
64
}
97
65
}
98
66
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
+ );
100
79
}
101
80
102
81
@ Override
@@ -110,6 +89,8 @@ public String getContextualName(MongoHandlerContext context) {
110
89
String collectionName = context .getCollectionName ();
111
90
CommandStartedEvent commandStartedEvent = context .getCommandStartedEvent ();
112
91
92
+ Assert .notNull (commandStartedEvent , "CommandStartedEvent must not be null" );
93
+
113
94
if (ObjectUtils .isEmpty (collectionName )) {
114
95
return commandStartedEvent .getCommandName ();
115
96
}
0 commit comments