Update okhttp-3 to new agent api
MODIFY
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/RequestBuilderInjectAdapter.java
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/RequestBuilderInjectAdapter.java
package datadog.trace.instrumentation.okhttp3;
-import io.opentracing.propagation.TextMap;
-import java.util.Iterator;
-import java.util.Map;
+import datadog.trace.instrumentation.api.AgentPropagation;
import okhttp3.Request;
/**
@@ -10,21 +8,12 @@ import okhttp3.Request;
*
* @author Pavol Loffay
*/
-public class RequestBuilderInjectAdapter implements TextMap {
+public class RequestBuilderInjectAdapter implements AgentPropagation.Setter<Request.Builder> {
- private final Request.Builder requestBuilder;
-
- public RequestBuilderInjectAdapter(final Request.Builder request) {
- this.requestBuilder = request;
- }
-
- @Override
- public Iterator<Map.Entry<String, String>> iterator() {
- throw new UnsupportedOperationException("Should be used only with tracer#inject()");
- }
+ public static final RequestBuilderInjectAdapter SETTER = new RequestBuilderInjectAdapter();
@Override
- public void put(final String key, final String value) {
- requestBuilder.addHeader(key, value);
+ public void set(final Request.Builder carrier, final String key, final String value) {
+ carrier.addHeader(key, value);
}
}

MODIFY
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TagWrapper.java
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TagWrapper.java
package datadog.trace.instrumentation.okhttp3;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
/**
* Tag wrapper to store parent span context and user defined tags.
@@ -8,7 +8,7 @@ import io.opentracing.Span;
* @author Pavol Loffay
*/
public class TagWrapper {
- private Span span;
+ private AgentSpan span;
private Object tag;
@@ -21,9 +21,9 @@ public class TagWrapper {
* @param wrapper previous wrapper
* @param span span
*/
- TagWrapper(final TagWrapper wrapper, final Span span) {
+ TagWrapper(final TagWrapper wrapper, final AgentSpan span) {
this.span = span;
- this.tag = wrapper.tag;
+ tag = wrapper.tag;
}
public void setTag(final Object tag) {
@@ -34,7 +34,7 @@ public class TagWrapper {
return tag;
}
- Span getSpan() {
+ AgentSpan getSpan() {
return span;
}
}

MODIFY
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingCallFactory.java
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingCallFactory.java
package datadog.trace.instrumentation.okhttp3;
+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.okhttp3.OkHttpClientDecorator.DECORATE;
import static datadog.trace.instrumentation.okhttp3.OkHttpClientDecorator.NETWORK_DECORATE;
+import static datadog.trace.instrumentation.okhttp3.RequestBuilderInjectAdapter.SETTER;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.SpanContext;
-import io.opentracing.propagation.Format;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
+import datadog.trace.instrumentation.api.AgentSpan.Context;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Interceptor;
@@ -25,14 +27,14 @@ public class TracingCallFactory implements Call.Factory {
@Override
public Call newCall(final Request request) {
- final Span span = GlobalTracer.get().buildSpan("okhttp.http").start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
- DECORATE.afterStart(scope);
+ final AgentSpan span = startSpan("okhttp.http");
+ try (final AgentScope scope = activateSpan(span, false)) {
+ DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
/** In case of exception network interceptor is not called */
final OkHttpClient.Builder okBuilder = okHttpClient.newBuilder();
- okBuilder.networkInterceptors().add(0, new NetworkInterceptor(scope.span().context()));
+ okBuilder.networkInterceptors().add(0, new NetworkInterceptor(span.context()));
okBuilder
.interceptors()
@@ -41,8 +43,7 @@ public class TracingCallFactory implements Call.Factory {
new Interceptor() {
@Override
public Response intercept(final Chain chain) throws IOException {
- try (final Scope interceptorScope =
- GlobalTracer.get().scopeManager().activate(span, false)) {
+ try (final AgentScope interceptorScope = activateSpan(span, false)) {
return chain.proceed(chain.request());
} catch (final Exception ex) {
DECORATE.onError(scope, ex);
@@ -62,27 +63,23 @@ public class TracingCallFactory implements Call.Factory {
}
static class NetworkInterceptor implements Interceptor {
- public SpanContext parentContext;
+ public Context parentContext;
- NetworkInterceptor(final SpanContext spanContext) {
+ NetworkInterceptor(final Context spanContext) {
parentContext = spanContext;
}
@Override
public Response intercept(final Chain chain) throws IOException {
- try (final Scope networkScope =
- GlobalTracer.get().buildSpan("okhttp.http").asChildOf(parentContext).startActive(true)) {
- NETWORK_DECORATE.afterStart(networkScope);
- NETWORK_DECORATE.onRequest(networkScope.span(), chain.request());
+ final AgentSpan networkSpan = startSpan("okhttp.http", parentContext);
+ try (final AgentScope networkScope = activateSpan(networkSpan, true)) {
+ NETWORK_DECORATE.afterStart(networkSpan);
+ NETWORK_DECORATE.onRequest(networkSpan, chain.request());
NETWORK_DECORATE.onPeerConnection(
- networkScope.span(), chain.connection().socket().getInetAddress());
+ networkSpan, chain.connection().socket().getInetAddress());
final Request.Builder requestBuilder = chain.request().newBuilder();
- GlobalTracer.get()
- .inject(
- networkScope.span().context(),
- Format.Builtin.HTTP_HEADERS,
- new RequestBuilderInjectAdapter(requestBuilder));
+ propagate().inject(networkSpan, requestBuilder, SETTER);
final Response response;
try {
@@ -92,7 +89,7 @@ public class TracingCallFactory implements Call.Factory {
throw e;
}
- NETWORK_DECORATE.onResponse(networkScope.span(), response);
+ NETWORK_DECORATE.onResponse(networkSpan, response);
NETWORK_DECORATE.beforeFinish(networkScope);
return response;
}

MODIFY
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingInterceptor.java
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingInterceptor.java
package datadog.trace.instrumentation.okhttp3;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.okhttp3.OkHttpClientDecorator.DECORATE;
-import io.opentracing.Scope;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
@@ -23,16 +25,17 @@ public class TracingInterceptor implements Interceptor {
// application interceptor?
if (chain.connection() == null) {
- final Scope scope = GlobalTracer.get().buildSpan("okhttp.http").startActive(true);
- DECORATE.afterStart(scope);
+ final AgentSpan span = startSpan("okhttp.http");
+ DECORATE.afterStart(span);
final Request.Builder requestBuilder = chain.request().newBuilder();
final Object tag = chain.request().tag();
final TagWrapper tagWrapper =
tag instanceof TagWrapper ? (TagWrapper) tag : new TagWrapper(tag);
- requestBuilder.tag(new TagWrapper(tagWrapper, scope.span()));
+ requestBuilder.tag(new TagWrapper(tagWrapper, span));
+ final AgentScope scope = activateSpan(span, true);
try {
response = chain.proceed(requestBuilder.build());
} catch (final Throwable ex) {

Update okhttp-3 to new agent api