Netty refactoring
~Built on top of other netty PRs, will rebase after those are merged.~
MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/AbstractChannelHandlerContextInstrumentation.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/AbstractChannelHandlerContextInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_0;
+import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.HttpClientRequestTracingHandler.HTTP_CLIENT_REQUEST;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -54,8 +55,7 @@ public class AbstractChannelHandlerContextInstrumentation implements TypeInstrum
if (clientContext != null) {
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).remove();
contextAttr.remove();
- HttpRequestAndChannel request =
- ctx.channel().attr(AttributeKeys.CLIENT_REQUEST).getAndRemove();
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndRemove();
instrumenter().end(clientContext, request, null, throwable);
return;
}

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/AttributeKeys.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/AttributeKeys.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_0;
-import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.AttributeKey;
import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -26,21 +24,12 @@ public class AttributeKeys {
public static final AttributeKey<Context> SERVER_CONTEXT =
attributeKey(AttributeKeys.class.getName() + ".server-context");
- public static final AttributeKey<HttpRequestAndChannel> SERVER_REQUEST =
- attributeKey(AttributeKeys.class.getName() + ".http-server-request");
-
public static final AttributeKey<Context> CLIENT_CONTEXT =
attributeKey(AttributeKeys.class.getName() + ".client-context");
public static final AttributeKey<Context> CLIENT_PARENT_CONTEXT =
attributeKey(AttributeKeys.class.getName() + ".client-parent-context");
- public static final AttributeKey<HttpRequestAndChannel> CLIENT_REQUEST =
- attributeKey(AttributeKeys.class.getName() + ".http-client-request");
-
- public static final AttributeKey<HttpResponse> CLIENT_RESPONSE =
- attributeKey(AttributeKeys.class.getName() + ".http-client-response");
-
/**
* Generate an attribute key or reuse the one existing in the global app map. This implementation
* creates attributes only once even if the current class is loaded by several class loaders and

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java
@@ -12,6 +12,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.Attribute;
+import io.netty.util.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
@@ -19,6 +20,9 @@ import io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys;
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
+ public static final AttributeKey<HttpRequestAndChannel> HTTP_CLIENT_REQUEST =
+ AttributeKeys.attributeKey(AttributeKeys.class.getName() + ".http-client-request");
+
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) throws Exception {
if (!(msg instanceof HttpRequest)) {
@@ -36,7 +40,7 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
Attribute<Context> parentContextAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
Attribute<Context> contextAttr = ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(AttributeKeys.CLIENT_REQUEST);
+ Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(HTTP_CLIENT_REQUEST);
Context context = instrumenter().start(parentContext, request);
parentContextAttr.set(parentContext);

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientResponseTracingHandler.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_0.client;
+import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.HttpClientRequestTracingHandler.HTTP_CLIENT_REQUEST;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyClientSingletons.instrumenter;
import io.netty.channel.ChannelHandlerContext;
@@ -13,6 +14,7 @@ import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.Attribute;
+import io.netty.util.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
@@ -20,6 +22,9 @@ import io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys;
public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapter {
+ private static final AttributeKey<HttpResponse> HTTP_CLIENT_RESPONSE =
+ AttributeKeys.attributeKey(AttributeKeys.class.getName() + ".http-client-response");
+
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Attribute<Context> contextAttr = ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT);
@@ -30,31 +35,24 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
}
Attribute<Context> parentContextAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(AttributeKeys.CLIENT_REQUEST);
-
Context parentContext = parentContextAttr.get();
- HttpRequestAndChannel request = requestAttr.get();
if (msg instanceof FullHttpResponse) {
- parentContextAttr.remove();
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndSet(null);
+ instrumenter().end(context, request, (HttpResponse) msg, null);
contextAttr.remove();
- requestAttr.remove();
+ parentContextAttr.remove();
} else if (msg instanceof HttpResponse) {
// Headers before body have been received, store them to use when finishing the span.
- ctx.channel().attr(AttributeKeys.CLIENT_RESPONSE).set((HttpResponse) msg);
- } else if (msg instanceof LastHttpContent) {
- // Not a FullHttpResponse so this is content that has been received after headers. Finish the
- // span using what we stored in attrs.
- parentContextAttr.remove();
- contextAttr.remove();
- requestAttr.remove();
- }
-
- if (msg instanceof FullHttpResponse) {
- instrumenter().end(context, request, (HttpResponse) msg, null);
+ ctx.channel().attr(HTTP_CLIENT_RESPONSE).set((HttpResponse) msg);
} else if (msg instanceof LastHttpContent) {
- HttpResponse response = ctx.channel().attr(AttributeKeys.CLIENT_RESPONSE).getAndRemove();
+ // Not a FullHttpResponse so this is content that has been received after headers.
+ // Finish the span using what we stored in attrs.
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndSet(null);
+ HttpResponse response = ctx.channel().attr(HTTP_CLIENT_RESPONSE).getAndRemove();
instrumenter().end(context, request, response, null);
+ contextAttr.remove();
+ parentContextAttr.remove();
}
// We want the callback in the scope of the parent, not the client span

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java
@@ -12,6 +12,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.Attribute;
+import io.netty.util.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
@@ -19,11 +20,14 @@ import io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys;
public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapter {
+ static final AttributeKey<HttpRequestAndChannel> HTTP_SERVER_REQUEST =
+ AttributeKeys.attributeKey(AttributeKeys.class.getName() + ".http-server-request");
+
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel channel = ctx.channel();
Attribute<Context> contextAttr = channel.attr(AttributeKeys.SERVER_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = channel.attr(AttributeKeys.SERVER_REQUEST);
+ Attribute<HttpRequestAndChannel> requestAttr = channel.attr(HTTP_SERVER_REQUEST);
if (!(msg instanceof HttpRequest)) {
Context serverContext = contextAttr.get();

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_0.server;
+import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.server.HttpServerRequestTracingHandler.HTTP_SERVER_REQUEST;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.server.NettyServerSingletons.instrumenter;
import io.netty.channel.Channel;
@@ -41,7 +42,7 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
// make sure to remove the server context on end() call
private static void end(Channel channel, HttpResponse response, @Nullable Throwable error) {
Context context = channel.attr(AttributeKeys.SERVER_CONTEXT).getAndRemove();
- HttpRequestAndChannel request = channel.attr(AttributeKeys.SERVER_REQUEST).getAndRemove();
+ HttpRequestAndChannel request = channel.attr(HTTP_SERVER_REQUEST).getAndRemove();
error = NettyErrorHolder.getOrDefault(context, error);
instrumenter().end(context, request, response, error);
}

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/AbstractChannelHandlerContextInstrumentation.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/AbstractChannelHandlerContextInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_1;
-import static io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientRequestTracingHandler.HTTP_REQUEST;
+import static io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientRequestTracingHandler.HTTP_CLIENT_REQUEST;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -52,7 +52,7 @@ public class AbstractChannelHandlerContextInstrumentation implements TypeInstrum
if (clientContext != null) {
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).remove();
contextAttr.remove();
- HttpRequestAndChannel request = ctx.channel().attr(HTTP_REQUEST).getAndRemove();
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndRemove();
instrumenter().end(clientContext, request, null, throwable);
return;
}

MODIFY
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java
@@ -24,7 +24,7 @@ import io.opentelemetry.instrumentation.netty.v4_1.internal.AttributeKeys;
*/
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
- public static final AttributeKey<HttpRequestAndChannel> HTTP_REQUEST =
+ public static final AttributeKey<HttpRequestAndChannel> HTTP_CLIENT_REQUEST =
AttributeKey.valueOf(HttpClientRequestTracingHandler.class, "http-client-request");
private final Instrumenter<HttpRequestAndChannel, HttpResponse> instrumenter;
@@ -54,7 +54,7 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
Attribute<Context> parentContextAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
Attribute<Context> contextAttr = ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(HTTP_REQUEST);
+ Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(HTTP_CLIENT_REQUEST);
Context context = instrumenter.start(parentContext, request);
parentContextAttr.set(parentContext);

MODIFY
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientResponseTracingHandler.java
package io.opentelemetry.instrumentation.netty.v4_1.internal.client;
-import static io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientRequestTracingHandler.HTTP_REQUEST;
+import static io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientRequestTracingHandler.HTTP_CLIENT_REQUEST;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -26,7 +26,7 @@ import io.opentelemetry.instrumentation.netty.v4_1.internal.AttributeKeys;
*/
public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapter {
- private static final AttributeKey<HttpResponse> HTTP_RESPONSE =
+ private static final AttributeKey<HttpResponse> HTTP_CLIENT_RESPONSE =
AttributeKey.valueOf(HttpClientResponseTracingHandler.class, "http-client-response");
private final Instrumenter<HttpRequestAndChannel, HttpResponse> instrumenter;
@@ -46,30 +46,24 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
}
Attribute<Context> parentContextAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = ctx.channel().attr(HTTP_REQUEST);
-
Context parentContext = parentContextAttr.get();
- HttpRequestAndChannel request = requestAttr.get();
if (msg instanceof FullHttpResponse) {
- parentContextAttr.set(null);
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndSet(null);
+ instrumenter.end(context, request, (HttpResponse) msg, null);
contextAttr.set(null);
- requestAttr.set(null);
+ parentContextAttr.set(null);
} else if (msg instanceof HttpResponse) {
// Headers before body have been received, store them to use when finishing the span.
- ctx.channel().attr(HTTP_RESPONSE).set((HttpResponse) msg);
+ ctx.channel().attr(HTTP_CLIENT_RESPONSE).set((HttpResponse) msg);
} else if (msg instanceof LastHttpContent) {
- // Not a FullHttpResponse so this is content that has been received after headers. Finish the
- // span using what we stored in attrs.
- parentContextAttr.set(null);
+ // Not a FullHttpResponse so this is content that has been received after headers.
+ // Finish the span using what we stored in attrs.
+ HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndSet(null);
+ HttpResponse response = ctx.channel().attr(HTTP_CLIENT_RESPONSE).getAndSet(null);
+ instrumenter.end(context, request, response, null);
contextAttr.set(null);
- requestAttr.set(null);
- }
-
- if (msg instanceof FullHttpResponse) {
- instrumenter.end(context, request, (HttpResponse) msg, null);
- } else if (msg instanceof LastHttpContent) {
- instrumenter.end(context, request, ctx.channel().attr(HTTP_RESPONSE).getAndSet(null), null);
+ parentContextAttr.set(null);
}
// We want the callback in the scope of the parent, not the client span

MODIFY
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java
@@ -24,7 +24,7 @@ import io.opentelemetry.instrumentation.netty.v4_1.internal.AttributeKeys;
*/
public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapter {
- static final AttributeKey<HttpRequestAndChannel> HTTP_REQUEST =
+ static final AttributeKey<HttpRequestAndChannel> HTTP_SERVER_REQUEST =
AttributeKey.valueOf(HttpServerRequestTracingHandler.class, "http-server-request");
private final Instrumenter<HttpRequestAndChannel, HttpResponse> instrumenter;
@@ -38,7 +38,7 @@ public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapte
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel channel = ctx.channel();
Attribute<Context> contextAttr = channel.attr(AttributeKeys.SERVER_CONTEXT);
- Attribute<HttpRequestAndChannel> requestAttr = channel.attr(HTTP_REQUEST);
+ Attribute<HttpRequestAndChannel> requestAttr = channel.attr(HTTP_SERVER_REQUEST);
if (!(msg instanceof HttpRequest)) {
Context serverContext = contextAttr.get();

MODIFY
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerResponseTracingHandler.java
instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerResponseTracingHandler.java
package io.opentelemetry.instrumentation.netty.v4_1.internal.server;
-import static io.opentelemetry.instrumentation.netty.v4_1.internal.server.HttpServerRequestTracingHandler.HTTP_REQUEST;
+import static io.opentelemetry.instrumentation.netty.v4_1.internal.server.HttpServerRequestTracingHandler.HTTP_SERVER_REQUEST;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
@@ -31,7 +31,7 @@ import javax.annotation.Nullable;
*/
public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdapter {
- static final AttributeKey<HttpResponse> HTTP_RESPONSE =
+ private static final AttributeKey<HttpResponse> HTTP_SERVER_RESPONSE =
AttributeKey.valueOf(HttpServerResponseTracingHandler.class, "http-server-response");
private final Instrumenter<HttpRequestAndChannel, HttpResponse> instrumenter;
@@ -75,14 +75,14 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
future ->
end(
ctx.channel(),
- ctx.channel().attr(HTTP_RESPONSE).getAndSet(null),
+ ctx.channel().attr(HTTP_SERVER_RESPONSE).getAndSet(null),
writePromise));
}
} else {
writePromise = prm;
if (msg instanceof HttpResponse) {
// Headers before body has been sent, store them to use when finishing the span.
- ctx.channel().attr(HTTP_RESPONSE).set((HttpResponse) msg);
+ ctx.channel().attr(HTTP_SERVER_RESPONSE).set((HttpResponse) msg);
}
}
@@ -102,7 +102,7 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
// make sure to remove the server context on end() call
private void end(Channel channel, @Nullable HttpResponse response, @Nullable Throwable error) {
Context context = channel.attr(AttributeKeys.SERVER_CONTEXT).getAndSet(null);
- HttpRequestAndChannel request = channel.attr(HTTP_REQUEST).getAndSet(null);
+ HttpRequestAndChannel request = channel.attr(HTTP_SERVER_REQUEST).getAndSet(null);
error = NettyErrorHolder.getOrDefault(context, error);
instrumenter.end(context, request, response, error);
}

Netty refactoring (#7738)

~Built on top of other netty PRs, will rebase after those are merged.~