diff --git a/instrumentation/akka-http-10.0/javaagent/akka-http-10.0-javaagent.gradle b/instrumentation/akka-http-10.0/javaagent/akka-http-10.0-javaagent.gradle index 82861a26c2b2..a641f3c1a5d2 100644 --- a/instrumentation/akka-http-10.0/javaagent/akka-http-10.0-javaagent.gradle +++ b/instrumentation/akka-http-10.0/javaagent/akka-http-10.0-javaagent.gradle @@ -43,9 +43,9 @@ muzzle { dependencies { library "com.typesafe.akka:akka-http_2.11:10.0.0" library "com.typesafe.akka:akka-stream_2.11:2.4.14" -} -tasks.withType(Test).configureEach { - // https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/2639 - jvmArgs "-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=false" -} \ No newline at end of file + // these instrumentations are not needed for the tests to pass + // they are here to test for context leaks + testInstrumentation project(':instrumentation:akka-actor-2.5:javaagent') + testInstrumentation project(':instrumentation:akka-actor-fork-join-2.5:javaagent') +} diff --git a/instrumentation/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/client/PoolMasterActorInstrumentation.java b/instrumentation/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/client/PoolMasterActorInstrumentation.java new file mode 100644 index 000000000000..3092e7362445 --- /dev/null +++ b/instrumentation/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/client/PoolMasterActorInstrumentation.java @@ -0,0 +1,46 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.akkahttp.client; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +public class PoolMasterActorInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("akka.http.impl.engine.client.PoolMasterActor"); + } + + @Override + public void transform(TypeTransformer transformer) { + // scala compiler mangles method names + transformer.applyAdviceToMethod( + named("akka$http$impl$engine$client$PoolMasterActor$$startPoolInterface") + .or(named("akka$http$impl$engine$client$PoolMasterActor$$startPoolInterfaceActor")), + ClearContextAdvice.class.getName()); + } + + public static class ClearContextAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static Scope enter() { + return Java8BytecodeBridge.rootContext().makeCurrent(); + } + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void exit(@Advice.Enter Scope scope) { + if (scope != null) { + scope.close(); + } + } + } +} diff --git a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/Java8BytecodeBridge.java b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/Java8BytecodeBridge.java index e0d95912aa7f..52ad825f34b4 100644 --- a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/Java8BytecodeBridge.java +++ b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/Java8BytecodeBridge.java @@ -22,6 +22,11 @@ public static Context currentContext() { return Context.current(); } + /** Calls {@link Context#root()}. */ + public static Context rootContext() { + return Context.root(); + } + /** Calls {@link Span#current()}. */ public static Span currentSpan() { return Span.current();