Get rid of half baked map methods. Close parentScope in a finally block (+forward port)
MODIFY
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/ChannelFutureListenerInstrumentation.java
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/ChannelFutureListenerInstrumentation.java
@@ -92,7 +92,8 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
final TraceScope.Continuation continuation =
contextStore
.putIfAbsent(future.getChannel(), ChannelTraceContext.Factory.INSTANCE)
- .getConnectionContinuationAndRemove();
+ .getConnectionContinuation();
+ contextStore.get(future.getChannel()).setConnectionContinuation(null);
if (continuation == null) {
return null;
}

MODIFY
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/ChannelTraceContext.java
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/ChannelTraceContext.java
@@ -20,20 +20,4 @@ public class ChannelTraceContext {
AgentSpan serverSpan;
AgentSpan clientSpan;
AgentSpan clientParentSpan;
-
- public boolean compareAndSet(
- final TraceScope.Continuation compareTo, final TraceScope.Continuation setTo) {
- if (connectionContinuation == compareTo) {
- connectionContinuation = setTo;
- return true;
- } else {
- return false;
- }
- }
-
- public TraceScope.Continuation getConnectionContinuationAndRemove() {
- final TraceScope.Continuation current = connectionContinuation;
- connectionContinuation = null;
- return current;
- }
}

MODIFY
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/NettyChannelInstrumentation.java
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/NettyChannelInstrumentation.java
@@ -76,10 +76,13 @@ public class NettyChannelInstrumentation extends Instrumenter.Default {
final ContextStore<Channel, ChannelTraceContext> contextStore =
InstrumentationContext.get(Channel.class, ChannelTraceContext.class);
- if (!contextStore
+ if (contextStore
.putIfAbsent(channel, ChannelTraceContext.Factory.INSTANCE)
- .compareAndSet(null, continuation)) {
+ .getConnectionContinuation()
+ != null) {
continuation.close();
+ } else {
+ contextStore.get(channel).setConnectionContinuation(continuation);
}
}
}

MODIFY
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/NettyChannelPipelineInstrumentation.java
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/NettyChannelPipelineInstrumentation.java
@@ -139,8 +139,6 @@ public class NettyChannelPipelineInstrumentation extends Instrumenter.Default {
HttpClientResponseTracingHandler.class.getName(),
new HttpClientResponseTracingHandler(contextStore));
}
- } catch (final IllegalArgumentException e) {
- // Prevented adding duplicate handlers.
} finally {
CallDepthThreadLocalMap.reset(ChannelPipeline.class);
}

MODIFY
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/client/HttpClientRequestTracingHandler.java
dd-java-agent/instrumentation/netty-3.8/src/main/java/datadog/trace/instrumentation/netty38/client/HttpClientRequestTracingHandler.java
@@ -70,10 +70,10 @@ public class HttpClientRequestTracingHandler extends SimpleChannelDownstreamHand
span.finish();
throw throwable;
}
- }
-
- if (null != parentScope) {
+ } finally {
+ if (parentScope != null) {
parentScope.close();
}
}
}
+}

MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientRequestTracingHandler.java
@@ -60,10 +60,10 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
span.finish();
throw throwable;
}
- }
-
+ } finally {
if (null != parentScope) {
parentScope.close();
}
}
}
+}

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java
@@ -60,10 +60,10 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
span.finish();
throw throwable;
}
- }
-
+ } finally {
if (null != parentScope) {
parentScope.close();
}
}
}
+}

Get rid of half baked map methods. Close parentScope in a finally block (+forward port)