17
17
package io .opentelemetry .instrumentation .hypertrace .jaxrs .v2_0 ;
18
18
19
19
import io .opentelemetry .api .trace .Span ;
20
+ import io .opentelemetry .context .Context ;
20
21
import io .opentelemetry .javaagent .instrumentation .jaxrsclient .v2_0 .ClientTracingFilter ;
21
22
import java .io .ByteArrayOutputStream ;
22
23
import java .io .IOException ;
@@ -46,34 +47,35 @@ public class JaxrsClientEntityInterceptor implements ReaderInterceptor, WriterIn
46
47
47
48
/** Writing response body to input stream */
48
49
@ Override
49
- public Object aroundReadFrom (ReaderInterceptorContext context )
50
+ public Object aroundReadFrom (ReaderInterceptorContext responseContext )
50
51
throws IOException , WebApplicationException {
51
52
52
- MediaType mediaType = context .getMediaType ();
53
+ MediaType mediaType = responseContext .getMediaType ();
53
54
AgentConfig agentConfig = HypertraceConfig .get ();
54
55
if (mediaType == null
55
56
|| !ContentTypeUtils .shouldCapture (mediaType .toString ())
56
57
|| !agentConfig .getDataCapture ().getHttpBody ().getResponse ().getValue ()) {
57
- return context .proceed ();
58
+ return responseContext .proceed ();
58
59
}
59
60
60
- Object spanObj = context .getProperty (ClientTracingFilter .SPAN_PROPERTY_NAME );
61
- if (!(spanObj instanceof Span )) {
61
+ Object contextObj = responseContext .getProperty (ClientTracingFilter .CONTEXT_PROPERTY_NAME );
62
+ if (!(contextObj instanceof Context )) {
62
63
log .error (
63
64
"Span object is not present in the context properties, response object will not be captured" );
64
- return context .proceed ();
65
+ return responseContext .proceed ();
65
66
}
66
- Span currentSpan = (Span ) spanObj ;
67
+ Context context = (Context ) contextObj ;
68
+ Span currentSpan = Span .fromContext (context );
67
69
68
70
// TODO as optimization the type could be checked here and if it is a primitive type e.g. String
69
71
// it could be read directly.
70
72
// context.getType();
71
73
72
- InputStream entityStream = context .getInputStream ();
74
+ InputStream entityStream = responseContext .getInputStream ();
73
75
Object entity = null ;
74
76
try {
75
- String encodingStr = context .getHeaders ().getFirst (HttpHeaders .CONTENT_ENCODING );
76
- String contentLengthStr = context .getHeaders ().getFirst (HttpHeaders .CONTENT_LENGTH );
77
+ String encodingStr = responseContext .getHeaders ().getFirst (HttpHeaders .CONTENT_ENCODING );
78
+ String contentLengthStr = responseContext .getHeaders ().getFirst (HttpHeaders .CONTENT_LENGTH );
77
79
int contentLength = ContentLengthUtils .parseLength (contentLengthStr );
78
80
79
81
ByteArrayOutputStream buffer = new ByteArrayOutputStream (contentLength );
@@ -84,7 +86,7 @@ public Object aroundReadFrom(ReaderInterceptorContext context)
84
86
buffer ,
85
87
HypertraceSemanticAttributes .HTTP_RESPONSE_BODY ,
86
88
ContentEncodingUtils .toCharset (encodingStr )));
87
- entity = context .proceed ();
89
+ entity = responseContext .proceed ();
88
90
} catch (Exception ex ) {
89
91
log .error ("Exception while capturing response body" , ex );
90
92
}
@@ -93,33 +95,34 @@ public Object aroundReadFrom(ReaderInterceptorContext context)
93
95
94
96
/** Writing request body to output stream */
95
97
@ Override
96
- public void aroundWriteTo (WriterInterceptorContext context )
98
+ public void aroundWriteTo (WriterInterceptorContext requestContext )
97
99
throws IOException , WebApplicationException {
98
100
99
- Object spanObj = context .getProperty (ClientTracingFilter .SPAN_PROPERTY_NAME );
100
- if (!(spanObj instanceof Span )) {
101
+ Object contextObj = requestContext .getProperty (ClientTracingFilter .CONTEXT_PROPERTY_NAME );
102
+ if (!(contextObj instanceof Context )) {
101
103
log .error (
102
104
"Span object is not present in the context properties, request body will not be captured" );
103
- context .proceed ();
105
+ requestContext .proceed ();
104
106
return ;
105
107
}
106
- Span currentSpan = (Span ) spanObj ;
108
+ Context context = (Context ) contextObj ;
109
+ Span currentSpan = Span .fromContext (context );
107
110
108
111
AgentConfig agentConfig = HypertraceConfig .get ();
109
112
if (agentConfig .getDataCapture ().getHttpBody ().getRequest ().getValue ()) {
110
- MediaType mediaType = context .getMediaType ();
113
+ MediaType mediaType = requestContext .getMediaType ();
111
114
if (mediaType == null || !ContentTypeUtils .shouldCapture (mediaType .toString ())) {
112
- context .proceed ();
115
+ requestContext .proceed ();
113
116
return ;
114
117
}
115
118
}
116
119
117
120
// TODO length is not known
118
121
ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
119
- OutputStream entityStream = context .getOutputStream ();
122
+ OutputStream entityStream = requestContext .getOutputStream ();
120
123
try {
121
124
GlobalObjectRegistry .outputStreamToBufferMap .put (entityStream , buffer );
122
- context .proceed ();
125
+ requestContext .proceed ();
123
126
} catch (Exception ex ) {
124
127
log .error ("Failed to capture request body" , ex );
125
128
} finally {
0 commit comments