Skip to content

Commit

Permalink
Fix context leak in akka-http instrumentation (DataDog/dd-trace-java#…
Browse files Browse the repository at this point in the history
…2320) (open-telemetry#3264)

Co-authored-by: Björn Antonsson <bjorn.antonsson@datadoghq.com>
  • Loading branch information
2 people authored and robododge committed Jun 17, 2021
1 parent 337d210 commit f01af8a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
// 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')
}
Original file line number Diff line number Diff line change
@@ -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<TypeDescription> 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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f01af8a

Please sign in to comment.