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

Fix context leak in akka-http instrumentation (DataDog/dd-trace-java#2320) #3264

Merged
merged 1 commit into from
Jun 11, 2021
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 @@ -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