30
30
import com .google .auto .service .AutoService ;
31
31
import io .opentelemetry .api .trace .Span ;
32
32
import io .opentelemetry .javaagent .instrumentation .api .CallDepthThreadLocalMap ;
33
+ import io .opentelemetry .javaagent .instrumentation .api .ContextStore ;
34
+ import io .opentelemetry .javaagent .instrumentation .api .InstrumentationContext ;
33
35
import io .opentelemetry .javaagent .instrumentation .api .Java8BytecodeBridge ;
34
36
import io .opentelemetry .javaagent .tooling .InstrumentationModule ;
35
37
import io .opentelemetry .javaagent .tooling .TypeInstrumentation ;
@@ -72,6 +74,13 @@ public int getOrder() {
72
74
return 1 ;
73
75
}
74
76
77
+ @ Override
78
+ protected Map <String , String > contextStore () {
79
+ Map <String , String > context = new HashMap <>();
80
+ context .put ("org.apache.http.HttpEntity" , Span .class .getName ());
81
+ return context ;
82
+ }
83
+
75
84
@ Override
76
85
public List <TypeInstrumentation > typeInstrumentations () {
77
86
return Arrays .asList (new HttpEntityInstrumentation (), new ApacheClientInstrumentation ());
@@ -118,7 +127,9 @@ public static boolean enter(@Advice.Argument(0) HttpMessage request) {
118
127
if (callDepth > 0 ) {
119
128
return false ;
120
129
}
121
- ApacheHttpClientUtils .traceRequest (request );
130
+ ContextStore <HttpEntity , Span > contextStore =
131
+ InstrumentationContext .get (HttpEntity .class , Span .class );
132
+ ApacheHttpClientUtils .traceRequest (contextStore , request );
122
133
return true ;
123
134
}
124
135
@@ -138,7 +149,9 @@ public static boolean enter(@Advice.Argument(1) HttpMessage request) {
138
149
if (callDepth > 0 ) {
139
150
return false ;
140
151
}
141
- ApacheHttpClientUtils .traceRequest (request );
152
+ ContextStore <HttpEntity , Span > contextStore =
153
+ InstrumentationContext .get (HttpEntity .class , Span .class );
154
+ ApacheHttpClientUtils .traceRequest (contextStore , request );
142
155
return true ;
143
156
}
144
157
@@ -178,8 +191,13 @@ public static void exit(@Advice.Return Object response, @Advice.Enter boolean re
178
191
179
192
if (agentConfig .getDataCapture ().getHttpBody ().getResponse ().getValue ()) {
180
193
HttpEntity entity = httpResponse .getEntity ();
194
+ ContextStore <HttpEntity , Span > contextStore =
195
+ InstrumentationContext .get (HttpEntity .class , Span .class );
181
196
ApacheHttpClientUtils .traceEntity (
182
- currentSpan , HypertraceSemanticAttributes .HTTP_RESPONSE_BODY .getKey (), entity );
197
+ contextStore ,
198
+ currentSpan ,
199
+ HypertraceSemanticAttributes .HTTP_RESPONSE_BODY .getKey (),
200
+ entity );
183
201
}
184
202
}
185
203
}
@@ -213,7 +231,9 @@ static class HttpEntity_GetContentAdvice {
213
231
@ Advice .OnMethodExit (suppress = Throwable .class )
214
232
public static void exit (@ Advice .This HttpEntity thizz , @ Advice .Return InputStream inputStream ) {
215
233
// here the Span.current() is finished for response entities
216
- Span clientSpan = ApacheHttpClientObjectRegistry .httpEntityToSpanMap .remove (thizz );
234
+ ContextStore <HttpEntity , Span > contextStore =
235
+ InstrumentationContext .get (HttpEntity .class , Span .class );
236
+ Span clientSpan = contextStore .get (thizz );
217
237
// HttpEntity might be wrapped multiple times
218
238
// this ensures that the advice runs only for the most outer one
219
239
// the returned inputStream is put into globally accessible map
@@ -250,7 +270,9 @@ static class HttpEntity_WriteToAdvice {
250
270
@ Advice .OnMethodEnter (suppress = Throwable .class )
251
271
public static void enter (
252
272
@ Advice .This HttpEntity thizz , @ Advice .Argument (0 ) OutputStream outputStream ) {
253
- if (!ApacheHttpClientObjectRegistry .httpEntityToSpanMap .containsKey (thizz )) {
273
+ ContextStore <HttpEntity , Span > contextStore =
274
+ InstrumentationContext .get (HttpEntity .class , Span .class );
275
+ if (contextStore .get (thizz ) == null ) {
254
276
return ;
255
277
}
256
278
@@ -267,7 +289,9 @@ public static void enter(
267
289
@ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
268
290
public static void exit (
269
291
@ Advice .This HttpEntity thizz , @ Advice .Argument (0 ) OutputStream outputStream ) {
270
- Span clientSpan = ApacheHttpClientObjectRegistry .httpEntityToSpanMap .remove (thizz );
292
+ ContextStore <HttpEntity , Span > contextStore =
293
+ InstrumentationContext .get (HttpEntity .class , Span .class );
294
+ Span clientSpan = contextStore .get (thizz );
271
295
if (clientSpan == null ) {
272
296
return ;
273
297
}
0 commit comments