|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pulsar.v28; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.instrumentation.pulsar.v28.telemetry.PulsarSingletons.startAndEndConsumerReceive; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isProtected; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; |
| 15 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 16 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 17 | + |
| 18 | +import io.opentelemetry.context.Context; |
| 19 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 20 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import net.bytebuddy.asm.Advice; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.matcher.ElementMatcher; |
| 25 | +import org.apache.pulsar.client.api.Consumer; |
| 26 | +import org.apache.pulsar.client.api.Message; |
| 27 | +import org.apache.pulsar.client.api.PulsarClient; |
| 28 | +import org.apache.pulsar.client.impl.PulsarClientImpl; |
| 29 | + |
| 30 | +public class ConsumerImplInstrumentation implements TypeInstrumentation { |
| 31 | + |
| 32 | + @Override |
| 33 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 34 | + return namedOneOf( |
| 35 | + "org.apache.pulsar.client.impl.ConsumerImpl", |
| 36 | + "org.apache.pulsar.client.impl.MultiTopicsConsumerImpl"); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void transform(TypeTransformer transformer) { |
| 41 | + String klassName = ConsumerImplInstrumentation.class.getName(); |
| 42 | + |
| 43 | + transformer.applyAdviceToMethod(isConstructor(), klassName + "$ConsumerConstructorAdviser"); |
| 44 | + |
| 45 | + // internalReceive will apply to Consumer#receive(long,TimeUnit) |
| 46 | + // and called before MessageListener#receive. |
| 47 | + transformer.applyAdviceToMethod( |
| 48 | + isMethod() |
| 49 | + .and(isProtected()) |
| 50 | + .and(named("internalReceive")) |
| 51 | + .and(takesArguments(2)) |
| 52 | + .and(takesArgument(1, named("java.util.concurrent.TimeUnit"))), |
| 53 | + klassName + "$ConsumerInternalReceiveAdviser"); |
| 54 | + // receive/batchReceive will apply to Consumer#receive()/Consumer#batchReceive() |
| 55 | + transformer.applyAdviceToMethod( |
| 56 | + isMethod() |
| 57 | + .and(isPublic()) |
| 58 | + .and(namedOneOf("receive", "batchReceive")) |
| 59 | + .and(takesArguments(0)), |
| 60 | + klassName + "$ConsumerSyncReceiveAdviser"); |
| 61 | + // receiveAsync/batchReceiveAsync will apply to |
| 62 | + // Consumer#receiveAsync()/Consumer#batchReceiveAsync() |
| 63 | + transformer.applyAdviceToMethod( |
| 64 | + isMethod() |
| 65 | + .and(isPublic()) |
| 66 | + .and(namedOneOf("receiveAsync", "batchReceiveAsync")) |
| 67 | + .and(takesArguments(0)), |
| 68 | + klassName + "$ConsumerAsyncReceiveAdviser"); |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("unused") |
| 72 | + public static class ConsumerConstructorAdviser { |
| 73 | + private ConsumerConstructorAdviser() {} |
| 74 | + |
| 75 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 76 | + public static void after( |
| 77 | + @Advice.This Consumer<?> consumer, @Advice.Argument(value = 0) PulsarClient client) { |
| 78 | + |
| 79 | + PulsarClientImpl pulsarClient = (PulsarClientImpl) client; |
| 80 | + String url = pulsarClient.getLookup().getServiceUrl(); |
| 81 | + VirtualFieldStore.inject(consumer, url); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("unused") |
| 86 | + public static class ConsumerInternalReceiveAdviser { |
| 87 | + private ConsumerInternalReceiveAdviser() {} |
| 88 | + |
| 89 | + @Advice.OnMethodEnter |
| 90 | + public static void before(@Advice.Local(value = "startTime") long startTime) { |
| 91 | + startTime = System.currentTimeMillis(); |
| 92 | + } |
| 93 | + |
| 94 | + @Advice.OnMethodExit(onThrowable = Throwable.class) |
| 95 | + public static void after( |
| 96 | + @Advice.This Consumer<?> consumer, |
| 97 | + @Advice.Return Message<?> message, |
| 98 | + @Advice.Thrown Throwable t, |
| 99 | + @Advice.Local(value = "startTime") long startTime) { |
| 100 | + if (t != null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + Context parent = Context.current(); |
| 105 | + Context current = startAndEndConsumerReceive(parent, message, startTime, consumer); |
| 106 | + if (current != null) { |
| 107 | + // ConsumerBase#internalReceive(long,TimeUnit) will be called before |
| 108 | + // ConsumerListener#receive(Consumer,Message), so, need to inject Context into Message. |
| 109 | + VirtualFieldStore.inject(message, current); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings("unused") |
| 115 | + public static class ConsumerSyncReceiveAdviser { |
| 116 | + private ConsumerSyncReceiveAdviser() {} |
| 117 | + |
| 118 | + @Advice.OnMethodEnter |
| 119 | + public static void before(@Advice.Local(value = "startTime") long startTime) { |
| 120 | + startTime = System.currentTimeMillis(); |
| 121 | + } |
| 122 | + |
| 123 | + @Advice.OnMethodExit(onThrowable = Throwable.class) |
| 124 | + public static void after( |
| 125 | + @Advice.This Consumer<?> consumer, |
| 126 | + @Advice.Return Message<?> message, |
| 127 | + @Advice.Thrown Throwable t, |
| 128 | + @Advice.Local(value = "startTime") long startTime) { |
| 129 | + if (t != null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + Context parent = Context.current(); |
| 134 | + startAndEndConsumerReceive(parent, message, startTime, consumer); |
| 135 | + // No need to inject context to message. |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings("unused") |
| 140 | + public static class ConsumerAsyncReceiveAdviser { |
| 141 | + private ConsumerAsyncReceiveAdviser() {} |
| 142 | + |
| 143 | + @Advice.OnMethodEnter |
| 144 | + public static void before(@Advice.Local(value = "startTime") long startTime) { |
| 145 | + startTime = System.currentTimeMillis(); |
| 146 | + } |
| 147 | + |
| 148 | + @Advice.OnMethodExit(onThrowable = Throwable.class) |
| 149 | + public static void after( |
| 150 | + @Advice.This Consumer<?> consumer, |
| 151 | + @Advice.Return CompletableFuture<Message<?>> future, |
| 152 | + @Advice.Local(value = "startTime") long startTime) { |
| 153 | + future.whenComplete( |
| 154 | + (message, t) -> { |
| 155 | + if (t != null) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + Context parent = Context.current(); |
| 160 | + startAndEndConsumerReceive(parent, message, startTime, consumer); |
| 161 | + // No need to inject context to message. |
| 162 | + }); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments