Test callbacks for okhttp library instrumentation
MODIFY
instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttpTracing.java
instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttpTracing.java
package io.opentelemetry.instrumentation.okhttp.v3_0;
import io.opentelemetry.api.OpenTelemetry;
+import java.util.concurrent.ExecutorService;
+import okhttp3.Callback;
+import okhttp3.Dispatcher;
import okhttp3.Interceptor;
/** Entrypoint for tracing OkHttp clients. */
@@ -24,7 +27,19 @@ public final class OkHttpTracing {
/**
* Returns a new {@link Interceptor} that can be used with methods like {@link
- * okhttp3.OkHttpClient.Builder#addInterceptor(Interceptor)}.
+ * okhttp3.OkHttpClient.Builder#addInterceptor(Interceptor)}. Note that asynchronous calls using
+ * {@link okhttp3.Call.Factory#enqueue(Callback)} will not work correctly unless you also decorate
+ * the {@linkplain Dispatcher#executorService() dispatcher's executor service} with {@link
+ * io.opentelemetry.context.Context#taskWrapping(ExecutorService)}. For example, if using the
+ * default {@link Dispatcher}, you will need to configure {@link okhttp3.OkHttpClient.Builder}
+ * something like
+ *
+ * <pre>{@code
+ * new OkHttpClient.Builder()
+ * .dispatcher(new Dispatcher(Context.taskWrapping(new Dispatcher().executorService())))
+ * .addInterceptor(OkHttpTracing.create(openTelemetry).newInterceptor())
+ * ...
+ * }</pre>
*/
public Interceptor newInterceptor() {
return new TracingInterceptor(tracer);

MODIFY
instrumentation/okhttp/okhttp-3.0/library/src/test/groovy/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttp3Test.groovy
instrumentation/okhttp/okhttp-3.0/library/src/test/groovy/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttp3Test.groovy
package io.opentelemetry.instrumentation.okhttp.v3_0
+import io.opentelemetry.context.Context
import io.opentelemetry.instrumentation.test.LibraryTestTrait
+import okhttp3.Dispatcher
import okhttp3.OkHttpClient
class OkHttp3Test extends AbstractOkHttp3Test implements LibraryTestTrait {
@Override
OkHttpClient.Builder configureClient(OkHttpClient.Builder clientBuilder) {
- return clientBuilder.addInterceptor(OkHttpTracing.create(getOpenTelemetry()).newInterceptor())
+ return clientBuilder
+ // The double "new Dispatcher" style is the simplest way to decorate the default executor.
+ .dispatcher(new Dispatcher(Context.taskWrapping(new Dispatcher().executorService())))
+ .addInterceptor(OkHttpTracing.create(getOpenTelemetry()).newInterceptor())
}
// library instrumentation doesn't have a good way of suppressing nested CLIENT spans yet
@@ -19,11 +24,4 @@ class OkHttp3Test extends AbstractOkHttp3Test implements LibraryTestTrait {
boolean testWithClientParent() {
false
}
-
- // TODO(anuraaga): Enable after https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/Context.java#L128
- // is released.
- @Override
- boolean testCallback() {
- false
- }
}

Test callbacks for okhttp library instrumentation (#2752)