|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0.JettyHttpClientSingletons.JETTY_CLIENT_CONTEXT_KEY; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0.JettyHttpClientSingletons.instrumenter; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.nameContains; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 14 | + |
| 15 | +import io.opentelemetry.context.Context; |
| 16 | +import io.opentelemetry.context.Scope; |
| 17 | +import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyClientTracingListener; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 19 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | +import net.bytebuddy.description.type.TypeDescription; |
| 22 | +import net.bytebuddy.matcher.ElementMatcher; |
| 23 | +import org.eclipse.jetty.client.transport.HttpRequest; |
| 24 | + |
| 25 | +public class JettyHttpClient12Instrumentation implements TypeInstrumentation { |
| 26 | + |
| 27 | + @Override |
| 28 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 29 | + return named("org.eclipse.jetty.client.transport.HttpRequest"); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void transform(TypeTransformer transformer) { |
| 34 | + transformer.applyAdviceToMethod( |
| 35 | + isMethod() |
| 36 | + .and(named("send")) |
| 37 | + .and(takesArgument(0, named("org.eclipse.jetty.client.Response$CompleteListener"))), |
| 38 | + JettyHttpClient12Instrumentation.class.getName() + "$JettyHttpClient12SendAdvice"); |
| 39 | + // For request listeners |
| 40 | + transformer.applyAdviceToMethod( |
| 41 | + isMethod().and(nameContains("notify")), |
| 42 | + JettyHttpClient12Instrumentation.class.getName() + "$JettyHttpClient12NotifyAdvice"); |
| 43 | + } |
| 44 | + |
| 45 | + @SuppressWarnings("unused") |
| 46 | + public static class JettyHttpClient12SendAdvice { |
| 47 | + |
| 48 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 49 | + public static void onEnterSend( |
| 50 | + @Advice.This HttpRequest request, |
| 51 | + @Advice.Local("otelContext") Context context, |
| 52 | + @Advice.Local("otelScope") Scope scope) { |
| 53 | + // start span |
| 54 | + Context parentContext = Context.current(); |
| 55 | + context = JettyClientTracingListener.handleRequest(parentContext, request, instrumenter()); |
| 56 | + if (context == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + // set context for responseListeners |
| 60 | + request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext); |
| 61 | + |
| 62 | + scope = context.makeCurrent(); |
| 63 | + } |
| 64 | + |
| 65 | + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
| 66 | + public static void onExitSend( |
| 67 | + @Advice.This HttpRequest request, |
| 68 | + @Advice.Thrown Throwable throwable, |
| 69 | + @Advice.Local("otelContext") Context context, |
| 70 | + @Advice.Local("otelScope") Scope scope) { |
| 71 | + if (scope == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // not ending span here unless error, span ended in the interceptor |
| 76 | + scope.close(); |
| 77 | + if (throwable != null) { |
| 78 | + instrumenter().end(context, request, null, throwable); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings("unused") |
| 84 | + public static class JettyHttpClient12NotifyAdvice { |
| 85 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 86 | + public static void onEnterNotify( |
| 87 | + @Advice.This HttpRequest request, |
| 88 | + @Advice.Local("otelContext") Context context, |
| 89 | + @Advice.Local("otelScope") Scope scope) { |
| 90 | + context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY); |
| 91 | + if (context == null) { |
| 92 | + return; |
| 93 | + } |
| 94 | + scope = context.makeCurrent(); |
| 95 | + } |
| 96 | + |
| 97 | + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
| 98 | + public static void onExitNotify( |
| 99 | + @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { |
| 100 | + if (scope == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + scope.close(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments