Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ jobs:
env:
JVM_OPTS: -Xmx1g --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED
TERM: dumb

- name: Upload jar as pipeline artifact
uses: actions/upload-artifact@v4
with:
name: javaagent
path: javaagent/build/libs/*-all.jar
if-no-files-found: error
retention-days: 5
muzzle:
runs-on: ubuntu-latest
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.HttpClientRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.HttpClientResponseTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.HttpClientTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.OtelHttpClientRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.server.HttpServerBlockingRequestHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.server.HttpServerRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.server.HttpServerResponseTracingHandler;
Expand Down Expand Up @@ -136,13 +135,14 @@ public static void addHandler(
HttpClientTracingHandler.class.getName(),
new HttpClientTracingHandler());

// add our custom request handler to start spans with proper context propagation
// add OTEL request handler to start spans
pipeline.addAfter(
HttpClientTracingHandler.class.getName(),
io.opentelemetry.javaagent.instrumentation.netty.v4_0.client
.HttpClientRequestTracingHandler.class
.getName(),
new OtelHttpClientRequestTracingHandler());
new io.opentelemetry.javaagent.instrumentation.netty.v4_0.client
.HttpClientRequestTracingHandler());
} else if (handler instanceof HttpRequestEncoder) {
pipeline.addLast(
HttpClientRequestTracingHandler.class.getName(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.AttributeKeys;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.DataCaptureUtils;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.OtelHttpClientRequestTracingHandler;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -59,54 +57,47 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.fireChannelRead(msg);
return;
}
Span span = Span.fromContext(context);

// Store the server context in our ThreadLocal for later use by client handlers
// This is CRITICAL for proper context propagation to client spans
OtelHttpClientRequestTracingHandler.storeServerContext(context);
if (msg instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) msg;

try (Scope ignored = context.makeCurrent()) {
Span span = Span.fromContext(context);

if (msg instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) msg;

Map<String, String> headersMap = headersToMap(httpRequest);
if (instrumentationConfig.httpHeaders().request()) {
headersMap.forEach(span::setAttribute);
}
// used by blocking handler
channel.attr(AttributeKeys.REQUEST_HEADERS).set(headersMap);
Map<String, String> headersMap = headersToMap(httpRequest);
if (instrumentationConfig.httpHeaders().request()) {
headersMap.forEach(span::setAttribute);
}
// used by blocking handler
channel.attr(AttributeKeys.REQUEST_HEADERS).set(headersMap);

CharSequence contentType = DataCaptureUtils.getContentType(httpRequest);
if (instrumentationConfig.httpBody().request()
&& contentType != null
&& ContentTypeUtils.shouldCapture(contentType.toString())) {
CharSequence contentType = DataCaptureUtils.getContentType(httpRequest);
if (instrumentationConfig.httpBody().request()
&& contentType != null
&& ContentTypeUtils.shouldCapture(contentType.toString())) {

CharSequence contentLengthHeader = DataCaptureUtils.getContentLength(httpRequest);
int contentLength = ContentLengthUtils.parseLength(contentLengthHeader);
CharSequence contentLengthHeader = DataCaptureUtils.getContentLength(httpRequest);
int contentLength = ContentLengthUtils.parseLength(contentLengthHeader);

String charsetString = ContentTypeUtils.parseCharset(contentType.toString());
Charset charset = ContentTypeCharsetUtils.toCharset(charsetString);
String charsetString = ContentTypeUtils.parseCharset(contentType.toString());
Charset charset = ContentTypeCharsetUtils.toCharset(charsetString);

// set the buffer to capture response body
// the buffer is used byt captureBody method
Attribute<BoundedByteArrayOutputStream> bufferAttr =
ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER);
bufferAttr.set(BoundedBuffersFactory.createStream(contentLength, charset));
// set the buffer to capture response body
// the buffer is used byt captureBody method
Attribute<BoundedByteArrayOutputStream> bufferAttr =
ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER);
bufferAttr.set(BoundedBuffersFactory.createStream(contentLength, charset));

channel.attr(AttributeKeys.CHARSET).set(charset);
}
channel.attr(AttributeKeys.CHARSET).set(charset);
}
}

if ((msg instanceof HttpContent || msg instanceof ByteBuf)
&& instrumentationConfig.httpBody().request()) {
Charset charset = channel.attr(AttributeKeys.CHARSET).get();
if (charset == null) {
charset = ContentTypeCharsetUtils.getDefaultCharset();
}
DataCaptureUtils.captureBody(
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset);
if ((msg instanceof HttpContent || msg instanceof ByteBuf)
&& instrumentationConfig.httpBody().request()) {
Charset charset = channel.attr(AttributeKeys.CHARSET).get();
if (charset == null) {
charset = ContentTypeCharsetUtils.getDefaultCharset();
}
DataCaptureUtils.captureBody(
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset);
}

ctx.fireChannelRead(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.client.HttpClientRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.client.HttpClientResponseTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.client.HttpClientTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.client.OtelHttpClientRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.server.HttpServerBlockingRequestHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.server.HttpServerRequestTracingHandler;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.server.HttpServerResponseTracingHandler;
Expand Down Expand Up @@ -134,13 +133,14 @@ public static void addHandler(
HttpClientTracingHandler.class.getName(),
new HttpClientTracingHandler());

// add our custom request handler to start spans with proper context propagation
// add OTEL request handler to start spans
pipeline.addAfter(
HttpClientTracingHandler.class.getName(),
io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientTracingHandler
.class
.getName(),
new OtelHttpClientRequestTracingHandler(NettyClientSingletons.instrumenter()));
new io.opentelemetry.instrumentation.netty.v4_1.internal.client
.HttpClientRequestTracingHandler(NettyClientSingletons.instrumenter()));
} else if (handler instanceof HttpRequestEncoder) {
pipeline.addLast(
HttpClientRequestTracingHandler.class.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
span.setStatus(code >= 100 && code < 400 ? StatusCode.UNSET : StatusCode.ERROR);
}
if (msg instanceof LastHttpContent) {
// When we end the span, we should set the client context and client parent attr to null.
// so that for the next request a new context is made and stored in channel.
ctx.channel()
.attr(io.opentelemetry.instrumentation.netty.v4_1.internal.AttributeKeys.CLIENT_CONTEXT)
.set(null);
ctx.channel()
.attr(
io.opentelemetry.instrumentation.netty.v4_1.internal.AttributeKeys
.CLIENT_PARENT_CONTEXT)
.set(null);
span.end();
}
}
Expand Down
Loading
Loading