Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert okhttp library instrumentation back to using standard reflection to support Android usage #3910

Merged
merged 2 commits into from
Aug 24, 2021
Merged
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 @@ -9,9 +9,8 @@
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 java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand All @@ -23,23 +22,21 @@
class TracingCallFactory implements Call.Factory {
private static final Cache<Request, Context> contextsByRequest =
Cache.newBuilder().setWeakKeys().build();

@Nullable private static MethodHandle timeoutMethodHandle;
@Nullable private static MethodHandle cloneMethodHandle;
// We use old-school reflection here, rather than MethodHandles because Android doesn't support
// MethodHandles until API 26.
@Nullable private static Method timeoutMethod;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here about not being able to use method handles (so no one changes it back), at least until we get some build tooling in place to prevent similar to otel-java repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Nullable private static Method cloneMethod;

static {
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
try {
MethodType methodType = MethodType.methodType(Timeout.class);
timeoutMethodHandle = lookup.findVirtual(Call.class, "timeout", methodType);
} catch (NoSuchMethodException | IllegalAccessException e) {
timeoutMethodHandle = null;
timeoutMethod = Call.class.getMethod("timeout");
} catch (NoSuchMethodException e) {
timeoutMethod = null;
}
try {
MethodType methodType = MethodType.methodType(Call.class);
cloneMethodHandle = lookup.findVirtual(Call.class, "clone", methodType);
} catch (NoSuchMethodException | IllegalAccessException e) {
cloneMethodHandle = null;
cloneMethod = Call.class.getDeclaredMethod("clone");
} catch (NoSuchMethodException e) {
cloneMethod = null;
}
}

Expand Down Expand Up @@ -78,14 +75,14 @@ public void cancel() {

@Override
public Call clone() throws CloneNotSupportedException {
if (cloneMethodHandle == null) {
if (cloneMethod == null) {
return (Call) super.clone();
}
try {
// we pull the current context here, because the cloning might be happening in a different
// context than the original call creation.
return new TracingCall((Call) cloneMethodHandle.invoke(delegate), Context.current());
} catch (Throwable e) {
return new TracingCall((Call) cloneMethod.invoke(delegate), Context.current());
} catch (IllegalAccessException | InvocationTargetException e) {
return (Call) super.clone();
}
}
Expand Down Expand Up @@ -119,12 +116,12 @@ public Request request() {

// @Override method was introduced in 3.12
public Timeout timeout() {
if (timeoutMethodHandle == null) {
if (timeoutMethod == null) {
return Timeout.NONE;
}
try {
return (Timeout) timeoutMethodHandle.invoke(delegate);
} catch (Throwable e) {
return (Timeout) timeoutMethod.invoke(delegate);
} catch (IllegalAccessException | InvocationTargetException e) {
// do nothing...we're before 3.12, or something else has gone wrong that we can't do
// anything about.
return Timeout.NONE;
Expand Down