-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move TypeInstrumention into standalone class
- Loading branch information
Showing
2 changed files
with
87 additions
and
72 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...etry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2; | ||
|
||
import static io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyClientWrapUtil.wrapResponseListeners; | ||
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext; | ||
import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2.JettyHttpClientSingletons.instrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyHttpClient9TracingInterceptor; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.List; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.eclipse.jetty.client.HttpRequest; | ||
import org.eclipse.jetty.client.api.Response; | ||
|
||
public class JettyHttpClient9Instrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.eclipse.jetty.client.HttpClient"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("send")) | ||
.and(takesArgument(0, named("org.eclipse.jetty.client.HttpRequest"))) | ||
.and(takesArgument(1, List.class)), | ||
JettyHttpClient9Instrumentation.class.getName() + "$JettyHttpClient9Advice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class JettyHttpClient9Advice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void addTracingEnter( | ||
@Advice.Argument(value = 0) HttpRequest httpRequest, | ||
@Advice.Argument(value = 1, readOnly = false) List<Response.ResponseListener> listeners, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
Context parentContext = currentContext(); | ||
if (!instrumenter().shouldStart(parentContext, httpRequest)) { | ||
return; | ||
} | ||
|
||
// First step is to attach the tracer to the Jetty request. Request listeners are wrapped here | ||
JettyHttpClient9TracingInterceptor requestInterceptor = | ||
new JettyHttpClient9TracingInterceptor(parentContext, instrumenter()); | ||
requestInterceptor.attachToRequest(httpRequest); | ||
|
||
// Second step is to wrap all the important result callback | ||
listeners = wrapResponseListeners(parentContext, listeners); | ||
|
||
context = requestInterceptor.getContext(); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void exitTracingInterceptor( | ||
@Advice.Argument(value = 0) HttpRequest httpRequest, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (scope == null) { | ||
return; | ||
} | ||
|
||
// not ending span here unless error, span ended in the interceptor | ||
scope.close(); | ||
if (throwable != null) { | ||
instrumenter().end(context, httpRequest, null, throwable); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters