Skip to content

Commit

Permalink
Okhttp3: fix concurrency test with callback (#3669)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Jul 26, 2021
1 parent 437e568 commit 26dc106
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.ExecutorInstrumentationUtils;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.State;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class OkHttp3DispatcherInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("okhttp3.Dispatcher");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
named("enqueue").and(takesArgument(0, named("okhttp3.RealCall$AsyncCall"))),
OkHttp3DispatcherInstrumentation.class.getName() + "$AttachStateAdvice");
}

@SuppressWarnings("unused")
public static class AttachStateAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static State onEnter(@Advice.Argument(0) Runnable call) {
if (ExecutorInstrumentationUtils.shouldAttachStateToTask(call)) {
ContextStore<Runnable, State> contextStore =
InstrumentationContext.get(Runnable.class, State.class);
return ExecutorInstrumentationUtils.setupState(
contextStore, call, Java8BytecodeBridge.currentContext());
}
return null;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Enter State state, @Advice.Thrown Throwable throwable) {
ExecutorInstrumentationUtils.cleanUpOnMethodExit(state, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;

import static java.util.Collections.singletonList;
import static java.util.Arrays.asList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
Expand All @@ -21,6 +21,6 @@ public OkHttp3InstrumentationModule() {

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new OkHttp3Instrumentation());
return asList(new OkHttp3Instrumentation(), new OkHttp3DispatcherInstrumentation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ class OkHttp3Test extends AbstractOkHttp3Test implements LibraryTestTrait {
boolean testWithClientParent() {
false
}

@Override
boolean testCausalityWithCallback() {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ abstract class AbstractOkHttp3Test extends HttpClientTest<Request> {
}

@Override
boolean testRedirects() {
false
}

@Override
boolean testCausality() {
boolean testCircularRedirects() {
false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
def "high concurrency test with callback"() {
setup:
assumeTrue(testCausality())
assumeTrue(testCausalityWithCallback())
assumeTrue(testCallback())
assumeTrue(testCallbackWithParent())

Expand Down Expand Up @@ -1120,6 +1121,10 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
true
}

boolean testCausalityWithCallback() {
true
}

boolean testCallback() {
return true
}
Expand Down

0 comments on commit 26dc106

Please sign in to comment.