1010import datadog .appsec .api .blocking .BlockingException ;
1111import datadog .trace .agent .tooling .Instrumenter ;
1212import datadog .trace .agent .tooling .InstrumenterModule ;
13+ import datadog .trace .bootstrap .InstrumentationContext ;
1314import datadog .trace .bootstrap .instrumentation .api .AgentScope ;
15+ import java .util .Collections ;
16+ import java .util .Map ;
1417import net .bytebuddy .asm .Advice ;
1518import net .bytebuddy .description .type .TypeDescription ;
1619import net .bytebuddy .implementation .bytecode .assign .Assigner ;
1720import net .bytebuddy .matcher .ElementMatcher ;
1821import org .apache .hc .core5 .http .ClassicHttpRequest ;
1922import org .apache .hc .core5 .http .HttpHost ;
23+ import org .apache .hc .core5 .http .HttpRequest ;
2024import org .apache .hc .core5 .http .io .HttpClientResponseHandler ;
2125import org .apache .hc .core5 .http .protocol .HttpContext ;
2226
@@ -68,6 +72,11 @@ public String[] helperClassNames() {
6872 };
6973 }
7074
75+ @ Override
76+ public Map <String , String > contextStore () {
77+ return Collections .singletonMap ("org.apache.hc.core5.http.HttpRequest" , "java.lang.Integer" );
78+ }
79+
7180 @ Override
7281 public void methodAdvice (MethodTransformer transformer ) {
7382 transformer .applyAdvice (
@@ -117,20 +126,28 @@ public static class RequestAdvice {
117126 @ Advice .OnMethodEnter (suppress = Throwable .class )
118127 public static AgentScope methodEnter (@ Advice .Argument (0 ) final ClassicHttpRequest request ) {
119128 try {
120- return HelperMethods .doMethodEnter (request );
129+ return HelperMethods .doMethodEnter (
130+ InstrumentationContext .get (HttpRequest .class , Integer .class ), request );
121131 } catch (BlockingException e ) {
122- HelperMethods .onBlockingRequest ();
132+ HelperMethods .onBlockingRequest (
133+ InstrumentationContext .get (HttpRequest .class , Integer .class ), request );
123134 // re-throw blocking exceptions
124135 throw e ;
125136 }
126137 }
127138
128139 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
129140 public static void methodExit (
141+ @ Advice .Argument (0 ) final ClassicHttpRequest request ,
130142 @ Advice .Enter final AgentScope scope ,
131143 @ Advice .Return final Object result ,
132144 @ Advice .Thrown final Throwable throwable ) {
133- HelperMethods .doMethodExit (scope , result , throwable );
145+ HelperMethods .doMethodExit (
146+ InstrumentationContext .get (HttpRequest .class , Integer .class ),
147+ request ,
148+ scope ,
149+ result ,
150+ throwable );
134151 }
135152 }
136153
@@ -140,20 +157,28 @@ public static AgentScope methodEnter(
140157 @ Advice .Argument (0 ) final HttpHost host ,
141158 @ Advice .Argument (1 ) final ClassicHttpRequest request ) {
142159 try {
143- return HelperMethods .doMethodEnter (host , request );
160+ return HelperMethods .doMethodEnter (
161+ InstrumentationContext .get (HttpRequest .class , Integer .class ), host , request );
144162 } catch (BlockingException e ) {
145- HelperMethods .onBlockingRequest ();
163+ HelperMethods .onBlockingRequest (
164+ InstrumentationContext .get (HttpRequest .class , Integer .class ), request );
146165 // re-throw blocking exceptions
147166 throw e ;
148167 }
149168 }
150169
151170 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
152171 public static void methodExit (
172+ @ Advice .Argument (1 ) final ClassicHttpRequest request ,
153173 @ Advice .Enter final AgentScope scope ,
154174 @ Advice .Return final Object result ,
155175 @ Advice .Thrown final Throwable throwable ) {
156- HelperMethods .doMethodExit (scope , result , throwable );
176+ HelperMethods .doMethodExit (
177+ InstrumentationContext .get (HttpRequest .class , Integer .class ),
178+ request ,
179+ scope ,
180+ result ,
181+ throwable );
157182 }
158183 }
159184
@@ -171,7 +196,9 @@ public static AgentScope methodEnter(
171196 readOnly = false )
172197 Object handler ) {
173198 try {
174- final AgentScope scope = HelperMethods .doMethodEnter (host , request );
199+ final AgentScope scope =
200+ HelperMethods .doMethodEnter (
201+ InstrumentationContext .get (HttpRequest .class , Integer .class ), host , request );
175202 // Wrap the handler so we capture the status code
176203 if (null != scope && handler instanceof HttpClientResponseHandler ) {
177204 handler =
@@ -180,18 +207,25 @@ public static AgentScope methodEnter(
180207 }
181208 return scope ;
182209 } catch (BlockingException e ) {
183- HelperMethods .onBlockingRequest ();
210+ HelperMethods .onBlockingRequest (
211+ InstrumentationContext .get (HttpRequest .class , Integer .class ), request );
184212 // re-throw blocking exceptions
185213 throw e ;
186214 }
187215 }
188216
189217 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
190218 public static void methodExit (
219+ @ Advice .Argument (1 ) final ClassicHttpRequest request ,
191220 @ Advice .Enter final AgentScope scope ,
192221 @ Advice .Return final Object result ,
193222 @ Advice .Thrown final Throwable throwable ) {
194- HelperMethods .doMethodExit (scope , result , throwable );
223+ HelperMethods .doMethodExit (
224+ InstrumentationContext .get (HttpRequest .class , Integer .class ),
225+ request ,
226+ scope ,
227+ result ,
228+ throwable );
195229 }
196230 }
197231}
0 commit comments