Important: asynchronous calls using {@link okhttp3.Call.Factory#enqueue(Callback)} will + * *not* work correctly using just this interceptor. + * + *
It is strongly recommended that you use the {@link #newCallFactory(OkHttpClient)} method to + * decorate your {@link OkHttpClient}, rather than using this method directly. + * + * @deprecated Please use the {@link #newCallFactory(OkHttpClient)} method instead. */ + @Deprecated public Interceptor newInterceptor() { return new TracingInterceptor(instrumenter, propagators); } + + /** + * Construct a new OpenTelemetry tracing-enabled {@link okhttp3.Call.Factory} using the provided + * {@link OkHttpClient} instance. + * + *
Using this method will result in proper propagation and span parenting, for both {@linkplain
+ * Call#execute() synchronous} and {@linkplain Call#enqueue(Callback) asynchronous} usages.
+ *
+ * @param baseClient An instance of OkHttpClient configured as desired.
+ * @return a {@link Call.Factory} for creating new {@link Call} instances.
+ */
+ public Call.Factory newCallFactory(OkHttpClient baseClient) {
+ OkHttpClient tracingClient = baseClient.newBuilder().addInterceptor(newInterceptor()).build();
+ return new TracingCallFactory(tracingClient);
+ }
}
diff --git a/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/TracingCallFactory.java b/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/TracingCallFactory.java
new file mode 100644
index 0000000000..8ed22f6673
--- /dev/null
+++ b/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/TracingCallFactory.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.instrumentation.okhttp.v3_0;
+
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.Scope;
+import io.opentelemetry.instrumentation.api.caching.Cache;
+import java.io.IOException;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import okio.Timeout;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+class TracingCallFactory implements Call.Factory {
+ private static final Cache> traces = testing.traces();