|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0.ApolloConfigSingletons.REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY; |
| 10 | +import static io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0.ApolloConfigSingletons.instrumenter; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | + |
| 13 | +import io.opentelemetry.context.Context; |
| 14 | +import io.opentelemetry.context.Scope; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 17 | +import net.bytebuddy.asm.Advice; |
| 18 | +import net.bytebuddy.description.type.TypeDescription; |
| 19 | +import net.bytebuddy.matcher.ElementMatcher; |
| 20 | + |
| 21 | +public class ApolloRepositoryChangeInstrumentation implements TypeInstrumentation { |
| 22 | + |
| 23 | + @Override |
| 24 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 25 | + return named("com.ctrip.framework.apollo.internals.AbstractConfigRepository"); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void transform(TypeTransformer transformer) { |
| 30 | + String adviceName = this.getClass().getName() + "$ApolloRepositoryChangeAdvice"; |
| 31 | + transformer.applyAdviceToMethod(named("fireRepositoryChange"), adviceName); |
| 32 | + } |
| 33 | + |
| 34 | + @SuppressWarnings("unused") |
| 35 | + public static class ApolloRepositoryChangeAdvice { |
| 36 | + |
| 37 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 38 | + public static void onEnter( |
| 39 | + @Advice.Argument(value = 0) String namespace, |
| 40 | + @Advice.Local("otelContext") Context context, |
| 41 | + @Advice.Local("otelScope") Scope scope) { |
| 42 | + Context parentContext = currentContext(); |
| 43 | + String repeat = parentContext.get(REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY); |
| 44 | + if (repeat != null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + if (!instrumenter().shouldStart(parentContext, namespace)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + context = instrumenter().start(parentContext, namespace); |
| 52 | + context = context.with(REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY, "1"); |
| 53 | + scope = context.makeCurrent(); |
| 54 | + } |
| 55 | + |
| 56 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 57 | + public static void onExit( |
| 58 | + @Advice.Argument(value = 0) String namespace, |
| 59 | + @Advice.Thrown Throwable throwable, |
| 60 | + @Advice.Local("otelContext") Context context, |
| 61 | + @Advice.Local("otelScope") Scope scope) { |
| 62 | + if (scope == null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + scope.close(); |
| 66 | + instrumenter().end(context, namespace, null, throwable); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments