Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update azure-core-tracing-opentelemetry version and fix sync suppression #10350

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
package io.opentelemetry.javaagent.instrumentation.azurecore.v1_36;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.SuppressNestedClientHelper.disallowNestedClientSpanMono;
import static io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.SuppressNestedClientHelper.disallowNestedClientSpanSync;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;

import com.azure.core.http.HttpResponse;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
Expand All @@ -33,15 +36,37 @@ public void transform(TypeTransformer transformer) {
.and(isPublic())
.and(named("send"))
.and(returns(named("reactor.core.publisher.Mono"))),
this.getClass().getName() + "$SuppressNestedClientAdvice");
this.getClass().getName() + "$SuppressNestedClientMonoAdvice");
transformer.applyAdviceToMethod(
isMethod()
.and(isPublic())
.and(named("sendSync"))
.and(returns(named("com.azure.core.http.HttpResponse"))),
this.getClass().getName() + "$SuppressNestedClientSyncAdvice");
}

@SuppressWarnings("unused")
public static class SuppressNestedClientMonoAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void asyncSendExit(@Advice.Return(readOnly = false) Mono<HttpResponse> mono) {
mono = disallowNestedClientSpanMono(mono);
}
}

@SuppressWarnings("unused")
public static class SuppressNestedClientAdvice {
public static class SuppressNestedClientSyncAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void syncSendEnter(@Advice.Local("otelScope") Scope scope) {
scope = disallowNestedClientSpanSync();
}

@Advice.OnMethodExit(suppress = Throwable.class)
public static void methodExit(@Advice.Return(readOnly = false) Mono<HttpResponse> mono) {
mono = new SuppressNestedClientMono<>(mono);
public static void syncSendExit(
@Advice.Return HttpResponse response, @Advice.Local("otelScope") Scope scope) {
if (scope != null) {
scope.close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Mono;

public class SuppressNestedClientMono<T> extends Mono<T> {
public class SuppressNestedClientHelper {

private final Mono<T> delegate;

public SuppressNestedClientMono(Mono<T> delegate) {
this.delegate = delegate;
}

@Override
public void subscribe(CoreSubscriber<? super T> actual) {
public static Scope disallowNestedClientSpanSync() {
Context parentContext = currentContext();
if (doesNotHaveClientSpan(parentContext)) {
try (Scope ignored = disallowNestedClientSpan(parentContext).makeCurrent()) {
delegate.subscribe(actual);
}
} else {
delegate.subscribe(actual);
return disallowNestedClientSpan(parentContext).makeCurrent();
}
return null;
}

public static <T> Mono<T> disallowNestedClientSpanMono(Mono<T> delegate) {
return new Mono<T>() {
@Override
public void subscribe(CoreSubscriber<? super T> coreSubscriber) {
Context parentContext = currentContext();
if (doesNotHaveClientSpan(parentContext)) {
try (Scope ignored = disallowNestedClientSpan(parentContext).makeCurrent()) {
delegate.subscribe(coreSubscriber);
}
} else {
delegate.subscribe(coreSubscriber);
}
}
};
}

private static boolean doesNotHaveClientSpan(Context parentContext) {
Expand All @@ -44,4 +50,6 @@ private static Context disallowNestedClientSpan(Context parentContext) {
return SpanKey.HTTP_CLIENT.storeInContext(
SpanKey.KIND_CLIENT.storeInContext(parentContext, span), span);
}

private SuppressNestedClientHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
group = "io.opentelemetry.javaagent.instrumentation"

dependencies {
implementation("com.azure:azure-core-tracing-opentelemetry:1.0.0-beta.32")
implementation("com.azure:azure-core-tracing-opentelemetry:1.0.0-beta.42")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package com.azure.core.tracing.opentelemetry;

import com.azure.core.util.TracingOptions;

/**
* Replace {@link OpenTelemetryTracingOptions} from com.azure:azure-core-tracing-opentelemetry with
* a stub. Auto instrumentation does not use {@link OpenTelemetryTracingOptions}. This is needed
* because {@link OpenTelemetryTracingOptions} calls super constructor in {@link TracingOptions}
* that does exist in com.azure:azure-core:1.36.0 which triggers muzzle failure.
*/
public class OpenTelemetryTracingOptions extends TracingOptions {}
Loading