Update http-url-connection to new agent api
ADD
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HeadersInjectAdapter.java
+package datadog.trace.instrumentation.http_url_connection;
+
+import datadog.trace.instrumentation.api.AgentPropagation;
+import java.net.HttpURLConnection;
+
+public class HeadersInjectAdapter implements AgentPropagation.Setter<HttpURLConnection> {
+
+ public static final HeadersInjectAdapter SETTER = new HeadersInjectAdapter();
+
+ @Override
+ public void set(final HttpURLConnection carrier, final String key, final String value) {
+ carrier.setRequestProperty(key, value);
+ }
+}

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
package datadog.trace.instrumentation.http_url_connection;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.propagate;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
+import static datadog.trace.instrumentation.http_url_connection.HeadersInjectAdapter.SETTER;
import static datadog.trace.instrumentation.http_url_connection.HttpUrlConnectionDecorator.DECORATE;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -13,14 +17,9 @@ 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;
-import io.opentracing.propagation.TextMap;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.net.HttpURLConnection;
-import java.util.Iterator;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
@@ -48,7 +47,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
"datadog.trace.agent.decorator.ClientDecorator",
"datadog.trace.agent.decorator.HttpClientDecorator",
packageName + ".HttpUrlConnectionDecorator",
- HttpUrlConnectionInstrumentation.class.getName() + "$HeadersInjectAdapter",
+ packageName + ".HeadersInjectAdapter",
HttpUrlConnectionInstrumentation.class.getName() + "$HttpUrlState",
HttpUrlConnectionInstrumentation.class.getName() + "$HttpUrlState$1",
};
@@ -86,11 +85,9 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
if (!state.hasSpan() && !state.isFinished()) {
- final Span span = state.startSpan(thiz);
+ final AgentSpan span = state.start(thiz);
if (!connected) {
- GlobalTracer.get()
- .inject(
- span.context(), Format.Builtin.HTTP_HEADERS, new HeadersInjectAdapter(thiz));
+ propagate().inject(span, thiz, SETTER);
}
}
return state;
@@ -122,32 +119,6 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
}
- public static class HeadersInjectAdapter implements TextMap {
-
- private final HttpURLConnection connection;
-
- public HeadersInjectAdapter(final HttpURLConnection connection) {
- this.connection = connection;
- }
-
- @Override
- public void put(final String key, final String value) {
- try {
- connection.setRequestProperty(key, value);
- } catch (final IllegalStateException e) {
- // There are cases when this can through an exception. E.g. some implementations have
- // 'connecting' state. Just guard against that here, there is not much we can do at this
- // point.
- }
- }
-
- @Override
- public Iterator<Map.Entry<String, String>> iterator() {
- throw new UnsupportedOperationException(
- "This class should be used only with tracer#inject()");
- }
- }
-
public static class HttpUrlState {
public static final String OPERATION_NAME = "http.request";
@@ -160,13 +131,12 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
};
- private volatile Span span = null;
+ private volatile AgentSpan span = null;
private volatile boolean finished = false;
- 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)) {
+ public AgentSpan start(final HttpURLConnection connection) {
+ span = startSpan(OPERATION_NAME);
+ try (final AgentScope scope = activateSpan(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, connection);
return span;
@@ -186,7 +156,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
public void finishSpan(final Throwable throwable) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
+ try (final AgentScope scope = activateSpan(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
@@ -202,7 +172,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
* (e.g. breaks getOutputStream).
*/
if (responseCode > 0) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
+ try (final AgentScope scope = activateSpan(span, false)) {
DECORATE.onResponse(span, responseCode);
DECORATE.beforeFinish(span);
span.finish();

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
package datadog.trace.instrumentation.http_url_connection;
-import static io.opentracing.log.Fields.ERROR_OBJECT;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -13,13 +14,11 @@ import datadog.trace.api.Config;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
import datadog.trace.bootstrap.InternalJarURLHandler;
-import io.opentracing.Scope;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.tag.Tags;
-import io.opentracing.util.GlobalTracer;
import java.net.URL;
import java.net.URLStreamHandler;
-import java.util.Collections;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
@@ -65,23 +64,22 @@ public class UrlInstrumentation extends Instrumenter.Default {
String protocol = url.getProtocol();
protocol = protocol != null ? protocol : "url";
- final Span span =
- GlobalTracer.get()
- .buildSpan(protocol + ".request")
- .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
- .withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
- .withTag(Tags.COMPONENT.getKey(), COMPONENT)
- .start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
- 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());
+ final AgentSpan span =
+ startSpan(protocol + ".request")
+ .setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
+ .setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
+ .setTag(Tags.COMPONENT.getKey(), COMPONENT);
+
+ try (final AgentScope scope = activateSpan(span, false)) {
+ span.setTag(Tags.HTTP_URL.getKey(), url.toString());
+ span.setTag(Tags.PEER_PORT.getKey(), url.getPort() == -1 ? 80 : url.getPort());
+ span.setTag(Tags.PEER_HOSTNAME.getKey(), url.getHost());
if (Config.get().isHttpClientSplitByDomain()) {
span.setTag(DDTags.SERVICE_NAME, url.getHost());
}
- Tags.ERROR.set(span, true);
- span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
+ span.setError(true);
+ span.addThrowable(throwable);
span.finish();
}
}

Update http-url-connection to new agent api