-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable context propagation when virtual thread is switched to the ca…
…rrier thread (#10854)
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...va/io/opentelemetry/javaagent/instrumentation/executors/VirtualThreadInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.executors; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class VirtualThreadInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("java.lang.VirtualThread"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
// Disable context propagation when virtual thread is switched to the carrier thread. We should | ||
// not propagate context on the carrier thread. Also, context propagation code can cause the | ||
// carrier thread to park when it normally does not park, which may be unexpected for the jvm. | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/10747 | ||
transformer.applyAdviceToMethod( | ||
named("switchToCarrierThread").and(takesArguments(0)), | ||
this.getClass().getName() + "$SwitchToCarrierAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("switchToVirtualThread").and(takesArguments(1)), | ||
this.getClass().getName() + "$SwitchToVirtualAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class SwitchToCarrierAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void exit() { | ||
ExecutorAdviceHelper.disablePropagation(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class SwitchToVirtualAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void enter() { | ||
ExecutorAdviceHelper.enablePropagation(); | ||
} | ||
} | ||
} |