Add scopes to http server response (DataDog/dd-trace-java#1408)

This commit is contained in:
Nikolay Martynov 2020-04-29 14:05:30 -04:00 committed by Trask Stalnaker
parent 6f472a62a0
commit c63b4fd9a3
3 changed files with 44 additions and 30 deletions

View File

@ -15,9 +15,13 @@
*/ */
package io.opentelemetry.auto.instrumentation.netty.v3_8.server; package io.opentelemetry.auto.instrumentation.netty.v3_8.server;
import static io.opentelemetry.auto.instrumentation.netty.v3_8.server.NettyHttpServerDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.netty.v3_8.server.NettyHttpServerDecorator.TRACER;
import io.opentelemetry.auto.bootstrap.ContextStore; import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.instrumentation.api.Tags; import io.opentelemetry.auto.instrumentation.api.Tags;
import io.opentelemetry.auto.instrumentation.netty.v3_8.ChannelTraceContext; import io.opentelemetry.auto.instrumentation.netty.v3_8.ChannelTraceContext;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
@ -45,18 +49,20 @@ public class HttpServerResponseTracingHandler extends SimpleChannelDownstreamHan
return; return;
} }
try (final Scope scope = TRACER.withSpan(span)) {
final HttpResponse response = (HttpResponse) msg.getMessage(); final HttpResponse response = (HttpResponse) msg.getMessage();
try { try {
ctx.sendDownstream(msg); ctx.sendDownstream(msg);
} catch (final Throwable throwable) { } catch (final Throwable throwable) {
NettyHttpServerDecorator.DECORATE.onError(span, throwable); DECORATE.onError(span, throwable);
span.setAttribute(Tags.HTTP_STATUS, 500); span.setAttribute(Tags.HTTP_STATUS, 500);
span.end(); // Finish the span manually since finishSpanOnClose was false span.end(); // Finish the span manually since finishSpanOnClose was false
throw throwable; throw throwable;
} }
NettyHttpServerDecorator.DECORATE.onResponse(span, response); DECORATE.onResponse(span, response);
NettyHttpServerDecorator.DECORATE.beforeFinish(span); DECORATE.beforeFinish(span);
span.end(); // Finish the span manually since finishSpanOnClose was false span.end(); // Finish the span manually since finishSpanOnClose was false
} }
} }
}

View File

@ -16,6 +16,7 @@
package io.opentelemetry.auto.instrumentation.netty.v4_0.server; package io.opentelemetry.auto.instrumentation.netty.v4_0.server;
import static io.opentelemetry.auto.instrumentation.netty.v4_0.server.NettyHttpServerDecorator.DECORATE; import static io.opentelemetry.auto.instrumentation.netty.v4_0.server.NettyHttpServerDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.netty.v4_0.server.NettyHttpServerDecorator.TRACER;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelOutboundHandlerAdapter;
@ -23,6 +24,7 @@ import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.opentelemetry.auto.instrumentation.api.Tags; import io.opentelemetry.auto.instrumentation.api.Tags;
import io.opentelemetry.auto.instrumentation.netty.v4_0.AttributeKeys; import io.opentelemetry.auto.instrumentation.netty.v4_0.AttributeKeys;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span;
public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdapter { public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdapter {
@ -35,6 +37,7 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
return; return;
} }
try (final Scope scope = TRACER.withSpan(span)) {
final HttpResponse response = (HttpResponse) msg; final HttpResponse response = (HttpResponse) msg;
try { try {
@ -50,3 +53,4 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
span.end(); // Finish the span manually since finishSpanOnClose was false span.end(); // Finish the span manually since finishSpanOnClose was false
} }
} }
}

View File

@ -16,6 +16,7 @@
package io.opentelemetry.auto.instrumentation.netty.v4_1.server; package io.opentelemetry.auto.instrumentation.netty.v4_1.server;
import static io.opentelemetry.auto.instrumentation.netty.v4_1.server.NettyHttpServerDecorator.DECORATE; import static io.opentelemetry.auto.instrumentation.netty.v4_1.server.NettyHttpServerDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.netty.v4_1.server.NettyHttpServerDecorator.TRACER;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelOutboundHandlerAdapter;
@ -23,6 +24,7 @@ import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.opentelemetry.auto.instrumentation.api.Tags; import io.opentelemetry.auto.instrumentation.api.Tags;
import io.opentelemetry.auto.instrumentation.netty.v4_1.AttributeKeys; import io.opentelemetry.auto.instrumentation.netty.v4_1.AttributeKeys;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span;
public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdapter { public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdapter {
@ -35,6 +37,7 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
return; return;
} }
try (final Scope scope = TRACER.withSpan(span)) {
final HttpResponse response = (HttpResponse) msg; final HttpResponse response = (HttpResponse) msg;
try { try {
@ -50,3 +53,4 @@ public class HttpServerResponseTracingHandler extends ChannelOutboundHandlerAdap
span.end(); // Finish the span manually since finishSpanOnClose was false span.end(); // Finish the span manually since finishSpanOnClose was false
} }
} }
}