Skip to content

Commit 350d05f

Browse files
committed
Instrument instrumentation suppression api
1 parent 94a8ffa commit 350d05f

File tree

8 files changed

+187
-1
lines changed

8 files changed

+187
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.named;
9+
import static net.bytebuddy.matcher.ElementMatchers.returns;
10+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
11+
12+
import application.io.opentelemetry.context.Context;
13+
import io.opentelemetry.api.internal.InstrumentationUtil;
14+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
16+
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
17+
import net.bytebuddy.asm.Advice;
18+
import net.bytebuddy.description.type.TypeDescription;
19+
import net.bytebuddy.matcher.ElementMatcher;
20+
21+
public class InstrumentationUtilInstrumentation implements TypeInstrumentation {
22+
@Override
23+
public ElementMatcher<TypeDescription> typeMatcher() {
24+
return named("application.io.opentelemetry.api.internal.InstrumentationUtil");
25+
}
26+
27+
@Override
28+
public void transform(TypeTransformer transformer) {
29+
transformer.applyAdviceToMethod(
30+
named("shouldSuppressInstrumentation")
31+
.and(takesArgument(0, named("application.io.opentelemetry.context.Context")))
32+
.and(returns(boolean.class)),
33+
this.getClass().getName() + "$ShouldSuppressAdvice");
34+
transformer.applyAdviceToMethod(
35+
named("suppressInstrumentation").and(takesArgument(0, Runnable.class)),
36+
this.getClass().getName() + "$SuppressAdvice");
37+
}
38+
39+
@SuppressWarnings("unused")
40+
public static class ShouldSuppressAdvice {
41+
42+
@Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class)
43+
public static boolean methodEnter() {
44+
return true;
45+
}
46+
47+
@Advice.OnMethodExit(suppress = Throwable.class)
48+
public static void methodEnter(
49+
@Advice.Argument(0) Context context, @Advice.Return(readOnly = false) boolean result) {
50+
result =
51+
InstrumentationUtil.shouldSuppressInstrumentation(
52+
AgentContextStorage.getAgentContext(context));
53+
}
54+
}
55+
56+
@SuppressWarnings("unused")
57+
public static class SuppressAdvice {
58+
59+
@Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class)
60+
public static boolean methodEnter(@Advice.Argument(0) Runnable runnable) {
61+
InstrumentationUtil.suppressInstrumentation(runnable);
62+
return true;
63+
}
64+
}
65+
}

instrumentation/opentelemetry-api/opentelemetry-api-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/OpenTelemetryApiInstrumentationModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public List<TypeInstrumentation> typeInstrumentations() {
2727
new ContextInstrumentation(),
2828
new ContextStorageWrappersInstrumentation(),
2929
new OpenTelemetryInstrumentation(),
30-
new SpanInstrumentation());
30+
new SpanInstrumentation(),
31+
new InstrumentationUtilInstrumentation());
3132
}
3233

3334
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
id("otel.javaagent-testing")
3+
}
4+
5+
dependencies {
6+
compileOnly(project(":opentelemetry-api-shaded-for-instrumenting", configuration = "shadow"))
7+
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent"))
8+
testInstrumentation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent"))
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.named;
9+
10+
import application.io.opentelemetry.context.Context;
11+
import io.opentelemetry.api.internal.InstrumentationUtil;
12+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
14+
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
15+
import net.bytebuddy.asm.Advice;
16+
import net.bytebuddy.description.type.TypeDescription;
17+
import net.bytebuddy.matcher.ElementMatcher;
18+
19+
public class TestInstrumentation implements TypeInstrumentation {
20+
@Override
21+
public ElementMatcher<TypeDescription> typeMatcher() {
22+
return named("io.opentelemetry.javaagent.instrumentation.opentelemetryapi.TestClass");
23+
}
24+
25+
@Override
26+
public void transform(TypeTransformer transformer) {
27+
transformer.applyAdviceToMethod(
28+
named("shouldSuppressInstrumentation"), this.getClass().getName() + "$TestAdvice");
29+
}
30+
31+
@SuppressWarnings("unused")
32+
public static class TestAdvice {
33+
34+
@Advice.OnMethodExit(suppress = Throwable.class)
35+
public static void onExit(
36+
@Advice.Argument(0) Context context, @Advice.Return(readOnly = false) boolean result) {
37+
result =
38+
InstrumentationUtil.shouldSuppressInstrumentation(
39+
AgentContextStorage.getAgentContext(context));
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
7+
8+
import static java.util.Collections.singletonList;
9+
10+
import com.google.auto.service.AutoService;
11+
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
12+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import java.util.List;
14+
15+
@AutoService(InstrumentationModule.class)
16+
public class TestInstrumentationModule extends InstrumentationModule {
17+
public TestInstrumentationModule() {
18+
super("test");
19+
}
20+
21+
@Override
22+
public List<TypeInstrumentation> typeInstrumentations() {
23+
return singletonList(new TestInstrumentation());
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.api.internal.InstrumentationUtil;
11+
import io.opentelemetry.context.Context;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class InstrumentationUtilTest {
15+
16+
@Test
17+
void instrumentationSuppression() {
18+
Context[] contexts = new Context[1];
19+
InstrumentationUtil.suppressInstrumentation(() -> contexts[0] = Context.current());
20+
21+
assertThat(InstrumentationUtil.shouldSuppressInstrumentation(contexts[0])).isTrue();
22+
assertThat(TestClass.shouldSuppressInstrumentation(contexts[0])).isTrue();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi;
7+
8+
import io.opentelemetry.context.Context;
9+
10+
class TestClass {
11+
static boolean shouldSuppressInstrumentation(Context context) {
12+
// this method is instrumented to call
13+
// InstrumentationUtil.shouldSuppressInstrumentation(context) to simulate agent code calling
14+
// that method
15+
return false;
16+
}
17+
18+
private TestClass() {}
19+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ include(":instrumentation:opensearch:opensearch-rest-3.0:javaagent")
434434
include(":instrumentation:opensearch:opensearch-rest-common:javaagent")
435435
include(":instrumentation:opensearch:opensearch-rest-common:testing")
436436
include(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")
437+
include(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:testing")
437438
include(":instrumentation:opentelemetry-api:opentelemetry-api-1.4:javaagent")
438439
include(":instrumentation:opentelemetry-api:opentelemetry-api-1.10:javaagent")
439440
include(":instrumentation:opentelemetry-api:opentelemetry-api-1.15:javaagent")

0 commit comments

Comments
 (0)