|
| 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 | +} |
0 commit comments