|
16 | 16 |
|
17 | 17 | package io.opentelemetry.instrumentation.hypertrace.jaxrs.v2_0;
|
18 | 18 |
|
19 |
| -import io.opentelemetry.api.OpenTelemetry; |
20 | 19 | import io.opentelemetry.api.trace.Span;
|
21 |
| -import io.opentelemetry.api.trace.Tracer; |
22 |
| -import io.opentelemetry.context.Context; |
23 | 20 | import io.opentelemetry.javaagent.instrumentation.jaxrsclient.v2_0.ClientTracingFilter;
|
| 21 | +import java.io.ByteArrayOutputStream; |
24 | 22 | import java.io.IOException;
|
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.OutputStream; |
25 | 25 | import javax.ws.rs.WebApplicationException;
|
| 26 | +import javax.ws.rs.core.HttpHeaders; |
26 | 27 | import javax.ws.rs.core.MediaType;
|
27 | 28 | import javax.ws.rs.ext.ReaderInterceptor;
|
28 | 29 | import javax.ws.rs.ext.ReaderInterceptorContext;
|
| 30 | +import javax.ws.rs.ext.WriterInterceptor; |
| 31 | +import javax.ws.rs.ext.WriterInterceptorContext; |
29 | 32 | import org.hypertrace.agent.config.Config.AgentConfig;
|
| 33 | +import org.hypertrace.agent.core.ContentEncodingUtils; |
| 34 | +import org.hypertrace.agent.core.ContentLengthUtils; |
30 | 35 | import org.hypertrace.agent.core.ContentTypeUtils;
|
| 36 | +import org.hypertrace.agent.core.GlobalObjectRegistry; |
| 37 | +import org.hypertrace.agent.core.GlobalObjectRegistry.SpanAndBuffer; |
31 | 38 | import org.hypertrace.agent.core.HypertraceConfig;
|
32 | 39 | import org.hypertrace.agent.core.HypertraceSemanticAttributes;
|
33 | 40 | import org.slf4j.Logger;
|
34 | 41 | import org.slf4j.LoggerFactory;
|
35 | 42 |
|
36 |
| -public class JaxrsClientEntityInterceptor implements ReaderInterceptor { |
| 43 | +public class JaxrsClientEntityInterceptor implements ReaderInterceptor, WriterInterceptor { |
37 | 44 |
|
38 | 45 | private static final Logger log = LoggerFactory.getLogger(JaxrsClientEntityInterceptor.class);
|
39 | 46 |
|
40 |
| - private static final Tracer TRACER = |
41 |
| - OpenTelemetry.getGlobalTracer("org.hypertrace.java.jaxrs.client"); |
42 |
| - |
| 47 | + /** Writing response body to input stream */ |
43 | 48 | @Override
|
44 | 49 | public Object aroundReadFrom(ReaderInterceptorContext context)
|
45 | 50 | throws IOException, WebApplicationException {
|
46 | 51 |
|
47 |
| - Object entity = context.proceed(); |
| 52 | + MediaType mediaType = context.getMediaType(); |
| 53 | + AgentConfig agentConfig = HypertraceConfig.get(); |
| 54 | + if (mediaType == null |
| 55 | + || !ContentTypeUtils.shouldCapture(mediaType.toString()) |
| 56 | + || !agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) { |
| 57 | + return context.proceed(); |
| 58 | + } |
48 | 59 |
|
49 | 60 | Object spanObj = context.getProperty(ClientTracingFilter.SPAN_PROPERTY_NAME);
|
50 | 61 | if (!(spanObj instanceof Span)) {
|
51 | 62 | log.error(
|
52 | 63 | "Span object is not present in the context properties, response object will not be captured");
|
53 |
| - return entity; |
| 64 | + return context.proceed(); |
54 | 65 | }
|
55 | 66 | Span currentSpan = (Span) spanObj;
|
56 | 67 |
|
57 |
| - MediaType mediaType = context.getMediaType(); |
58 |
| - AgentConfig agentConfig = HypertraceConfig.get(); |
59 |
| - if (mediaType == null |
60 |
| - || !ContentTypeUtils.shouldCapture(mediaType.toString()) |
61 |
| - || !agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) { |
62 |
| - return entity; |
| 68 | + // TODO as optimization the type could be checked here and if it is a primitive type e.g. String |
| 69 | + // it could be read directly. |
| 70 | + // context.getType(); |
| 71 | + |
| 72 | + InputStream entityStream = context.getInputStream(); |
| 73 | + Object entity = null; |
| 74 | + try { |
| 75 | + String encodingStr = context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); |
| 76 | + String contentLengthStr = context.getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH); |
| 77 | + int contentLength = ContentLengthUtils.parseLength(contentLengthStr); |
| 78 | + |
| 79 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(contentLength); |
| 80 | + GlobalObjectRegistry.inputStreamToSpanAndBufferMap.put( |
| 81 | + entityStream, |
| 82 | + new SpanAndBuffer( |
| 83 | + currentSpan, |
| 84 | + buffer, |
| 85 | + HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, |
| 86 | + ContentEncodingUtils.toCharset(encodingStr))); |
| 87 | + entity = context.proceed(); |
| 88 | + } catch (Exception ex) { |
| 89 | + log.error("Exception while capturing response body", ex); |
63 | 90 | }
|
| 91 | + return entity; |
| 92 | + } |
| 93 | + |
| 94 | + /** Writing request body to output stream */ |
| 95 | + @Override |
| 96 | + public void aroundWriteTo(WriterInterceptorContext context) |
| 97 | + throws IOException, WebApplicationException { |
| 98 | + |
| 99 | + Object spanObj = context.getProperty(ClientTracingFilter.SPAN_PROPERTY_NAME); |
| 100 | + if (!(spanObj instanceof Span)) { |
| 101 | + log.error( |
| 102 | + "Span object is not present in the context properties, request body will not be captured"); |
| 103 | + context.proceed(); |
| 104 | + return; |
| 105 | + } |
| 106 | + Span currentSpan = (Span) spanObj; |
64 | 107 |
|
65 |
| - if (currentSpan.isRecording()) { |
66 |
| - currentSpan.setAttribute(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, entity.toString()); |
67 |
| - } else { |
68 |
| - TRACER |
69 |
| - .spanBuilder(HypertraceSemanticAttributes.ADDITIONAL_DATA_SPAN_NAME) |
70 |
| - .setParent(Context.root().with(currentSpan)) |
71 |
| - .setAttribute(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, entity.toString()) |
72 |
| - .startSpan() |
73 |
| - .end(); |
| 108 | + AgentConfig agentConfig = HypertraceConfig.get(); |
| 109 | + if (agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) { |
| 110 | + MediaType mediaType = context.getMediaType(); |
| 111 | + if (mediaType == null || !ContentTypeUtils.shouldCapture(mediaType.toString())) { |
| 112 | + context.proceed(); |
| 113 | + return; |
| 114 | + } |
74 | 115 | }
|
75 | 116 |
|
76 |
| - return entity; |
| 117 | + // TODO length is not known |
| 118 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 119 | + OutputStream entityStream = context.getOutputStream(); |
| 120 | + try { |
| 121 | + GlobalObjectRegistry.outputStreamToBufferMap.put(entityStream, buffer); |
| 122 | + context.proceed(); |
| 123 | + } catch (Exception ex) { |
| 124 | + log.error("Failed to capture request body", ex); |
| 125 | + } finally { |
| 126 | + GlobalObjectRegistry.outputStreamToBufferMap.remove(entityStream); |
| 127 | + // TODO encoding is not known |
| 128 | + currentSpan.setAttribute(HypertraceSemanticAttributes.HTTP_REQUEST_BODY, buffer.toString()); |
| 129 | + } |
77 | 130 | }
|
78 | 131 | }
|
0 commit comments