-
Notifications
You must be signed in to change notification settings - Fork 967
support jettyclient 12 #11519
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
Merged
Merged
support jettyclient 12 #11519
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7bbd787
support jettyclient 12
123liuziming 2af2aef
support library instrumentation
123liuziming 2f41645
set min java version
123liuziming aa045ea
set library min java version
123liuziming 6c6e4bd
polish code
123liuziming 10d0908
modify muzzle range in jetty httpclient
123liuziming 13e9d9b
revert muzzle range
123liuziming 9d2f2fc
add supported-libraries
123liuziming e25cd90
delete useless field in AbstractJettyClient12Test
123liuziming c981ca0
polish code
123liuziming 1aa49ef
spotless apply
123liuziming c8765f5
fix test
123liuziming df00962
polish supported-libraries
123liuziming e5dbf9f
polish code
123liuziming 6fdb189
spotless apply
123liuziming 5332e2d
polish code
123liuziming 779bfa5
simplify
laurit 53427da
small changes
laurit 9777405
merge
laurit 13ebb47
apply the same refactor as was done with other http client instrument…
laurit 8adc845
Update docs/supported-libraries.md
trask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/build.gradle.kts
This file contains hidden or 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,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")) | ||
} |
114 changes: 114 additions & 0 deletions
114
...ry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12Instrumentation.java
This file contains hidden or 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,114 @@ | ||
/* | ||
* 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.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.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 | ||
request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext); | ||
|
||
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) { | ||
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(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...aagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12InstrumentationModule.java
This file contains hidden or 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,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"); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...instrumentation/jetty/httpclient/v12_0/JettyHttpClient12RespListenersInstrumentation.java
This file contains hidden or 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,109 @@ | ||
/* | ||
* 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 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.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
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) { | ||
Map<String, Object> attr = response.getRequest().getAttributes(); | ||
context = (Context) response.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY); | ||
if (context != null) { | ||
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; | ||
} | ||
|
||
scope.close(); | ||
} | ||
} | ||
|
||
@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) { | ||
context = (Context) result.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY); | ||
if (context != null) { | ||
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; | ||
} | ||
|
||
scope.close(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...telemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClientSingletons.java
This file contains hidden or 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,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() {} | ||
} |
31 changes: 31 additions & 0 deletions
31
...elemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12AgentTest.java
This file contains hidden or 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,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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
instrumentation/jetty-httpclient/jetty-httpclient-12.0/library/build.gradle.kts
This file contains hidden or 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,13 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
} | ||
|
||
otelJava { | ||
minJavaVersionSupported.set(JavaVersion.VERSION_17) | ||
} | ||
|
||
dependencies { | ||
library("org.eclipse.jetty:jetty-client:12.0.0") | ||
|
||
testImplementation(project(":instrumentation:jetty-httpclient::jetty-httpclient-12.0:testing")) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.