|  | 
| 27 | 27 | import io.opentelemetry.api.common.AttributeKey; | 
| 28 | 28 | import io.opentelemetry.api.trace.Span; | 
| 29 | 29 | import io.opentelemetry.context.Context; | 
| 30 |  | -import io.opentelemetry.context.Scope; | 
| 31 | 30 | import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.AttributeKeys; | 
| 32 | 31 | import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.DataCaptureUtils; | 
| 33 |  | -import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.OtelHttpClientRequestTracingHandler; | 
| 34 | 32 | import java.nio.charset.Charset; | 
| 35 | 33 | import java.util.HashMap; | 
| 36 | 34 | import java.util.Map; | 
| @@ -59,54 +57,47 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { | 
| 59 | 57 |       ctx.fireChannelRead(msg); | 
| 60 | 58 |       return; | 
| 61 | 59 |     } | 
|  | 60 | +    Span span = Span.fromContext(context); | 
| 62 | 61 | 
 | 
| 63 |  | -    // Store the server context in our ThreadLocal for later use by client handlers | 
| 64 |  | -    // This is CRITICAL for proper context propagation to client spans | 
| 65 |  | -    OtelHttpClientRequestTracingHandler.storeServerContext(context); | 
|  | 62 | +    if (msg instanceof HttpRequest) { | 
|  | 63 | +      HttpRequest httpRequest = (HttpRequest) msg; | 
| 66 | 64 | 
 | 
| 67 |  | -    try (Scope ignored = context.makeCurrent()) { | 
| 68 |  | -      Span span = Span.fromContext(context); | 
| 69 |  | - | 
| 70 |  | -      if (msg instanceof HttpRequest) { | 
| 71 |  | -        HttpRequest httpRequest = (HttpRequest) msg; | 
| 72 |  | - | 
| 73 |  | -        Map<String, String> headersMap = headersToMap(httpRequest); | 
| 74 |  | -        if (instrumentationConfig.httpHeaders().request()) { | 
| 75 |  | -          headersMap.forEach(span::setAttribute); | 
| 76 |  | -        } | 
| 77 |  | -        // used by blocking handler | 
| 78 |  | -        channel.attr(AttributeKeys.REQUEST_HEADERS).set(headersMap); | 
|  | 65 | +      Map<String, String> headersMap = headersToMap(httpRequest); | 
|  | 66 | +      if (instrumentationConfig.httpHeaders().request()) { | 
|  | 67 | +        headersMap.forEach(span::setAttribute); | 
|  | 68 | +      } | 
|  | 69 | +      // used by blocking handler | 
|  | 70 | +      channel.attr(AttributeKeys.REQUEST_HEADERS).set(headersMap); | 
| 79 | 71 | 
 | 
| 80 |  | -        CharSequence contentType = DataCaptureUtils.getContentType(httpRequest); | 
| 81 |  | -        if (instrumentationConfig.httpBody().request() | 
| 82 |  | -            && contentType != null | 
| 83 |  | -            && ContentTypeUtils.shouldCapture(contentType.toString())) { | 
|  | 72 | +      CharSequence contentType = DataCaptureUtils.getContentType(httpRequest); | 
|  | 73 | +      if (instrumentationConfig.httpBody().request() | 
|  | 74 | +          && contentType != null | 
|  | 75 | +          && ContentTypeUtils.shouldCapture(contentType.toString())) { | 
| 84 | 76 | 
 | 
| 85 |  | -          CharSequence contentLengthHeader = DataCaptureUtils.getContentLength(httpRequest); | 
| 86 |  | -          int contentLength = ContentLengthUtils.parseLength(contentLengthHeader); | 
|  | 77 | +        CharSequence contentLengthHeader = DataCaptureUtils.getContentLength(httpRequest); | 
|  | 78 | +        int contentLength = ContentLengthUtils.parseLength(contentLengthHeader); | 
| 87 | 79 | 
 | 
| 88 |  | -          String charsetString = ContentTypeUtils.parseCharset(contentType.toString()); | 
| 89 |  | -          Charset charset = ContentTypeCharsetUtils.toCharset(charsetString); | 
|  | 80 | +        String charsetString = ContentTypeUtils.parseCharset(contentType.toString()); | 
|  | 81 | +        Charset charset = ContentTypeCharsetUtils.toCharset(charsetString); | 
| 90 | 82 | 
 | 
| 91 |  | -          // set the buffer to capture response body | 
| 92 |  | -          // the buffer is used byt captureBody method | 
| 93 |  | -          Attribute<BoundedByteArrayOutputStream> bufferAttr = | 
| 94 |  | -              ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER); | 
| 95 |  | -          bufferAttr.set(BoundedBuffersFactory.createStream(contentLength, charset)); | 
|  | 83 | +        // set the buffer to capture response body | 
|  | 84 | +        // the buffer is used byt captureBody method | 
|  | 85 | +        Attribute<BoundedByteArrayOutputStream> bufferAttr = | 
|  | 86 | +            ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER); | 
|  | 87 | +        bufferAttr.set(BoundedBuffersFactory.createStream(contentLength, charset)); | 
| 96 | 88 | 
 | 
| 97 |  | -          channel.attr(AttributeKeys.CHARSET).set(charset); | 
| 98 |  | -        } | 
|  | 89 | +        channel.attr(AttributeKeys.CHARSET).set(charset); | 
| 99 | 90 |       } | 
|  | 91 | +    } | 
| 100 | 92 | 
 | 
| 101 |  | -      if ((msg instanceof HttpContent || msg instanceof ByteBuf) | 
| 102 |  | -          && instrumentationConfig.httpBody().request()) { | 
| 103 |  | -        Charset charset = channel.attr(AttributeKeys.CHARSET).get(); | 
| 104 |  | -        if (charset == null) { | 
| 105 |  | -          charset = ContentTypeCharsetUtils.getDefaultCharset(); | 
| 106 |  | -        } | 
| 107 |  | -        DataCaptureUtils.captureBody( | 
| 108 |  | -            span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset); | 
|  | 93 | +    if ((msg instanceof HttpContent || msg instanceof ByteBuf) | 
|  | 94 | +        && instrumentationConfig.httpBody().request()) { | 
|  | 95 | +      Charset charset = channel.attr(AttributeKeys.CHARSET).get(); | 
|  | 96 | +      if (charset == null) { | 
|  | 97 | +        charset = ContentTypeCharsetUtils.getDefaultCharset(); | 
| 109 | 98 |       } | 
|  | 99 | +      DataCaptureUtils.captureBody( | 
|  | 100 | +          span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset); | 
| 110 | 101 |     } | 
| 111 | 102 | 
 | 
| 112 | 103 |     ctx.fireChannelRead(msg); | 
|  | 
0 commit comments