Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support jettyclient 12 #11519

Merged
merged 21 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ These are the supported libraries and frameworks:
| [Dropwizard Views](https://www.dropwizard.io/en/latest/manual/views.html) | 0.7+ | N/A | Controller Spans [3] |
| [Eclipse Grizzly](https://javaee.github.io/grizzly/httpserverframework.html) | 2.3+ | N/A | [HTTP Server Spans], [HTTP Server Metrics] |
| [Eclipse Jersey](https://eclipse-ee4j.github.io/jersey/) | 2.0+ | N/A | Provides `http.route` [2], Controller Spans [3] |
| [Eclipse Jetty HTTP Client](https://www.eclipse.org/jetty/javadoc/jetty-9/org/eclipse/jetty/client/HttpClient.html) | 9.2+ (not including 10+ yet) | [opentelemetry-jetty-httpclient-9.2](../instrumentation/jetty-httpclient/jetty-httpclient-9.2/library) | [HTTP Client Spans], [HTTP Client Metrics] |
| [Eclipse Jetty HTTP Client](https://www.eclipse.org/jetty/javadoc/jetty-9/org/eclipse/jetty/client/HttpClient.html) | 9.2 - 10.0,<br>12.0+ | [opentelemetry-jetty-httpclient-9.2](../instrumentation/jetty-httpclient/jetty-httpclient-9.2/library) | [HTTP Client Spans], [HTTP Client Metrics] |
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
| [Eclipse Metro](https://projects.eclipse.org/projects/ee4j.metro) | 2.2+ | N/A | Provides `http.route` [2], Controller Spans [3] |
| [Eclipse Mojarra](https://projects.eclipse.org/projects/ee4j.mojarra) | 1.2+ | N/A | Provides `http.route` [2], Controller Spans [3] |
| [Elasticsearch API Client](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html) | 7.16 - 7.17.19,<br>8.0 - 8.9.+ [4] | N/A | [Elasticsearch Client Spans] |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("org.eclipse.jetty")
module.set("jetty-client")
versions.set("[12,)")
}
}

otelJava {
minJavaVersionSupported.set(JavaVersion.VERSION_17)
}

dependencies {
implementation(project(":instrumentation:jetty-httpclient:jetty-httpclient-12.0:library"))

library("org.eclipse.jetty:jetty-client:12.0.0")

testImplementation(project(":instrumentation:jetty-httpclient:jetty-httpclient-12.0:testing"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0;

import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0.JettyHttpClientSingletons.JETTY_CLIENT_CONTEXT_KEY;
import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0.JettyHttpClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.nameContains;
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.api.util.VirtualField;
import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyHttpClient12TracingInterceptor;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.client.transport.HttpRequest;

public class JettyHttpClient12Instrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.eclipse.jetty.client.transport.HttpRequest");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(named("send"))
.and(takesArgument(0, named("org.eclipse.jetty.client.Response$CompleteListener"))),
JettyHttpClient12Instrumentation.class.getName() + "$JettyHttpClient12SendAdvice");
// For request listeners
transformer.applyAdviceToMethod(
isMethod().and(nameContains("notify")),
JettyHttpClient12Instrumentation.class.getName() + "$JettyHttpClient12NotifyAdvice");
}

@SuppressWarnings("unused")
public static class JettyHttpClient12SendAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterSend(
@Advice.This HttpRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
// start span
Context parentContext = Context.current();
JettyHttpClient12TracingInterceptor interceptor =
new JettyHttpClient12TracingInterceptor(parentContext, instrumenter());
interceptor.attachToRequest(request);
context = interceptor.getContext();
if (context == null) {
return;
}
// set context for responseListeners
VirtualField<Request, Context> virtualField = VirtualField.find(Request.class, Context.class);
virtualField.set(request, parentContext);
request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext);
123liuziming marked this conversation as resolved.
Show resolved Hide resolved

scope = context.makeCurrent();
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExitSend(
@Advice.This HttpRequest request,
@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, request, null, throwable);
}
}
}

@SuppressWarnings("unused")
public static class JettyHttpClient12NotifyAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterNotify(
@Advice.This HttpRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (!request.getAttributes().containsKey(JETTY_CLIENT_CONTEXT_KEY)) {
return;
}
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
if (context == null) {
return;
}
scope = context.makeCurrent();
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExitNotify(
@Advice.This HttpRequest request,
@Advice.Thrown Throwable throwable,
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
@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
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
scope.close();
if (throwable != null) {
instrumenter().end(context, request, null, throwable);
}
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Arrays.asList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class JettyHttpClient12InstrumentationModule extends InstrumentationModule {
public JettyHttpClient12InstrumentationModule() {
super("jetty-httpclient", "jetty-httpclient-12.0");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new JettyHttpClient12Instrumentation(),
new JettyHttpClient12RespListenersInstrumentation());
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("org.eclipse.jetty.client.transport.HttpRequest");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0;

import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0.JettyHttpClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameContains;
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.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.client.Response;
import org.eclipse.jetty.client.Result;

public class JettyHttpClient12RespListenersInstrumentation implements TypeInstrumentation {
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.eclipse.jetty.client.transport.ResponseListeners");
}

@Override
public void transform(TypeTransformer transformer) {
// for response listeners
transformer.applyAdviceToMethod(
isMethod()
.and(
nameContains("notify")
.and(isPublic())
.and(takesArgument(0, named("org.eclipse.jetty.client.Response")))),
JettyHttpClient12RespListenersInstrumentation.class.getName()
+ "$JettyHttpClient12RespListenersNotifyAdvice");

// for complete listeners
transformer.applyAdviceToMethod(
isMethod()
.and(
nameContains("notifyComplete")
.and(isPublic())
.and(takesArgument(0, named("org.eclipse.jetty.client.Result")))),
JettyHttpClient12RespListenersInstrumentation.class.getName()
+ "$JettyHttpClient12CompleteListenersNotifyAdvice");
}

@SuppressWarnings("unused")
public static class JettyHttpClient12RespListenersNotifyAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterNotify(
@Advice.Argument(0) Response response,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
VirtualField<Request, Context> virtualField = VirtualField.find(Request.class, Context.class);
context = virtualField.get(response.getRequest());
if (context == null) {
return;
}
scope = context.makeCurrent();
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExitNotify(
@Advice.Argument(0) Response response,
@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, response.getRequest(), response, throwable);
}
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
}
}

@SuppressWarnings("unused")
public static class JettyHttpClient12CompleteListenersNotifyAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterComplete(
@Advice.Argument(0) Result result,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
VirtualField<Request, Context> virtualField = VirtualField.find(Request.class, Context.class);
context = virtualField.get(result.getRequest());
if (context == null) {
return;
}
scope = context.makeCurrent();
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExitComplete(
@Advice.Argument(0) Result result,
@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, result.getRequest(), result.getResponse(), throwable);
}
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0;

import static java.util.Collections.singletonList;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientPeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyClientHttpAttributesGetter;
import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyClientInstrumenterFactory;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import java.util.function.Function;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.client.Response;

public final class JettyHttpClientSingletons {

static final String JETTY_CLIENT_CONTEXT_KEY = "otel-jetty-client-context";

private static final Instrumenter<Request, Response> INSTRUMENTER =
JettyClientInstrumenterFactory.create(
GlobalOpenTelemetry.get(),
builder ->
builder
.setCapturedRequestHeaders(CommonConfig.get().getClientRequestHeaders())
.setCapturedResponseHeaders(CommonConfig.get().getClientResponseHeaders())
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods()),
builder -> builder.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods()),
Function.identity(),
singletonList(
HttpClientPeerServiceAttributesExtractor.create(
JettyClientHttpAttributesGetter.INSTANCE,
CommonConfig.get().getPeerServiceResolver())),
CommonConfig.get().shouldEmitExperimentalHttpClientTelemetry());

public static Instrumenter<Request, Response> instrumenter() {
return INSTRUMENTER;
}

private JettyHttpClientSingletons() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v12_0;

import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.AbstractJettyClient12Test;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.extension.RegisterExtension;

class JettyHttpClient12AgentTest extends AbstractJettyClient12Test {

@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();

@Override
protected HttpClient createStandardClient() {
return new HttpClient();
}

@Override
protected HttpClient createHttpsClient(SslContextFactory.Client sslContextFactory) {
HttpClient httpClient = new HttpClient();
httpClient.setSslContextFactory(sslContextFactory);
return httpClient;
}
}
Loading
Loading