From d9f703010730536cbc2e3f1511620e41c6fba62c Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 4 Sep 2024 02:48:10 +0800 Subject: [PATCH] convert module apache-dubbo-2.7 test case from groovy to java (#12008) Co-authored-by: Jay DeLuca --- .../apachedubbo/v2_7/DubboTest.groovy | 11 - .../v2_7/DubboTraceChainTest.groovy | 11 - .../apachedubbo/v2_7/DubboAgentTest.java | 22 + .../v2_7/DubboAgentTraceChainTest.java | 22 + .../apachedubbo/v2_7/DubboHeadersGetter.java | 2 + .../apachedubbo/v2_7/TracingFilter.java | 1 + .../apachedubbo/v2_7/DubboTest.groovy | 11 - .../v2_7/DubboTraceChainTest.groovy | 11 - .../apachedubbo/v2_7/DubboTest.java | 21 + .../apachedubbo/v2_7/DubboTraceChainTest.java | 21 + .../apachedubbo/v2_7/AbstractDubboTest.groovy | 211 --------- .../v2_7/AbstractDubboTraceChainTest.groovy | 287 ------------ .../apachedubbo/v2_7/DubboTestUtil.groovy | 26 -- .../apachedubbo/v2_7/AbstractDubboTest.java | 287 ++++++++++++ .../v2_7/AbstractDubboTraceChainTest.java | 411 ++++++++++++++++++ .../apachedubbo/v2_7/DubboTestUtil.java | 49 +++ 16 files changed, 836 insertions(+), 568 deletions(-) delete mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy delete mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy create mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTest.java create mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTraceChainTest.java delete mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy delete mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy create mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.java create mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.java delete mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.groovy delete mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.groovy delete mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.groovy create mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.java create mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.java create mode 100644 instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.java diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy deleted file mode 100644 index 36f81f58b828..000000000000 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.instrumentation.test.AgentTestTrait - -class DubboTest extends AbstractDubboTest implements AgentTestTrait { -} diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy deleted file mode 100644 index 432be0305633..000000000000 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.instrumentation.test.AgentTestTrait - -class DubboTraceChainTest extends AbstractDubboTraceChainTest implements AgentTestTrait { -} diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTest.java b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTest.java new file mode 100644 index 000000000000..1a7e2fb42405 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTest.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.instrumentation.apachedubbo.v2_7.AbstractDubboTest; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class DubboAgentTest extends AbstractDubboTest { + + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + @Override + protected InstrumentationExtension testing() { + return testing; + } +} diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTraceChainTest.java b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTraceChainTest.java new file mode 100644 index 000000000000..bf34ae8633fe --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/testDubbo/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentTraceChainTest.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.instrumentation.apachedubbo.v2_7.AbstractDubboTraceChainTest; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class DubboAgentTraceChainTest extends AbstractDubboTraceChainTest { + + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + @Override + protected InstrumentationExtension testing() { + return testing; + } +} 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 2b8e262bf7b0..eef5238bb46d 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 @@ -11,11 +11,13 @@ enum DubboHeadersGetter implements TextMapGetter { INSTANCE; @Override + @SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15 public Iterable keys(DubboRequest request) { return request.invocation().getAttachments().keySet(); } @Override + @SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15 public String get(DubboRequest request, String key) { return request.invocation().getAttachment(key); } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java index a19e8ef6d5db..d3d239096bb1 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java @@ -29,6 +29,7 @@ final class TracingFilter implements Filter { } @Override + @SuppressWarnings("deprecation") // deprecation for RpcContext.getContext() public Result invoke(Invoker invoker, Invocation invocation) { if (!(invocation instanceof RpcInvocation)) { return invoker.invoke(invocation); diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy deleted file mode 100644 index daa71bc333c6..000000000000 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.groovy +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.instrumentation.test.LibraryTestTrait - -class DubboTest extends AbstractDubboTest implements LibraryTestTrait { -} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy deleted file mode 100644 index ee0b1040dec5..000000000000 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.groovy +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.instrumentation.test.LibraryTestTrait - -class DubboTraceChainTest extends AbstractDubboTraceChainTest implements LibraryTestTrait { -} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.java new file mode 100644 index 000000000000..84667a51e074 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTest.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class DubboTest extends AbstractDubboTest { + + @RegisterExtension + static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); + + @Override + protected InstrumentationExtension testing() { + return testing; + } +} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.java new file mode 100644 index 000000000000..dab4e6f03150 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTraceChainTest.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class DubboTraceChainTest extends AbstractDubboTraceChainTest { + + @RegisterExtension + static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); + + @Override + protected InstrumentationExtension testing() { + return testing; + } +} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.groovy b/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.groovy deleted file mode 100644 index 6cd2d132a41d..000000000000 --- a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.groovy +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService -import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.HelloServiceImpl -import io.opentelemetry.instrumentation.test.InstrumentationSpecification -import io.opentelemetry.instrumentation.test.utils.PortUtils -import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes -import io.opentelemetry.semconv.ServerAttributes -import io.opentelemetry.semconv.NetworkAttributes -import org.apache.dubbo.common.utils.NetUtils -import org.apache.dubbo.config.ApplicationConfig -import org.apache.dubbo.config.ProtocolConfig -import org.apache.dubbo.config.ReferenceConfig -import org.apache.dubbo.config.RegistryConfig -import org.apache.dubbo.config.ServiceConfig -import org.apache.dubbo.config.bootstrap.DubboBootstrap -import org.apache.dubbo.rpc.service.GenericService -import spock.lang.Shared -import spock.lang.Unroll - -import static io.opentelemetry.api.trace.SpanKind.CLIENT -import static io.opentelemetry.api.trace.SpanKind.SERVER -import static io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTestUtil.newDubboBootstrap -import static io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTestUtil.newFrameworkModel - -@Unroll -abstract class AbstractDubboTest extends InstrumentationSpecification { - - @Shared - def protocolConfig = new ProtocolConfig() - - def setupSpec() { - NetUtils.LOCAL_ADDRESS = InetAddress.getLoopbackAddress() - } - - ReferenceConfig configureClient(int port) { - ReferenceConfig reference = new ReferenceConfig<>() - reference.setInterface(HelloService) - reference.setGeneric("true") - reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000") - return reference - } - - ServiceConfig configureServer() { - def registerConfig = new RegistryConfig() - registerConfig.setAddress("N/A") - ServiceConfig service = new ServiceConfig<>() - service.setInterface(HelloService) - service.setRef(new HelloServiceImpl()) - service.setRegistry(registerConfig) - return service - } - - def "test apache dubbo base #dubbo"() { - setup: - def port = PortUtils.findOpenPort() - protocolConfig.setPort(port) - - def frameworkModel = newFrameworkModel() - DubboBootstrap bootstrap = newDubboBootstrap(frameworkModel) - bootstrap.application(new ApplicationConfig("dubbo-test-provider")) - .service(configureServer()) - .protocol(protocolConfig) - .start() - - def consumerProtocolConfig = new ProtocolConfig() - consumerProtocolConfig.setRegister(false) - - def reference = configureClient(port) - DubboBootstrap consumerBootstrap = newDubboBootstrap(frameworkModel) - consumerBootstrap.application(new ApplicationConfig("dubbo-demo-api-consumer")) - .reference(reference) - .protocol(consumerProtocolConfig) - .start() - - when: - GenericService genericService = reference.get() - def o = new Object[1] - o[0] = "hello" - def response = runWithSpan("parent") { - genericService.$invoke("hello", [String.getName()] as String[], o) - } - - then: - response == "hello" - assertTraces(1) { - trace(0, 3) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "org.apache.dubbo.rpc.service.GenericService/\$invoke" - kind CLIENT - childOf span(0) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "org.apache.dubbo.rpc.service.GenericService" - "$RpcIncubatingAttributes.RPC_METHOD" "\$invoke" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - "$ServerAttributes.SERVER_PORT" Long - "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == null || it instanceof String} - "$NetworkAttributes.NETWORK_PEER_PORT" { it == null || it instanceof Long} - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(2) { - name "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello" - kind SERVER - childOf span(1) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService" - "$RpcIncubatingAttributes.RPC_METHOD" "hello" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" String - "$NetworkAttributes.NETWORK_PEER_PORT" Long - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - } - } - - cleanup: - bootstrap.destroy() - consumerBootstrap.destroy() - frameworkModel?.destroy() - } - - def "test apache dubbo test #dubbo"() { - setup: - def port = PortUtils.findOpenPort() - protocolConfig.setPort(port) - - def frameworkModel = newFrameworkModel() - DubboBootstrap bootstrap = newDubboBootstrap(frameworkModel) - bootstrap.application(new ApplicationConfig("dubbo-test-async-provider")) - .service(configureServer()) - .protocol(protocolConfig) - .start() - - def consumerProtocolConfig = new ProtocolConfig() - consumerProtocolConfig.setRegister(false) - - def reference = configureClient(port) - DubboBootstrap consumerBootstrap = newDubboBootstrap(frameworkModel) - consumerBootstrap.application(new ApplicationConfig("dubbo-demo-async-api-consumer")) - .reference(reference) - .protocol(consumerProtocolConfig) - .start() - - when: - GenericService genericService = reference.get() - def o = new Object[1] - o[0] = "hello" - def responseAsync = runWithSpan("parent") { - genericService.$invokeAsync("hello", [String.getName()] as String[], o) - } - - then: - responseAsync.get() == "hello" - assertTraces(1) { - trace(0, 3) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "org.apache.dubbo.rpc.service.GenericService/\$invokeAsync" - kind CLIENT - childOf span(0) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "org.apache.dubbo.rpc.service.GenericService" - "$RpcIncubatingAttributes.RPC_METHOD" "\$invokeAsync" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - "$ServerAttributes.SERVER_PORT" Long - "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == null || it instanceof String} - "$NetworkAttributes.NETWORK_PEER_PORT" { it == null || it instanceof Long} - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(2) { - name "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello" - kind SERVER - childOf span(1) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService" - "$RpcIncubatingAttributes.RPC_METHOD" "hello" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" String - "$NetworkAttributes.NETWORK_PEER_PORT" Long - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - } - } - - cleanup: - bootstrap.destroy() - consumerBootstrap.destroy() - frameworkModel?.destroy() - } -} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.groovy b/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.groovy deleted file mode 100644 index a7e2f6150b67..000000000000 --- a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.groovy +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService -import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService -import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.HelloServiceImpl -import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.MiddleServiceImpl -import io.opentelemetry.instrumentation.test.InstrumentationSpecification -import io.opentelemetry.instrumentation.test.utils.PortUtils -import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes -import io.opentelemetry.semconv.ServerAttributes -import io.opentelemetry.semconv.NetworkAttributes -import org.apache.dubbo.common.utils.NetUtils -import org.apache.dubbo.config.ApplicationConfig -import org.apache.dubbo.config.ProtocolConfig -import org.apache.dubbo.config.ReferenceConfig -import org.apache.dubbo.config.RegistryConfig -import org.apache.dubbo.config.ServiceConfig -import org.apache.dubbo.config.bootstrap.DubboBootstrap -import org.apache.dubbo.rpc.service.GenericService -import spock.lang.Unroll - -import static io.opentelemetry.api.trace.SpanKind.CLIENT -import static io.opentelemetry.api.trace.SpanKind.SERVER -import static io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTestUtil.newDubboBootstrap -import static io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTestUtil.newFrameworkModel - -@Unroll -abstract class AbstractDubboTraceChainTest extends InstrumentationSpecification { - - def setupSpec() { - NetUtils.LOCAL_ADDRESS = InetAddress.getLoopbackAddress() - } - - ReferenceConfig configureClient(int port) { - ReferenceConfig reference = new ReferenceConfig<>() - reference.setInterface(HelloService) - reference.setGeneric("true") - reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000") - return reference - } - - ReferenceConfig configureLocalClient(int port) { - ReferenceConfig reference = new ReferenceConfig<>() - reference.setInterface(HelloService) - reference.setGeneric("true") - reference.setUrl("injvm://localhost:" + port + "/?timeout=30000") - return reference - } - - ReferenceConfig configureMiddleClient(int port) { - ReferenceConfig reference = new ReferenceConfig<>() - reference.setInterface(MiddleService) - reference.setGeneric("true") - reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000") - return reference - } - - ServiceConfig configureServer() { - def registerConfig = new RegistryConfig() - registerConfig.setAddress("N/A") - ServiceConfig service = new ServiceConfig<>() - service.setInterface(HelloService) - service.setRef(new HelloServiceImpl()) - service.setRegistry(registerConfig) - return service - } - - ServiceConfig configureMiddleServer(ReferenceConfig referenceConfig) { - def registerConfig = new RegistryConfig() - registerConfig.setAddress("N/A") - ServiceConfig service = new ServiceConfig<>() - service.setInterface(MiddleService) - service.setRef(new MiddleServiceImpl(referenceConfig)) - service.setRegistry(registerConfig) - return service - } - - def "test that context is propagated correctly in chained dubbo calls"() { - setup: - def port = PortUtils.findOpenPorts(2) - def middlePort = port + 1 - def protocolConfig = new ProtocolConfig() - protocolConfig.setPort(port) - - def frameworkModel = newFrameworkModel() - DubboBootstrap bootstrap = newDubboBootstrap(frameworkModel) - bootstrap.application(new ApplicationConfig("dubbo-test-provider")) - .service(configureServer()) - .protocol(protocolConfig) - .start() - - def middleProtocolConfig = new ProtocolConfig() - middleProtocolConfig.setPort(middlePort) - - def reference = configureClient(port) - DubboBootstrap middleBootstrap = newDubboBootstrap(frameworkModel) - middleBootstrap.application(new ApplicationConfig("dubbo-demo-middle")) - .reference(reference) - .service(configureMiddleServer(reference)) - .protocol(middleProtocolConfig) - .start() - - - def consumerProtocolConfig = new ProtocolConfig() - consumerProtocolConfig.setRegister(false) - - def middleReference = configureMiddleClient(middlePort) - DubboBootstrap consumerBootstrap = newDubboBootstrap(frameworkModel) - consumerBootstrap.application(new ApplicationConfig("dubbo-demo-api-consumer")) - .reference(middleReference) - .protocol(consumerProtocolConfig) - .start() - - when: - GenericService genericService = middleReference.get() - def response = runWithSpan("parent") { - genericService.$invoke("hello", [String.getName()] as String[], ["hello"] as Object[]) - } - - then: - response == "hello" - assertTraces(1) { - trace(0, 5) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "org.apache.dubbo.rpc.service.GenericService/\$invoke" - kind CLIENT - childOf span(0) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "org.apache.dubbo.rpc.service.GenericService" - "$RpcIncubatingAttributes.RPC_METHOD" "\$invoke" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - "$ServerAttributes.SERVER_PORT" Long - "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == null || it instanceof String} - "$NetworkAttributes.NETWORK_PEER_PORT" { it == null || it instanceof Long} - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(2) { - name "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService/hello" - kind SERVER - childOf span(1) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService" - "$RpcIncubatingAttributes.RPC_METHOD" "hello" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" String - "$NetworkAttributes.NETWORK_PEER_PORT" Long - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(3) { - name "org.apache.dubbo.rpc.service.GenericService/\$invoke" - kind CLIENT - childOf span(2) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "org.apache.dubbo.rpc.service.GenericService" - "$RpcIncubatingAttributes.RPC_METHOD" "\$invoke" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - "$ServerAttributes.SERVER_PORT" Long - "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == null || it instanceof String } - "$NetworkAttributes.NETWORK_PEER_PORT" { it == null || it instanceof Long } - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(4) { - name "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello" - kind SERVER - childOf span(3) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService" - "$RpcIncubatingAttributes.RPC_METHOD" "hello" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" String - "$NetworkAttributes.NETWORK_PEER_PORT" Long - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - } - } - - cleanup: - bootstrap.destroy() - middleBootstrap.destroy() - consumerBootstrap.destroy() - } - - def "test ignore injvm calls"() { - setup: - def port = PortUtils.findOpenPorts(2) - def middlePort = port + 1 - def protocolConfig = new ProtocolConfig() - protocolConfig.setPort(port) - - def frameworkModel = newFrameworkModel() - DubboBootstrap bootstrap = newDubboBootstrap(frameworkModel) - bootstrap.application(new ApplicationConfig("dubbo-test-provider")) - .service(configureServer()) - .protocol(protocolConfig) - .start() - - def middleProtocolConfig = new ProtocolConfig() - middleProtocolConfig.setPort(middlePort) - - def reference = configureLocalClient(port) - DubboBootstrap middleBootstrap = newDubboBootstrap(frameworkModel) - middleBootstrap.application(new ApplicationConfig("dubbo-demo-middle")) - .reference(reference) - .service(configureMiddleServer(reference)) - .protocol(middleProtocolConfig) - .start() - - - def consumerProtocolConfig = new ProtocolConfig() - consumerProtocolConfig.setRegister(false) - - def middleReference = configureMiddleClient(middlePort) - DubboBootstrap consumerBootstrap = newDubboBootstrap(frameworkModel) - consumerBootstrap.application(new ApplicationConfig("dubbo-demo-api-consumer")) - .reference(middleReference) - .protocol(consumerProtocolConfig) - .start() - - when: - GenericService genericService = middleReference.get() - def response = runWithSpan("parent") { - genericService.$invoke("hello", [String.getName()] as String[], ["hello"] as Object[]) - } - - then: - response == "hello" - assertTraces(1) { - trace(0, 3) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "org.apache.dubbo.rpc.service.GenericService/\$invoke" - kind CLIENT - childOf span(0) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "org.apache.dubbo.rpc.service.GenericService" - "$RpcIncubatingAttributes.RPC_METHOD" "\$invoke" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - "$ServerAttributes.SERVER_PORT" Long - "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == null || it instanceof String} - "$NetworkAttributes.NETWORK_PEER_PORT" { it == null || it instanceof Long} - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - span(2) { - name "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService/hello" - kind SERVER - childOf span(1) - attributes { - "$RpcIncubatingAttributes.RPC_SYSTEM" "apache_dubbo" - "$RpcIncubatingAttributes.RPC_SERVICE" "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService" - "$RpcIncubatingAttributes.RPC_METHOD" "hello" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" String - "$NetworkAttributes.NETWORK_PEER_PORT" Long - "$NetworkAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null } - } - } - } - } - - cleanup: - bootstrap.destroy() - middleBootstrap.destroy() - consumerBootstrap.destroy() - } -} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.groovy b/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.groovy deleted file mode 100644 index 03ec85a3955d..000000000000 --- a/instrumentation/apache-dubbo-2.7/testing/src/main/groovy/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.groovy +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7 - -import org.apache.dubbo.config.bootstrap.DubboBootstrap - -class DubboTestUtil { - static newFrameworkModel() { - try { - // only present in latest dep - return Class.forName("org.apache.dubbo.rpc.model.FrameworkModel").newInstance() - } catch (ClassNotFoundException exception) { - return null - } - } - - static DubboBootstrap newDubboBootstrap(Object frameworkModel) { - if (frameworkModel == null) { - return DubboBootstrap.newInstance() - } - return DubboBootstrap.newInstance(frameworkModel) - } -} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.java b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.java new file mode 100644 index 000000000000..85ea385128cd --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTest.java @@ -0,0 +1,287 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.HelloServiceImpl; +import io.opentelemetry.instrumentation.test.utils.PortUtils; +import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.semconv.NetworkAttributes; +import io.opentelemetry.semconv.ServerAttributes; +import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes; +import java.lang.reflect.Field; +import java.net.InetAddress; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.rpc.service.GenericService; +import org.assertj.core.api.AbstractAssert; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public abstract class AbstractDubboTest { + + private final ProtocolConfig protocolConfig = new ProtocolConfig(); + + protected abstract InstrumentationExtension testing(); + + @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); + + @BeforeAll + static void setUp() throws Exception { + System.setProperty("dubbo.application.qos-enable", "false"); + Field field = NetUtils.class.getDeclaredField("LOCAL_ADDRESS"); + field.setAccessible(true); + field.set(null, InetAddress.getLoopbackAddress()); + } + + @AfterAll + static void tearDown() { + System.clearProperty("dubbo.application.qos-enable"); + } + + ReferenceConfig configureClient(int port) { + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(HelloService.class); + reference.setGeneric("true"); + reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000"); + return reference; + } + + ServiceConfig configureServer() { + RegistryConfig registerConfig = new RegistryConfig(); + registerConfig.setAddress("N/A"); + ServiceConfig service = new ServiceConfig<>(); + service.setInterface(HelloService.class); + service.setRef(new HelloServiceImpl()); + service.setRegistry(registerConfig); + return service; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + ReferenceConfig convertReference(ReferenceConfig config) { + return (ReferenceConfig) config; + } + + @Test + void testApacheDubboBase() throws ReflectiveOperationException { + int port = PortUtils.findOpenPort(); + protocolConfig.setPort(port); + // provider boostrap + DubboBootstrap bootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(bootstrap::destroy); + bootstrap + .application(new ApplicationConfig("dubbo-test-provider")) + .service(configureServer()) + .protocol(protocolConfig) + .start(); + + // consumer boostrap + DubboBootstrap consumerBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(consumerBootstrap::destroy); + ReferenceConfig referenceConfig = configureClient(port); + ProtocolConfig consumerProtocolConfig = new ProtocolConfig(); + consumerProtocolConfig.setRegister(false); + consumerBootstrap + .application(new ApplicationConfig("dubbo-demo-api-consumer")) + .reference(referenceConfig) + .protocol(consumerProtocolConfig) + .start(); + + // generic call + ReferenceConfig reference = convertReference(referenceConfig); + GenericService genericService = reference.get(); + + Object response = + runWithSpan( + "parent", + () -> + genericService.$invoke( + "hello", new String[] {String.class.getName()}, new Object[] {"hello"})); + + assertThat(response).isEqualTo("hello"); + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("org.apache.dubbo.rpc.service.GenericService/$invoke") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "org.apache.dubbo.rpc.service.GenericService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "$invoke"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), + satisfies( + ServerAttributes.SERVER_PORT, k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(String.class))), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(Long.class))), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName( + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttributesSatisfying( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "hello"), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> k.isInstanceOf(String.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_TYPE, AbstractAssert::isNull)))); + } + + @Test + void testApacheDubboTest() + throws ExecutionException, InterruptedException, ReflectiveOperationException { + int port = PortUtils.findOpenPort(); + protocolConfig.setPort(port); + + DubboBootstrap bootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(bootstrap::destroy); + bootstrap + .application(new ApplicationConfig("dubbo-test-async-provider")) + .service(configureServer()) + .protocol(protocolConfig) + .start(); + + ProtocolConfig consumerProtocolConfig = new ProtocolConfig(); + consumerProtocolConfig.setRegister(false); + + ReferenceConfig referenceConfig = configureClient(port); + DubboBootstrap consumerBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(consumerBootstrap::destroy); + consumerBootstrap + .application(new ApplicationConfig("dubbo-demo-async-api-consumer")) + .reference(referenceConfig) + .protocol(consumerProtocolConfig) + .start(); + + // generic call + ReferenceConfig reference = convertReference(referenceConfig); + GenericService genericService = reference.get(); + CompletableFuture response = + runWithSpan( + "parent", + () -> + genericService.$invokeAsync( + "hello", new String[] {String.class.getName()}, new Object[] {"hello"})); + + assertThat(response.get()).isEqualTo("hello"); + + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("org.apache.dubbo.rpc.service.GenericService/$invokeAsync") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "org.apache.dubbo.rpc.service.GenericService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "$invokeAsync"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), + satisfies( + ServerAttributes.SERVER_PORT, k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(String.class))), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(Long.class))), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName( + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttributesSatisfying( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "hello"), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> k.isInstanceOf(String.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))))); + } +} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.java b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.java new file mode 100644 index 000000000000..4f90694b49f8 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/AbstractDubboTraceChainTest.java @@ -0,0 +1,411 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.HelloServiceImpl; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.impl.MiddleServiceImpl; +import io.opentelemetry.instrumentation.test.utils.PortUtils; +import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.semconv.NetworkAttributes; +import io.opentelemetry.semconv.ServerAttributes; +import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes; +import java.lang.reflect.Field; +import java.net.InetAddress; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.rpc.service.GenericService; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public abstract class AbstractDubboTraceChainTest { + + @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); + + @BeforeAll + static void setUp() throws Exception { + System.setProperty("dubbo.application.qos-enable", "false"); + Field field = NetUtils.class.getDeclaredField("LOCAL_ADDRESS"); + field.setAccessible(true); + field.set(null, InetAddress.getLoopbackAddress()); + } + + @AfterAll + static void tearDown() { + System.clearProperty("dubbo.application.qos-enable"); + } + + protected abstract InstrumentationExtension testing(); + + ReferenceConfig configureClient(int port) { + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(HelloService.class); + reference.setGeneric("true"); + reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000"); + return reference; + } + + ReferenceConfig configureLocalClient(int port) { + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(HelloService.class); + reference.setGeneric("true"); + reference.setUrl("injvm://localhost:" + port + "/?timeout=30000"); + return reference; + } + + ReferenceConfig configureMiddleClient(int port) { + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(MiddleService.class); + reference.setGeneric("true"); + reference.setUrl("dubbo://localhost:" + port + "/?timeout=30000"); + return reference; + } + + ServiceConfig configureServer() { + RegistryConfig registerConfig = new RegistryConfig(); + registerConfig.setAddress("N/A"); + ServiceConfig service = new ServiceConfig<>(); + service.setInterface(HelloService.class); + service.setRef(new HelloServiceImpl()); + service.setRegistry(registerConfig); + return service; + } + + ServiceConfig configureMiddleServer( + ReferenceConfig referenceConfig) { + RegistryConfig registerConfig = new RegistryConfig(); + registerConfig.setAddress("N/A"); + ServiceConfig service = new ServiceConfig<>(); + service.setInterface(MiddleService.class); + service.setRef(new MiddleServiceImpl(referenceConfig)); + service.setRegistry(registerConfig); + return service; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + ReferenceConfig convertReference(ReferenceConfig config) { + return (ReferenceConfig) config; + } + + @Test + @DisplayName("test that context is propagated correctly in chained dubbo calls") + void testDubboChain() throws ReflectiveOperationException { + int port = PortUtils.findOpenPorts(2); + int middlePort = port + 1; + + // setup hello service provider + ProtocolConfig protocolConfig = new ProtocolConfig(); + protocolConfig.setPort(port); + + DubboBootstrap bootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(bootstrap::destroy); + bootstrap + .application(new ApplicationConfig("dubbo-test-provider")) + .service(configureServer()) + .protocol(protocolConfig) + .start(); + + // setup middle service provider, hello service consumer + ProtocolConfig middleProtocolConfig = new ProtocolConfig(); + middleProtocolConfig.setPort(middlePort); + + ReferenceConfig clientReference = configureClient(port); + DubboBootstrap middleBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(middleBootstrap::destroy); + middleBootstrap + .application(new ApplicationConfig("dubbo-demo-middle")) + .reference(clientReference) + .service(configureMiddleServer(clientReference)) + .protocol(middleProtocolConfig) + .start(); + + // setup middle service consumer + ProtocolConfig consumerProtocolConfig = new ProtocolConfig(); + consumerProtocolConfig.setRegister(false); + + ReferenceConfig middleReference = configureMiddleClient(middlePort); + DubboBootstrap consumerBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(consumerBootstrap::destroy); + consumerBootstrap + .application(new ApplicationConfig("dubbo-demo-api-consumer")) + .reference(middleReference) + .protocol(consumerProtocolConfig) + .start(); + + GenericService genericService = convertReference(middleReference).get(); + + Object response = + runWithSpan( + "parent", + () -> + genericService.$invoke( + "hello", new String[] {String.class.getName()}, new Object[] {"hello"})); + + assertThat(response).isEqualTo("hello"); + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("org.apache.dubbo.rpc.service.GenericService/$invoke") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "org.apache.dubbo.rpc.service.GenericService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "$invoke"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), + satisfies( + ServerAttributes.SERVER_PORT, k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(String.class))), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(Long.class))), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName( + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService/hello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttributesSatisfying( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "hello"), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> k.isInstanceOf(String.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName("org.apache.dubbo.rpc.service.GenericService/$invoke") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(2)) + .hasAttributesSatisfyingExactly( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "org.apache.dubbo.rpc.service.GenericService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "$invoke"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), + satisfies( + ServerAttributes.SERVER_PORT, k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(String.class))), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(Long.class))), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName( + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService/hello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(3)) + .hasAttributesSatisfying( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.HelloService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "hello"), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> k.isInstanceOf(String.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))))); + } + + @Test + @DisplayName("test ignore injvm calls") + void testDubboChainInJvm() throws ReflectiveOperationException { + int port = PortUtils.findOpenPorts(2); + int middlePort = port + 1; + + // setup hello service provider + ProtocolConfig protocolConfig = new ProtocolConfig(); + protocolConfig.setPort(port); + + DubboBootstrap bootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(bootstrap::destroy); + bootstrap + .application(new ApplicationConfig("dubbo-test-provider")) + .service(configureServer()) + .protocol(protocolConfig) + .start(); + + // setup middle service provider, hello service consumer + ProtocolConfig middleProtocolConfig = new ProtocolConfig(); + middleProtocolConfig.setPort(middlePort); + + ReferenceConfig clientReference = configureLocalClient(port); + DubboBootstrap middleBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(middleBootstrap::destroy); + middleBootstrap + .application(new ApplicationConfig("dubbo-demo-middle")) + .service(configureMiddleServer(clientReference)) + .protocol(middleProtocolConfig) + .start(); + + // setup middle service consumer + ProtocolConfig consumerProtocolConfig = new ProtocolConfig(); + consumerProtocolConfig.setRegister(false); + + ReferenceConfig middleReference = configureMiddleClient(middlePort); + DubboBootstrap consumerBootstrap = DubboTestUtil.newDubboBootstrap(); + cleanup.deferCleanup(consumerBootstrap::destroy); + consumerBootstrap + .application(new ApplicationConfig("dubbo-demo-api-consumer")) + .reference(middleReference) + .protocol(consumerProtocolConfig) + .start(); + + GenericService genericService = convertReference(middleReference).get(); + + Object response = + runWithSpan( + "parent", + () -> + genericService.$invoke( + "hello", new String[] {String.class.getName()}, new Object[] {"hello"})); + + assertThat(response).isEqualTo("hello"); + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("org.apache.dubbo.rpc.service.GenericService/$invoke") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "org.apache.dubbo.rpc.service.GenericService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "$invoke"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), + satisfies( + ServerAttributes.SERVER_PORT, k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(String.class))), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isInstanceOf(Long.class))), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))), + span -> + span.hasName( + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService/hello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttributesSatisfying( + equalTo( + RpcIncubatingAttributes.RPC_SYSTEM, + RpcIncubatingAttributes.RpcSystemValues.APACHE_DUBBO), + equalTo( + RpcIncubatingAttributes.RPC_SERVICE, + "io.opentelemetry.instrumentation.apachedubbo.v2_7.api.MiddleService"), + equalTo(RpcIncubatingAttributes.RPC_METHOD, "hello"), + satisfies( + NetworkAttributes.NETWORK_PEER_ADDRESS, + k -> k.isInstanceOf(String.class)), + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + k -> k.isInstanceOf(Long.class)), + satisfies( + NetworkAttributes.NETWORK_TYPE, + k -> + k.satisfiesAnyOf( + val -> assertThat(val).isNull(), + val -> assertThat(val).isEqualTo("ipv4"), + val -> assertThat(val).isEqualTo("ipv6")))))); + } +} diff --git a/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.java b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.java new file mode 100644 index 000000000000..1de3cef738ce --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/testing/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTestUtil.java @@ -0,0 +1,49 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; + +/** compatible with dubbo3.x and dubbo 2.7 */ +class DubboTestUtil { + + private DubboTestUtil() {} + + static Object newFrameworkModel() { + try { + // only present in latest dep + return Class.forName("org.apache.dubbo.rpc.model.FrameworkModel") + .getDeclaredConstructor() + .newInstance(); + } catch (ReflectiveOperationException exception) { + return null; + } + } + + static DubboBootstrap newDubboBootstrap() throws ReflectiveOperationException { + Object newFrameworkModel = newFrameworkModel(); + if (newFrameworkModel == null) { + return newDubboBootstrapV27(); + } else { + return newDubboBootstrapV3(newFrameworkModel); + } + } + + private static DubboBootstrap newDubboBootstrapV3(Object newFrameworkModel) + throws ReflectiveOperationException { + Method getInstance = + DubboBootstrap.class.getDeclaredMethod("newInstance", newFrameworkModel.getClass()); + return (DubboBootstrap) getInstance.invoke(null, newFrameworkModel); + } + + private static DubboBootstrap newDubboBootstrapV27() throws ReflectiveOperationException { + Constructor constructor = DubboBootstrap.class.getDeclaredConstructor(); + constructor.setAccessible(true); + return constructor.newInstance(); + } +}