Skip to content

Commit b33528c

Browse files
committed
fix netty timeout tests
1 parent ce77411 commit b33528c

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

dd-java-agent/instrumentation/netty/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientRequestTracingHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static datadog.trace.instrumentation.netty40.client.NettyResponseInjectAdapter.SETTER;
1515

1616
import datadog.context.Context;
17-
import datadog.context.ContextScope;
1817
import datadog.trace.api.Config;
1918
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2019
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
@@ -79,10 +78,7 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann
7978
NettyHttpClientDecorator decorate = isSecure ? DECORATE_SECURE : DECORATE;
8079

8180
final AgentSpan span = startSpan("netty", NETTY_CLIENT_REQUEST);
82-
final Context spanContext;
83-
try (final ContextScope contextScope = getCurrentContext().with(span).attach()) {
84-
spanContext = contextScope.context();
85-
}
81+
final Context spanContext = getCurrentContext().with(span);
8682
try (final AgentScope scope = activateSpan(span)) {
8783
decorate.afterStart(span);
8884
decorate.onRequest(span, request);

dd-java-agent/instrumentation/netty/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientResponseTracingHandler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static datadog.trace.instrumentation.netty40.client.NettyHttpClientDecorator.DECORATE;
99

1010
import datadog.context.Context;
11-
import datadog.context.ContextScope;
1211
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1312
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1413
import io.netty.channel.ChannelHandler;
@@ -31,8 +30,8 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
3130
final AgentSpan span = spanFromContext(storedContext);
3231

3332
// Set parent context back to maintain the same functionality as getAndSet(parent)
34-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
35-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
33+
if (storedContext != null) {
34+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
3635
}
3736

3837
final boolean finishSpan = msg instanceof HttpResponse;
@@ -60,8 +59,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
6059
final AgentSpan span = spanFromContext(storedContext);
6160

6261
// Set parent context back to maintain the same functionality as getAndSet(parent)
63-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
64-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
62+
if (storedContext != null) {
63+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
6564
}
6665

6766
if (span != null) {
@@ -88,8 +87,8 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
8887
final AgentSpan span = spanFromContext(storedContext);
8988

9089
// Set parent context back to maintain the same functionality as getAndSet(parent)
91-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
92-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
90+
if (storedContext != null) {
91+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
9392
}
9493

9594
if (span != null && span != parent) {

dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static datadog.trace.instrumentation.netty41.client.NettyResponseInjectAdapter.SETTER;
1515

1616
import datadog.context.Context;
17-
import datadog.context.ContextScope;
1817
import datadog.trace.api.Config;
1918
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2019
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
@@ -80,10 +79,7 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann
8079
NettyHttpClientDecorator decorate = isSecure ? DECORATE_SECURE : DECORATE;
8180

8281
final AgentSpan span = startSpan("netty", NETTY_CLIENT_REQUEST);
83-
final Context spanContext;
84-
try (final ContextScope contextScope = getCurrentContext().with(span).attach()) {
85-
spanContext = contextScope.context();
86-
}
82+
final Context spanContext = getCurrentContext().with(span);
8783
try (final AgentScope scope = activateSpan(span)) {
8884
decorate.afterStart(span);
8985
decorate.onRequest(span, request);

dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientResponseTracingHandler.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.trace.instrumentation.netty41.client;
22

3-
import static datadog.context.Context.current;
43
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
54
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
65
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
@@ -9,7 +8,6 @@
98
import static datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator.DECORATE;
109

1110
import datadog.context.Context;
12-
import datadog.context.ContextScope;
1311
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1412
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1513
import io.netty.channel.ChannelHandler;
@@ -34,8 +32,8 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
3432
final AgentSpan span = spanFromContext(storedContext);
3533

3634
// Set parent context back to maintain the same functionality as getAndSet(parent)
37-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
38-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
35+
if (storedContext != null) {
36+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
3937
}
4038

4139
if (span != null) {
@@ -51,8 +49,8 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
5149
span.finish();
5250
}
5351
} else {
54-
try (final ContextScope contextScope = current().with(span).attach()) {
55-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(contextScope.context());
52+
if (storedContext != null) {
53+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(span));
5654
}
5755
}
5856
}
@@ -72,8 +70,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
7270
final AgentSpan span = spanFromContext(storedContext);
7371

7472
// Set parent context back to maintain the same functionality as getAndSet(parent)
75-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
76-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
73+
if (storedContext != null) {
74+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
7775
}
7876

7977
if (span != null) {
@@ -100,8 +98,8 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
10098
final AgentSpan span = spanFromContext(storedContext);
10199

102100
// Set parent context back to maintain the same functionality as getAndSet(parent)
103-
try (final ContextScope parentScope = storedContext.with(parent).attach()) {
104-
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(parentScope.context());
101+
if (storedContext != null) {
102+
ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent));
105103
}
106104

107105
if (span != null && span != parent) {

0 commit comments

Comments
 (0)