Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.okhttp.v3_0.OkHttp3Singletons.PROPAGATED_CONTEXT;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
Expand Down Expand Up @@ -42,9 +42,7 @@ public static class AttachStateAdvice {
public static PropagatedContext onEnter(@Advice.Argument(0) Runnable call) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, call)) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, call);
return ExecutorAdviceHelper.attachContextToTask(context, PROPAGATED_CONTEXT, call);
}
return null;
}
Expand All @@ -54,9 +52,8 @@ public static void onExit(
@Advice.Argument(0) Runnable call,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, call);
ExecutorAdviceHelper.cleanUpAfterSubmit(
propagatedContext, throwable, PROPAGATED_CONTEXT, call);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public void transform(TypeTransformer transformer) {
public static class ConstructorAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) {
callDepth = CallDepth.forClass(OkHttpClient.Builder.class);
public static CallDepth trackCallDepth() {
CallDepth callDepth = CallDepth.forClass(OkHttpClient.Builder.class);
callDepth.getAndIncrement();
return callDepth;
}

@Advice.OnMethodExit(suppress = Throwable.class)
public static void addTracingInterceptor(
@Advice.This OkHttpClient.Builder builder,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
@Advice.This OkHttpClient.Builder builder, @Advice.Enter CallDepth callDepth) {
// No-args constructor is automatically called by constructors with args, but we only want to
// run once from the constructor with args because that is where the dedupe needs to happen.
if (callDepth.decrementAndGet() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class OkHttp3InstrumentationModule extends InstrumentationModule {
public class OkHttp3InstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public OkHttp3InstrumentationModule() {
super("okhttp", "okhttp-3.0");
Expand All @@ -23,4 +25,9 @@ public OkHttp3InstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return asList(new OkHttp3Instrumentation(), new OkHttp3DispatcherInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientRequestResendCount;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.ConnectionErrorSpanInterceptor;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpClientInstrumenterBuilderFactory;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.TracingInterceptor;
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpClientInstrumenters;
import okhttp3.Interceptor;
import okhttp3.Response;

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

public static final VirtualField<Runnable, PropagatedContext> PROPAGATED_CONTEXT =
VirtualField.find(Runnable.class, PropagatedContext.class);
private static final Instrumenter<Interceptor.Chain, Response> INSTRUMENTER =
JavaagentHttpClientInstrumenters.create(
OkHttpClientInstrumenterBuilderFactory.create(GlobalOpenTelemetry.get()));
Expand Down
Loading