|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_57; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.isStatic; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 13 | + |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 16 | +import net.bytebuddy.asm.Advice; |
| 17 | +import net.bytebuddy.asm.Advice.AssignReturned; |
| 18 | +import net.bytebuddy.description.type.TypeDescription; |
| 19 | +import net.bytebuddy.matcher.ElementMatcher; |
| 20 | + |
| 21 | +public class OpenTelemetryInstrumentation implements TypeInstrumentation { |
| 22 | + |
| 23 | + @Override |
| 24 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 25 | + return named("application.io.opentelemetry.api.GlobalOpenTelemetry"); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void transform(TypeTransformer transformer) { |
| 30 | + transformer.applyAdviceToMethod( |
| 31 | + isMethod() |
| 32 | + .and(isStatic()) |
| 33 | + .and(named("isSet")) |
| 34 | + .and(takesArguments(0)) |
| 35 | + .and(returns(boolean.class)), |
| 36 | + OpenTelemetryInstrumentation.class.getName() + "$IsSetAdvice"); |
| 37 | + transformer.applyAdviceToMethod( |
| 38 | + isMethod() |
| 39 | + .and(isStatic()) |
| 40 | + .and(named("getOrNoop")) |
| 41 | + .and(takesArguments(0)) |
| 42 | + .and(returns(named("application.io.opentelemetry.api.OpenTelemetry"))), |
| 43 | + OpenTelemetryInstrumentation.class.getName() + "$GetOrNoopAdvice"); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("unused") |
| 47 | + public static class IsSetAdvice { |
| 48 | + @AssignReturned.ToReturned |
| 49 | + @Advice.OnMethodExit |
| 50 | + public static boolean methodExit() { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unused") |
| 56 | + public static class GetOrNoopAdvice { |
| 57 | + @AssignReturned.ToReturned |
| 58 | + @Advice.OnMethodExit |
| 59 | + public static application.io.opentelemetry.api.OpenTelemetry methodExit() { |
| 60 | + return application.io.opentelemetry.api.GlobalOpenTelemetry.get(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments