Skip to content

Commit 6f3ed60

Browse files
committed
./gradlew spotlessApply
1 parent 9bdb037 commit 6f3ed60

File tree

2 files changed

+86
-65
lines changed

2 files changed

+86
-65
lines changed

instrumentation/kafka/kafka-connect-2.6/testing/src/test/java/io/opentelemetry/instrumentation/kafkaconnect/v2_6/MongoKafkaConnectSinkTaskTest.java

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.testcontainers.containers.MongoDBContainer;
5252
import org.testcontainers.containers.Network;
5353
import org.testcontainers.containers.output.Slf4jLogConsumer;
54-
5554
import org.testcontainers.containers.wait.strategy.Wait;
5655
import org.testcontainers.junit.jupiter.Testcontainers;
5756
import 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
}

instrumentation/kafka/kafka-connect-2.6/testing/src/test/java/io/opentelemetry/instrumentation/kafkaconnect/v2_6/PostgresKafkaConnectSinkTaskTest.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.testcontainers.containers.Network;
5555
import org.testcontainers.containers.PostgreSQLContainer;
5656
import org.testcontainers.containers.output.Slf4jLogConsumer;
57-
5857
import org.testcontainers.containers.wait.strategy.Wait;
5958
import org.testcontainers.junit.jupiter.Testcontainers;
6059
import org.testcontainers.lifecycle.Startables;
@@ -117,7 +116,6 @@ class PostgresKafkaConnectSinkTaskTest {
117116

118117
// Static methods
119118

120-
121119
private static String getKafkaConnectUrl() {
122120
return format(
123121
Locale.ROOT,
@@ -406,18 +404,24 @@ public void testKafkaConnectSinkTaskInstrumentation() throws IOException, Interr
406404
}
407405

408406
// Look for JDBC spans (executeBatch, executeUpdate, etc.)
409-
if (spanName.contains("INSERT") || spanName.contains("UPDATE")
410-
|| spanName.contains("DELETE") || spanName.contains("SELECT")
411-
|| spanName.contains("executeBatch") || spanName.contains("executeUpdate")) {
407+
if (spanName.contains("INSERT")
408+
|| spanName.contains("UPDATE")
409+
|| spanName.contains("DELETE")
410+
|| spanName.contains("SELECT")
411+
|| spanName.contains("executeBatch")
412+
|| spanName.contains("executeUpdate")) {
412413
foundJdbcSpan = true;
413414
logger.info("Found JDBC span: {}", spanName);
414-
415+
415416
// Check if this JDBC span is a child of Kafka Connect span
416-
if (parentSpanIdNode != null && kafkaConnectSpanId != null
417+
if (parentSpanIdNode != null
418+
&& kafkaConnectSpanId != null
417419
&& kafkaConnectSpanId.equals(parentSpanIdNode.asText())) {
418420
foundParentChildRelationship = true;
419-
logger.info("Found parent-child relationship: JDBC span {} is child of Kafka Connect span {}",
420-
spanName, kafkaConnectSpanId);
421+
logger.info(
422+
"Found parent-child relationship: JDBC span {} is child of Kafka Connect span {}",
423+
spanName,
424+
kafkaConnectSpanId);
421425
}
422426
}
423427
}
@@ -439,32 +443,36 @@ public void testKafkaConnectSinkTaskInstrumentation() throws IOException, Interr
439443
assertThat(spanCount).as("Should find at least one span").isGreaterThan(0);
440444

441445
assertThat(foundKafkaConnectSpan).as("Should find Kafka Connect span").isTrue();
442-
446+
443447
// JDBC spans are created by the instrumentation, as confirmed by container logs.
444448
// However, in this test environment, we may see spans from both:
445449
// 1. Test setup operations (CREATE TABLE, SELECT COUNT(*) - no parent-child relationship)
446450
// 2. Actual Kafka Connect container operations (with perfect parent-child relationships)
447-
//
451+
//
448452
// Container logs show perfect trace propagation: Producer → Kafka Connect → JDBC operations
449453
// But test environment may only capture the test-related JDBC spans, not container spans.
450454
assertThat(foundJdbcSpan)
451455
.as("Should find JDBC spans - JDBC instrumentation is active")
452456
.isTrue();
453-
457+
454458
logger.info("JDBC instrumentation test result: JDBC spans were found");
455-
459+
456460
// Parent-child relationships depend on the span source:
457461
// - Container spans (actual Kafka Connect operations): Perfect parent-child relationships ✅
458462
// - Test environment spans (test setup): No parent-child relationships ❌
459-
//
463+
//
460464
// The container logs prove that trace propagation works perfectly when both
461465
// instrumentations run in the same JVM process.
462466
if (foundParentChildRelationship) {
463-
logger.info("✅ SUCCESS: Parent-child relationship found - complete trace propagation verified!");
467+
logger.info(
468+
"✅ SUCCESS: Parent-child relationship found - complete trace propagation verified!");
464469
} else {
465-
logger.info("ℹ️ No parent-child relationship in test spans (expected for cross-process scenario)");
466-
logger.info("📋 Container logs confirm perfect trace propagation: Producer → Kafka Connect → Database");
467-
logger.info("🎯 This demonstrates the instrumentation works correctly for same-JVM deployments");
470+
logger.info(
471+
"ℹ️ No parent-child relationship in test spans (expected for cross-process scenario)");
472+
logger.info(
473+
"📋 Container logs confirm perfect trace propagation: Producer → Kafka Connect → Database");
474+
logger.info(
475+
"🎯 This demonstrates the instrumentation works correctly for same-JVM deployments");
468476
}
469477
}
470478

0 commit comments

Comments
 (0)