From 7a9721a65aeeaf36f49b4df300cea463f4862daa Mon Sep 17 00:00:00 2001 From: "Xin,Zhang" Date: Sat, 24 Nov 2018 18:34:33 +0800 Subject: [PATCH] Fix the components of Spring resttemplate is incorrect (#1956) * Fix the components of Spring resttemplate is incorrect * Fix check style * Fix the version is incorrect * Fix checkstyle --- .../concurrent-util-4.x-plugin/pom.xml | 5 ++ .../FailureCallbackInterceptor.java | 23 +++---- .../SuccessCallbackInterceptor.java | 21 ++---- .../apm-sdk-plugin/spring-plugins/pom.xml | 1 + .../resttemplate-4.x-plugin/pom.xml | 6 ++ .../async/ResponseCallBackInterceptor.java | 1 - .../async/RestExecuteInterceptor.java | 9 ++- .../async/RestRequestInterceptor.java | 3 +- .../spring-plugins/spring-commons/pom.xml | 32 ++++++++++ .../spring/commons/EnhanceCacheObjects.java | 64 +++++++++++++++++++ 10 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/commons/EnhanceCacheObjects.java diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/pom.xml index ff9326de3..ebf2fc013 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/pom.xml @@ -41,5 +41,10 @@ ${spring-core.version} provided + + org.apache.skywalking + spring-commons + ${project.version} + diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/FailureCallbackInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/FailureCallbackInterceptor.java index 234224ba3..a174effc8 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/FailureCallbackInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/FailureCallbackInterceptor.java @@ -16,42 +16,35 @@ * */ - package org.apache.skywalking.apm.plugin.spring.concurrent; import java.lang.reflect.Method; -import java.net.URI; -import org.apache.skywalking.apm.agent.core.context.tag.Tags; -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.context.ContextManager; -import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; 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.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.spring.commons.EnhanceCacheObjects; public class FailureCallbackInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return; } - URI uri = (URI)cacheValues[0]; - AbstractSpan span = ContextManager.createLocalSpan("future/failureCallback:" + uri.getPath()); - span.errorOccurred().log((Throwable)allArguments[0]).setComponent(ComponentsDefine.SPRING_REST_TEMPLATE).setLayer(SpanLayer.HTTP); - Tags.URL.set(span, uri.getPath()); - ContextManager.continued((ContextSnapshot)cacheValues[2]); + AbstractSpan span = ContextManager.createLocalSpan("future/failureCallback:" + cacheValues.getOperationName()); + span.errorOccurred().log((Throwable)allArguments[0]).setComponent(cacheValues.getComponent()).setLayer(cacheValues.getSpanLayer()); + ContextManager.continued(cacheValues.getContextSnapshot()); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return ret; } @@ -61,7 +54,7 @@ public class FailureCallbackInterceptor implements InstanceMethodsAroundIntercep @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/SuccessCallbackInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/SuccessCallbackInterceptor.java index dcacaab7a..e44ccf612 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/SuccessCallbackInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/concurrent-util-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/concurrent/SuccessCallbackInterceptor.java @@ -16,42 +16,35 @@ * */ - package org.apache.skywalking.apm.plugin.spring.concurrent; import java.lang.reflect.Method; -import java.net.URI; 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; public class SuccessCallbackInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return; } - URI uri = (URI)cacheValues[0]; - AbstractSpan span = ContextManager.createLocalSpan("future/successCallback:" + uri.getPath()); - span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE).setLayer(SpanLayer.HTTP); - Tags.URL.set(span, uri.getPath()); - ContextManager.continued((ContextSnapshot)cacheValues[2]); + AbstractSpan span = ContextManager.createLocalSpan("future/successCallback:" + cacheValues.getOperationName()); + span.setComponent(cacheValues.getComponent()).setLayer(cacheValues.getSpanLayer()); + ContextManager.continued(cacheValues.getContextSnapshot()); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return ret; } @@ -61,7 +54,7 @@ public class SuccessCallbackInterceptor implements InstanceMethodsAroundIntercep @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { - Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); + EnhanceCacheObjects cacheValues = (EnhanceCacheObjects)objInst.getSkyWalkingDynamicField(); if (cacheValues == null) { return; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml index b49c68aa4..b83eea861 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml @@ -35,6 +35,7 @@ mvc-annotation-3.x-plugin core-patch mvc-annotation-commons + spring-commons pom 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 a62b10aff..cc92ae2ff 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 @@ -41,5 +41,11 @@ ${spring-web.version} provided + + + org.apache.skywalking + spring-commons + ${project.version} + 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/ResponseCallBackInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java index f967a5ffc..bdba4ef59 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/ResponseCallBackInterceptor.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.plugin.spring.resttemplate.async; import java.lang.reflect.Method; 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/RestExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java index f6148968c..9b80b6ac5 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java @@ -21,14 +21,16 @@ 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.context.ContextManager; -import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; 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.springframework.http.HttpMethod; public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor { @@ -59,7 +61,8 @@ public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor Object[] cacheValues = (Object[])objInst.getSkyWalkingDynamicField(); cacheValues[2] = ContextManager.capture(); if (ret != null) { - ((EnhancedInstance)ret).setSkyWalkingDynamicField(cacheValues); + URI uri = (URI)cacheValues[0]; + ((EnhancedInstance)ret).setSkyWalkingDynamicField(new EnhanceCacheObjects(uri.getPath(), ComponentsDefine.SPRING_REST_TEMPLATE, SpanLayer.HTTP, (ContextSnapshot)cacheValues[2])); } ContextManager.stopSpan(); return ret; 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/RestRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java index ba75bb407..fc4452424 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java @@ -16,13 +16,12 @@ * */ - 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.context.CarrierItem; 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.AsyncClientHttpRequest; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/pom.xml new file mode 100644 index 000000000..860453952 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/pom.xml @@ -0,0 +1,32 @@ + + + + + spring-plugins + org.apache.skywalking + 6.0.0-beta-SNAPSHOT + + 4.0.0 + + spring-commons + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/commons/EnhanceCacheObjects.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/commons/EnhanceCacheObjects.java new file mode 100644 index 000000000..0acf909aa --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/commons/EnhanceCacheObjects.java @@ -0,0 +1,64 @@ +/* + * 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.commons; + +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.network.trace.component.OfficialComponent; + +public class EnhanceCacheObjects { + private OfficialComponent component; + private SpanLayer spanLayer; + + private String operationName; + private ContextSnapshot contextSnapshot; + + public EnhanceCacheObjects(String operationName, OfficialComponent component, SpanLayer spanLayer, + ContextSnapshot snapshot) { + this.component = component; + this.spanLayer = spanLayer; + this.operationName = operationName; + contextSnapshot = snapshot; + } + + public EnhanceCacheObjects(String operationName, OfficialComponent component, + ContextSnapshot snapshot) { + this(operationName, component, null, snapshot); + } + + public EnhanceCacheObjects(String operationName, ContextSnapshot snapshot) { + this(operationName, null, snapshot); + } + + public OfficialComponent getComponent() { + return component; + } + + public SpanLayer getSpanLayer() { + return spanLayer; + } + + public String getOperationName() { + return operationName; + } + + public ContextSnapshot getContextSnapshot() { + return contextSnapshot; + } +}