From 7fe1aaceda45fce1723a7a049f4d42b00919cf23 Mon Sep 17 00:00:00 2001 From: Bryan Herbst Date: Thu, 23 Feb 2023 13:49:52 -0600 Subject: [PATCH 1/2] Enable animal sniffer for RxJava2 instrumentation --- instrumentation/rxjava/rxjava-2.0/library/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/rxjava/rxjava-2.0/library/build.gradle.kts b/instrumentation/rxjava/rxjava-2.0/library/build.gradle.kts index ac32a8f375b7..ecc9ad8a068d 100644 --- a/instrumentation/rxjava/rxjava-2.0/library/build.gradle.kts +++ b/instrumentation/rxjava/rxjava-2.0/library/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("otel.library-instrumentation") + id("otel.animalsniffer-conventions") } dependencies { From e94215830a119db4159172848ccf553d5b6b17e2 Mon Sep 17 00:00:00 2001 From: Bryan Herbst Date: Thu, 23 Feb 2023 13:50:30 -0600 Subject: [PATCH 2/2] Use older java.lang.reflect APIs for RxJava2 instrumentation --- .../rxjava/v2_0/TracingObserver.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/instrumentation/rxjava/rxjava-2.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v2_0/TracingObserver.java b/instrumentation/rxjava/rxjava-2.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v2_0/TracingObserver.java index 477654abc4f1..cf06d0370094 100644 --- a/instrumentation/rxjava/rxjava-2.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v2_0/TracingObserver.java +++ b/instrumentation/rxjava/rxjava-2.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v2_0/TracingObserver.java @@ -27,11 +27,10 @@ import io.reactivex.Observer; import io.reactivex.internal.fuseable.QueueDisposable; import io.reactivex.internal.observers.BasicFuseableObserver; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; class TracingObserver extends BasicFuseableObserver { - private static final MethodHandle queueDisposableGetter = getQueueDisposableGetter(); + private static final Field queueDisposableField = getQueueDisposableField(); // BasicFuseableObserver#actual has been renamed to downstream in newer versions, we can't use it // in this class @@ -81,34 +80,35 @@ public T poll() throws Exception { return getQueueDisposable().poll(); } + @SuppressWarnings("unchecked") private QueueDisposable getQueueDisposable() { try { - return (QueueDisposable) queueDisposableGetter.invoke(this); + return (QueueDisposable) queueDisposableField.get(this); } catch (Throwable throwable) { throw new IllegalStateException(throwable); } } - private static MethodHandle getGetterHandle(String fieldName) { + private static Field getField(String fieldName) { + try { - return MethodHandles.lookup() - .findGetter(BasicFuseableObserver.class, fieldName, QueueDisposable.class); - } catch (NoSuchFieldException | IllegalAccessException ignored) { + return BasicFuseableObserver.class.getDeclaredField(fieldName); + } catch (NoSuchFieldException ignored) { // Ignore } return null; } - private static MethodHandle getQueueDisposableGetter() { - MethodHandle getter = getGetterHandle("qd"); - if (getter == null) { + private static Field getQueueDisposableField() { + Field queueDisposableField = getField("qd"); + if (queueDisposableField == null) { // in versions before 2.2.1 field was named "qs" - getter = getGetterHandle("qs"); + queueDisposableField = getField("qs"); } - return getter; + return queueDisposableField; } public static boolean canEnable() { - return queueDisposableGetter != null; + return queueDisposableField != null; } }