From 0ac1bc637d4d8109373855809a2d389e2d108115 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Tue, 31 Dec 2024 16:49:27 +0800 Subject: [PATCH 01/11] Resolve the issue of failing to retrieve attachments in dubbo 2.7.6+ --- .../apachedubbo/v2_7/DubboHeadersGetter.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index eef5238bb46d..04dfc835846e 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -6,6 +6,9 @@ package io.opentelemetry.instrumentation.apachedubbo.v2_7; import io.opentelemetry.context.propagation.TextMapGetter; +import java.lang.reflect.Method; +import java.util.Map; +import org.apache.dubbo.rpc.RpcInvocation; enum DubboHeadersGetter implements TextMapGetter { INSTANCE; @@ -13,7 +16,19 @@ enum DubboHeadersGetter implements TextMapGetter { @Override @SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15 public Iterable keys(DubboRequest request) { - return request.invocation().getAttachments().keySet(); + RpcInvocation invocation = request.invocation(); + Map attachments = invocation.getAttachments(); + // in 2.7.6+, type of attachments is StringToObjectMap, it doesn't contains keySet method. + if ("ObjectToStringMap".equals(attachments.getClass().getSimpleName())) { + Method getObjectAttachmentsMethod = null; + try { + getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); + return ((Map) getObjectAttachmentsMethod.invoke(invocation)).keySet(); + } catch (Exception e) { + // ignore + } + } + return invocation.getAttachments().keySet(); } @Override From 3cd545e6baa0fa4a17720db40dc93a01718835c4 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Tue, 31 Dec 2024 17:26:28 +0800 Subject: [PATCH 02/11] Fix compile error --- .../instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 04dfc835846e..17e46d2d6286 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -14,11 +14,11 @@ enum DubboHeadersGetter implements TextMapGetter { INSTANCE; @Override - @SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15 + @SuppressWarnings("unchecked") // unchecked for dubbo 2.7.6 public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); Map attachments = invocation.getAttachments(); - // in 2.7.6+, type of attachments is StringToObjectMap, it doesn't contains keySet method. + // in 2.7.6+, type of attachments is StringToObjectMap, it doesn't contain keySet method. if ("ObjectToStringMap".equals(attachments.getClass().getSimpleName())) { Method getObjectAttachmentsMethod = null; try { From a9e11800b0124935e2adf9b09ea63bc1a9090b59 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Thu, 2 Jan 2025 21:03:31 +0800 Subject: [PATCH 03/11] Optimize codes --- .../apachedubbo/v2_7/DubboHeadersGetter.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 17e46d2d6286..9a91dc25d791 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -8,18 +8,20 @@ import io.opentelemetry.context.propagation.TextMapGetter; import java.lang.reflect.Method; import java.util.Map; +import java.util.Set; import org.apache.dubbo.rpc.RpcInvocation; enum DubboHeadersGetter implements TextMapGetter { INSTANCE; @Override - @SuppressWarnings("unchecked") // unchecked for dubbo 2.7.6 + @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); Map attachments = invocation.getAttachments(); - // in 2.7.6+, type of attachments is StringToObjectMap, it doesn't contain keySet method. - if ("ObjectToStringMap".equals(attachments.getClass().getSimpleName())) { + Set keys = invocation.getAttachments().keySet(); + // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet. + if (keys.size() == 0 && "ObjectToStringMap".equals(attachments.getClass().getSimpleName())) { Method getObjectAttachmentsMethod = null; try { getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); @@ -28,7 +30,7 @@ public Iterable keys(DubboRequest request) { // ignore } } - return invocation.getAttachments().keySet(); + return keys; } @Override From 52c242141026aa2c3506eee748675e3cc43d34bf Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 6 Jan 2025 15:41:02 +0800 Subject: [PATCH 04/11] Add unit test --- .../library-autoconfigure/build.gradle.kts | 19 +++++++++ .../apachedubbo/v2_7/DubboHeadersGetter.java | 19 ++++----- .../v2_7/DubboHeadersGetterTest.java | 41 +++++++++++++++++++ 3 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts index c37558dd4f6d..5a5d26564114 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts @@ -13,6 +13,19 @@ dependencies { testLibrary("org.apache.dubbo:dubbo-config-api:2.7.0") } +val latestDepTest = findProperty("testLatestDeps") as Boolean + +testing { + suites { + val testLatestDepDubbo by registering(JvmTestSuite::class) { + dependencies { + implementation("org.apache.dubbo:dubbo:+") + implementation(project(":instrumentation:apache-dubbo-2.7:library-autoconfigure")) + } + } + } +} + tasks.withType().configureEach { jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") // to suppress non-fatal errors on jdk17 @@ -20,3 +33,9 @@ tasks.withType().configureEach { // required on jdk17 jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") } + +if (!(findProperty("testLatestDeps") as Boolean)) { + tasks.check { + dependsOn(testing.suites) + } +} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 9a91dc25d791..8bad04c9f3c6 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -8,7 +8,6 @@ import io.opentelemetry.context.propagation.TextMapGetter; import java.lang.reflect.Method; import java.util.Map; -import java.util.Set; import org.apache.dubbo.rpc.RpcInvocation; enum DubboHeadersGetter implements TextMapGetter { @@ -18,19 +17,17 @@ enum DubboHeadersGetter implements TextMapGetter { @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); - Map attachments = invocation.getAttachments(); - Set keys = invocation.getAttachments().keySet(); - // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet. - if (keys.size() == 0 && "ObjectToStringMap".equals(attachments.getClass().getSimpleName())) { - Method getObjectAttachmentsMethod = null; - try { - getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); + // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet, + // so always call getObjectAttachments when it is present. + try { + Method getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); + if (getObjectAttachmentsMethod != null) { return ((Map) getObjectAttachmentsMethod.invoke(invocation)).keySet(); - } catch (Exception e) { - // ignore } + } catch (Exception e) { + // ignore } - return keys; + return invocation.getAttachments().keySet(); } @Override diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java new file mode 100644 index 000000000000..f3da3f81ebcd --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java @@ -0,0 +1,41 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.net.InetSocketAddress; +import java.util.Iterator; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcInvocation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class DubboHeadersGetterTest { + + @Mock RpcContext context; + + @Test + void testKeys() { + when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); + when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); + when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); + + RpcInvocation invocation = new RpcInvocation(); + invocation.setAttachment("key", "value"); + DubboRequest request = DubboRequest.create(invocation, context); + + Iterator iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); + assertThat(iterator.hasNext()).isTrue(); + assertThat(iterator.next()).isEqualTo("key"); + assertThat(iterator.hasNext()).isFalse(); + } +} From 22e27d8f85b01deab0bc55ebd1e4194ba4da8a03 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 6 Jan 2025 15:54:29 +0800 Subject: [PATCH 05/11] Optimize codes --- .../apachedubbo/v2_7/DubboHeadersGetter.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 8bad04c9f3c6..90d0b265e1be 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -7,6 +7,7 @@ import io.opentelemetry.context.propagation.TextMapGetter; import java.lang.reflect.Method; +import java.util.Collections; import java.util.Map; import org.apache.dubbo.rpc.RpcInvocation; @@ -17,17 +18,19 @@ enum DubboHeadersGetter implements TextMapGetter { @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); - // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet, - // so always call getObjectAttachments when it is present. try { + // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet. + // Therefore, it's advisable to always call getObjectAttachments when it is available. Method getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); if (getObjectAttachmentsMethod != null) { return ((Map) getObjectAttachmentsMethod.invoke(invocation)).keySet(); + } else { + return invocation.getAttachments().keySet(); } } catch (Exception e) { // ignore } - return invocation.getAttachments().keySet(); + return Collections.emptyList(); } @Override From 2d6a796455edd28a719fb33e0feb5907c1124ac5 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 6 Jan 2025 16:16:51 +0800 Subject: [PATCH 06/11] spotlessApply --- .../instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 90d0b265e1be..51b312092a1e 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -19,7 +19,8 @@ enum DubboHeadersGetter implements TextMapGetter { public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); try { - // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet. + // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the + // keySet. // Therefore, it's advisable to always call getObjectAttachments when it is available. Method getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); if (getObjectAttachmentsMethod != null) { From eb37ffe8c2e2ef9e7668b9034297406d66029710 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 6 Jan 2025 16:18:33 +0800 Subject: [PATCH 07/11] spotlessApply --- .../instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index 51b312092a1e..b1c01eb2a513 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -20,8 +20,7 @@ public Iterable keys(DubboRequest request) { RpcInvocation invocation = request.invocation(); try { // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the - // keySet. - // Therefore, it's advisable to always call getObjectAttachments when it is available. + // keySet. Therefore, it's advisable to always call getObjectAttachments when it is available. Method getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); if (getObjectAttachmentsMethod != null) { return ((Map) getObjectAttachmentsMethod.invoke(invocation)).keySet(); From c33a3cb3a5919d937eb1bae9f30231cf245dce7b Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 6 Jan 2025 17:12:25 +0800 Subject: [PATCH 08/11] Fix CI failings --- .../instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java index f3da3f81ebcd..3c92605f2dd1 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java @@ -24,6 +24,7 @@ class DubboHeadersGetterTest { @Mock RpcContext context; @Test + @SuppressWarnings("deprecation") // deprecation for RpcInvocation() void testKeys() { when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); From b10b471c62e705f1d9af7121c0144111a9e5d141 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Tue, 7 Jan 2025 20:09:07 +0800 Subject: [PATCH 09/11] Optimize code --- .../apachedubbo/v2_7/DubboHeadersGetter.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index b1c01eb2a513..efdec042c7cf 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -6,14 +6,17 @@ package io.opentelemetry.instrumentation.apachedubbo.v2_7; import io.opentelemetry.context.propagation.TextMapGetter; -import java.lang.reflect.Method; -import java.util.Collections; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.util.Map; import org.apache.dubbo.rpc.RpcInvocation; enum DubboHeadersGetter implements TextMapGetter { INSTANCE; + private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); + @Override @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 public Iterable keys(DubboRequest request) { @@ -21,16 +24,14 @@ public Iterable keys(DubboRequest request) { try { // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the // keySet. Therefore, it's advisable to always call getObjectAttachments when it is available. - Method getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments"); - if (getObjectAttachmentsMethod != null) { - return ((Map) getObjectAttachmentsMethod.invoke(invocation)).keySet(); - } else { - return invocation.getAttachments().keySet(); - } - } catch (Exception e) { + MethodHandle getObjectAttachments = + lookup.findStatic( + RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class)); + return ((Map) getObjectAttachments.invoke(invocation)).keySet(); + } catch (Throwable t) { // ignore } - return Collections.emptyList(); + return invocation.getAttachments().keySet(); } @Override From 065372d13f17d40671ce7bb5146438d3be33a6ba Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Wed, 8 Jan 2025 21:58:43 +0800 Subject: [PATCH 10/11] Address review comments --- .../apachedubbo/v2_7/DubboHeadersGetter.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index efdec042c7cf..baa7792762b8 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -15,7 +15,20 @@ enum DubboHeadersGetter implements TextMapGetter { INSTANCE; - private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); + private static final MethodHandle GET_OBJECT_ATTACHMENTS; + + static { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodHandle getObjectAttachments = null; + try { + getObjectAttachments = + lookup.findStatic( + RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class)); + } catch (Throwable t) { + // ignore + } + GET_OBJECT_ATTACHMENTS = getObjectAttachments; + } @Override @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 @@ -24,10 +37,7 @@ public Iterable keys(DubboRequest request) { try { // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the // keySet. Therefore, it's advisable to always call getObjectAttachments when it is available. - MethodHandle getObjectAttachments = - lookup.findStatic( - RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class)); - return ((Map) getObjectAttachments.invoke(invocation)).keySet(); + return ((Map) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet(); } catch (Throwable t) { // ignore } From 1c9e8e632395b45932e48e5db4a9aad5a051e132 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Thu, 9 Jan 2025 12:04:29 +0800 Subject: [PATCH 11/11] Fix unit test --- .../library-autoconfigure/build.gradle.kts | 6 +-- .../apachedubbo/v2_7/DubboHeadersGetter.java | 9 ++-- .../v2_7/DubboHeadersGetterTest.java | 42 +++++++++++++++++++ .../v2_7/DubboHeadersGetterTest.java | 10 +++-- 4 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts index 5a5d26564114..52c5e5cb08ac 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts @@ -13,14 +13,12 @@ dependencies { testLibrary("org.apache.dubbo:dubbo-config-api:2.7.0") } -val latestDepTest = findProperty("testLatestDeps") as Boolean - testing { suites { val testLatestDepDubbo by registering(JvmTestSuite::class) { dependencies { - implementation("org.apache.dubbo:dubbo:+") implementation(project(":instrumentation:apache-dubbo-2.7:library-autoconfigure")) + implementation("org.apache.dubbo:dubbo:+") } } } @@ -34,7 +32,7 @@ tasks.withType().configureEach { jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") } -if (!(findProperty("testLatestDeps") as Boolean)) { +if (findProperty("testLatestDeps") as Boolean) { tasks.check { dependsOn(testing.suites) } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java index baa7792762b8..8a90da1302c2 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java @@ -21,9 +21,10 @@ enum DubboHeadersGetter implements TextMapGetter { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle getObjectAttachments = null; try { + Class rpcInvocation = Class.forName("org.apache.dubbo.rpc.RpcInvocation"); getObjectAttachments = - lookup.findStatic( - RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class)); + lookup.findVirtual( + rpcInvocation, "getObjectAttachments", MethodType.methodType(Map.class)); } catch (Throwable t) { // ignore } @@ -37,7 +38,9 @@ public Iterable keys(DubboRequest request) { try { // In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the // keySet. Therefore, it's advisable to always call getObjectAttachments when it is available. - return ((Map) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet(); + if (GET_OBJECT_ATTACHMENTS != null) { + return ((Map) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet(); + } } catch (Throwable t) { // ignore } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java new file mode 100644 index 000000000000..3c92605f2dd1 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java @@ -0,0 +1,42 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.net.InetSocketAddress; +import java.util.Iterator; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcInvocation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class DubboHeadersGetterTest { + + @Mock RpcContext context; + + @Test + @SuppressWarnings("deprecation") // deprecation for RpcInvocation() + void testKeys() { + when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); + when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); + when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); + + RpcInvocation invocation = new RpcInvocation(); + invocation.setAttachment("key", "value"); + DubboRequest request = DubboRequest.create(invocation, context); + + Iterator iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); + assertThat(iterator.hasNext()).isTrue(); + assertThat(iterator.next()).isEqualTo("key"); + assertThat(iterator.hasNext()).isFalse(); + } +} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java index 3c92605f2dd1..b8eabc74136d 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.when; import java.net.InetSocketAddress; +import java.util.Collections; import java.util.Iterator; import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.RpcContext; @@ -23,16 +24,17 @@ class DubboHeadersGetterTest { @Mock RpcContext context; + @Mock RpcInvocation rpcInvocation; + @Test - @SuppressWarnings("deprecation") // deprecation for RpcInvocation() + @SuppressWarnings({"deprecation", "unchecked"}) // deprecation for RpcInvocation() void testKeys() { when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); - RpcInvocation invocation = new RpcInvocation(); - invocation.setAttachment("key", "value"); - DubboRequest request = DubboRequest.create(invocation, context); + when(rpcInvocation.getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value")); + DubboRequest request = DubboRequest.create(rpcInvocation, context); Iterator iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); assertThat(iterator.hasNext()).isTrue();