From e4f997b4ab878326edceeaf90bba6136ec448c23 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Thu, 6 Jun 2024 12:27:28 -0500 Subject: [PATCH] Reorder checking of delegate vs. wrapper in OTel tracer unwrap (#8855) * Reorder checking of delegate vs. wrapper in OTel tracer unwrap Signed-off-by: Tim Quinn * Relax the test a bit making sure unwrap(Object.class) returns what we expect --------- Signed-off-by: Tim Quinn --- .../providers/opentelemetry/OpenTelemetryTracer.java | 7 ++++--- .../tracing/providers/opentelemetry/TestUnwrap.java | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tracing/providers/opentelemetry/src/main/java/io/helidon/tracing/providers/opentelemetry/OpenTelemetryTracer.java b/tracing/providers/opentelemetry/src/main/java/io/helidon/tracing/providers/opentelemetry/OpenTelemetryTracer.java index f38bb09cadd..d50c67c3700 100644 --- a/tracing/providers/opentelemetry/src/main/java/io/helidon/tracing/providers/opentelemetry/OpenTelemetryTracer.java +++ b/tracing/providers/opentelemetry/src/main/java/io/helidon/tracing/providers/opentelemetry/OpenTelemetryTracer.java @@ -96,12 +96,13 @@ public void inject(SpanContext spanContext, HeaderProvider inboundHeadersProvide @Override public T unwrap(Class tracerClass) { - if (tracerClass.isInstance(this)) { - return tracerClass.cast(this); - } if (tracerClass.isAssignableFrom(delegate.getClass())) { return tracerClass.cast(delegate); } + if (tracerClass.isInstance(this)) { + return tracerClass.cast(this); + } + throw new IllegalArgumentException("Cannot provide an instance of " + tracerClass.getName() + ", telemetry tracer is: " + delegate.getClass().getName()); } diff --git a/tracing/providers/opentelemetry/src/test/java/io/helidon/tracing/providers/opentelemetry/TestUnwrap.java b/tracing/providers/opentelemetry/src/test/java/io/helidon/tracing/providers/opentelemetry/TestUnwrap.java index d654364844f..387f2e8e125 100644 --- a/tracing/providers/opentelemetry/src/test/java/io/helidon/tracing/providers/opentelemetry/TestUnwrap.java +++ b/tracing/providers/opentelemetry/src/test/java/io/helidon/tracing/providers/opentelemetry/TestUnwrap.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; class TestUnwrap { @@ -32,6 +33,14 @@ void testTracer() { assertThat("Tracer unwrapped", tracer.unwrap(Tracer.class), instanceOf(Tracer.class)); + + assertThat("Delegate unwrapped", + tracer.unwrap(io.opentelemetry.api.trace.Tracer.class), + instanceOf(io.opentelemetry.api.trace.Tracer.class)); + + assertThat("Object.toString()", + tracer.unwrap(Object.class).toString(), + containsString("io.opentelemetry")); } @Test