Skip to content

Capture buffers in netty 4.0, small cleanup #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMessage;
Expand All @@ -35,7 +36,7 @@ public static void captureBody(
Span span,
Channel channel,
AttributeKey<BoundedByteArrayOutputStream> attributeKey,
HttpContent httpContent) {
Object httpContentOrBuffer) {

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

final ByteArrayOutputStream finalBuffer = buffer;
httpContent
.content()
.forEachByte(
value -> {
finalBuffer.write(value);
return true;
});
ByteBuf content = castToBuf(httpContentOrBuffer);
if (content != null && content.isReadable()) {
final ByteArrayOutputStream finalBuffer = buffer;
content.forEachByte(
value -> {
finalBuffer.write(value);
return true;
});
}

if (httpContent instanceof LastHttpContent) {
if (httpContentOrBuffer instanceof LastHttpContent) {
bufferAttr.remove();
try {
span.setAttribute(attributeKey.name(), buffer.toStringWithSuppliedCharset());
Expand All @@ -63,6 +65,16 @@ public static void captureBody(
}
}

private static ByteBuf castToBuf(Object msg) {
if (msg instanceof ByteBuf) {
return (ByteBuf) msg;
} else if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
return httpContent.content();
}
return null;
}

// see io.netty.handler.codec.http.HttpUtil
public static CharSequence getContentType(HttpMessage message) {
return message.headers().get("content-type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {

if (msg instanceof HttpContent
&& agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) {
DataCaptureUtils.captureBody(
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, (HttpContent) msg);
DataCaptureUtils.captureBody(span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg);
}

ctx.fireChannelRead(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) {

if (msg instanceof HttpContent
&& agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
DataCaptureUtils.captureBody(
span, ctx.channel(), AttributeKeys.RESPONSE_BODY_BUFFER, (HttpContent) msg);
DataCaptureUtils.captureBody(span, ctx.channel(), AttributeKeys.RESPONSE_BODY_BUFFER, msg);
}

try (Scope ignored = context.makeCurrent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.Attribute;
Expand Down Expand Up @@ -77,10 +78,10 @@ private static ByteBuf castToBuf(Object msg) {

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

public static CharSequence getContentLength(HttpMessage message) {
return message.headers().get("content-length");
return message.headers().get(HttpHeaderNames.CONTENT_LENGTH);
}
}