This is an automated email from the ASF dual-hosted git repository. kezhenxu94 pushed a commit to branch spring6 in repository https://gitbox.apache.org/repos/asf/skywalking-java.git
commit 8ac1effebac76239c77b3dd28f4c5000c5a9d50b Author: kezhenxu94 <kezhenx...@apache.org> AuthorDate: Fri Feb 24 14:11:19 2023 +0800 Try to fix Spring 6 test case --- .github/workflows/plugins-jdk17-test.1.yaml | 3 + .github/workflows/plugins-test.2.yaml | 23 +----- .github/workflows/plugins-test.3.yaml | 53 +------------- apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml | 1 + .../spring-plugins/resttemplate-4.x-plugin/pom.xml | 5 ++ ... ResponseExtractorFutureInstrumentation4x.java} | 60 +-------------- ...ion.java => RestTemplateInstrumentation4x.java} | 59 +-------------- .../sync/define/RestTemplateInstrumentation4x.java | 47 ++++++++++++ .../src/main/resources/skywalking-plugin.def | 6 +- .../pom.xml | 6 +- .../resttemplate/async/FutureGetInterceptor.java | 51 +++++++++++++ .../async/ResponseCallBackInterceptor.java | 51 +++++++++++++ .../resttemplate/async/RestExecuteInterceptor.java | 85 ++++++++++++++++++++++ .../resttemplate/async/RestRequestInterceptor.java | 58 +++++++++++++++ .../ResponseExtractorFutureInstrumentation.java | 0 .../async/define/RestTemplateInstrumentation.java | 0 .../helper/RestTemplateRuntimeContextHelper.java | 53 ++++++++++++++ .../resttemplate/sync/RestExecuteInterceptor.java | 70 ++++++++++++++++++ .../resttemplate/sync/RestRequestInterceptor.java | 60 +++++++++++++++ .../resttemplate/sync/RestResponseInterceptor.java | 57 +++++++++++++++ .../sync/define/RestTemplateInstrumentation.java | 9 +-- .../sync/define/RestTemplateInstrumentation.java | 7 +- 22 files changed, 562 insertions(+), 202 deletions(-) diff --git a/.github/workflows/plugins-jdk17-test.1.yaml b/.github/workflows/plugins-jdk17-test.1.yaml index d820466d8c..6a671c312b 100644 --- a/.github/workflows/plugins-jdk17-test.1.yaml +++ b/.github/workflows/plugins-jdk17-test.1.yaml @@ -17,6 +17,9 @@ name: Test on: + push: + branches: + - spring6 pull_request: paths: - '.github/workflows/plugins-*.yaml' diff --git a/.github/workflows/plugins-test.2.yaml b/.github/workflows/plugins-test.2.yaml index 1b6ecaa9f9..c58dd82782 100644 --- a/.github/workflows/plugins-test.2.yaml +++ b/.github/workflows/plugins-test.2.yaml @@ -17,6 +17,9 @@ name: Test on: + push: + branches: + - spring6 pull_request: paths: - '.github/workflows/plugins-*.yaml' @@ -52,31 +55,11 @@ jobs: strategy: matrix: case: - - okhttp-scenario - - play-scenario - - postgresql-scenario - - pulsar-scenario - - rabbitmq-scenario - - redisson-scenario - - resttemplate-4.x-scenario - - servicecomb-1.x-scenario - - servicecomb-2.x-scenario - - shardingsphere-3.x-scenario - - shardingsphere-4.0.x-scenario - - shardingsphere-4.x-scenario - - shardingsphere-5.0.0-scenario - - sofarpc-scenario - - solrj-7.x-scenario - spring-3.0.x-scenario - spring-cloud-feign-1.1.x-scenario - spring-cloud-feign-1.2.x-scenario - spring-cloud-feign-2.x-scenario - spring-tx-scenario - - struts2.3-scenario - - struts2.5-scenario - - cxf-scenario - - okhttp2-scenario - - rocketmq-scenario steps: - uses: actions/checkout@v2 with: diff --git a/.github/workflows/plugins-test.3.yaml b/.github/workflows/plugins-test.3.yaml index 3127472d31..3d917bdd89 100644 --- a/.github/workflows/plugins-test.3.yaml +++ b/.github/workflows/plugins-test.3.yaml @@ -17,6 +17,9 @@ name: Test on: + push: + branches: + - spring6 pull_request: paths: - '.github/workflows/plugins-*.yaml' @@ -52,60 +55,10 @@ jobs: strategy: matrix: case: - - mysql-scenario - - undertow-scenario - - webflux-scenario - - zookeeper-scenario - spring-3.1.x-scenario - spring-4.1.x-scenario - spring-4.3.x-scenario - spring-async-scenario - - vertx-core-4.x-scenario - - vertx-eventbus-3.x-scenario - - vertx-web-3.54minus-scenario - - vertx-web-3.6plus-scenario - - mariadb-scenario - - micronaut-http-scenario - - nats-2.14.x-2.15.x-scenario - - quasar-scenario - - baidu-brpc-scenario - - baidu-brpc-3.x-scenario - - retransform-class-scenario - - retransform-class-tomcat-scenario - - graphql-8.x-scenario - - graphql-9.x-scenario - - graphql-16plus-scenario - - graphql-12.x-15.x-scenario - - hbase-scenario - - spring-kafka-1.3.x-scenario - - spring-kafka-2.2.x-scenario - - spring-kafka-2.3.x-scenario - - spring-scheduled-scenario - - elasticjob-2.x-scenario - - quartz-scheduler-2.x-scenario - - xxl-job-2.2.0-scenario - - xxl-job-2.3.x-scenario - - thrift-scenario - - dbcp-2.x-scenario - - jsonrpc4j-1.x-scenario - - gateway-3.x-scenario - - neo4j-4.x-scenario - - oracle-scenario - - druid-1.x-scenario - - hikaricp-scenario - - clickhouse-0.3.1-scenario - - clickhouse-0.3.2.x-scenario - - kylin-jdbc-2.6.x-3.x-4.x-scenario - - undertow-worker-thread-pool-scenario - - tomcat-thread-pool-scenario - - guava-eventbus-scenario - - shenyu-2.4.x-scenario - - jdk-threadpool-scenario - - shenyu-2.4.x-dubbo-scenario - - grpc-generic-call-scenario - - shenyu-2.4.x-grpc-scenario - - shenyu-2.4.x-sofarpc-scenario - - impala-jdbc-2.6.x-scenario steps: - uses: actions/checkout@v2 with: diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml index da106f5e68..2d50091fe4 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml @@ -42,6 +42,7 @@ <module>spring-kafka-2.x-plugin</module> <module>scheduled-annotation-plugin</module> <module>spring-webflux-5.x-webclient-plugin</module> + <module>resttemplate-commons</module> </modules> <packaging>pom</packaging> diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml index bcc756a8af..071a420215 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml @@ -47,5 +47,10 @@ <artifactId>spring-commons</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.skywalking</groupId> + <artifactId>resttemplate-commons</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project> diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation4x.java similarity index 51% copy from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java copy to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation4x.java index cc42085927..549b5ddb88 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation4x.java @@ -32,7 +32,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterc import static net.bytebuddy.matcher.ElementMatchers.named; /** - * {@link ResponseExtractorFutureInstrumentation} enhance the <code>addCallback</code> method and + * {@link ResponseExtractorFutureInstrumentation4x} enhance the <code>addCallback</code> method and * <code>getDefault</code> method of <code>org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture</code> * by * <code>ResponseCallBackInterceptor</code> and @@ -41,63 +41,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; * {@link ResponseCallBackInterceptor} set the {@link URI} and {@link ContextSnapshot} to inherited * <code>org.springframework.util.concurrent.SuccessCallback</code> and <code>org.springframework.util.concurrent.FailureCallback</code> */ -public class ResponseExtractorFutureInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { - - private static final String ADD_CALLBACK_METHOD_NAME = "addCallback"; - private static final String ADD_CALLBACK_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.resttemplate.async.ResponseCallBackInterceptor"; - private static final String ENHANCE_CLASS = "org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture"; - private static final String GET_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.resttemplate.async.FutureGetInterceptor"; - private static final String GET_METHOD_NAME = "get"; - - @Override - public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[0]; - - } - - @Override - public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher<MethodDescription> getMethodsMatcher() { - return named(ADD_CALLBACK_METHOD_NAME); - } - - @Override - public String getMethodsInterceptor() { - return ADD_CALLBACK_INTERCEPTOR; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher<MethodDescription> getMethodsMatcher() { - return named(GET_METHOD_NAME); - } - - @Override - public String getMethodsInterceptor() { - return GET_METHOD_INTERCEPTOR; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - } - }; - } - - @Override - protected ClassMatch enhanceClass() { - return NameMatch.byName(ENHANCE_CLASS); - } - +public class ResponseExtractorFutureInstrumentation4x extends ResponseExtractorFutureInstrumentation { @Override protected String[] witnessClasses() { return new String[] { diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation4x.java similarity index 50% copy from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java copy to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation4x.java index c0722f8326..1dbe763642 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation4x.java @@ -31,7 +31,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterc import static net.bytebuddy.matcher.ElementMatchers.named; /** - * {@link RestTemplateInstrumentation} enhance the <code>doExecute</code> method and <code>createAsyncRequest</code> + * {@link RestTemplateInstrumentation4x} enhance the <code>doExecute</code> method and <code>createAsyncRequest</code> * method of <code>org.springframework.web.client.AsyncRestTemplate</code> by <code>RestExecuteInterceptor</code> and * <code>org.springframework.http.client.RestRequestInterceptor</code>. * @@ -39,62 +39,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; * <code>org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture</code> for propagate trace context * after execute <code>doExecute</code> . */ -public class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { - - private static final String ENHANCE_CLASS = "org.springframework.web.client.AsyncRestTemplate"; - private static final String DO_EXECUTE_METHOD_NAME = "doExecute"; - private static final String DO_EXECUTE_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.resttemplate.async.RestExecuteInterceptor"; - private static final String CREATE_REQUEST_METHOD_NAME = "createAsyncRequest"; - private static final String CREATE_REQUEST_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.resttemplate.async.RestRequestInterceptor"; - - @Override - public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[0]; - } - - @Override - public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher<MethodDescription> getMethodsMatcher() { - return named(DO_EXECUTE_METHOD_NAME); - } - - @Override - public String getMethodsInterceptor() { - return DO_EXECUTE_INTERCEPTOR; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher<MethodDescription> getMethodsMatcher() { - return named(CREATE_REQUEST_METHOD_NAME); - } - - @Override - public String getMethodsInterceptor() { - return CREATE_REQUEST_INTERCEPTOR; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - } - }; - } - - @Override - protected ClassMatch enhanceClass() { - return NameMatch.byName(ENHANCE_CLASS); - } - +public class RestTemplateInstrumentation4x extends RestTemplateInstrumentation { @Override protected String[] witnessClasses() { return new String[] { diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation4x.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation4x.java new file mode 100644 index 0000000000..2c32db1a53 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation4x.java @@ -0,0 +1,47 @@ +/* + * 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.spring.resttemplate.sync.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * {@link RestTemplateInstrumentation} enhance the <code>doExecute</code> method,<code>handleResponse</code> method and + * <code>handleResponse</code> method of <code>org.springframework.web.client.RestTemplate</code> by + * <code>RestExecuteInterceptor</code>, + * <code>RestResponseInterceptor</code> and + * <code>RestRequestInterceptor</code>. + * + * <code>RestResponseInterceptor</code> set context to header for + * propagate trace context after execute <code>createRequest</code>. + */ +public class RestTemplateInstrumentation4x extends RestTemplateInstrumentation { + @Override + protected String[] witnessClasses() { + return new String[] { + "org.springframework.web.context.support.ServletContextAttributeFactoryBean" + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/resources/skywalking-plugin.def index 98a8a1b7cb..909ddfb3f8 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/resources/skywalking-plugin.def @@ -14,6 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.sync.define.RestTemplateInstrumentation -spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.async.define.ResponseExtractorFutureInstrumentation -spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.async.define.RestTemplateInstrumentation +spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.sync.define.RestTemplateInstrumentation4x +spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.async.define.ResponseExtractorFutureInstrumentation4x +spring-resttemplate-4.x=org.apache.skywalking.apm.plugin.spring.resttemplate.async.define.RestTemplateInstrumentation4x diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/pom.xml similarity index 91% copy from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml copy to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/pom.xml index bcc756a8af..0a2feba4c3 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/pom.xml @@ -24,11 +24,9 @@ </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>apm-resttemplate-4.3.x-plugin</artifactId> - <packaging>jar</packaging> + <artifactId>resttemplate-commons</artifactId> - <name>resttemplate-4.3.x-plugin</name> - <url>http://maven.apache.org</url> + <name>resttemplate-commons</name> <properties> <spring-web.version>4.3.10.RELEASE</spring-web.version> diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/FutureGetInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/FutureGetInterceptor.java new file mode 100644 index 0000000000..03975d4514 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/FutureGetInterceptor.java @@ -0,0 +1,51 @@ +/* + * 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.spring.resttemplate.async; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.plugin.spring.commons.EnhanceCacheObjects; + +public class FutureGetInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects) objInst.getSkyWalkingDynamicField(); + ContextManager.createLocalSpan("future/get:" + cacheValues.getOperationName()); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + AbstractSpan activeSpan = ContextManager.activeSpan(); + activeSpan.log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java new file mode 100644 index 0000000000..ee31c3f0eb --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java @@ -0,0 +1,51 @@ +/* + * 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.spring.resttemplate.async; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; + +public class ResponseCallBackInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + EnhancedInstance successCallBak = (EnhancedInstance) allArguments[0]; + successCallBak.setSkyWalkingDynamicField(objInst.getSkyWalkingDynamicField()); + + if (allArguments.length == 2) { + EnhancedInstance failedCallBack = (EnhancedInstance) allArguments[1]; + failedCallBack.setSkyWalkingDynamicField(objInst.getSkyWalkingDynamicField()); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java new file mode 100644 index 0000000000..8965845301 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java @@ -0,0 +1,85 @@ +/* + * 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.spring.resttemplate.async; + +import java.lang.reflect.Method; +import java.net.URI; +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.spring.commons.EnhanceCacheObjects; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; +import org.springframework.http.HttpMethod; + +public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + final URI requestURL = (URI) allArguments[0]; + final HttpMethod httpMethod = (HttpMethod) allArguments[1]; + final ContextCarrier contextCarrier = new ContextCarrier(); + + String remotePeer = requestURL.getHost() + ":" + (requestURL.getPort() > 0 ? requestURL.getPort() : "https".equalsIgnoreCase(requestURL + .getScheme()) ? 443 : 80); + + String uri = requestURL.getPath(); + AbstractSpan span = ContextManager.createExitSpan(uri, contextCarrier, remotePeer); + + span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE); + Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + + (requestURL.getPort() > 0 ? ":" + requestURL.getPort() : "") + requestURL.getPath()); + Tags.HTTP.METHOD.set(span, httpMethod.toString()); + SpanLayer.asHttp(span); + RestTemplateRuntimeContextHelper.addUri(uri); + RestTemplateRuntimeContextHelper.addContextCarrier(contextCarrier); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + try { + ContextSnapshot contextSnapshot = ContextManager.capture(); + if (ret != null) { + String uri = RestTemplateRuntimeContextHelper.getUri(); + ((EnhancedInstance) ret).setSkyWalkingDynamicField( + new EnhanceCacheObjects(uri, ComponentsDefine.SPRING_REST_TEMPLATE, SpanLayer.HTTP, contextSnapshot) + ); + } + ContextManager.stopSpan(); + } finally { + RestTemplateRuntimeContextHelper.cleanUri(); + RestTemplateRuntimeContextHelper.cleanContextCarrier(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java new file mode 100644 index 0000000000..2b33c480e7 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java @@ -0,0 +1,58 @@ +/* + * 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.spring.resttemplate.async; + +import java.lang.reflect.Method; +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.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; +import org.springframework.http.client.AsyncClientHttpRequest; + +public class RestRequestInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + AsyncClientHttpRequest clientHttpRequest = (AsyncClientHttpRequest) ret; + if (ret != null) { + ContextCarrier contextCarrier = RestTemplateRuntimeContextHelper.getContextCarrier(); + CarrierItem next = contextCarrier.items(); + while (next.hasNext()) { + next = next.next(); + clientHttpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue()); + } + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java similarity index 100% rename from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java rename to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/ResponseExtractorFutureInstrumentation.java diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java similarity index 100% rename from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java rename to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/define/RestTemplateInstrumentation.java diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java new file mode 100644 index 0000000000..9016cc776e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java @@ -0,0 +1,53 @@ +/* + * 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.spring.resttemplate.helper; + +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; +import org.apache.skywalking.apm.agent.core.context.ContextManager; + +public class RestTemplateRuntimeContextHelper { + + private static final String REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT = "REST_TEMPLATE_CONTEXT_CARRIER"; + + private static final String REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT = "REST_TEMPLATE_URI"; + + public static void cleanUri() { + ContextManager.getRuntimeContext().remove(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT); + } + + public static void cleanContextCarrier() { + ContextManager.getRuntimeContext().remove(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT); + } + + public static void addUri(String uri) { + ContextManager.getRuntimeContext().put(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT, uri); + } + + public static void addContextCarrier(ContextCarrier contextCarrier) { + ContextManager.getRuntimeContext().put(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT, contextCarrier); + } + + public static String getUri() { + return (String) ContextManager.getRuntimeContext().get(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT); + } + + public static ContextCarrier getContextCarrier() { + return (ContextCarrier) ContextManager.getRuntimeContext().get(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java new file mode 100644 index 0000000000..2e9a1290fb --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java @@ -0,0 +1,70 @@ +/* + * 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.spring.resttemplate.sync; + +import java.lang.reflect.Method; +import java.net.URI; +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; +import org.springframework.http.HttpMethod; + +public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + final URI requestURL = (URI) allArguments[0]; + final HttpMethod httpMethod = (HttpMethod) allArguments[1]; + final ContextCarrier contextCarrier = new ContextCarrier(); + + String remotePeer = requestURL.getHost() + ":" + (requestURL.getPort() > 0 ? requestURL.getPort() : "https".equalsIgnoreCase(requestURL + .getScheme()) ? 443 : 80); + String formatURIPath = requestURL.getPath(); + AbstractSpan span = ContextManager.createExitSpan(formatURIPath, contextCarrier, remotePeer); + + span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE); + Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + + (requestURL.getPort() > 0 ? ":" + requestURL.getPort() : "") + requestURL.getPath()); + Tags.HTTP.METHOD.set(span, httpMethod.toString()); + SpanLayer.asHttp(span); + + RestTemplateRuntimeContextHelper.addContextCarrier(contextCarrier); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + RestTemplateRuntimeContextHelper.cleanContextCarrier(); + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java new file mode 100644 index 0000000000..490608036f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java @@ -0,0 +1,60 @@ +/* + * 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.spring.resttemplate.sync; + +import java.lang.reflect.Method; +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.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; +import org.springframework.http.client.AbstractClientHttpRequest; +import org.springframework.http.client.ClientHttpRequest; + +public class RestRequestInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + ClientHttpRequest clientHttpRequest = (ClientHttpRequest) ret; + if (clientHttpRequest instanceof AbstractClientHttpRequest) { + AbstractClientHttpRequest httpRequest = (AbstractClientHttpRequest) clientHttpRequest; + ContextCarrier contextCarrier = RestTemplateRuntimeContextHelper.getContextCarrier(); + CarrierItem next = contextCarrier.items(); + while (next.hasNext()) { + next = next.next(); + httpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue()); + } + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestResponseInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestResponseInterceptor.java new file mode 100644 index 0000000000..550a600123 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestResponseInterceptor.java @@ -0,0 +1,57 @@ +/* + * 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.spring.resttemplate.sync; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.springframework.http.client.ClientHttpResponse; + +public class RestResponseInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, + Object ret) throws Throwable { + + ClientHttpResponse response = (ClientHttpResponse) allArguments[2]; + int statusCode = response.getStatusCode().value(); + AbstractSpan span = ContextManager.activeSpan(); + Tags.HTTP_RESPONSE_STATUS_CODE.set(span, statusCode); + if (statusCode >= 400) { + span.errorOccurred(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class<?>[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java similarity index 94% rename from apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java rename to apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java index 9c98b81c50..8089e2a7b7 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/define/RestTemplateInstrumentation.java @@ -38,7 +38,7 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName * <code>RestResponseInterceptor</code> set context to header for * propagate trace context after execute <code>createRequest</code>. */ -public class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { +public abstract class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "org.springframework.web.client.RestTemplate"; private static final String DO_EXECUTE_METHOD_NAME = "doExecute"; @@ -111,11 +111,4 @@ public class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePlug protected ClassMatch enhanceClass() { return byName(ENHANCE_CLASS); } - - @Override - protected String[] witnessClasses() { - return new String[] { - "org.springframework.web.context.support.ServletContextAttributeFactoryBean" - }; - } } diff --git a/apm-sniffer/optional-plugins/optional-spring-plugins/resttemplate-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/v6x/sync/define/RestTemplateInstrumentation.java b/apm-sniffer/optional-plugins/optional-spring-plugins/resttemplate-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/v6x/sync/define/RestTemplateInstrumentation.java index c9fdb3823d..2c22415b4e 100644 --- a/apm-sniffer/optional-plugins/optional-spring-plugins/resttemplate-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/v6x/sync/define/RestTemplateInstrumentation.java +++ b/apm-sniffer/optional-plugins/optional-spring-plugins/resttemplate-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/v6x/sync/define/RestTemplateInstrumentation.java @@ -26,6 +26,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterc import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** @@ -59,7 +60,9 @@ public class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePlug new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { - return named(DO_EXECUTE_METHOD_NAME); + return named(DO_EXECUTE_METHOD_NAME) + .and(takesArgument(0, named("java.net.URI"))) + .and(takesArgument(2, named("org.springframework.http.HttpMethod"))); } @Override @@ -115,7 +118,7 @@ public class RestTemplateInstrumentation extends ClassInstanceMethodsEnhancePlug @Override protected String[] witnessClasses() { return new String[] { - "org.springframework.web.client.ClientHttpResponseDecorator" + "org.springframework.http.client.reactive.JdkClientHttpConnector" }; } }