3434import java .util .function .Predicate ;
3535import java .util .function .Supplier ;
3636
37- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .CLIENT_CONNECTION_ID ;
38- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .COLLECTION ;
39- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .COMMAND_NAME ;
40- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .NAMESPACE ;
41- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .OPERATION_NAME ;
42- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .OPERATION_SUMMARY ;
43- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .NETWORK_TRANSPORT ;
44- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .QUERY_SUMMARY ;
45- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .SERVER_ADDRESS ;
46- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .SERVER_CONNECTION_ID ;
47- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .SERVER_PORT ;
48- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .SESSION_ID ;
49- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .SYSTEM ;
50- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .TRANSACTION_NUMBER ;
51- import static com .mongodb .internal .observability .micrometer .MongodbObservation .LowCardinalityKeyNames .CURSOR_ID ;
37+ import static com .mongodb .internal .observability .micrometer .MongodbObservation .MONGODB_COMMAND ;
38+ import static com .mongodb .internal .observability .micrometer .MongodbObservation .MONGODB_OPERATION ;
39+ import static com .mongodb .internal .observability .micrometer .MongodbObservation .CommandLowCardinalityKeyNames ;
40+ import static com .mongodb .internal .observability .micrometer .MongodbObservation .OperationLowCardinalityKeyNames ;
5241import static java .lang .System .getenv ;
5342
5443/**
@@ -109,35 +98,31 @@ public TracingManager(@Nullable final ObservabilitySettings observabilitySetting
10998 }
11099
111100 /**
112- * Creates a new span with the specified name and parent trace context.
113- * <p>
114- * This method is used to create a span that is linked to a parent context,
115- * enabling hierarchical tracing of operations.
116- * </p>
101+ * Creates a new span with the specified observation type, name and parent trace context.
117102 *
118- * @param name The name of the span.
119- * @param parentContext The parent trace context to associate with the span.
103+ * @param observationType The observation type (operation or command).
104+ * @param name The name of the span.
105+ * @param parentContext The parent trace context to associate with the span.
120106 * @return The created span.
121107 */
122- public Span addSpan (final String name , @ Nullable final TraceContext parentContext ) {
123- return tracer .nextSpan (name , parentContext , null );
108+ public Span addSpan (final MongodbObservation observationType , final String name ,
109+ @ Nullable final TraceContext parentContext ) {
110+ return tracer .nextSpan (observationType , name , parentContext , null );
124111 }
125112
126113 /**
127- * Creates a new span with the specified name, parent trace context, and MongoDB namespace.
128- * <p>
129- * This method is used to create a span that is linked to a parent context,
130- * enabling hierarchical tracing of operations. The MongoDB namespace can be used
131- * by nested spans to access the database and collection name (which might not be easily accessible at connection layer).
132- * </p>
114+ * Creates a new span with the specified observation type, name, parent trace context,
115+ * and MongoDB namespace.
133116 *
134- * @param name The name of the span.
135- * @param parentContext The parent trace context to associate with the span.
136- * @param namespace The MongoDB namespace associated with the operation.
117+ * @param observationType The observation type (operation or command).
118+ * @param name The name of the span.
119+ * @param parentContext The parent trace context to associate with the span.
120+ * @param namespace The MongoDB namespace associated with the operation.
137121 * @return The created span.
138122 */
139- public Span addSpan (final String name , @ Nullable final TraceContext parentContext , final MongoNamespace namespace ) {
140- return tracer .nextSpan (name , parentContext , namespace );
123+ public Span addSpan (final MongodbObservation observationType , final String name ,
124+ @ Nullable final TraceContext parentContext , final MongoNamespace namespace ) {
125+ return tracer .nextSpan (observationType , name , parentContext , namespace );
141126 }
142127
143128 /**
@@ -146,8 +131,8 @@ public Span addSpan(final String name, @Nullable final TraceContext parentContex
146131 * @return The created transaction span.
147132 */
148133 public Span addTransactionSpan () {
149- Span span = tracer .nextSpan ("transaction" , null , null );
150- span .tagLowCardinality (SYSTEM .withValue ("mongodb" ));
134+ Span span = tracer .nextSpan (MONGODB_OPERATION , "transaction" , null , null );
135+ span .tagLowCardinality (OperationLowCardinalityKeyNames . SYSTEM .withValue ("mongodb" ));
151136 return span ;
152137 }
153138
@@ -205,13 +190,13 @@ public Span createTracingSpan(final CommandMessage message,
205190 }
206191
207192 Span operationSpan = operationContext .getTracingSpan ();
208- Span span = addSpan (commandName , operationSpan != null ? operationSpan .context () : null );
193+ Span span = addSpan (MONGODB_COMMAND , commandName , operationSpan != null ? operationSpan .context () : null );
209194
210195 if (command .containsKey ("getMore" )) {
211196 long cursorId = command .getInt64 ("getMore" ).longValue ();
212- span .tagLowCardinality (CURSOR_ID .withValue (String .valueOf (cursorId )));
197+ span .tagLowCardinality (CommandLowCardinalityKeyNames . CURSOR_ID .withValue (String .valueOf (cursorId )));
213198 if (operationSpan != null ) {
214- operationSpan .tagLowCardinality (CURSOR_ID .withValue (String .valueOf (cursorId )));
199+ operationSpan .tagLowCardinality (CommandLowCardinalityKeyNames . CURSOR_ID .withValue (String .valueOf (cursorId )));
215200 }
216201 }
217202
@@ -234,33 +219,33 @@ public Span createTracingSpan(final CommandMessage message,
234219 String summary = commandName + " " + namespace + (collection .isEmpty () ? "" : "." + collection );
235220
236221 KeyValues keyValues = KeyValues .of (
237- SYSTEM .withValue ("mongodb" ),
238- NAMESPACE .withValue (namespace ),
239- QUERY_SUMMARY .withValue (summary ),
240- COMMAND_NAME .withValue (commandName ));
222+ CommandLowCardinalityKeyNames . SYSTEM .withValue ("mongodb" ),
223+ CommandLowCardinalityKeyNames . NAMESPACE .withValue (namespace ),
224+ CommandLowCardinalityKeyNames . QUERY_SUMMARY .withValue (summary ),
225+ CommandLowCardinalityKeyNames . COMMAND_NAME .withValue (commandName ));
241226
242227 if (!collection .isEmpty ()) {
243- keyValues = keyValues .and (COLLECTION .withValue (collection ));
228+ keyValues = keyValues .and (CommandLowCardinalityKeyNames . COLLECTION .withValue (collection ));
244229 }
245230 span .tagLowCardinality (keyValues );
246231
247232 // tag server and connection info
248233 ServerAddress serverAddress = serverAddressSupplier .get ();
249234 ConnectionId connectionId = connectionIdSupplier .get ();
250235 span .tagLowCardinality (KeyValues .of (
251- SERVER_ADDRESS .withValue (serverAddress .getHost ()),
252- SERVER_PORT .withValue (String .valueOf (serverAddress .getPort ())),
253- CLIENT_CONNECTION_ID .withValue (String .valueOf (connectionId .getLocalValue ())),
254- SERVER_CONNECTION_ID .withValue (String .valueOf (connectionId .getServerValue ())),
255- NETWORK_TRANSPORT .withValue (serverAddress instanceof UnixServerAddress ? "unix" : "tcp" )
236+ CommandLowCardinalityKeyNames . SERVER_ADDRESS .withValue (serverAddress .getHost ()),
237+ CommandLowCardinalityKeyNames . SERVER_PORT .withValue (String .valueOf (serverAddress .getPort ())),
238+ CommandLowCardinalityKeyNames . CLIENT_CONNECTION_ID .withValue (String .valueOf (connectionId .getLocalValue ())),
239+ CommandLowCardinalityKeyNames . SERVER_CONNECTION_ID .withValue (String .valueOf (connectionId .getServerValue ())),
240+ CommandLowCardinalityKeyNames . NETWORK_TRANSPORT .withValue (serverAddress instanceof UnixServerAddress ? "unix" : "tcp" )
256241 ));
257242
258243 // tag session and transaction info
259244 SessionContext sessionContext = operationContext .getSessionContext ();
260245 if (sessionContext .hasSession () && !sessionContext .isImplicitSession ()) {
261246 span .tagLowCardinality (KeyValues .of (
262- TRANSACTION_NUMBER .withValue (String .valueOf (sessionContext .getTransactionNumber ())),
263- SESSION_ID .withValue (String .valueOf (sessionContext .getSessionId ()
247+ CommandLowCardinalityKeyNames . TRANSACTION_NUMBER .withValue (String .valueOf (sessionContext .getTransactionNumber ())),
248+ CommandLowCardinalityKeyNames . SESSION_ID .withValue (String .valueOf (sessionContext .getSessionId ()
264249 .get (sessionContext .getSessionId ().getFirstKey ())
265250 .asBinary ().asUuid ()))
266251 ));
@@ -298,15 +283,15 @@ public Span createOperationSpan(@Nullable final TransactionSpan transactionSpan,
298283 : "." + namespace .getCollectionName ());
299284
300285 KeyValues keyValues = KeyValues .of (
301- SYSTEM .withValue ("mongodb" ),
302- NAMESPACE .withValue (namespace .getDatabaseName ()));
286+ OperationLowCardinalityKeyNames . SYSTEM .withValue ("mongodb" ),
287+ OperationLowCardinalityKeyNames . NAMESPACE .withValue (namespace .getDatabaseName ()));
303288 if (!MongoNamespaceHelper .COMMAND_COLLECTION_NAME .equalsIgnoreCase (namespace .getCollectionName ())) {
304- keyValues = keyValues .and (COLLECTION .withValue (namespace .getCollectionName ()));
289+ keyValues = keyValues .and (OperationLowCardinalityKeyNames . COLLECTION .withValue (namespace .getCollectionName ()));
305290 }
306- keyValues = keyValues .and (OPERATION_NAME .withValue (commandName ),
307- OPERATION_SUMMARY .withValue (name ));
291+ keyValues = keyValues .and (OperationLowCardinalityKeyNames . OPERATION_NAME .withValue (commandName ),
292+ OperationLowCardinalityKeyNames . OPERATION_SUMMARY .withValue (name ));
308293
309- Span span = addSpan (name , parentContext , namespace );
294+ Span span = addSpan (MONGODB_OPERATION , name , parentContext , namespace );
310295 span .tagLowCardinality (keyValues );
311296 operationContext .setTracingSpan (span );
312297 return span ;
0 commit comments