|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.nats.v2_17; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.nats.v2_17.NatsSingletons.PRODUCER_INSTRUMENTER; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 14 | + |
| 15 | +import io.nats.client.Connection; |
| 16 | +import io.nats.client.Message; |
| 17 | +import io.nats.client.impl.Headers; |
| 18 | +import io.opentelemetry.context.Context; |
| 19 | +import io.opentelemetry.context.Scope; |
| 20 | +import io.opentelemetry.instrumentation.nats.v2_17.internal.NatsMessageWritableHeaders; |
| 21 | +import io.opentelemetry.instrumentation.nats.v2_17.internal.NatsRequest; |
| 22 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 23 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 24 | +import net.bytebuddy.asm.Advice; |
| 25 | +import net.bytebuddy.description.type.TypeDescription; |
| 26 | +import net.bytebuddy.matcher.ElementMatcher; |
| 27 | + |
| 28 | +public class ConnectionPublishInstrumentation implements TypeInstrumentation { |
| 29 | + |
| 30 | + @Override |
| 31 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 32 | + return implementsInterface(named("io.nats.client.Connection")); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void transform(TypeTransformer transformer) { |
| 37 | + transformer.applyAdviceToMethod( |
| 38 | + isPublic() |
| 39 | + .and(named("publish")) |
| 40 | + .and(takesArguments(2)) |
| 41 | + .and(takesArgument(0, String.class)) |
| 42 | + .and(takesArgument(1, byte[].class)), |
| 43 | + ConnectionPublishInstrumentation.class.getName() + "$PublishBodyAdvice"); |
| 44 | + transformer.applyAdviceToMethod( |
| 45 | + isPublic() |
| 46 | + .and(named("publish")) |
| 47 | + .and(takesArguments(3)) |
| 48 | + .and(takesArgument(0, String.class)) |
| 49 | + .and(takesArgument(1, named("io.nats.client.impl.Headers"))) |
| 50 | + .and(takesArgument(2, byte[].class)), |
| 51 | + ConnectionPublishInstrumentation.class.getName() + "$PublishHeadersBodyAdvice"); |
| 52 | + transformer.applyAdviceToMethod( |
| 53 | + isPublic() |
| 54 | + .and(named("publish")) |
| 55 | + .and(takesArguments(3)) |
| 56 | + .and(takesArgument(0, String.class)) |
| 57 | + .and(takesArgument(1, String.class)) |
| 58 | + .and(takesArgument(2, byte[].class)), |
| 59 | + ConnectionPublishInstrumentation.class.getName() + "$PublishReplyToBodyAdvice"); |
| 60 | + transformer.applyAdviceToMethod( |
| 61 | + isPublic() |
| 62 | + .and(named("publish")) |
| 63 | + .and(takesArguments(4)) |
| 64 | + .and(takesArgument(0, String.class)) |
| 65 | + .and(takesArgument(1, String.class)) |
| 66 | + .and(takesArgument(2, named("io.nats.client.impl.Headers"))) |
| 67 | + .and(takesArgument(3, byte[].class)), |
| 68 | + ConnectionPublishInstrumentation.class.getName() + "$PublishReplyToHeadersBodyAdvice"); |
| 69 | + transformer.applyAdviceToMethod( |
| 70 | + isPublic() |
| 71 | + .and(named("publish")) |
| 72 | + .and(takesArguments(1)) |
| 73 | + .and(takesArgument(0, named("io.nats.client.Message"))), |
| 74 | + ConnectionPublishInstrumentation.class.getName() + "$PublishMessageAdvice"); |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unused") |
| 78 | + public static class PublishBodyAdvice { |
| 79 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) |
| 80 | + public static boolean onEnter( |
| 81 | + @Advice.This Connection connection, |
| 82 | + @Advice.Argument(0) String subject, |
| 83 | + @Advice.Argument(1) byte[] body) { |
| 84 | + // call the instrumented publish method |
| 85 | + connection.publish(subject, null, null, body); |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @SuppressWarnings("unused") |
| 91 | + public static class PublishHeadersBodyAdvice { |
| 92 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) |
| 93 | + public static boolean onEnter( |
| 94 | + @Advice.This Connection connection, |
| 95 | + @Advice.Argument(0) String subject, |
| 96 | + @Advice.Argument(1) Headers headers, |
| 97 | + @Advice.Argument(2) byte[] body) { |
| 98 | + // call the instrumented publish method |
| 99 | + connection.publish(subject, null, headers, body); |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @SuppressWarnings("unused") |
| 105 | + public static class PublishReplyToBodyAdvice { |
| 106 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) |
| 107 | + public static boolean onEnter( |
| 108 | + @Advice.This Connection connection, |
| 109 | + @Advice.Argument(0) String subject, |
| 110 | + @Advice.Argument(1) String replyTo, |
| 111 | + @Advice.Argument(2) byte[] body) { |
| 112 | + // call the instrumented publish method |
| 113 | + connection.publish(subject, replyTo, null, body); |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @SuppressWarnings("unused") |
| 119 | + public static class PublishReplyToHeadersBodyAdvice { |
| 120 | + |
| 121 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 122 | + public static void onEnter( |
| 123 | + @Advice.This Connection connection, |
| 124 | + @Advice.Argument(0) String subject, |
| 125 | + @Advice.Argument(1) String replyTo, |
| 126 | + @Advice.Argument(value = 2, readOnly = false) Headers headers, |
| 127 | + @Advice.Argument(3) byte[] body, |
| 128 | + @Advice.Local("otelContext") Context otelContext, |
| 129 | + @Advice.Local("otelScope") Scope otelScope, |
| 130 | + @Advice.Local("natsRequest") NatsRequest natsRequest) { |
| 131 | + headers = NatsMessageWritableHeaders.create(headers); |
| 132 | + |
| 133 | + Context parentContext = Context.current(); |
| 134 | + natsRequest = NatsRequest.create(connection, subject, replyTo, headers, body); |
| 135 | + |
| 136 | + if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); |
| 141 | + otelScope = otelContext.makeCurrent(); |
| 142 | + } |
| 143 | + |
| 144 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 145 | + public static void onExit( |
| 146 | + @Advice.Thrown Throwable throwable, |
| 147 | + @Advice.Local("otelContext") Context otelContext, |
| 148 | + @Advice.Local("otelScope") Scope otelScope, |
| 149 | + @Advice.Local("natsRequest") NatsRequest natsRequest) { |
| 150 | + if (otelScope == null) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + otelScope.close(); |
| 155 | + PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @SuppressWarnings("unused") |
| 160 | + public static class PublishMessageAdvice { |
| 161 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) |
| 162 | + public static boolean onEnter( |
| 163 | + @Advice.This Connection connection, @Advice.Argument(0) Message message) { |
| 164 | + if (message == null) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + // call the instrumented publish method |
| 169 | + connection.publish( |
| 170 | + message.getSubject(), message.getReplyTo(), message.getHeaders(), message.getData()); |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments