Skip to content

Commit cdd53e9

Browse files
opentelemetrybotlmolkovalaurit
authored
[release/v1.32.x] Update azure-core-tracing-opentelemetry version and fix sync suppression (#10382)
Co-authored-by: Liudmila Molkova <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 2ce68fe commit cdd53e9

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

instrumentation/azure-core/azure-core-1.36/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/azurecore/v1_36/AzureHttpClientInstrumentation.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
package io.opentelemetry.javaagent.instrumentation.azurecore.v1_36;
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
9+
import static io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.SuppressNestedClientHelper.disallowNestedClientSpanMono;
10+
import static io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.SuppressNestedClientHelper.disallowNestedClientSpanSync;
911
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1012
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
1113
import static net.bytebuddy.matcher.ElementMatchers.named;
1214
import static net.bytebuddy.matcher.ElementMatchers.returns;
1315

1416
import com.azure.core.http.HttpResponse;
17+
import io.opentelemetry.context.Scope;
1518
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1619
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1720
import net.bytebuddy.asm.Advice;
@@ -33,15 +36,37 @@ public void transform(TypeTransformer transformer) {
3336
.and(isPublic())
3437
.and(named("send"))
3538
.and(returns(named("reactor.core.publisher.Mono"))),
36-
this.getClass().getName() + "$SuppressNestedClientAdvice");
39+
this.getClass().getName() + "$SuppressNestedClientMonoAdvice");
40+
transformer.applyAdviceToMethod(
41+
isMethod()
42+
.and(isPublic())
43+
.and(named("sendSync"))
44+
.and(returns(named("com.azure.core.http.HttpResponse"))),
45+
this.getClass().getName() + "$SuppressNestedClientSyncAdvice");
46+
}
47+
48+
@SuppressWarnings("unused")
49+
public static class SuppressNestedClientMonoAdvice {
50+
@Advice.OnMethodExit(suppress = Throwable.class)
51+
public static void asyncSendExit(@Advice.Return(readOnly = false) Mono<HttpResponse> mono) {
52+
mono = disallowNestedClientSpanMono(mono);
53+
}
3754
}
3855

3956
@SuppressWarnings("unused")
40-
public static class SuppressNestedClientAdvice {
57+
public static class SuppressNestedClientSyncAdvice {
58+
59+
@Advice.OnMethodEnter(suppress = Throwable.class)
60+
public static void syncSendEnter(@Advice.Local("otelScope") Scope scope) {
61+
scope = disallowNestedClientSpanSync();
62+
}
4163

4264
@Advice.OnMethodExit(suppress = Throwable.class)
43-
public static void methodExit(@Advice.Return(readOnly = false) Mono<HttpResponse> mono) {
44-
mono = new SuppressNestedClientMono<>(mono);
65+
public static void syncSendExit(
66+
@Advice.Return HttpResponse response, @Advice.Local("otelScope") Scope scope) {
67+
if (scope != null) {
68+
scope.close();
69+
}
4570
}
4671
}
4772
}
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,30 @@
1414
import reactor.core.CoreSubscriber;
1515
import reactor.core.publisher.Mono;
1616

17-
public class SuppressNestedClientMono<T> extends Mono<T> {
17+
public class SuppressNestedClientHelper {
1818

19-
private final Mono<T> delegate;
20-
21-
public SuppressNestedClientMono(Mono<T> delegate) {
22-
this.delegate = delegate;
23-
}
24-
25-
@Override
26-
public void subscribe(CoreSubscriber<? super T> actual) {
19+
public static Scope disallowNestedClientSpanSync() {
2720
Context parentContext = currentContext();
2821
if (doesNotHaveClientSpan(parentContext)) {
29-
try (Scope ignored = disallowNestedClientSpan(parentContext).makeCurrent()) {
30-
delegate.subscribe(actual);
31-
}
32-
} else {
33-
delegate.subscribe(actual);
22+
return disallowNestedClientSpan(parentContext).makeCurrent();
3423
}
24+
return null;
25+
}
26+
27+
public static <T> Mono<T> disallowNestedClientSpanMono(Mono<T> delegate) {
28+
return new Mono<T>() {
29+
@Override
30+
public void subscribe(CoreSubscriber<? super T> coreSubscriber) {
31+
Context parentContext = currentContext();
32+
if (doesNotHaveClientSpan(parentContext)) {
33+
try (Scope ignored = disallowNestedClientSpan(parentContext).makeCurrent()) {
34+
delegate.subscribe(coreSubscriber);
35+
}
36+
} else {
37+
delegate.subscribe(coreSubscriber);
38+
}
39+
}
40+
};
3541
}
3642

3743
private static boolean doesNotHaveClientSpan(Context parentContext) {
@@ -44,4 +50,6 @@ private static Context disallowNestedClientSpan(Context parentContext) {
4450
return SpanKey.HTTP_CLIENT.storeInContext(
4551
SpanKey.KIND_CLIENT.storeInContext(parentContext, span), span);
4652
}
53+
54+
private SuppressNestedClientHelper() {}
4755
}

instrumentation/azure-core/azure-core-1.36/library-instrumentation-shaded/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
group = "io.opentelemetry.javaagent.instrumentation"
88

99
dependencies {
10-
implementation("com.azure:azure-core-tracing-opentelemetry:1.0.0-beta.32")
10+
implementation("com.azure:azure-core-tracing-opentelemetry:1.0.0-beta.42")
1111
}
1212

1313
tasks {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package com.azure.core.tracing.opentelemetry;
7+
8+
import com.azure.core.util.TracingOptions;
9+
10+
/**
11+
* Replace {@link OpenTelemetryTracingOptions} from com.azure:azure-core-tracing-opentelemetry with
12+
* a stub. Auto instrumentation does not use {@link OpenTelemetryTracingOptions}. This is needed
13+
* because {@link OpenTelemetryTracingOptions} calls super constructor in {@link TracingOptions}
14+
* that does exist in com.azure:azure-core:1.36.0 which triggers muzzle failure.
15+
*/
16+
public class OpenTelemetryTracingOptions extends TracingOptions {}

0 commit comments

Comments
 (0)