From 93c783e4988c9f67bc7df3de6b2cacec36c439cb Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 11:53:57 +0800 Subject: [PATCH 01/10] fix Dubbo3.x Context problem #8525 --- .../plugin/asf/dubbo3/DubboInterceptor.java | 33 +++++++------- .../config/expectedData.yaml | 45 +++++++++++++++++++ .../apm/testcase/dubbo3/Application.java | 21 ++++++--- .../dubbo3/services/ExceptionService.java | 6 +++ .../services/impl/ExceptionServiceImpl.java | 11 +++++ .../services/impl/GreetServiceImpl.java | 21 +++++++++ 6 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java create mode 100644 test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 0a374e9c0c..6d2dbca90f 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -18,15 +18,9 @@ package org.apache.skywalking.apm.plugin.asf.dubbo3; -import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcContextAttachment; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcServiceContext; +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.rpc.*; import org.apache.skywalking.apm.agent.core.context.CarrierItem; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; @@ -65,15 +59,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr Invoker invoker = (Invoker) allArguments[0]; Invocation invocation = (Invocation) allArguments[1]; - RpcServiceContext serviceContext = RpcContext.getServiceContext(); - boolean isConsumer; - URL url = serviceContext.getUrl(); - if (url == null) { - url = serviceContext.getConsumerUrl(); - isConsumer = url != null; - } else { - isConsumer = serviceContext.isConsumerSide(); - } + boolean isConsumer = !isProvider((RpcInvocation) invocation); RpcContextAttachment attachment = isConsumer ? RpcContext.getClientAttachment() : RpcContext.getServerAttachment(); URL requestURL = invoker.getUrl(); @@ -151,6 +137,19 @@ private void dealException(Throwable throwable) { span.log(throwable); } + /** + * to judge if current is in provider side. + * + * @param rpcInvocation + * @return + */ + private static boolean isProvider(RpcInvocation rpcInvocation) { + Invoker invoker = rpcInvocation.getInvoker(); + return invoker.getUrl() + .getParameter("side", "consumer") + .equals("provider"); + } + /** * Format operation name. e.g. org.apache.skywalking.apm.plugin.test.Test.test(String) * diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml index 609c17d546..f37dd144cb 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml @@ -18,8 +18,53 @@ segmentItems: - serviceName: dubbo-3.x-scenario segmentSize: ge 3 segments: + - segmentId: not null + spans: + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: true + spanType: Entry + peer: not null + tags: + - { key: url, value: not null } + logs: + - logEvent: + - { key: event, value: error } + - { key: error.kind, value: java.lang.RuntimeException } + - { key: message, value: test exception! } + - { key: "stack", value: "not null" } + refs: + - { parentEndpoint: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String), networkAddress: not null, + refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, parentServiceInstance: not null, + parentService: dubbo-3.x-scenario, traceId: not null } + skipAnalysis: 'false' - segmentId: not null spans: + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() + parentSpanId: 0 + spanId: 1 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: true + spanType: Exit + peer: not null + tags: + - { key: url, value: not null } + logs: + - logEvent: + - { key: event, value: error } + - { key: error.kind, value: java.lang.RuntimeException } + - { key: message, value: test exception! } + - { key: "stack", value: "not null" } + skipAnalysis: 'false' - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String) parentSpanId: -1 spanId: 0 diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java index 238c416ecb..443b519ce3 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java @@ -18,13 +18,11 @@ package org.apache.skywalking.apm.testcase.dubbo3; -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.*; import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService; import org.apache.skywalking.apm.testcase.dubbo3.services.GreetService; +import org.apache.skywalking.apm.testcase.dubbo3.services.impl.ExceptionServiceImpl; import org.apache.skywalking.apm.testcase.dubbo3.services.impl.GreetServiceImpl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -64,6 +62,19 @@ public ServiceConfig service() { return serviceConfig; } + @Bean(destroyMethod = "unexport") + public ServiceConfig service2() { + ServiceConfig serviceConfig = new ServiceConfig<>(); + serviceConfig.setApplication(applicationConfig); + serviceConfig.setRegistry(registryConfig); + serviceConfig.setProtocol(protocolConfig); + serviceConfig.setInterface(ExceptionService.class); + serviceConfig.setRef(new ExceptionServiceImpl()); + serviceConfig.setTimeout(500000); + serviceConfig.export(); + return serviceConfig; + } + @Bean(destroyMethod = "destroy") public ReferenceConfig reference() { ReferenceConfig referenceConfig = new ReferenceConfig<>(); diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java new file mode 100644 index 0000000000..248d479329 --- /dev/null +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java @@ -0,0 +1,6 @@ +package org.apache.skywalking.apm.testcase.dubbo3.services; + +public interface ExceptionService { + + void exceptionCall(); +} diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java new file mode 100644 index 0000000000..dee4911ce7 --- /dev/null +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java @@ -0,0 +1,11 @@ +package org.apache.skywalking.apm.testcase.dubbo3.services.impl; + + +import org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService; + +public class ExceptionServiceImpl implements ExceptionService { + @Override + public void exceptionCall() { + throw new RuntimeException("test exception!"); + } +} diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/GreetServiceImpl.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/GreetServiceImpl.java index 472957cefa..1d05e93974 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/GreetServiceImpl.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/GreetServiceImpl.java @@ -18,12 +18,33 @@ package org.apache.skywalking.apm.testcase.dubbo3.services.impl; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService; import org.apache.skywalking.apm.testcase.dubbo3.services.GreetService; public class GreetServiceImpl implements GreetService { + private RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181"); + @Override public String doBusiness(String s) { + try { + ReferenceConfig referenceConfig = new ReferenceConfig<>(); + referenceConfig.setRegistry(registryConfig); + referenceConfig.setInterface(ExceptionService.class); + referenceConfig.setScope("remote"); + referenceConfig.setInjvm(false); + referenceConfig.setTimeout(500000); + referenceConfig.setAsync(false); + referenceConfig.setCheck(false); + + ExceptionService exceptionService = referenceConfig.get(); + exceptionService.exceptionCall(); + } catch (Exception e) { + // do nothing + e.printStackTrace(); + } return "{name:'" + s + "'}"; } } From 041ed5c069867f937527933f9fcf367992a7a44f Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 11:58:45 +0800 Subject: [PATCH 02/10] CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index d0faa98a31..ebb0236875 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Release Notes. * Support Tomcat thread pool metric collect. * Remove plugin for ServiceComb Java Chassis 0.x * Add Guava EventBus plugin. +* Fix Dubbo 3.x plugin's tracing problem. #### Documentation From c555104eee7d12e5ab6becc26267e839374019dc Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 13:54:24 +0800 Subject: [PATCH 03/10] refactor import and add licences --- .../plugin/asf/dubbo3/DubboInterceptor.java | 13 ++++++++----- .../dubbo3/services/ExceptionService.java | 18 ++++++++++++++++++ .../services/impl/ExceptionServiceImpl.java | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 6d2dbca90f..565b04fa36 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -20,7 +20,13 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.constants.CommonConstants; -import org.apache.dubbo.rpc.*; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcContextAttachment; +import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.RpcInvocation; import org.apache.skywalking.apm.agent.core.context.CarrierItem; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; @@ -138,10 +144,7 @@ private void dealException(Throwable throwable) { } /** - * to judge if current is in provider side. - * - * @param rpcInvocation - * @return + * To judge if current is in provider side. */ private static boolean isProvider(RpcInvocation rpcInvocation) { Invoker invoker = rpcInvocation.getInvoker(); diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java index 248d479329..05f16dbc58 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/ExceptionService.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package org.apache.skywalking.apm.testcase.dubbo3.services; public interface ExceptionService { diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java index dee4911ce7..f636b4d7c5 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package org.apache.skywalking.apm.testcase.dubbo3.services.impl; From c0d1d54109e6f9866b722fb5b5dd0ca84c530c74 Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 14:31:21 +0800 Subject: [PATCH 04/10] refactor import --- .../apache/skywalking/apm/testcase/dubbo3/Application.java | 6 +++++- .../testcase/dubbo3/services/impl/ExceptionServiceImpl.java | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java index 443b519ce3..5e4fa47d4e 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/Application.java @@ -18,7 +18,11 @@ package org.apache.skywalking.apm.testcase.dubbo3; -import org.apache.dubbo.config.*; +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.rpc.model.ApplicationModel; import org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService; import org.apache.skywalking.apm.testcase.dubbo3.services.GreetService; diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java index f636b4d7c5..732ed5407d 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java +++ b/test/plugin/scenarios/dubbo-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dubbo3/services/impl/ExceptionServiceImpl.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.testcase.dubbo3.services.impl; - import org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService; public class ExceptionServiceImpl implements ExceptionService { From d6b7544d88d2610b1b3974ff569b894698173319 Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 17:20:52 +0800 Subject: [PATCH 05/10] modify test data --- .../config/expectedData.yaml | 189 +++++++++--------- 1 file changed, 94 insertions(+), 95 deletions(-) diff --git a/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml index f37dd144cb..9f92f74cff 100644 --- a/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/dubbo-3.x-scenario/config/expectedData.yaml @@ -18,98 +18,97 @@ segmentItems: - serviceName: dubbo-3.x-scenario segmentSize: ge 3 segments: - - segmentId: not null - spans: - - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() - operationId: 0 - parentSpanId: -1 - spanId: 0 - spanLayer: RPCFramework - startTime: nq 0 - endTime: nq 0 - componentId: 3 - isError: true - spanType: Entry - peer: not null - tags: - - { key: url, value: not null } - logs: - - logEvent: - - { key: event, value: error } - - { key: error.kind, value: java.lang.RuntimeException } - - { key: message, value: test exception! } - - { key: "stack", value: "not null" } - refs: - - { parentEndpoint: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String), networkAddress: not null, - refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, parentServiceInstance: not null, - parentService: dubbo-3.x-scenario, traceId: not null } - skipAnalysis: 'false' - - segmentId: not null - spans: - - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() - parentSpanId: 0 - spanId: 1 - spanLayer: RPCFramework - startTime: nq 0 - endTime: nq 0 - componentId: 3 - isError: true - spanType: Exit - peer: not null - tags: - - { key: url, value: not null } - logs: - - logEvent: - - { key: event, value: error } - - { key: error.kind, value: java.lang.RuntimeException } - - { key: message, value: test exception! } - - { key: "stack", value: "not null" } - skipAnalysis: 'false' - - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String) - parentSpanId: -1 - spanId: 0 - spanLayer: RPCFramework - startTime: nq 0 - endTime: nq 0 - componentId: 3 - isError: false - spanType: Entry - peer: not null - tags: - - {key: url, value: not null} - - {key: arguments, value: helloWorld} - refs: - - {parentEndpoint: GET:/dubbo-3.x-scenario/case/dubbo, networkAddress: not null, - refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, - parentServiceInstance: not null, parentService: dubbo-3.x-scenario, traceId: not null} - skipAnalysis: 'false' - - segmentId: not null - spans: - - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String) - parentSpanId: 0 - spanId: 1 - spanLayer: RPCFramework - startTime: nq 0 - endTime: nq 0 - componentId: 3 - isError: false - spanType: Exit - peer: not null - tags: - - {key: url, value: not null} - - {key: arguments, value: helloWorld} - skipAnalysis: 'false' - - operationName: GET:/dubbo-3.x-scenario/case/dubbo - parentSpanId: -1 - spanId: 0 - spanLayer: Http - startTime: nq 0 - endTime: nq 0 - componentId: 1 - isError: false - spanType: Entry - peer: '' - tags: - - {key: url, value: 'http://localhost:8080/dubbo-3.x-scenario/case/dubbo'} - - {key: http.method, value: GET} - skipAnalysis: 'false' + - segmentId: not null + spans: + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() + parentSpanId: -1 + spanId: 0 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: true + spanType: Entry + peer: not null + tags: + - { key: url, value: not null } + logs: + - logEvent: + - { key: event, value: error } + - { key: error.kind, value: java.lang.RuntimeException } + - { key: message, value: test exception! } + - { key: "stack", value: "not null" } + refs: + - { parentEndpoint: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String), networkAddress: not null, + refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, parentServiceInstance: not null, + parentService: dubbo-3.x-scenario, traceId: not null } + skipAnalysis: 'false' + - segmentId: not null + spans: + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.ExceptionService.exceptionCall() + parentSpanId: 0 + spanId: 1 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: true + spanType: Exit + peer: not null + tags: + - { key: url, value: not null } + logs: + - logEvent: + - { key: event, value: error } + - { key: error.kind, value: java.lang.RuntimeException } + - { key: message, value: test exception! } + - { key: "stack", value: "not null" } + skipAnalysis: 'false' + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String) + parentSpanId: -1 + spanId: 0 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: false + spanType: Entry + peer: not null + tags: + - {key: url, value: not null} + - {key: arguments, value: helloWorld} + refs: + - {parentEndpoint: GET:/dubbo-3.x-scenario/case/dubbo, networkAddress: not null, + refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, + parentServiceInstance: not null, parentService: dubbo-3.x-scenario, traceId: not null} + skipAnalysis: 'false' + - segmentId: not null + spans: + - operationName: org.apache.skywalking.apm.testcase.dubbo3.services.GreetService.doBusiness(String) + parentSpanId: 0 + spanId: 1 + spanLayer: RPCFramework + startTime: nq 0 + endTime: nq 0 + componentId: 3 + isError: false + spanType: Exit + peer: not null + tags: + - {key: url, value: not null} + - {key: arguments, value: helloWorld} + skipAnalysis: 'false' + - operationName: GET:/dubbo-3.x-scenario/case/dubbo + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: nq 0 + endTime: nq 0 + componentId: 1 + isError: false + spanType: Entry + peer: '' + tags: + - {key: url, value: 'http://localhost:8080/dubbo-3.x-scenario/case/dubbo'} + - {key: http.method, value: GET} + skipAnalysis: 'false' From a703ac4a7eb78768c401d66d8cb1087c26417141 Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 11 Feb 2022 20:25:22 +0800 Subject: [PATCH 06/10] fix log error twice problem --- .../apm/plugin/asf/dubbo/DubboInterceptor.java | 8 -------- .../apm/plugin/asf/dubbo3/DubboInterceptor.java | 14 +++----------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java index 4aff0bf805..f5dca326f4 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java @@ -110,14 +110,6 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { Result result = (Result) ret; - try { - if (result != null && result.getException() != null) { - dealException(result.getException()); - } - } catch (RpcException e) { - dealException(e); - } - ContextManager.stopSpan(); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 565b04fa36..1e5a66dd3d 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -65,7 +65,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr Invoker invoker = (Invoker) allArguments[0]; Invocation invocation = (Invocation) allArguments[1]; - boolean isConsumer = !isProvider((RpcInvocation) invocation); + boolean isConsumer = !isProvider(invocation); RpcContextAttachment attachment = isConsumer ? RpcContext.getClientAttachment() : RpcContext.getServerAttachment(); URL requestURL = invoker.getUrl(); @@ -117,14 +117,6 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { Result result = (Result) ret; - try { - if (result != null && result.getException() != null) { - dealException(result.getException()); - } - } catch (RpcException e) { - dealException(e); - } - ContextManager.stopSpan(); return ret; } @@ -146,8 +138,8 @@ private void dealException(Throwable throwable) { /** * To judge if current is in provider side. */ - private static boolean isProvider(RpcInvocation rpcInvocation) { - Invoker invoker = rpcInvocation.getInvoker(); + private static boolean isProvider(Invocation invocation) { + Invoker invoker = invocation.getInvoker(); return invoker.getUrl() .getParameter("side", "consumer") .equals("provider"); From 6c1f6801df890acc9a81dd33c1293ceed336995f Mon Sep 17 00:00:00 2001 From: honganan Date: Sat, 12 Feb 2022 12:14:48 +0800 Subject: [PATCH 07/10] refactor imports --- .../skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java | 1 - .../skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java index f5dca326f4..1456c1929a 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java @@ -24,7 +24,6 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcException; import org.apache.skywalking.apm.agent.core.context.CarrierItem; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 1e5a66dd3d..48ad875ae8 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -25,8 +25,6 @@ import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcContextAttachment; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcInvocation; import org.apache.skywalking.apm.agent.core.context.CarrierItem; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; From 805be6ae8c854b6e0c281af6cc4dab91485caa18 Mon Sep 17 00:00:00 2001 From: honganan Date: Thu, 24 Feb 2022 19:09:11 +0800 Subject: [PATCH 08/10] fix enhance twice causing of class inherit problem --- .../plugin/asf/dubbo/DubboInterceptor.java | 9 +++++++ .../dubbo3/DubboConsumerInstrumentation.java | 27 ------------------- ...ionBase.java => DubboInstrumentation.java} | 14 +++------- .../plugin/asf/dubbo3/DubboInterceptor.java | 7 +++++ .../dubbo3/DubboProviderInstrumentation.java | 27 ------------------- .../src/main/resources/skywalking-plugin.def | 3 +-- .../plugin/dubbo3/DubboInterceptorTest.java | 6 ++--- 7 files changed, 23 insertions(+), 70 deletions(-) delete mode 100644 apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboConsumerInstrumentation.java rename apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/{DubboInstrumentationBase.java => DubboInstrumentation.java} (86%) delete mode 100644 apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboProviderInstrumentation.java diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java index 1456c1929a..4aff0bf805 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo/DubboInterceptor.java @@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcException; import org.apache.skywalking.apm.agent.core.context.CarrierItem; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; @@ -109,6 +110,14 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { Result result = (Result) ret; + try { + if (result != null && result.getException() != null) { + dealException(result.getException()); + } + } catch (RpcException e) { + dealException(e); + } + ContextManager.stopSpan(); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboConsumerInstrumentation.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboConsumerInstrumentation.java deleted file mode 100644 index 27d206ccad..0000000000 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboConsumerInstrumentation.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.skywalking.apm.plugin.asf.dubbo3; - -public class DubboConsumerInstrumentation extends DubboInstrumentationBase { - - public DubboConsumerInstrumentation() { - super(CONSUMER_ENHANCE_CLASS); - } - -} diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentationBase.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentation.java similarity index 86% rename from apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentationBase.java rename to apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentation.java index c983b9c0a0..3b45d0304d 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentationBase.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInstrumentation.java @@ -33,11 +33,9 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.returns; -public class DubboInstrumentationBase extends ClassInstanceMethodsEnhancePluginDefine { +public class DubboInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { - public static final String PROVIDER_ENHANCE_CLASS = "org.apache.dubbo.monitor.support.MonitorFilter"; - - public static final String CONSUMER_ENHANCE_CLASS = "org.apache.dubbo.monitor.support.MonitorClusterFilter"; + public static final String ENHANCE_CLASS = "org.apache.dubbo.monitor.support.MonitorFilter"; public static final String INTERCEPT_POINT_METHOD = "invoke"; @@ -49,15 +47,9 @@ public class DubboInstrumentationBase extends ClassInstanceMethodsEnhancePluginD public static final String CONTEXT_ATTACHMENT_TYPE_NAME = "org.apache.dubbo.rpc.RpcContextAttachment"; - private final String enhanceClassName; - - public DubboInstrumentationBase(String enhanceClassName) { - this.enhanceClassName = enhanceClassName; - } - @Override protected ClassMatch enhanceClass() { - return NameMatch.byName(enhanceClassName); + return NameMatch.byName(ENHANCE_CLASS); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 48ad875ae8..6d5fdf45db 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -115,6 +115,10 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { Result result = (Result) ret; + if (result != null && result.getException() != null) { + dealException(result.getException()); + } + ContextManager.stopSpan(); return ret; } @@ -138,6 +142,9 @@ private void dealException(Throwable throwable) { */ private static boolean isProvider(Invocation invocation) { Invoker invoker = invocation.getInvoker(); + // As RpcServiceContext may not been reset when it's role switched from provider + // to consumer in the same thread, but RpcInvocation is always correctly bounded + // to the current request or serve request, https://github.com/apache/skywalking-java/pull/110 return invoker.getUrl() .getParameter("side", "consumer") .equals("provider"); diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboProviderInstrumentation.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboProviderInstrumentation.java deleted file mode 100644 index 183c9bbab0..0000000000 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboProviderInstrumentation.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.skywalking.apm.plugin.asf.dubbo3; - -public class DubboProviderInstrumentation extends DubboInstrumentationBase { - - public DubboProviderInstrumentation() { - super(PROVIDER_ENHANCE_CLASS); - } - -} diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/resources/skywalking-plugin.def index 4b5a95e797..efa61d9e5a 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -14,5 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -dubbo-3.x=org.apache.skywalking.apm.plugin.asf.dubbo3.DubboProviderInstrumentation -dubbo-3.x=org.apache.skywalking.apm.plugin.asf.dubbo3.DubboConsumerInstrumentation +dubbo-3.x=org.apache.skywalking.apm.plugin.asf.dubbo3.DubboInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java index b432314ff8..d39aa20de2 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java @@ -108,6 +108,7 @@ public void setUp() throws Exception { when(invocation.getMethodName()).thenReturn("test"); when(invocation.getParameterTypes()).thenReturn(new Class[] {String.class}); when(invocation.getArguments()).thenReturn(new Object[] {"abc"}); + when(invocation.getInvoker()).thenReturn(invoker); when(RpcContext.getServiceContext()).thenReturn(serviceContext); when(serviceContext.getUrl()).thenReturn(null); when(serviceContext.getConsumerUrl()).thenReturn(url); @@ -175,9 +176,8 @@ public void testConsumerWithResultHasException() throws Throwable { @Test public void testProviderWithAttachment() throws Throwable { - when(serviceContext.getUrl()).thenReturn( - URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService")); - when(serviceContext.getConsumerUrl()).thenReturn(null); + when(invoker.getUrl()).thenReturn( + URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService?side=provider")); when(RpcContext.getServerAttachment()).thenReturn(contextAttachment); when(contextAttachment.getAttachment( SW8CarrierItem.HEADER_NAME)).thenReturn( From 10f45e8b5144b390c24e32521bed216dfaf4148e Mon Sep 17 00:00:00 2001 From: hongan Date: Thu, 24 Feb 2022 21:33:55 +0800 Subject: [PATCH 09/10] refactor method name --- .../apm/plugin/asf/dubbo3/DubboInterceptor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java index 6d5fdf45db..b31db6d580 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/asf/dubbo3/DubboInterceptor.java @@ -63,7 +63,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr Invoker invoker = (Invoker) allArguments[0]; Invocation invocation = (Invocation) allArguments[1]; - boolean isConsumer = !isProvider(invocation); + boolean isConsumer = isConsumer(invocation); RpcContextAttachment attachment = isConsumer ? RpcContext.getClientAttachment() : RpcContext.getServerAttachment(); URL requestURL = invoker.getUrl(); @@ -140,14 +140,14 @@ private void dealException(Throwable throwable) { /** * To judge if current is in provider side. */ - private static boolean isProvider(Invocation invocation) { + private static boolean isConsumer(Invocation invocation) { Invoker invoker = invocation.getInvoker(); // As RpcServiceContext may not been reset when it's role switched from provider // to consumer in the same thread, but RpcInvocation is always correctly bounded // to the current request or serve request, https://github.com/apache/skywalking-java/pull/110 return invoker.getUrl() - .getParameter("side", "consumer") - .equals("provider"); + .getParameter("side", "provider") + .equals("consumer"); } /** From 0b9a1ab2e5aa7be6c055eff6bd9bb98cf0e53592 Mon Sep 17 00:00:00 2001 From: honganan Date: Fri, 25 Feb 2022 11:36:38 +0800 Subject: [PATCH 10/10] enhance unit test scenario --- .../plugin/dubbo3/DubboInterceptorTest.java | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java index d39aa20de2..1ab5dd9b01 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/dubbo3/DubboInterceptorTest.java @@ -87,40 +87,52 @@ public class DubboInterceptorTest { @Mock private RpcContextAttachment contextAttachment; @Mock - private Invoker invoker; + private Invoker consumerInvoker; @Mock - private Invocation invocation; + private Invoker providerInvoker; + @Mock + private Invocation consumerInvocation; + @Mock + private Invocation providerInvocation; @Mock private MethodInterceptResult methodInterceptResult; @Mock private Result result; - private Object[] allArguments; + private Object[] consumerArguments; + private Object[] providerArguments; private Class[] argumentTypes; + private static final URL CONSUMER_URL = URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService?side=consumer"); + private static final URL PROVIDER_URL = URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService?side=provider"); @Before public void setUp() throws Exception { dubboInterceptor = new DubboInterceptor(); PowerMockito.mockStatic(RpcContext.class); - URL url = URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService"); - when(invoker.getUrl()).thenReturn(url); - when(invocation.getMethodName()).thenReturn("test"); - when(invocation.getParameterTypes()).thenReturn(new Class[] {String.class}); - when(invocation.getArguments()).thenReturn(new Object[] {"abc"}); - when(invocation.getInvoker()).thenReturn(invoker); - when(RpcContext.getServiceContext()).thenReturn(serviceContext); - when(serviceContext.getUrl()).thenReturn(null); - when(serviceContext.getConsumerUrl()).thenReturn(url); + when(consumerInvoker.getUrl()).thenReturn(CONSUMER_URL); + when(consumerInvocation.getMethodName()).thenReturn("test"); + when(consumerInvocation.getParameterTypes()).thenReturn(new Class[] {String.class}); + when(consumerInvocation.getArguments()).thenReturn(new Object[] {"abc"}); + when(consumerInvocation.getInvoker()).thenReturn(consumerInvoker); when(RpcContext.getClientAttachment()).thenReturn(contextAttachment); + consumerArguments = new Object[] { + consumerInvoker, + consumerInvocation + }; - allArguments = new Object[] { - invoker, - invocation + when(providerInvoker.getUrl()).thenReturn(PROVIDER_URL); + when(providerInvocation.getMethodName()).thenReturn("test"); + when(providerInvocation.getParameterTypes()).thenReturn(new Class[] {String.class}); + when(providerInvocation.getArguments()).thenReturn(new Object[] {"abc"}); + when(providerInvocation.getInvoker()).thenReturn(providerInvoker); + providerArguments = new Object[] { + providerInvoker, + providerInvocation }; argumentTypes = new Class[] { - invoker.getClass(), - invocation.getClass() + consumerInvoker.getClass(), + consumerInvocation.getClass() }; Config.Agent.SERVICE_NAME = "DubboTestCases-APP"; } @@ -141,8 +153,8 @@ public void testServiceOverrideFromPlugin() { @Test public void testConsumerWithAttachment() throws Throwable { - dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult); - dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result); + dubboInterceptor.beforeMethod(enhancedInstance, null, consumerArguments, argumentTypes, methodInterceptResult); + dubboInterceptor.afterMethod(enhancedInstance, null, consumerArguments, argumentTypes, result); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -153,10 +165,10 @@ public void testConsumerWithAttachment() throws Throwable { @Test public void testConsumerWithException() throws Throwable { - dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult); + dubboInterceptor.beforeMethod(enhancedInstance, null, consumerArguments, argumentTypes, methodInterceptResult); dubboInterceptor.handleMethodException( - enhancedInstance, null, allArguments, argumentTypes, new RuntimeException()); - dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result); + enhancedInstance, null, consumerArguments, argumentTypes, new RuntimeException()); + dubboInterceptor.afterMethod(enhancedInstance, null, consumerArguments, argumentTypes, result); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); assertConsumerTraceSegmentInErrorCase(traceSegment); @@ -166,8 +178,8 @@ public void testConsumerWithException() throws Throwable { public void testConsumerWithResultHasException() throws Throwable { when(result.getException()).thenReturn(new RuntimeException()); - dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult); - dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result); + dubboInterceptor.beforeMethod(enhancedInstance, null, consumerArguments, argumentTypes, methodInterceptResult); + dubboInterceptor.afterMethod(enhancedInstance, null, consumerArguments, argumentTypes, result); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -176,15 +188,14 @@ public void testConsumerWithResultHasException() throws Throwable { @Test public void testProviderWithAttachment() throws Throwable { - when(invoker.getUrl()).thenReturn( - URL.valueOf("dubbo://127.0.0.1:20880/org.apache.skywalking.apm.test.TestDubboService?side=provider")); + when(providerInvoker.getUrl()).thenReturn(PROVIDER_URL); when(RpcContext.getServerAttachment()).thenReturn(contextAttachment); when(contextAttachment.getAttachment( SW8CarrierItem.HEADER_NAME)).thenReturn( "1-My40LjU=-MS4yLjM=-3-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA="); - dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult); - dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result); + dubboInterceptor.beforeMethod(enhancedInstance, null, providerArguments, argumentTypes, methodInterceptResult); + dubboInterceptor.afterMethod(enhancedInstance, null, providerArguments, argumentTypes, result); assertProvider(); }