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 eef5238bb46d..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 @@ -6,14 +6,31 @@ package io.opentelemetry.instrumentation.apachedubbo.v2_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; enum DubboHeadersGetter implements TextMapGetter { INSTANCE; @Override - @SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15 + @SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7 public Iterable keys(DubboRequest request) { - return request.invocation().getAttachments().keySet(); + 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. + 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 Collections.emptyList(); } @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..3c92605f2dd1 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/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(); + } +}