From 5f76fd249fdc65bca736a144f7e7a073d9f2b434 Mon Sep 17 00:00:00 2001 From: Stuart McCulloch Date: Tue, 26 Nov 2024 11:46:41 +0000 Subject: [PATCH] Fix missing OtelSpanEvent helper when using Event API from inside @WithSpan annotated method --- .../OpenTelemetryInstrumentation.java | 2 +- .../OpenTelemetryContextInstrumentation.java | 2 ++ .../OpenTelemetryContextStorageInstrumentation.java | 2 ++ dd-smoke-tests/opentelemetry/build.gradle | 1 + .../smoketest/opentelemetry/Application.java | 13 ++++++++++++- .../datadog/smoketest/OpenTelemetrySmokeTest.groovy | 10 ++++++++-- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java index 63883a317f5..5c28057fe09 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java @@ -73,12 +73,12 @@ public String[] helperClassNames() { "datadog.opentelemetry.shim.trace.OtelSpanBuilder", "datadog.opentelemetry.shim.trace.OtelSpanBuilder$1", "datadog.opentelemetry.shim.trace.OtelSpanContext", + "datadog.opentelemetry.shim.trace.OtelSpanEvent", "datadog.opentelemetry.shim.trace.OtelSpanEvent$AttributesJsonParser", "datadog.opentelemetry.shim.trace.OtelSpanLink", "datadog.opentelemetry.shim.trace.OtelTracer", "datadog.opentelemetry.shim.trace.OtelTracerBuilder", "datadog.opentelemetry.shim.trace.OtelTracerProvider", - "datadog.opentelemetry.shim.trace.OtelSpanEvent", }; } diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java index 440cb4afc7f..1aa555993e7 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java @@ -67,6 +67,8 @@ public String[] helperClassNames() { "datadog.opentelemetry.shim.trace.OtelSpanBuilder", "datadog.opentelemetry.shim.trace.OtelSpanBuilder$1", "datadog.opentelemetry.shim.trace.OtelSpanContext", + "datadog.opentelemetry.shim.trace.OtelSpanEvent", + "datadog.opentelemetry.shim.trace.OtelSpanEvent$AttributesJsonParser", "datadog.opentelemetry.shim.trace.OtelSpanLink", "datadog.opentelemetry.shim.trace.OtelTracer", "datadog.opentelemetry.shim.trace.OtelTracerBuilder", diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java index cdf63545a8a..345563d73bb 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java @@ -68,6 +68,8 @@ public String[] helperClassNames() { "datadog.opentelemetry.shim.trace.OtelSpanBuilder", "datadog.opentelemetry.shim.trace.OtelSpanBuilder$1", "datadog.opentelemetry.shim.trace.OtelSpanContext", + "datadog.opentelemetry.shim.trace.OtelSpanEvent", + "datadog.opentelemetry.shim.trace.OtelSpanEvent$AttributesJsonParser", "datadog.opentelemetry.shim.trace.OtelSpanLink", "datadog.opentelemetry.shim.trace.OtelTracer", "datadog.opentelemetry.shim.trace.OtelTracerBuilder", diff --git a/dd-smoke-tests/opentelemetry/build.gradle b/dd-smoke-tests/opentelemetry/build.gradle index 5deaa453fbc..6be9c1ece03 100644 --- a/dd-smoke-tests/opentelemetry/build.gradle +++ b/dd-smoke-tests/opentelemetry/build.gradle @@ -11,6 +11,7 @@ application { dependencies { implementation group: 'io.opentelemetry', name: 'opentelemetry-api', version: '1.4.0' + implementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: '1.20.0' testImplementation project(':dd-smoke-tests') } diff --git a/dd-smoke-tests/opentelemetry/src/main/java/datadog/smoketest/opentelemetry/Application.java b/dd-smoke-tests/opentelemetry/src/main/java/datadog/smoketest/opentelemetry/Application.java index 6f6d55fb546..ba5b26ecf0e 100644 --- a/dd-smoke-tests/opentelemetry/src/main/java/datadog/smoketest/opentelemetry/Application.java +++ b/dd-smoke-tests/opentelemetry/src/main/java/datadog/smoketest/opentelemetry/Application.java @@ -3,15 +3,26 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.instrumentation.annotations.WithSpan; /** * This application is a minimalistic application to create a trace with Open Telemetry Tracer API. */ public class Application { + + @WithSpan + static void annotatedSpan() { + Span.current().addEvent("annotated-span-event"); + } + public static void main(String[] args) throws InterruptedException { + // Start trace without touching GlobalOpenTelemetry + annotatedSpan(); + // Get an Open Telemetry tracer Tracer tracer = GlobalOpenTelemetry.getTracerProvider().tracerBuilder("smoketests").build(); - // Create a trace with few spans + + // Start some manual traces for (int i = 0; i < 10; i++) { Span span = tracer.spanBuilder("span-" + i).startSpan(); Thread.sleep(20); diff --git a/dd-smoke-tests/opentelemetry/src/test/groovy/datadog/smoketest/OpenTelemetrySmokeTest.groovy b/dd-smoke-tests/opentelemetry/src/test/groovy/datadog/smoketest/OpenTelemetrySmokeTest.groovy index 5cb5aaa2267..f9c85720b0b 100644 --- a/dd-smoke-tests/opentelemetry/src/test/groovy/datadog/smoketest/OpenTelemetrySmokeTest.groovy +++ b/dd-smoke-tests/opentelemetry/src/test/groovy/datadog/smoketest/OpenTelemetrySmokeTest.groovy @@ -1,13 +1,17 @@ package datadog.smoketest +import static java.util.concurrent.TimeUnit.SECONDS + class OpenTelemetrySmokeTest extends AbstractSmokeTest { + public static final int TIMEOUT_SECS = 30 + @Override ProcessBuilder createProcessBuilder() { def jarPath = System.getProperty("datadog.smoketest.shadowJar.path") def command = new ArrayList() command.add(javaPath()) command.addAll(defaultJavaProperties) - command.add("-Ddd.integration.opentelemetry.experimental.enabled=true") + command.add("-Ddd.trace.otel.enabled=true") command.addAll(["-jar", jarPath]) ProcessBuilder processBuilder = new ProcessBuilder(command) @@ -16,6 +20,8 @@ class OpenTelemetrySmokeTest extends AbstractSmokeTest { def 'receive trace'() { expect: - waitForTraceCount(1) + waitForTraceCount(11) // 1 annotated, 10 manual + assert testedProcess.waitFor(TIMEOUT_SECS, SECONDS) + assert testedProcess.exitValue() == 0 } }