-
Notifications
You must be signed in to change notification settings - Fork 344
feat(commons-httpclient-2.0): toolkit-generated regeneration #11717
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
base: master
Are you sure you want to change the base?
Changes from all commits
9da6f7d
7e64d5b
73ce2ea
fd4818c
143505f
2a80f09
cd4371a
c661f9d
725f6ee
ebc40f3
2ff02f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,15 @@ | ||
| package datadog.trace.instrumentation.commonshttpclient; | ||
|
|
||
| import static datadog.trace.agent.tooling.InstrumenterModule.TargetSystem.CONTEXT_TRACKING; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext; | ||
| import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.HTTP_REQUEST; | ||
| import static datadog.trace.instrumentation.commonshttpclient.HttpHeadersInjectAdapter.SETTER; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.appsec.api.blocking.BlockingException; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.agent.tooling.annotation.AppliesOn; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import net.bytebuddy.asm.Advice; | ||
| import org.apache.commons.httpclient.HttpClient; | ||
| import org.apache.commons.httpclient.HttpMethod; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
|
|
@@ -40,72 +28,40 @@ public String instrumentedType() { | |
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| packageName + ".CommonsHttpClientDecorator", packageName + ".HttpHeadersInjectAdapter", | ||
| packageName + ".CommonsHttpClientDecorator", | ||
| packageName + ".HttpHeadersInjectAdapter", | ||
| packageName + ".HelperMethods", | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvices( | ||
| // All executeMethod overloads delegate to the 3-arg version: | ||
| // executeMethod(HostConfiguration, HttpMethod, HttpState) | ||
| // Instrumenting only the delegate avoids duplicate spans. | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("executeMethod")) | ||
| .and(takesArguments(3)) | ||
| .and(takesArgument(1, named("org.apache.commons.httpclient.HttpMethod"))), | ||
| CommonsHttpClientInstrumentation.class.getName() + "$ExecAdvice", | ||
| CommonsHttpClientInstrumentation.class.getName() + "$ContextPropagationAdvice"); | ||
| .and(takesArgument(0, named("org.apache.commons.httpclient.HostConfiguration"))) | ||
| .and(takesArgument(1, named("org.apache.commons.httpclient.HttpMethod"))) | ||
| .and(takesArgument(2, named("org.apache.commons.httpclient.HttpState"))), | ||
| CommonsHttpClientInstrumentation.class.getName() + "$ExecuteMethodAdvice"); | ||
| } | ||
|
|
||
| public static class ExecAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope methodEnter(@Advice.Argument(1) final HttpMethod httpMethod) { | ||
|
|
||
| try { | ||
| final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class); | ||
| if (callDepth > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| final AgentSpan span = startSpan("commons-http-client", HTTP_REQUEST); | ||
| final AgentScope scope = activateSpan(span); | ||
| public static class ExecuteMethodAdvice { | ||
|
|
||
| DECORATE.afterStart(span); | ||
| DECORATE.onRequest(span, httpMethod); | ||
|
|
||
| return scope; | ||
| } catch (BlockingException e) { | ||
| CallDepthThreadLocalMap.reset(HttpClient.class); | ||
| // re-throw blocking exceptions | ||
| throw e; | ||
| } | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope methodEnter(@Advice.Argument(1) final HttpMethod method) { | ||
| return HelperMethods.doMethodEnter(method); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no value in introducing the HelperMethods class |
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void methodExit( | ||
| @Advice.Enter final AgentScope scope, | ||
| @Advice.Argument(1) final HttpMethod httpMethod, | ||
| @Advice.Argument(1) final HttpMethod method, | ||
| @Advice.Thrown final Throwable throwable) { | ||
|
|
||
| if (scope == null) { | ||
| return; | ||
| } | ||
| final AgentSpan span = scope.span(); | ||
| try { | ||
| DECORATE.onResponse(span, httpMethod); | ||
| DECORATE.onError(span, throwable); | ||
| DECORATE.beforeFinish(span); | ||
| } finally { | ||
| scope.close(); | ||
| span.finish(); | ||
| CallDepthThreadLocalMap.reset(HttpClient.class); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @AppliesOn(CONTEXT_TRACKING) | ||
| public static class ContextPropagationAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void methodEnter(@Advice.Argument(1) final HttpMethod httpMethod) { | ||
| DECORATE.injectContext(getCurrentContext(), httpMethod, SETTER); | ||
| HelperMethods.doMethodExit(scope, method, throwable); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package datadog.trace.instrumentation.commonshttpclient; | ||
|
|
||
| import static datadog.context.Context.current; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.HTTP_REQUEST; | ||
| import static datadog.trace.instrumentation.commonshttpclient.HttpHeadersInjectAdapter.SETTER; | ||
|
|
||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import org.apache.commons.httpclient.HttpClient; | ||
| import org.apache.commons.httpclient.HttpMethod; | ||
|
|
||
| public class HelperMethods { | ||
|
|
||
| public static AgentScope doMethodEnter(final HttpMethod method) { | ||
| final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class); | ||
| if (callDepth > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| final AgentSpan span = | ||
| startSpan(CommonsHttpClientDecorator.COMMONS_HTTP_CLIENT.toString(), HTTP_REQUEST); | ||
| DECORATE.afterStart(span); | ||
| DECORATE.onRequest(span, method); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AppSec/RASP blocks an outbound URL, Useful? React with 👍 / 👎. |
||
|
|
||
| final AgentScope scope = activateSpan(span); | ||
|
|
||
| DECORATE.injectContext(current(), method, SETTER); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This injects propagation only from the tracing helper, whereas the previous Useful? React with 👍 / 👎. |
||
|
|
||
| return scope; | ||
| } | ||
|
|
||
| public static void doMethodExit( | ||
| final AgentScope scope, final HttpMethod method, final Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| final AgentSpan span = scope.span(); | ||
| try { | ||
| if (throwable != null) { | ||
| DECORATE.onError(span, throwable); | ||
| } else { | ||
| DECORATE.onResponse(span, method); | ||
| } | ||
| DECORATE.beforeFinish(span); | ||
| } finally { | ||
| scope.close(); | ||
| span.finish(); | ||
| CallDepthThreadLocalMap.reset(HttpClient.class); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be verified for each version to make sure it alway such three arguments because of it's much stricter and Java allows method overloading. Restricting arguments other than the one that's used seems to be too limiting. That can cause inflexibility if in some versions of the library this method signature changes.
The comment about avoiding duplicate spans is misleading. It's avoided anyways with the call-depth check, that is kept as-is anyways, but moved to a helper class.