Skip to content

Commit dd0cdcf

Browse files
authored
Capture buffers in netty 4.0, small cleanup (#227)
Signed-off-by: Pavol Loffay <[email protected]>
1 parent 366cf31 commit dd0cdcf

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/server/DataCaptureUtils.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.server;
1818

19+
import io.netty.buffer.ByteBuf;
1920
import io.netty.channel.Channel;
2021
import io.netty.handler.codec.http.HttpContent;
2122
import io.netty.handler.codec.http.HttpMessage;
@@ -35,7 +36,7 @@ public static void captureBody(
3536
Span span,
3637
Channel channel,
3738
AttributeKey<BoundedByteArrayOutputStream> attributeKey,
38-
HttpContent httpContent) {
39+
Object httpContentOrBuffer) {
3940

4041
Attribute<BoundedByteArrayOutputStream> bufferAttr = channel.attr(attributeKey);
4142
BoundedByteArrayOutputStream buffer = bufferAttr.get();
@@ -44,16 +45,17 @@ public static void captureBody(
4445
return;
4546
}
4647

47-
final ByteArrayOutputStream finalBuffer = buffer;
48-
httpContent
49-
.content()
50-
.forEachByte(
51-
value -> {
52-
finalBuffer.write(value);
53-
return true;
54-
});
48+
ByteBuf content = castToBuf(httpContentOrBuffer);
49+
if (content != null && content.isReadable()) {
50+
final ByteArrayOutputStream finalBuffer = buffer;
51+
content.forEachByte(
52+
value -> {
53+
finalBuffer.write(value);
54+
return true;
55+
});
56+
}
5557

56-
if (httpContent instanceof LastHttpContent) {
58+
if (httpContentOrBuffer instanceof LastHttpContent) {
5759
bufferAttr.remove();
5860
try {
5961
span.setAttribute(attributeKey.name(), buffer.toStringWithSuppliedCharset());
@@ -63,6 +65,16 @@ public static void captureBody(
6365
}
6466
}
6567

68+
private static ByteBuf castToBuf(Object msg) {
69+
if (msg instanceof ByteBuf) {
70+
return (ByteBuf) msg;
71+
} else if (msg instanceof HttpContent) {
72+
HttpContent httpContent = (HttpContent) msg;
73+
return httpContent.content();
74+
}
75+
return null;
76+
}
77+
6678
// see io.netty.handler.codec.http.HttpUtil
6779
public static CharSequence getContentType(HttpMessage message) {
6880
return message.headers().get("content-type");

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/server/HttpServerRequestTracingHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
8585

8686
if (msg instanceof HttpContent
8787
&& agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) {
88-
DataCaptureUtils.captureBody(
89-
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, (HttpContent) msg);
88+
DataCaptureUtils.captureBody(span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg);
9089
}
9190

9291
ctx.fireChannelRead(msg);

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/server/HttpServerResponseTracingHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) {
8585

8686
if (msg instanceof HttpContent
8787
&& agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
88-
DataCaptureUtils.captureBody(
89-
span, ctx.channel(), AttributeKeys.RESPONSE_BODY_BUFFER, (HttpContent) msg);
88+
DataCaptureUtils.captureBody(span, ctx.channel(), AttributeKeys.RESPONSE_BODY_BUFFER, msg);
9089
}
9190

9291
try (Scope ignored = context.makeCurrent()) {

instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_1/server/DataCaptureUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.channel.Channel;
2121
import io.netty.handler.codec.http.HttpContent;
22+
import io.netty.handler.codec.http.HttpHeaderNames;
2223
import io.netty.handler.codec.http.HttpMessage;
2324
import io.netty.handler.codec.http.LastHttpContent;
2425
import io.netty.util.Attribute;
@@ -77,10 +78,10 @@ private static ByteBuf castToBuf(Object msg) {
7778

7879
// see io.netty.handler.codec.http.HttpUtil
7980
public static CharSequence getContentType(HttpMessage message) {
80-
return message.headers().get("content-type");
81+
return message.headers().get(HttpHeaderNames.CONTENT_TYPE);
8182
}
8283

8384
public static CharSequence getContentLength(HttpMessage message) {
84-
return message.headers().get("content-length");
85+
return message.headers().get(HttpHeaderNames.CONTENT_LENGTH);
8586
}
8687
}

0 commit comments

Comments
 (0)