5151import org .testcontainers .containers .MongoDBContainer ;
5252import org .testcontainers .containers .Network ;
5353import org .testcontainers .containers .output .Slf4jLogConsumer ;
54-
5554import org .testcontainers .containers .wait .strategy .Wait ;
5655import org .testcontainers .junit .jupiter .Testcontainers ;
5756import org .testcontainers .lifecycle .Startables ;
@@ -109,7 +108,6 @@ class MongoKafkaConnectSinkTaskTest {
109108
110109 // Static methods
111110
112-
113111 private static String getKafkaConnectUrl () {
114112 return format (
115113 Locale .ROOT ,
@@ -381,24 +379,28 @@ public void testKafkaConnectMongoSinkTaskInstrumentation()
381379 JsonNode nameNode = span .get ("name" );
382380 JsonNode spanIdNode = span .get ("spanId" );
383381 JsonNode parentSpanIdNode = span .get ("parentSpanId" );
384-
382+
385383 if (nameNode != null && spanIdNode != null ) {
386384 String spanName = nameNode .asText ();
387385 String spanId = spanIdNode .asText ();
388- String parentSpanId = parentSpanIdNode != null ? parentSpanIdNode .asText () : null ;
386+ String parentSpanId =
387+ parentSpanIdNode != null ? parentSpanIdNode .asText () : null ;
389388
390389 // Check for Kafka Connect spans
391390 if (spanName .equals ("KafkaConnect.put" )) {
392391 foundKafkaConnectSpan = true ;
393392 kafkaConnectSpanId = spanId ;
394393 logger .info ("Found Kafka Connect span with ID: {}" , spanId );
395394 }
396-
395+
397396 // Check for MongoDB spans (insert, update, delete commands)
398- if (spanName .equals ("insert" ) || spanName .equals ("update" ) || spanName .equals ("delete" )) {
397+ if (spanName .equals ("insert" )
398+ || spanName .equals ("update" )
399+ || spanName .equals ("delete" )) {
399400 foundMongoSpan = true ;
400- logger .info ("Found MongoDB span '{}' with parent ID: {}" , spanName , parentSpanId );
401-
401+ logger .info (
402+ "Found MongoDB span '{}' with parent ID: {}" , spanName , parentSpanId );
403+
402404 // Check if MongoDB span is a child of Kafka Connect span
403405 if (kafkaConnectSpanId != null && kafkaConnectSpanId .equals (parentSpanId )) {
404406 foundParentChildRelationship = true ;
@@ -418,67 +420,76 @@ public void testKafkaConnectMongoSinkTaskInstrumentation()
418420 assertThat (spanCount ).as ("Should find at least one span" ).isGreaterThan (0 );
419421
420422 assertThat (foundKafkaConnectSpan ).as ("Should find Kafka Connect span" ).isTrue ();
421-
422- // Note: MongoDB spans are NOT expected from the Kafka Connect integration because
423+
424+ // Note: MongoDB spans are NOT expected from the Kafka Connect integration because
423425 // the MongoDB Kafka Connector creates its own MongoDB client without TracingCommandListener
424- // See: https://github.com/mongodb/mongo-kafka/blob/master/src/main/java/com/mongodb/kafka/connect/sink/StartedMongoSinkTask.java
425- // This demonstrates that trace propagation depends on how connectors integrate with instrumented libraries.
426- //
426+ // See:
427+ // https://github.com/mongodb/mongo-kafka/blob/master/src/main/java/com/mongodb/kafka/connect/sink/StartedMongoSinkTask.java
428+ // This demonstrates that trace propagation depends on how connectors integrate with
429+ // instrumented libraries.
430+ //
427431 // Unlike JDBC Kafka Connector (which uses instrumented PreparedStatement operations),
428- // MongoDB Kafka Connector bypasses OpenTelemetry instrumentation, so we only get
432+ // MongoDB Kafka Connector bypasses OpenTelemetry instrumentation, so we only get
429433 // the Kafka Connect span with span links to producers.
430- logger .info ("MongoDB span found: {} (expected: false for Kafka Connect integration)" , foundMongoSpan );
431- logger .info ("Parent-child relationship found: {} (expected: false for Kafka Connect integration)" , foundParentChildRelationship );
432-
434+ logger .info (
435+ "MongoDB span found: {} (expected: false for Kafka Connect integration)" , foundMongoSpan );
436+ logger .info (
437+ "Parent-child relationship found: {} (expected: false for Kafka Connect integration)" ,
438+ foundParentChildRelationship );
439+
433440 // The separate testTracePropagationWithInstrumentedMongoDB() demonstrates that
434441 // MongoDB instrumentation works perfectly when properly configured
435-
442+
436443 }
437444
438445 @ Test
439446 public void testTracePropagationWithInstrumentedMongoDB () throws Exception {
440447 logger .info ("=== Testing Trace Propagation with Properly Instrumented MongoDB ===" );
441-
448+
442449 // Create a properly instrumented MongoDB client (this will have TracingCommandListener)
443- String mongoConnectionString = String .format (Locale .ROOT , "mongodb://%s:%d" ,
444- mongoDB .getHost (), mongoDB .getMappedPort (27017 ));
445-
446- try (com .mongodb .client .MongoClient instrumentedClient =
447- com .mongodb .client .MongoClients .create (mongoConnectionString )) {
448-
450+ String mongoConnectionString =
451+ String .format (
452+ Locale .ROOT , "mongodb://%s:%d" , mongoDB .getHost (), mongoDB .getMappedPort (27017 ));
453+
454+ try (com .mongodb .client .MongoClient instrumentedClient =
455+ com .mongodb .client .MongoClients .create (mongoConnectionString )) {
456+
449457 // Get the collection
450- com .mongodb .client .MongoCollection <org .bson .Document > collection =
458+ com .mongodb .client .MongoCollection <org .bson .Document > collection =
451459 instrumentedClient .getDatabase ("test" ).getCollection ("demo" );
452-
460+
453461 // Clear spans from backend to isolate our test
454462 clearBackendTraces ();
455-
463+
456464 // Perform a MongoDB operation - this should create a span with proper instrumentation
457- org .bson .Document doc = new org .bson .Document ("demo" , "trace-propagation-test" )
458- .append ("timestamp" , System .currentTimeMillis ());
465+ org .bson .Document doc =
466+ new org .bson .Document ("demo" , "trace-propagation-test" )
467+ .append ("timestamp" , System .currentTimeMillis ());
459468 collection .insertOne (doc );
460-
469+
461470 // Wait for spans to arrive
462471 Thread .sleep (1000 );
463-
472+
464473 // Check if MongoDB span was created
465474 String backendUrl = getBackendUrl ();
466- String tracesJson = given ()
467- .when ()
468- .get (backendUrl + "/get-traces" )
469- .then ()
470- .statusCode (200 )
471- .extract ()
472- .asString ();
473-
475+ String tracesJson =
476+ given ()
477+ .when ()
478+ .get (backendUrl + "/get-traces" )
479+ .then ()
480+ .statusCode (200 )
481+ .extract ()
482+ .asString ();
483+
474484 if (!tracesJson .equals ("[]" )) {
475485 logger .info ("✅ SUCCESS: MongoDB operation created spans with proper instrumentation!" );
476- logger .info ("This proves that trace propagation works when downstream systems are properly instrumented." );
477-
486+ logger .info (
487+ "This proves that trace propagation works when downstream systems are properly instrumented." );
488+
478489 // Parse and verify MongoDB spans
479490 ObjectMapper objectMapper = new ObjectMapper ();
480491 JsonNode tracesNode = objectMapper .readTree (tracesJson );
481-
492+
482493 boolean foundMongoSpan = false ;
483494 for (JsonNode trace : tracesNode ) {
484495 JsonNode resourceSpans = trace .get ("resourceSpans" );
@@ -505,14 +516,16 @@ public void testTracePropagationWithInstrumentedMongoDB() throws Exception {
505516 }
506517 }
507518 }
508-
519+
509520 assertThat (foundMongoSpan )
510521 .as ("Should find MongoDB span when using properly instrumented client" )
511522 .isTrue ();
512-
523+
513524 } else {
514- logger .info ("ℹ️ No spans captured - this may indicate MongoDB instrumentation is not active" );
515- // Don't fail the test - this demonstrates the difference between instrumented and non-instrumented clients
525+ logger .info (
526+ "ℹ️ No spans captured - this may indicate MongoDB instrumentation is not active" );
527+ // Don't fail the test - this demonstrates the difference between instrumented and
528+ // non-instrumented clients
516529 }
517530 }
518531 }
0 commit comments