|
21 | 21 | import io.opentelemetry.instrumentation.nats.v2_17.internal.NatsRequest; |
22 | 22 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
23 | 23 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 24 | +import javax.annotation.Nullable; |
24 | 25 | import net.bytebuddy.asm.Advice; |
| 26 | +import net.bytebuddy.asm.Advice.AssignReturned; |
| 27 | +import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; |
25 | 28 | import net.bytebuddy.description.type.TypeDescription; |
26 | 29 | import net.bytebuddy.matcher.ElementMatcher; |
27 | 30 |
|
@@ -118,41 +121,54 @@ public static boolean onEnter( |
118 | 121 | @SuppressWarnings("unused") |
119 | 122 | public static class PublishReplyToHeadersBodyAdvice { |
120 | 123 |
|
| 124 | + public static class AdviceScope { |
| 125 | + private final NatsRequest request; |
| 126 | + private final Context context; |
| 127 | + private final Scope scope; |
| 128 | + |
| 129 | + private AdviceScope(NatsRequest request, Context context, Scope scope) { |
| 130 | + this.request = request; |
| 131 | + this.context = context; |
| 132 | + this.scope = scope; |
| 133 | + } |
| 134 | + |
| 135 | + @Nullable |
| 136 | + public static AdviceScope start(NatsRequest natsRequest) { |
| 137 | + Context parentContext = Context.current(); |
| 138 | + if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + Context context = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); |
| 142 | + return new AdviceScope(natsRequest, context, context.makeCurrent()); |
| 143 | + } |
| 144 | + |
| 145 | + public void end(@Nullable Throwable throwable) { |
| 146 | + scope.close(); |
| 147 | + PRODUCER_INSTRUMENTER.end(context, request, null, throwable); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @AssignReturned.ToArguments(@ToArgument(value = 2, index = 1)) |
121 | 152 | @Advice.OnMethodEnter(suppress = Throwable.class) |
122 | | - public static void onEnter( |
| 153 | + public static Object[] onEnter( |
123 | 154 | @Advice.This Connection connection, |
124 | 155 | @Advice.Argument(0) String subject, |
125 | 156 | @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(); |
| 157 | + @Advice.Argument(2) Headers originalHeaders, |
| 158 | + @Advice.Argument(3) byte[] body) { |
| 159 | + Headers headers = NatsMessageWritableHeaders.create(originalHeaders); |
| 160 | + NatsRequest natsRequest = NatsRequest.create(connection, subject, replyTo, headers, body); |
| 161 | + AdviceScope adviceScope = AdviceScope.start(natsRequest); |
| 162 | + return new Object[] {adviceScope, headers}; |
142 | 163 | } |
143 | 164 |
|
144 | 165 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
145 | 166 | 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; |
| 167 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) { |
| 168 | + AdviceScope adviceScope = (AdviceScope) enterResult[0]; |
| 169 | + if (adviceScope != null) { |
| 170 | + adviceScope.end(throwable); |
152 | 171 | } |
153 | | - |
154 | | - otelScope.close(); |
155 | | - PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); |
156 | 172 | } |
157 | 173 | } |
158 | 174 |
|
|
0 commit comments