Skip to content

Commit

Permalink
Reorder checking of delegate vs. wrapper in OTel tracer unwrap (helid…
Browse files Browse the repository at this point in the history
…on-io#8855)

* Reorder checking of delegate vs. wrapper in OTel tracer unwrap

Signed-off-by: Tim Quinn <[email protected]>

* Relax the test a bit making sure unwrap(Object.class) returns what we expect

---------

Signed-off-by: Tim Quinn <[email protected]>
  • Loading branch information
tjquinno authored Jun 6, 2024
1 parent b091bee commit e4f997b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ public void inject(SpanContext spanContext, HeaderProvider inboundHeadersProvide

@Override
public <T> T unwrap(Class<T> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit e4f997b

Please sign in to comment.