|
25 | 25 | import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
26 | 26 |
|
27 | 27 | import com.google.auto.service.AutoService; |
28 | | -import io.opentelemetry.api.trace.Span; |
29 | 28 | import io.opentelemetry.context.Context; |
30 | 29 | import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; |
31 | 30 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
32 | 31 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
33 | 32 | import io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientInstrumentation.DelegatingRequestProducer; |
34 | 33 | import io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback; |
35 | | -import io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpclient.v4_0.ApacheHttpClientUtils; |
36 | | -import java.io.IOException; |
37 | | -import java.lang.invoke.MethodHandle; |
38 | | -import java.lang.invoke.MethodHandles; |
39 | | -import java.lang.invoke.MethodHandles.Lookup; |
40 | | -import java.lang.invoke.MethodType; |
41 | | -import java.lang.reflect.Field; |
42 | 34 | import java.util.Collections; |
43 | 35 | import java.util.List; |
44 | 36 | import net.bytebuddy.asm.Advice; |
45 | 37 | import net.bytebuddy.description.type.TypeDescription; |
46 | 38 | import net.bytebuddy.matcher.ElementMatcher; |
47 | | -import org.apache.http.HttpException; |
48 | | -import org.apache.http.HttpRequest; |
49 | | -import org.apache.http.HttpResponse; |
50 | 39 | import org.apache.http.concurrent.FutureCallback; |
51 | 40 | import org.apache.http.nio.protocol.HttpAsyncRequestProducer; |
52 | 41 | import org.apache.http.protocol.HttpContext; |
53 | | -import org.apache.http.protocol.HttpCoreContext; |
54 | | -import org.slf4j.Logger; |
55 | | -import org.slf4j.LoggerFactory; |
56 | 42 |
|
57 | 43 | @AutoService(InstrumentationModule.class) |
58 | 44 | public class ApacheAsyncClientInstrumentationModule extends InstrumentationModule { |
@@ -121,105 +107,4 @@ public static void enter( |
121 | 107 | } |
122 | 108 | } |
123 | 109 | } |
124 | | - |
125 | | - public static class DelegatingCaptureBodyRequestProducer extends DelegatingRequestProducer { |
126 | | - |
127 | | - private static final Logger log = |
128 | | - LoggerFactory.getLogger(DelegatingCaptureBodyRequestProducer.class); |
129 | | - |
130 | | - private static final Lookup lookup = MethodHandles.lookup(); |
131 | | - private static final MethodHandle getContext; |
132 | | - |
133 | | - static { |
134 | | - MethodHandle localGetContext; |
135 | | - try { |
136 | | - Field contextField = WrappedFutureCallback.class.getDeclaredField("context"); |
137 | | - contextField.setAccessible(true); |
138 | | - MethodHandle unReflectedField = lookup.unreflectGetter(contextField); |
139 | | - localGetContext = |
140 | | - unReflectedField.asType( |
141 | | - MethodType.methodType(Context.class, WrappedFutureCallback.class)); |
142 | | - } catch (NoSuchFieldException | IllegalAccessException e) { |
143 | | - log.debug("Could not find context field on super class", e); |
144 | | - localGetContext = null; |
145 | | - } |
146 | | - getContext = localGetContext; |
147 | | - } |
148 | | - |
149 | | - private final WrappedFutureCallback<?> wrappedFutureCallback; |
150 | | - private final BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback; |
151 | | - |
152 | | - public DelegatingCaptureBodyRequestProducer( |
153 | | - Context parentContext, |
154 | | - HttpAsyncRequestProducer delegate, |
155 | | - WrappedFutureCallback<?> wrappedFutureCallback, |
156 | | - BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback) { |
157 | | - super(parentContext, delegate, wrappedFutureCallback); |
158 | | - this.wrappedFutureCallback = wrappedFutureCallback; |
159 | | - this.bodyCaptureDelegatingCallback = bodyCaptureDelegatingCallback; |
160 | | - } |
161 | | - |
162 | | - @Override |
163 | | - public HttpRequest generateRequest() throws IOException, HttpException { |
164 | | - HttpRequest request = super.generateRequest(); |
165 | | - try { |
166 | | - /* |
167 | | - * Ideally, we should not rely on the getContext MethodHandle here. The client context isn't |
168 | | - * generated until super.generateRequest is called so ideally we should architect our code |
169 | | - * such that we can access the client span more idiomatically after it is created, like via |
170 | | - * the Java8BytecodeBridge.currentSpan call. |
171 | | - */ |
172 | | - Object getContextResult = getContext.invoke(wrappedFutureCallback); |
173 | | - if (getContextResult instanceof Context) { |
174 | | - Context context = (Context) getContextResult; |
175 | | - Span clientSpan = Span.fromContextOrNull(context); |
176 | | - bodyCaptureDelegatingCallback.clientContext = context; |
177 | | - ApacheHttpClientUtils.traceRequest(clientSpan, request); |
178 | | - } |
179 | | - } catch (Throwable t) { |
180 | | - log.debug("Could not access context field on super class", t); |
181 | | - } |
182 | | - return request; |
183 | | - } |
184 | | - } |
185 | | - |
186 | | - public static class BodyCaptureDelegatingCallback<T> implements FutureCallback<T> { |
187 | | - |
188 | | - final Context context; |
189 | | - final HttpContext httpContext; |
190 | | - private final FutureCallback<T> delegate; |
191 | | - private volatile Context clientContext; |
192 | | - |
193 | | - public BodyCaptureDelegatingCallback( |
194 | | - Context context, HttpContext httpContext, FutureCallback<T> delegate) { |
195 | | - this.context = context; |
196 | | - this.httpContext = httpContext; |
197 | | - this.delegate = delegate; |
198 | | - } |
199 | | - |
200 | | - @Override |
201 | | - public void completed(T result) { |
202 | | - HttpResponse httpResponse = getResponse(httpContext); |
203 | | - ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse); |
204 | | - delegate.completed(result); |
205 | | - } |
206 | | - |
207 | | - @Override |
208 | | - public void failed(Exception ex) { |
209 | | - HttpResponse httpResponse = getResponse(httpContext); |
210 | | - ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse); |
211 | | - delegate.failed(ex); |
212 | | - } |
213 | | - |
214 | | - @Override |
215 | | - public void cancelled() { |
216 | | - HttpResponse httpResponse = getResponse(httpContext); |
217 | | - ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse); |
218 | | - delegate.cancelled(); |
219 | | - } |
220 | | - |
221 | | - private static HttpResponse getResponse(HttpContext context) { |
222 | | - return (HttpResponse) context.getAttribute(HttpCoreContext.HTTP_RESPONSE); |
223 | | - } |
224 | | - } |
225 | 110 | } |
0 commit comments