-
Notifications
You must be signed in to change notification settings - Fork 1k
Instrument instrumentation suppression api #14565
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
Merged
laurit
merged 3 commits into
open-telemetry:main
from
laurit:instrumentation-suppression
Sep 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...emetry/javaagent/instrumentation/opentelemetryapi/InstrumentationUtilInstrumentation.java
This file contains hidden or 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,65 @@ | ||||||||||||||||
| /* | ||||||||||||||||
| * Copyright The OpenTelemetry Authors | ||||||||||||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||
| */ | ||||||||||||||||
|
|
||||||||||||||||
| package io.opentelemetry.javaagent.instrumentation.opentelemetryapi; | ||||||||||||||||
|
|
||||||||||||||||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||||||||||||||||
| import static net.bytebuddy.matcher.ElementMatchers.returns; | ||||||||||||||||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||||||||||||||||
|
|
||||||||||||||||
| import application.io.opentelemetry.context.Context; | ||||||||||||||||
| import io.opentelemetry.api.internal.InstrumentationUtil; | ||||||||||||||||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||||||||||||||||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||||||||||||||||
| import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; | ||||||||||||||||
| import net.bytebuddy.asm.Advice; | ||||||||||||||||
| import net.bytebuddy.description.type.TypeDescription; | ||||||||||||||||
| import net.bytebuddy.matcher.ElementMatcher; | ||||||||||||||||
|
|
||||||||||||||||
| public class InstrumentationUtilInstrumentation implements TypeInstrumentation { | ||||||||||||||||
| @Override | ||||||||||||||||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||||||||||||||||
| return named("application.io.opentelemetry.api.internal.InstrumentationUtil"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public void transform(TypeTransformer transformer) { | ||||||||||||||||
| transformer.applyAdviceToMethod( | ||||||||||||||||
| named("shouldSuppressInstrumentation") | ||||||||||||||||
| .and(takesArgument(0, named("application.io.opentelemetry.context.Context"))) | ||||||||||||||||
| .and(returns(boolean.class)), | ||||||||||||||||
| this.getClass().getName() + "$ShouldSuppressAdvice"); | ||||||||||||||||
| transformer.applyAdviceToMethod( | ||||||||||||||||
| named("suppressInstrumentation").and(takesArgument(0, Runnable.class)), | ||||||||||||||||
| this.getClass().getName() + "$SuppressAdvice"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @SuppressWarnings("unused") | ||||||||||||||||
| public static class ShouldSuppressAdvice { | ||||||||||||||||
|
|
||||||||||||||||
| @Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class) | ||||||||||||||||
| public static boolean methodEnter() { | ||||||||||||||||
| return true; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||||||||||||||||
| public static void methodEnter( | ||||||||||||||||
| @Advice.Argument(0) Context context, @Advice.Return(readOnly = false) boolean result) { | ||||||||||||||||
| result = | ||||||||||||||||
| InstrumentationUtil.shouldSuppressInstrumentation( | ||||||||||||||||
| AgentContextStorage.getAgentContext(context)); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @SuppressWarnings("unused") | ||||||||||||||||
| public static class SuppressAdvice { | ||||||||||||||||
|
|
||||||||||||||||
| @Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class) | ||||||||||||||||
| public static boolean methodEnter(@Advice.Argument(0) Runnable runnable) { | ||||||||||||||||
| InstrumentationUtil.suppressInstrumentation(runnable); | ||||||||||||||||
| return true; | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+60
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the return value needed here?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is needed |
||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
This file contains hidden or 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
9 changes: 9 additions & 0 deletions
9
instrumentation/opentelemetry-api/opentelemetry-api-1.0/testing/build.gradle.kts
This file contains hidden or 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,9 @@ | ||
| plugins { | ||
| id("otel.javaagent-testing") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(project(":opentelemetry-api-shaded-for-instrumenting", configuration = "shadow")) | ||
| implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")) | ||
| testInstrumentation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")) | ||
| } |
42 changes: 42 additions & 0 deletions
42
...java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/TestInstrumentation.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opentelemetryapi; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
|
||
| import application.io.opentelemetry.context.Context; | ||
| import io.opentelemetry.api.internal.InstrumentationUtil; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| public class TestInstrumentation implements TypeInstrumentation { | ||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return named("io.opentelemetry.javaagent.instrumentation.opentelemetryapi.TestClass"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| named("shouldSuppressInstrumentation"), this.getClass().getName() + "$TestAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class TestAdvice { | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Argument(0) Context context, @Advice.Return(readOnly = false) boolean result) { | ||
| result = | ||
| InstrumentationUtil.shouldSuppressInstrumentation( | ||
| AgentContextStorage.getAgentContext(context)); | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...o/opentelemetry/javaagent/instrumentation/opentelemetryapi/TestInstrumentationModule.java
This file contains hidden or 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,32 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opentelemetryapi; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; | ||
| import java.util.List; | ||
|
|
||
| @AutoService(InstrumentationModule.class) | ||
| public class TestInstrumentationModule extends InstrumentationModule | ||
| implements ExperimentalInstrumentationModule { | ||
| public TestInstrumentationModule() { | ||
| super("test"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getModuleGroup() { | ||
| return "opentelemetry-api-bridge"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<TypeInstrumentation> typeInstrumentations() { | ||
| return singletonList(new TestInstrumentation()); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
.../io/opentelemetry/javaagent/instrumentation/opentelemetryapi/InstrumentationUtilTest.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opentelemetryapi; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.api.internal.InstrumentationUtil; | ||
| import io.opentelemetry.context.Context; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class InstrumentationUtilTest { | ||
laurit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void instrumentationSuppression() { | ||
| Context[] contexts = new Context[1]; | ||
| InstrumentationUtil.suppressInstrumentation(() -> contexts[0] = Context.current()); | ||
|
|
||
| assertThat(InstrumentationUtil.shouldSuppressInstrumentation(contexts[0])).isTrue(); | ||
| assertThat(TestClass.shouldSuppressInstrumentation(contexts[0])).isTrue(); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
.../src/test/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/TestClass.java
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opentelemetryapi; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
|
|
||
| class TestClass { | ||
| static boolean shouldSuppressInstrumentation(Context context) { | ||
| // this method is instrumented to call | ||
| // InstrumentationUtil.shouldSuppressInstrumentation(context) to simulate agent code calling | ||
| // that method | ||
| return false; | ||
| } | ||
|
|
||
| private TestClass() {} | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.