From 6fb00daf21e8e69688258403fef59ae3a0d0d1e8 Mon Sep 17 00:00:00 2001 From: Jonas Kunz Date: Tue, 20 Feb 2024 15:12:30 +0100 Subject: [PATCH] Added support for Otel TracerBuilder (#3535) --- CHANGELOG.asciidoc | 8 ++++++ .../AbstractOpenTelemetryInstrumentation.java | 4 ++- .../tracing/OTelTracerProvider.java | 6 ++++ .../tracing/OtelTracerBuilder.java | 28 +++++++++++++++++++ .../tracing/ElasticOpenTelemetryTest.java | 17 +++++++++++ .../opentelemetry/OpenTelemetryVersionIT.java | 5 +--- docs/api-opentelemetry.asciidoc | 2 +- 7 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OtelTracerBuilder.java diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 74dd110e5d..a612a7cc1c 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -35,6 +35,14 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: ===== Features * Bumped base alpine docker image version - {pull}3524[#3524] +[float] +===== Bug fixes +* Added missing support for TracerBuilder in OpenTelemetry bridge - {pull}3535[#3535] + +[float] +===== Potentially breaking changes +* The minimum supported OpenTelemetry version has been increased to 1.4.0 - {pull}3535[#3535] + [[release-notes-1.x]] === Java Agent version 1.x diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/AbstractOpenTelemetryInstrumentation.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/AbstractOpenTelemetryInstrumentation.java index fece6d123d..6286a929b0 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/AbstractOpenTelemetryInstrumentation.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/AbstractOpenTelemetryInstrumentation.java @@ -30,7 +30,9 @@ public abstract class AbstractOpenTelemetryInstrumentation extends ElasticApmIns @Override public ElementMatcher.Junction getClassLoaderMatcher() { - return classLoaderCanLoadClass("io.opentelemetry.context.propagation.TextMapSetter"); + return classLoaderCanLoadClass("io.opentelemetry.context.propagation.TextMapSetter") + //TracerBuilder has been added in 1.4.0, which is the minimum supported API version + .and(classLoaderCanLoadClass("io.opentelemetry.api.trace.TracerBuilder")); } @Override diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelTracerProvider.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelTracerProvider.java index 1303735ac9..3c1a5ab4e1 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelTracerProvider.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelTracerProvider.java @@ -19,6 +19,7 @@ package co.elastic.apm.agent.opentelemetry.tracing; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerBuilder; import io.opentelemetry.api.trace.TracerProvider; import javax.annotation.Nullable; @@ -39,4 +40,9 @@ public Tracer get(String instrumentationName) { public Tracer get(String instrumentationName, @Nullable String instrumentationVersion) { return this.tracer; } + + @Override + public TracerBuilder tracerBuilder(String instrumentationScopeName) { + return new OtelTracerBuilder(tracer); + } } diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OtelTracerBuilder.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OtelTracerBuilder.java new file mode 100644 index 0000000000..469d826f2b --- /dev/null +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OtelTracerBuilder.java @@ -0,0 +1,28 @@ +package co.elastic.apm.agent.opentelemetry.tracing; + +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerBuilder; + +public class OtelTracerBuilder implements TracerBuilder { + + private final Tracer tracer; + + public OtelTracerBuilder(Tracer tracer) { + this.tracer = tracer; + } + + @Override + public TracerBuilder setSchemaUrl(String s) { + return this; + } + + @Override + public TracerBuilder setInstrumentationVersion(String s) { + return this; + } + + @Override + public Tracer build() { + return tracer; + } +} diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java index 58d6a75446..2c0a5ee8c2 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java @@ -30,6 +30,7 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextKey; import io.opentelemetry.context.Scope; @@ -81,6 +82,22 @@ public void testTransaction() { .isEqualTo(OTelSpanKind.INTERNAL); } + @Test + public void testTracerBuilder() { + Tracer tracer = openTelemetry.tracerBuilder("foo") + .setSchemaUrl("bar") + .setInstrumentationVersion("baz") + .build(); + + tracer.spanBuilder("transaction") + .startSpan() + .end(); + + assertThat(reporter.getTransactions()).hasSize(1); + Transaction transaction = reporter.getFirstTransaction(); + assertThat(transaction.getNameAsString()).isEqualTo("transaction"); + } + @Test public void testTransactionWithAttribute() { otelTracer.spanBuilder("transaction") diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-test/src/test/java/co/elastic/apm/agent/opentelemetry/OpenTelemetryVersionIT.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-test/src/test/java/co/elastic/apm/agent/opentelemetry/OpenTelemetryVersionIT.java index 0081f65c9b..a49f1bef15 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-test/src/test/java/co/elastic/apm/agent/opentelemetry/OpenTelemetryVersionIT.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-test/src/test/java/co/elastic/apm/agent/opentelemetry/OpenTelemetryVersionIT.java @@ -29,10 +29,7 @@ public class OpenTelemetryVersionIT { @ParameterizedTest @ValueSource(strings = { - "1.0.1", - "1.1.0", - "1.2.0", - "1.3.0", + "1.4.0", "1.4.1", "1.5.0", "1.6.0", diff --git a/docs/api-opentelemetry.asciidoc b/docs/api-opentelemetry.asciidoc index 821ba08deb..22f123e7b2 100644 --- a/docs/api-opentelemetry.asciidoc +++ b/docs/api-opentelemetry.asciidoc @@ -43,7 +43,7 @@ The first step in getting started with the OpenTelemetry API bridge is to declar compile "io.opentelemetry:opentelemetry-api:$openTelemetryVersion" ---- -The minimum required OpenTelemetry version is 1.0.1. +The minimum required OpenTelemetry version is 1.4.0. [float] [[otel-init-tracer]]