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 org .springframework .data .mongodb .observability .MongoObservation .LowCardinalityCommandKeyNames ;
22
- import org .springframework .util .Assert ;
23
- import org .springframework .util .ObjectUtils ;
24
-
25
18
import com .mongodb .ConnectionString ;
26
19
import com .mongodb .ServerAddress ;
27
20
import com .mongodb .connection .ConnectionDescription ;
28
21
import com .mongodb .connection .ConnectionId ;
29
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 .*;
30
28
31
29
/**
32
30
* Default {@link MongoHandlerObservationConvention} implementation.
@@ -41,54 +39,43 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio
41
39
@ Override
42
40
public KeyValues getLowCardinalityKeyValues (MongoHandlerContext context ) {
43
41
44
- KeyValues keyValues = KeyValues .of (LowCardinalityCommandKeyNames .DB_SYSTEM .withValue ("mongodb" ),
45
- LowCardinalityCommandKeyNames .MONGODB_COMMAND .withValue (context .getCommandName ()));
46
-
47
- ConnectionString connectionString = context .getConnectionString ();
48
- if (connectionString != null ) {
49
-
50
- keyValues = keyValues
51
- .and (LowCardinalityCommandKeyNames .DB_CONNECTION_STRING .withValue (connectionString .getConnectionString ()));
52
-
53
- String user = connectionString .getUsername ();
54
-
55
- if (!ObjectUtils .isEmpty (user )) {
56
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .DB_USER .withValue (user ));
57
- }
58
- }
59
-
60
- if (!ObjectUtils .isEmpty (context .getDatabaseName ())) {
61
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .DB_NAME .withValue (context .getDatabaseName ()));
62
- }
63
-
64
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .MONGODB_COLLECTION .withValue (
65
- ObjectUtils .isEmpty (context .getCollectionName ()) ? KeyValue .NONE_VALUE : context .getCollectionName ()));
66
-
67
42
if (context .getCommandStartedEvent () == null ) {
68
43
throw new IllegalStateException ("not command started event present" );
69
44
}
70
45
71
- ConnectionDescription connectionDescription = context .getCommandStartedEvent ().getConnectionDescription ();
46
+ ConnectionString connectionString = context .getConnectionString ();
47
+ String connectionStringValue = connectionString != null ? connectionString .getConnectionString () : null ;
48
+ String username = connectionString != null ? connectionString .getUsername () : null ;
72
49
50
+ String transport = null , peerName = null , peerPort =null , clusterId = null ;
51
+ ConnectionDescription connectionDescription = context .getCommandStartedEvent ().getConnectionDescription ();
73
52
if (connectionDescription != null ) {
74
-
75
53
ServerAddress serverAddress = connectionDescription .getServerAddress ();
76
54
77
55
if (serverAddress != null ) {
78
-
79
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .NET_TRANSPORT .withValue ("IP.TCP" ),
80
- LowCardinalityCommandKeyNames .NET_PEER_NAME .withValue (serverAddress .getHost ()),
81
- LowCardinalityCommandKeyNames .NET_PEER_PORT .withValue ("" + serverAddress .getPort ()));
56
+ transport = "IP.TCP" ;
57
+ peerName = serverAddress .getHost ();
58
+ peerPort = String .valueOf (serverAddress .getPort ());
82
59
}
83
60
84
61
ConnectionId connectionId = connectionDescription .getConnectionId ();
85
62
if (connectionId != null ) {
86
- keyValues = keyValues .and (LowCardinalityCommandKeyNames .MONGODB_CLUSTER_ID
87
- .withValue (connectionId .getServerId ().getClusterId ().getValue ()));
63
+ clusterId = connectionId .getServerId ().getClusterId ().getValue ();
88
64
}
89
65
}
90
66
91
- 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
+ );
92
79
}
93
80
94
81
@ Override
0 commit comments