Fix instrumentation of Azure SDK EventHubs library (#8916)

This commit is contained in:
Trask Stalnaker 2023-07-11 09:07:55 -07:00 committed by GitHub
parent fce77cc044
commit 7e4c0fd615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 2 deletions

View File

@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.azurecore.v1_36;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Arrays.asList;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.not;
import com.google.auto.service.AutoService;
@ -30,6 +30,11 @@ public class AzureSdkInstrumentationModule extends InstrumentationModule {
helperResourceBuilder.register(
"META-INF/services/com.azure.core.util.tracing.TracerProvider",
"azure-core-1.36/META-INF/services/com.azure.core.util.tracing.TracerProvider");
// some azure sdks (e.g. EventHubs) are still looking up Tracer via service loader
// and not yet using the new TracerProvider
helperResourceBuilder.register(
"META-INF/services/com.azure.core.util.tracing.Tracer",
"azure-core-1.36/META-INF/services/com.azure.core.util.tracing.Tracer");
}
@Override
@ -47,7 +52,8 @@ public class AzureSdkInstrumentationModule extends InstrumentationModule {
public static class EmptyTypeInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("com.azure.core.util.tracing.TracerProvider");
return namedOneOf(
"com.azure.core.util.tracing.TracerProvider", "com.azure.core.util.tracing.Tracer");
}
@Override

View File

@ -0,0 +1 @@
io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.shaded.com.azure.core.tracing.opentelemetry.OpenTelemetryTracer

View File

@ -0,0 +1,60 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import static org.assertj.core.api.Assertions.assertThat;
import com.azure.core.util.Context;
import com.azure.core.util.tracing.Tracer;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.trace.data.StatusData;
import java.util.Iterator;
import java.util.ServiceLoader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
// some azure sdks (e.g. EventHubs) are still looking up Tracer via service loader
// and not yet using the new TracerProvider
class AzureSdkTestOld {
@RegisterExtension
public static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
@Test
void testHelperClassesInjected() {
com.azure.core.util.tracing.Tracer azTracer = createAzTracer();
assertThat(azTracer.isEnabled()).isTrue();
assertThat(azTracer.getClass().getName())
.isEqualTo(
"io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.shaded"
+ ".com.azure.core.tracing.opentelemetry.OpenTelemetryTracer");
}
@Test
void testSpan() {
com.azure.core.util.tracing.Tracer azTracer = createAzTracer();
Context context = azTracer.start("hello", Context.NONE);
azTracer.end(null, null, context);
testing.waitAndAssertTracesWithoutScopeVersionVerification(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("hello")
.hasKind(SpanKind.INTERNAL)
.hasStatus(StatusData.unset())
.hasAttributesSatisfying(Attributes::isEmpty)));
}
private static com.azure.core.util.tracing.Tracer createAzTracer() {
Iterable<com.azure.core.util.tracing.Tracer> tracers =
ServiceLoader.load(com.azure.core.util.tracing.Tracer.class);
Iterator<Tracer> it = tracers.iterator();
return it.hasNext() ? it.next() : null;
}
}