|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.pulsar.v1_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.instrumentation.spring.pulsar.v1_0.SpringPulsarSingletons.instrumenter; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 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 io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.VirtualFieldStore; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import net.bytebuddy.description.type.TypeDescription; |
| 20 | +import net.bytebuddy.matcher.ElementMatcher; |
| 21 | +import org.apache.pulsar.client.api.Message; |
| 22 | + |
| 23 | +public class DefaultPulsarMessageListenerContainerInstrumentation implements TypeInstrumentation { |
| 24 | + @Override |
| 25 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 26 | + return named( |
| 27 | + "org.springframework.pulsar.listener.DefaultPulsarMessageListenerContainer$Listener"); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void transform(TypeTransformer transformer) { |
| 32 | + transformer.applyAdviceToMethod( |
| 33 | + named("dispatchMessageToListener") |
| 34 | + .and(takesArguments(3).or(takesArguments(2))) |
| 35 | + .and(takesArgument(0, named("org.apache.pulsar.client.api.Message"))), |
| 36 | + getClass().getName() + "$DispatchMessageToListenerAdvice"); |
| 37 | + } |
| 38 | + |
| 39 | + @SuppressWarnings("unused") |
| 40 | + public static class DispatchMessageToListenerAdvice { |
| 41 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 42 | + public static void onEnter( |
| 43 | + @Advice.Argument(0) Message<?> message, |
| 44 | + @Advice.Local("otelContext") Context context, |
| 45 | + @Advice.Local("otelScope") Scope scope) { |
| 46 | + Context parentContext = VirtualFieldStore.extract(message); |
| 47 | + if (instrumenter().shouldStart(parentContext, message)) { |
| 48 | + context = instrumenter().start(parentContext, message); |
| 49 | + scope = context.makeCurrent(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
| 54 | + public static void onExit( |
| 55 | + @Advice.Argument(0) Message<?> message, |
| 56 | + @Advice.Local("otelContext") Context context, |
| 57 | + @Advice.Local("otelScope") Scope scope, |
| 58 | + @Advice.Thrown Throwable throwable) { |
| 59 | + if (scope == null) { |
| 60 | + return; |
| 61 | + } |
| 62 | + scope.close(); |
| 63 | + instrumenter().end(context, message, null, throwable); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments