Skip to content

Commit efb8dee

Browse files
authored
Indy-ready - okhttp3 (#15078)
1 parent d850c76 commit efb8dee

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3DispatcherInstrumentation.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
9+
import static io.opentelemetry.javaagent.instrumentation.okhttp.v3_0.OkHttp3Singletons.PROPAGATED_CONTEXT;
910
import static net.bytebuddy.matcher.ElementMatchers.named;
1011
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
1112
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
1213

1314
import io.opentelemetry.context.Context;
14-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1515
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1616
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
1717
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
@@ -42,9 +42,7 @@ public static class AttachStateAdvice {
4242
public static PropagatedContext onEnter(@Advice.Argument(0) Runnable call) {
4343
Context context = Java8BytecodeBridge.currentContext();
4444
if (ExecutorAdviceHelper.shouldPropagateContext(context, call)) {
45-
VirtualField<Runnable, PropagatedContext> virtualField =
46-
VirtualField.find(Runnable.class, PropagatedContext.class);
47-
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, call);
45+
return ExecutorAdviceHelper.attachContextToTask(context, PROPAGATED_CONTEXT, call);
4846
}
4947
return null;
5048
}
@@ -54,9 +52,8 @@ public static void onExit(
5452
@Advice.Argument(0) Runnable call,
5553
@Advice.Enter PropagatedContext propagatedContext,
5654
@Advice.Thrown Throwable throwable) {
57-
VirtualField<Runnable, PropagatedContext> virtualField =
58-
VirtualField.find(Runnable.class, PropagatedContext.class);
59-
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, call);
55+
ExecutorAdviceHelper.cleanUpAfterSubmit(
56+
propagatedContext, throwable, PROPAGATED_CONTEXT, call);
6057
}
6158
}
6259
}

instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Instrumentation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public void transform(TypeTransformer transformer) {
3232
public static class ConstructorAdvice {
3333

3434
@Advice.OnMethodEnter(suppress = Throwable.class)
35-
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) {
36-
callDepth = CallDepth.forClass(OkHttpClient.Builder.class);
35+
public static CallDepth trackCallDepth() {
36+
CallDepth callDepth = CallDepth.forClass(OkHttpClient.Builder.class);
3737
callDepth.getAndIncrement();
38+
return callDepth;
3839
}
3940

4041
@Advice.OnMethodExit(suppress = Throwable.class)
4142
public static void addTracingInterceptor(
42-
@Advice.This OkHttpClient.Builder builder,
43-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
43+
@Advice.This OkHttpClient.Builder builder, @Advice.Enter CallDepth callDepth) {
4444
// No-args constructor is automatically called by constructors with args, but we only want to
4545
// run once from the constructor with args because that is where the dedupe needs to happen.
4646
if (callDepth.decrementAndGet() > 0) {

instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3InstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class OkHttp3InstrumentationModule extends InstrumentationModule {
17+
public class OkHttp3InstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719

1820
public OkHttp3InstrumentationModule() {
1921
super("okhttp", "okhttp-3.0");
@@ -23,4 +25,9 @@ public OkHttp3InstrumentationModule() {
2325
public List<TypeInstrumentation> typeInstrumentations() {
2426
return asList(new OkHttp3Instrumentation(), new OkHttp3DispatcherInstrumentation());
2527
}
28+
29+
@Override
30+
public boolean isIndyReady() {
31+
return true;
32+
}
2633
}

instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Singletons.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
import io.opentelemetry.context.Scope;
1111
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1212
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientRequestResendCount;
13+
import io.opentelemetry.instrumentation.api.util.VirtualField;
1314
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.ConnectionErrorSpanInterceptor;
1415
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpClientInstrumenterBuilderFactory;
1516
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.TracingInterceptor;
17+
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
1618
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpClientInstrumenters;
1719
import okhttp3.Interceptor;
1820
import okhttp3.Response;
1921

2022
/** Holder of singleton interceptors for adding to instrumented clients. */
2123
public final class OkHttp3Singletons {
2224

25+
public static final VirtualField<Runnable, PropagatedContext> PROPAGATED_CONTEXT =
26+
VirtualField.find(Runnable.class, PropagatedContext.class);
2327
private static final Instrumenter<Interceptor.Chain, Response> INSTRUMENTER =
2428
JavaagentHttpClientInstrumenters.create(
2529
OkHttpClientInstrumenterBuilderFactory.create(GlobalOpenTelemetry.get()));

0 commit comments

Comments
 (0)