From 1dd60fa3358149a745f1a8aa68e44673237f500b Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Wed, 11 Dec 2024 18:34:55 +0100 Subject: [PATCH 1/3] make extended tracer easier to use --- .../opentelemetry/api/incubator/trace/ExtendedTracer.java | 3 +++ .../main/java/io/opentelemetry/sdk/trace/SdkTracer.java | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedTracer.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedTracer.java index cb2cae27c24..c37ba913eb1 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedTracer.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedTracer.java @@ -20,4 +20,7 @@ public interface ExtendedTracer extends Tracer { default boolean isEnabled() { return true; } + + @Override + ExtendedSpanBuilder spanBuilder(String spanName); } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java index 7b74c035e82..c42d55b1c9a 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java @@ -5,8 +5,8 @@ package io.opentelemetry.sdk.trace; +import io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder; import io.opentelemetry.api.incubator.trace.ExtendedTracer; -import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; @@ -34,16 +34,16 @@ final class SdkTracer implements ExtendedTracer { } @Override - public SpanBuilder spanBuilder(String spanName) { + public ExtendedSpanBuilder spanBuilder(String spanName) { if (!tracerEnabled) { - return NOOP_TRACER.spanBuilder(spanName); + return (ExtendedSpanBuilder) NOOP_TRACER.spanBuilder(spanName); } if (spanName == null || spanName.trim().isEmpty()) { spanName = FALLBACK_SPAN_NAME; } if (sharedState.hasBeenShutdown()) { Tracer tracer = TracerProvider.noop().get(instrumentationScopeInfo.getName()); - return tracer.spanBuilder(spanName); + return (ExtendedSpanBuilder) tracer.spanBuilder(spanName); } return new SdkSpanBuilder( spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); From 7548c2ddd2e9ba55fafa5c8a728983a52d48f4f9 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Thu, 12 Dec 2024 10:07:33 +0100 Subject: [PATCH 2/3] make extended tracer easier to use --- .../api/incubator/trace/ExtendedDefaultTracer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java index 948f17e9996..45346099a5d 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java @@ -10,7 +10,6 @@ import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators; import io.opentelemetry.api.internal.ApiUsageLogger; import io.opentelemetry.api.trace.Span; -import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.api.trace.Tracer; @@ -33,7 +32,7 @@ static Tracer getNoop() { } @Override - public SpanBuilder spanBuilder(String spanName) { + public ExtendedSpanBuilder spanBuilder(String spanName) { return NoopSpanBuilder.create(); } From d37d633e9ece890b61193ca904911a7ba077a2a1 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Mon, 16 Dec 2024 11:29:18 +0100 Subject: [PATCH 3/3] add usage test --- .../trace/ExtendedTraceApiUsageTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java index 40bf10454de..752cd279650 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java @@ -166,6 +166,7 @@ void startAndCallOrRun() { // Get a Tracer for a scope Tracer tracer = tracerProvider.get("org.foo.my-scope"); + ExtendedTracer extendedTracer = (ExtendedTracer) tracer; // Wrap the resetCheckout method in a span String cartId = @@ -173,20 +174,24 @@ void startAndCallOrRun() { .setAttribute("key123", "val456") .startAndCall(() -> resetCheckoutAndReturn("abc123", /* throwException= */ false)); assertThat(cartId).isEqualTo("abc123"); + // ...or use ExtendedTracer instance // ...or runnable variation - ((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout")) + extendedTracer + .spanBuilder("reset_checkout") .startAndRun(() -> resetCheckout("abc123", /* throwException= */ false)); // Wrap the resetCheckout method in a span; resetCheckout throws an exception try { - ((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout_and_return")) + extendedTracer + .spanBuilder("reset_checkout_and_return") .startAndCall(() -> resetCheckoutAndReturn("def456", /* throwException= */ true)); } catch (Throwable e) { // Ignore expected exception } // ...or runnable variation try { - ((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout")) + extendedTracer + .spanBuilder("reset_checkout") .startAndRun(() -> resetCheckout("def456", /* throwException= */ true)); } catch (Throwable e) { // Ignore expected exception @@ -195,7 +200,8 @@ void startAndCallOrRun() { // Wrap the resetCheckout method in a span; resetCheckout throws an exception; use custom error // handler try { - ((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout_and_return")) + extendedTracer + .spanBuilder("reset_checkout_and_return") .startAndCall( () -> resetCheckoutAndReturn("ghi789", /* throwException= */ true), (span, throwable) -> span.setAttribute("my-attribute", "error")); @@ -204,7 +210,8 @@ void startAndCallOrRun() { } // ...or runnable variation try { - ((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout")) + extendedTracer + .spanBuilder("reset_checkout") .startAndRun( () -> resetCheckout("ghi789", /* throwException= */ true), (span, throwable) -> span.setAttribute("my-attribute", "error"));