Make sure span is opened and closed with scope in http_url_connection
MODIFY
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
@@ -13,6 +13,7 @@ import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.InstrumentationContext;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
@@ -178,10 +179,12 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
public Span startSpan(final HttpURLConnection connection) {
final Tracer.SpanBuilder builder = GlobalTracer.get().buildSpan(OPERATION_NAME);
span = builder.start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, connection);
return span;
}
+ }
public boolean hasSpan() {
return span != null;
@@ -196,12 +199,13 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
public void finishSpan(final Throwable throwable) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
- span.finish();
span = null;
finished = true;
}
+ }
public void finishSpan(final int responseCode) {
/*
@@ -210,12 +214,13 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
* (e.g. breaks getOutputStream).
*/
if (responseCode > 0) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
DECORATE.onResponse(span, responseCode);
DECORATE.beforeFinish(span);
- span.finish();
span = null;
finished = true;
}
}
}
}
+}

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/UrlInstrumentation.java
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/UrlInstrumentation.java
@@ -12,6 +12,7 @@ import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.api.Config;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
@@ -65,7 +66,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
.withTag(Tags.COMPONENT.getKey(), COMPONENT)
.start();
-
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
Tags.HTTP_URL.set(span, url.toString());
Tags.PEER_PORT.set(span, url.getPort() == -1 ? 80 : url.getPort());
Tags.PEER_HOSTNAME.set(span, url.getHost());
@@ -75,7 +76,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
Tags.ERROR.set(span, true);
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
- span.finish();
+ }
}
}
}

Make sure span is opened and closed with scope in http_url_connection