HTTP client instrumentation cleanup: netty
MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java
@@ -7,13 +7,10 @@ package io.opentelemetry.javaagent.instrumentation.netty.v3_8.client;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.client.NettyHttpClientTracer.tracer;
-import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
-import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.netty.v3_8.ChannelTraceContext;
-import java.net.InetSocketAddress;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
@@ -29,9 +26,10 @@ public class HttpClientRequestTracingHandler extends SimpleChannelDownstreamHand
}
@Override
- public void writeRequested(ChannelHandlerContext ctx, MessageEvent msg) {
- if (!(msg.getMessage() instanceof HttpRequest)) {
- ctx.sendDownstream(msg);
+ public void writeRequested(ChannelHandlerContext ctx, MessageEvent event) {
+ Object message = event.getMessage();
+ if (!(message instanceof HttpRequest)) {
+ ctx.sendDownstream(event);
return;
}
@@ -46,22 +44,16 @@ public class HttpClientRequestTracingHandler extends SimpleChannelDownstreamHand
}
if (!tracer().shouldStartSpan(parentContext)) {
- ctx.sendDownstream(msg);
+ ctx.sendDownstream(event);
return;
}
- channelTraceContext.setClientParentContext(parentContext);
-
- HttpRequest request = (HttpRequest) msg.getMessage();
-
- Context context = tracer().startSpan(parentContext, request, request.headers());
- // TODO (trask) move this setNetPeer() call into the Tracer
- NetPeerUtils.INSTANCE.setNetPeer(
- Span.fromContext(context), (InetSocketAddress) ctx.getChannel().getRemoteAddress());
+ Context context = tracer().startSpan(parentContext, ctx, (HttpRequest) message);
channelTraceContext.setContext(context);
+ channelTraceContext.setClientParentContext(parentContext);
try (Scope ignored = context.makeCurrent()) {
- ctx.sendDownstream(msg);
+ ctx.sendDownstream(event);
} catch (Throwable throwable) {
tracer().endExceptionally(context, throwable);
throw throwable;

MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientResponseTracingHandler.java
@@ -30,16 +30,18 @@ public class HttpClientResponseTracingHandler extends SimpleChannelUpstreamHandl
ChannelTraceContext channelTraceContext =
contextStore.putIfAbsent(ctx.getChannel(), ChannelTraceContext.Factory.INSTANCE);
- Context parentContext = channelTraceContext.getClientParentContext();
Context context = channelTraceContext.getContext();
+ if (context == null) {
+ ctx.sendUpstream(msg);
+ return;
+ }
- boolean finishSpan = msg.getMessage() instanceof HttpResponse;
-
- if (context != null && finishSpan) {
+ if (msg.getMessage() instanceof HttpResponse) {
tracer().end(context, (HttpResponse) msg.getMessage());
}
// We want the callback in the scope of the parent, not the client span
+ Context parentContext = channelTraceContext.getClientParentContext();
if (parentContext != null) {
try (Scope ignored = parentContext.makeCurrent()) {
ctx.sendUpstream(msg);

MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientTracer.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientTracer.java
package io.opentelemetry.javaagent.instrumentation.netty.v3_8.client;
+import static io.opentelemetry.api.trace.Span.Kind.CLIENT;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.client.NettyResponseInjectAdapter.SETTER;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.HOST;
-import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
+import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
+import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import org.checkerframework.checker.nullness.qual.Nullable;
+import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
@@ -25,6 +32,23 @@ public class NettyHttpClientTracer
return TRACER;
}
+ public Context startSpan(Context parentContext, ChannelHandlerContext ctx, HttpRequest request) {
+ Span span =
+ tracer
+ .spanBuilder(spanNameForRequest(request))
+ .setSpanKind(CLIENT)
+ .setParent(parentContext)
+ .startSpan();
+ onRequest(span, request);
+ NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.getChannel().getRemoteAddress());
+
+ Context context = withClientSpan(parentContext, span);
+ OpenTelemetry.getGlobalPropagators()
+ .getTextMapPropagator()
+ .inject(context, request.headers(), SETTER);
+ return context;
+ }
+
@Override
protected String method(HttpRequest httpRequest) {
return httpRequest.getMethod().getName();
@@ -61,7 +85,7 @@ public class NettyHttpClientTracer
}
@Override
- protected Setter<HttpHeaders> getSetter() {
+ protected TextMapPropagator.Setter<HttpHeaders> getSetter() {
return SETTER;
}

MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java
@@ -26,28 +26,29 @@ public class HttpServerRequestTracingHandler extends SimpleChannelUpstreamHandle
}
@Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent msg) {
+ public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
ChannelTraceContext channelTraceContext =
contextStore.putIfAbsent(ctx.getChannel(), ChannelTraceContext.Factory.INSTANCE);
- if (!(msg.getMessage() instanceof HttpRequest)) {
+ Object message = event.getMessage();
+ if (!(message instanceof HttpRequest)) {
Context serverContext = tracer().getServerContext(channelTraceContext);
if (serverContext == null) {
- ctx.sendUpstream(msg);
+ ctx.sendUpstream(event);
} else {
try (Scope ignored = serverContext.makeCurrent()) {
- ctx.sendUpstream(msg);
+ ctx.sendUpstream(event);
}
}
return;
}
- HttpRequest request = (HttpRequest) msg.getMessage();
+ HttpRequest request = (HttpRequest) message;
Context context =
tracer().startSpan(request, ctx.getChannel(), channelTraceContext, "netty.request");
try (Scope ignored = context.makeCurrent()) {
- ctx.sendUpstream(msg);
+ ctx.sendUpstream(event);
// the span is ended normally in HttpServerResponseTracingHandler
} catch (Throwable throwable) {
tracer().endExceptionally(context, throwable);

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
@@ -30,8 +30,8 @@ public class AttributeKeys {
public static final AttributeKey<Context> SERVER_SPAN =
attributeKey(AttributeKeys.class.getName() + ".server-span");
- public static final AttributeKey<Context> CLIENT_SPAN =
- attributeKey(AttributeKeys.class.getName() + ".client-span");
+ 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");

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
@@ -11,12 +11,9 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
-import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
-import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
import io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys;
-import java.net.InetSocketAddress;
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
@@ -37,16 +34,10 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
return;
}
- HttpRequest request = (HttpRequest) msg;
-
+ Context context = tracer().startSpan(parentContext, ctx, (HttpRequest) msg);
+ ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT).set(context);
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).set(parentContext);
- Context context = tracer().startSpan(parentContext, request, request.headers());
- // TODO (trask) move this setNetPeer() call into the Tracer
- NetPeerUtils.INSTANCE.setNetPeer(
- Span.fromContext(context), (InetSocketAddress) ctx.channel().remoteAddress());
- ctx.channel().attr(AttributeKeys.CLIENT_SPAN).set(context);
-
try (Scope ignored = context.makeCurrent()) {
ctx.write(msg, prm);
} catch (Throwable throwable) {

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
@@ -19,17 +19,19 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
- Attribute<Context> parentAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
- Context parentContext = parentAttr.get();
- Context context = ctx.channel().attr(AttributeKeys.CLIENT_SPAN).get();
-
- boolean finishSpan = msg instanceof HttpResponse;
+ Context context = ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT).get();
+ if (context == null) {
+ ctx.fireChannelRead(msg);
+ return;
+ }
- if (context != null && finishSpan) {
+ if (msg instanceof HttpResponse) {
tracer().end(context, (HttpResponse) msg);
}
// We want the callback in the scope of the parent, not the client span
+ Attribute<Context> parentAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
+ Context parentContext = parentAttr.get();
if (parentContext != null) {
try (Scope ignored = parentContext.makeCurrent()) {
ctx.fireChannelRead(msg);

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/NettyHttpClientTracer.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/NettyHttpClientTracer.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_0.client;
import static io.netty.handler.codec.http.HttpHeaders.Names.HOST;
+import static io.opentelemetry.api.trace.Span.Kind.CLIENT;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyResponseInjectAdapter.SETTER;
+import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
-import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
+import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
+import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -25,6 +32,23 @@ public class NettyHttpClientTracer
return TRACER;
}
+ public Context startSpan(Context parentContext, ChannelHandlerContext ctx, HttpRequest request) {
+ Span span =
+ tracer
+ .spanBuilder(spanNameForRequest(request))
+ .setSpanKind(CLIENT)
+ .setParent(parentContext)
+ .startSpan();
+ onRequest(span, request);
+ NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.channel().remoteAddress());
+
+ Context context = withClientSpan(parentContext, span);
+ OpenTelemetry.getGlobalPropagators()
+ .getTextMapPropagator()
+ .inject(context, request.headers(), SETTER);
+ return context;
+ }
+
@Override
protected String method(HttpRequest httpRequest) {
return httpRequest.getMethod().name();
@@ -61,7 +85,7 @@ public class NettyHttpClientTracer
}
@Override
- protected Setter<HttpHeaders> getSetter() {
+ protected TextMapPropagator.Setter<HttpHeaders> getSetter() {
return SETTER;
}

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/AttributeKeys.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/AttributeKeys.java
@@ -19,8 +19,8 @@ public class AttributeKeys {
public static final AttributeKey<Context> SERVER_SPAN =
AttributeKey.valueOf(AttributeKeys.class, "server-span");
- public static final AttributeKey<Context> CLIENT_SPAN =
- AttributeKey.valueOf(AttributeKeys.class, "client-span");
+ public static final AttributeKey<Context> CLIENT_CONTEXT =
+ AttributeKey.valueOf(AttributeKeys.class, "client-context");
public static final AttributeKey<Context> CLIENT_PARENT_CONTEXT =
AttributeKey.valueOf(AttributeKeys.class, "client-parent-context");

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientRequestTracingHandler.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientRequestTracingHandler.java
@@ -11,12 +11,9 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
-import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
-import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
import io.opentelemetry.javaagent.instrumentation.netty.v4_1.AttributeKeys;
-import java.net.InetSocketAddress;
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
@@ -26,26 +23,21 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
ctx.write(msg, prm);
return;
}
- HttpRequest request = (HttpRequest) msg;
Context parentContext = ctx.channel().attr(AttributeKeys.CONNECT_CONTEXT).getAndRemove();
if (parentContext == null) {
parentContext = Context.current();
}
- if (!tracer().shouldStartSpan(parentContext, request)) {
+ if (!tracer().shouldStartSpan(parentContext, (HttpRequest) msg)) {
ctx.write(msg, prm);
return;
}
+ Context context = tracer().startSpan(parentContext, ctx, (HttpRequest) msg);
+ ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT).set(context);
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).set(parentContext);
- Context context = tracer().startSpan(parentContext, request, request.headers());
- // TODO (trask) move this setNetPeer() call into the Tracer
- NetPeerUtils.INSTANCE.setNetPeer(
- Span.fromContext(context), (InetSocketAddress) ctx.channel().remoteAddress());
- ctx.channel().attr(AttributeKeys.CLIENT_SPAN).set(context);
-
try (Scope ignored = context.makeCurrent()) {
ctx.write(msg, prm);
// span is ended normally in HttpClientResponseTracingHandler

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientResponseTracingHandler.java
@@ -19,17 +19,19 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
- Attribute<Context> parentAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
- Context parentContext = parentAttr.get();
- Context context = ctx.channel().attr(AttributeKeys.CLIENT_SPAN).get();
-
- boolean finishSpan = msg instanceof HttpResponse;
+ Context context = ctx.channel().attr(AttributeKeys.CLIENT_CONTEXT).get();
+ if (context == null) {
+ ctx.fireChannelRead(msg);
+ return;
+ }
- if (context != null && finishSpan) {
+ if (msg instanceof HttpResponse) {
tracer().end(context, (HttpResponse) msg);
}
// We want the callback in the scope of the parent, not the client span
+ Attribute<Context> parentAttr = ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT);
+ Context parentContext = parentAttr.get();
if (parentContext != null) {
try (Scope ignored = parentContext.makeCurrent()) {
ctx.fireChannelRead(msg);

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/NettyHttpClientTracer.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/NettyHttpClientTracer.java
package io.opentelemetry.javaagent.instrumentation.netty.v4_1.client;
import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
+import static io.opentelemetry.api.trace.Span.Kind.CLIENT;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.client.NettyResponseInjectAdapter.SETTER;
+import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
-import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
+import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
+import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
+import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -26,6 +32,23 @@ public class NettyHttpClientTracer
return TRACER;
}
+ public Context startSpan(Context parentContext, ChannelHandlerContext ctx, HttpRequest request) {
+ Span span =
+ tracer
+ .spanBuilder(spanNameForRequest(request))
+ .setSpanKind(CLIENT)
+ .setParent(parentContext)
+ .startSpan();
+ onRequest(span, request);
+ NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.channel().remoteAddress());
+
+ Context context = withClientSpan(parentContext, span);
+ OpenTelemetry.getGlobalPropagators()
+ .getTextMapPropagator()
+ .inject(context, request.headers(), SETTER);
+ return context;
+ }
+
@Override
protected String method(HttpRequest httpRequest) {
return httpRequest.method().name();
@@ -62,7 +85,7 @@ public class NettyHttpClientTracer
}
@Override
- protected Setter<HttpHeaders> getSetter() {
+ protected TextMapPropagator.Setter<HttpHeaders> getSetter() {
return SETTER;
}
@@ -84,6 +107,15 @@ public class NettyHttpClientTracer
return true;
}
+ // TODO (trask) how best to prevent people from use this one instead of the above?
+ // should all shouldStartSpan methods take REQUEST so that they can be suppressed by REQUEST
+ // attributes?
+ @Override
+ @Deprecated
+ public boolean shouldStartSpan(Context parentContext) {
+ throw new UnsupportedOperationException();
+ }
+
@Override
protected String getInstrumentationName() {
return "io.opentelemetry.javaagent.netty";

HTTP client instrumentation cleanup: netty (#1909)