diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/Constants.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/Constants.java new file mode 100644 index 000000000..97c204c92 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/Constants.java @@ -0,0 +1,24 @@ +/* + * 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.hystrix.v1; + +public class Constants { + + public static final String ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT = "ISOLATE_STRATEGY"; +} diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java index c281f59f9..c6f1bcc97 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java @@ -18,6 +18,7 @@ package org.apache.skywalking.apm.plugin.hystrix.v1; +import com.netflix.hystrix.HystrixCommand; import java.lang.reflect.Method; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; @@ -27,6 +28,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import static org.apache.skywalking.apm.plugin.hystrix.v1.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT; + public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, @@ -41,6 +44,8 @@ public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterc ContextManager.continued(snapshot); // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing. enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + + ContextManager.getRuntimeContext().put(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT, ((HystrixCommand)objInst).getProperties().executionIsolationStrategy().get().name().toUpperCase()); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/HandlerMethodInvokerInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/HandlerMethodInvokerInterceptor.java index d55e12f41..19f2f6e78 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/HandlerMethodInvokerInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/HandlerMethodInvokerInterceptor.java @@ -16,16 +16,17 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v3; import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; -import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; 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.web.context.request.NativeWebRequest; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; + /** * {@link HandlerMethodInvokerInterceptor} pass the {@link NativeWebRequest} object into the {@link * org.springframework.stereotype.Controller} object. @@ -38,7 +39,7 @@ public class HandlerMethodInvokerInterceptor implements InstanceMethodsAroundInt MethodInterceptResult result) throws Throwable { Object handler = allArguments[1]; if (handler instanceof EnhancedInstance) { - ((EnhanceRequireObjectCache)((EnhancedInstance)handler).getSkyWalkingDynamicField()).setNativeWebRequest((NativeWebRequest)allArguments[2]); + ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, ((NativeWebRequest)allArguments[2]).getNativeResponse()); } } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/AbstractSpring3Instrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/AbstractSpring3Instrumentation.java index 9a4f7b3d3..e534a6e0e 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/AbstractSpring3Instrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/AbstractSpring3Instrumentation.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v3.define; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/ControllerInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/ControllerInstrumentation.java index e329fa7db..dd24d03e6 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/ControllerInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/ControllerInstrumentation.java @@ -21,9 +21,9 @@ package org.apache.skywalking.apm.plugin.spring.mvc.v3.define; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch; -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.any; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/InvocableHandlerInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/InvocableHandlerInstrumentation.java index 82c5af12e..88f7cc6fa 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/InvocableHandlerInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/define/InvocableHandlerInstrumentation.java @@ -16,13 +16,12 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v3.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.interceptor.InstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/GetBeanInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/GetBeanInterceptorTest.java index 4030bf27c..b4ec3f592 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/GetBeanInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/GetBeanInterceptorTest.java @@ -16,18 +16,17 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v3; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.GetBeanInterceptor; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.GetBeanInterceptor; import org.springframework.web.context.request.NativeWebRequest; import static org.mockito.Mockito.times; @@ -62,12 +61,4 @@ public class GetBeanInterceptorTest { verify(enhanceRet, times(0)).setSkyWalkingDynamicField(Matchers.any()); } - - @Test - public void testResultIsEnhanceInstance() throws Throwable { - interceptor.afterMethod(enhancedInstance, null, null, null, enhanceRet); - - verify(enhanceRet, times(0)).setSkyWalkingDynamicField(Matchers.any()); - } - } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/InvokeForRequestInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/InvokeForRequestInterceptorTest.java deleted file mode 100644 index 3760e1dca..000000000 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v3/InvokeForRequestInterceptorTest.java +++ /dev/null @@ -1,59 +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.spring.mvc.v3; - -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; -import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.InvokeForRequestInterceptor; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; -import org.springframework.web.context.request.NativeWebRequest; - -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -@RunWith(MockitoJUnitRunner.class) -public class InvokeForRequestInterceptorTest { - private InvokeForRequestInterceptor interceptor; - - @Mock - private EnhancedInstance enhancedInstance; - - @Mock - private NativeWebRequest nativeWebRequest; - - private Object argument[]; - - @Before - public void setUp() { - interceptor = new InvokeForRequestInterceptor(); - argument = new Object[] {nativeWebRequest}; - } - - @Test - public void testPassNativeWebRequest() throws Throwable { - interceptor.beforeMethod(enhancedInstance, null, argument, new Class[] {NativeWebRequest.class}, null); - - verify(enhancedInstance, times(1)).setSkyWalkingDynamicField(Matchers.any()); - } -} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/define/AbstractControllerInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/define/AbstractControllerInstrumentation.java index 42d7f5957..225b657bb 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/define/AbstractControllerInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/define/AbstractControllerInstrumentation.java @@ -16,16 +16,15 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v4.define; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java index e57bea4b9..bedbae744 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java @@ -16,22 +16,31 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v4; import java.lang.reflect.Method; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.helper.SegmentRefHelper; import org.apache.skywalking.apm.agent.test.helper.SpanHelper; +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; import org.apache.skywalking.apm.agent.test.tools.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; import org.apache.skywalking.apm.plugin.spring.mvc.commons.PathMappingCache; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RequestMappingMethodInterceptor; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -39,25 +48,15 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; -import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; -import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; -import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; -import org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.apache.skywalking.apm.agent.test.helper.SegmentRefHelper; -import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; -import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; -import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; -import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RequestMappingMethodInterceptor; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; +import static org.apache.skywalking.apm.agent.test.tools.SpanAssert.assertComponent; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.when; -import static org.apache.skywalking.apm.agent.test.tools.SpanAssert.assertComponent; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(TracingSegmentRunner.class) @@ -103,7 +102,6 @@ public class RequestMappingMethodInterceptorTest { when(request.getRequestURI()).thenReturn("/test/testRequestURL"); when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/testRequestURL")); when(response.getStatus()).thenReturn(200); - when(nativeWebRequest.getNativeResponse()).thenReturn(response); arguments = new Object[] {request, response}; argumentType = new Class[] {request.getClass(), response.getClass()}; @@ -112,31 +110,39 @@ public class RequestMappingMethodInterceptorTest { @Test public void testWithoutSerializedContextData() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RequestMappingClass1 mappingClass1 = new RequestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("testRequestURL"); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override + public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RequestMappingClass1 mappingClass1 = new RequestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("testRequestURL"); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertHttpSpan(spans.get(0)); } @Test public void testWithOccurException() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RequestMappingClass1 mappingClass1 = new RequestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("testRequestURL"); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RequestMappingClass1 mappingClass1 = new RequestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("testRequestURL"); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException()); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -168,8 +174,9 @@ public class RequestMappingMethodInterceptorTest { @Override public Object getSkyWalkingDynamicField() { + value.setPathMappingCache(new PathMappingCache("/test")); - value.setNativeWebRequest(nativeWebRequest); + return value; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RestMappingMethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RestMappingMethodInterceptorTest.java index 4456ff5d2..ccff690c5 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RestMappingMethodInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RestMappingMethodInterceptorTest.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.v4; import java.lang.reflect.Method; @@ -112,16 +111,20 @@ public class RestMappingMethodInterceptorTest { @Test public void testGetMapping() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("getRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/getRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("getRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/getRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -132,16 +135,21 @@ public class RestMappingMethodInterceptorTest { @Test public void testPostMapping() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("postRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/postRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("postRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/postRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -152,16 +160,21 @@ public class RestMappingMethodInterceptorTest { @Test public void testPutMapping() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("putRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/putRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("putRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/putRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -172,16 +185,21 @@ public class RestMappingMethodInterceptorTest { @Test public void testDeleteMapping() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("deleteRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/deleteRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("deleteRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/deleteRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -192,16 +210,20 @@ public class RestMappingMethodInterceptorTest { @Test public void testPatchMapping() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("patchRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/patchRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("patchRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/patchRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -212,16 +234,21 @@ public class RestMappingMethodInterceptorTest { @Test public void testDummy() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("dummy"); - when(request.getRequestURI()).thenReturn("/test"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("dummy"); + when(request.getRequestURI()).thenReturn("/test"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -232,17 +259,21 @@ public class RestMappingMethodInterceptorTest { @Test public void testWithOccurException() throws Throwable { - controllerConstructorInterceptor.onConstruct(enhancedInstance, null); - RestMappingClass1 mappingClass1 = new RestMappingClass1(); - Method m = mappingClass1.getClass().getMethod("getRequestURL"); - when(request.getRequestURI()).thenReturn("/test/testRequestURL"); - when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/getRequestURL")); - ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); - RequestContextHolder.setRequestAttributes(servletRequestAttributes); + SpringTestCaseHelper.createCaseHandler(request, response, new SpringTestCaseHelper.CaseHandler() { + @Override public void handleCase() throws Throwable { + controllerConstructorInterceptor.onConstruct(enhancedInstance, null); + RestMappingClass1 mappingClass1 = new RestMappingClass1(); + Method m = mappingClass1.getClass().getMethod("getRequestURL"); + when(request.getRequestURI()).thenReturn("/test/testRequestURL"); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/test/getRequestURL")); + ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response); + RequestContextHolder.setRequestAttributes(servletRequestAttributes); - interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); - interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException()); - interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult); + interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null); + } + }); assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); @@ -275,7 +306,6 @@ public class RestMappingMethodInterceptorTest { @Override public Object getSkyWalkingDynamicField() { value.setPathMappingCache(new PathMappingCache("/test")); - value.setNativeWebRequest(nativeWebRequest); return value; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java new file mode 100644 index 000000000..34b257aeb --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java @@ -0,0 +1,40 @@ +/* + * 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.mvc.v4; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants; + +public final class SpringTestCaseHelper { + + public final static void createCaseHandler(HttpServletRequest request, HttpServletResponse response, + CaseHandler a) throws Throwable { + ContextManager.createLocalSpan("For-Test"); + ContextManager.getRuntimeContext().put(Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT, request); + ContextManager.getRuntimeContext().put(Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT, response); + a.handleCase(); + ContextManager.stopSpan(); + } + + public interface CaseHandler { + void handleCase() throws Throwable; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java index 618d5778a..ca42b3ee8 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.commons; /** @@ -32,4 +31,12 @@ public class Constants { public static final String REQUEST_MAPPING_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RequestMappingMethodInterceptor"; public static final String REST_MAPPING_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RestMappingMethodInterceptor"; + + public static final String HYSTRIX_COMMAND_ANNOTATION = "com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand"; + + public static final String REQUEST_KEY_IN_RUNTIME_CONTEXT = "SW_REQUEST"; + + public static final String RESPONSE_KEY_IN_RUNTIME_CONTEXT = "SW_RESPONSE"; + + public static final String ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT = "ISOLATE_STRATEGY"; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java index cbadbfdab..8337591fb 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java @@ -18,28 +18,15 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons; -import org.springframework.web.context.request.NativeWebRequest; - -import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; public class EnhanceRequireObjectCache { private PathMappingCache pathMappingCache; - private ThreadLocal nativeWebRequest = new ThreadLocal(); - private ThreadLocal httpResponse = new ThreadLocal(); public void setPathMappingCache(PathMappingCache pathMappingCache) { this.pathMappingCache = pathMappingCache; } - public HttpServletResponse getHttpServletResponse() { - return httpResponse.get() == null ? (HttpServletResponse) nativeWebRequest.get().getNativeResponse() : httpResponse.get(); - } - - public void setNativeWebRequest(NativeWebRequest nativeWebRequest) { - this.nativeWebRequest.set(nativeWebRequest); - } - public String findPathMapping(Method method) { return pathMappingCache.findPathMapping(method); } @@ -51,14 +38,4 @@ public class EnhanceRequireObjectCache { public PathMappingCache getPathMappingCache() { return pathMappingCache; } - - public void setHttpResponse(HttpServletResponse httpResponse) { - this.httpResponse.set(httpResponse); - } - - public void clearRequestAndResponse() { - setNativeWebRequest(null); - setHttpResponse(null); - } - } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index 798a2216e..ded96e745 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -16,9 +16,11 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; +import java.lang.reflect.Method; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; 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; @@ -30,12 +32,10 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM 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.mvc.commons.EnhanceRequireObjectCache; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.lang.reflect.Method; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; /** * the abstract method inteceptor @@ -54,38 +54,45 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround requestURL = pathMappingCache.findPathMapping(method); } - HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); + String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); + HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); - ContextCarrier contextCarrier = new ContextCarrier(); - CarrierItem next = contextCarrier.items(); - while (next.hasNext()) { - next = next.next(); - next.setHeadValue(request.getHeader(next.getHeadKey())); + if (hystrixIsolateStrategy != null) { + ContextManager.createLocalSpan(requestURL); + } else if (request != null) { + ContextCarrier contextCarrier = new ContextCarrier(); + CarrierItem next = contextCarrier.items(); + while (next.hasNext()) { + next = next.next(); + next.setHeadValue(request.getHeader(next.getHeadKey())); + } + + AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier); + Tags.URL.set(span, request.getRequestURL().toString()); + Tags.HTTP.METHOD.set(span, request.getMethod()); + span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); + SpanLayer.asHttp(span); } - - AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier); - Tags.URL.set(span, request.getRequestURL().toString()); - Tags.HTTP.METHOD.set(span, request.getMethod()); - span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); - SpanLayer.asHttp(span); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - try { - HttpServletResponse response = ((EnhanceRequireObjectCache) objInst.getSkyWalkingDynamicField()).getHttpServletResponse(); + String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); + HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); + if (hystrixIsolateStrategy != null) { + ContextManager.stopSpan(); + } else if (response != null) { AbstractSpan span = ContextManager.activeSpan(); if (response.getStatus() >= 400) { span.errorOccurred(); Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); } ContextManager.stopSpan(); - return ret; - } finally { - ((EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField()).clearRequestAndResponse(); } + + return ret; } @Override diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java index 3d56c6711..613481fcd 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java @@ -16,15 +16,18 @@ * */ - package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; 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.mvc.commons.EnhanceRequireObjectCache; import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT; /** * {@link GetBeanInterceptor} pass the {@link NativeWebRequest} object into the {@link @@ -42,7 +45,7 @@ public class GetBeanInterceptor implements InstanceMethodsAroundInterceptor { public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { if (ret instanceof EnhancedInstance) { - ((EnhanceRequireObjectCache)((EnhancedInstance)ret).getSkyWalkingDynamicField()).setNativeWebRequest((NativeWebRequest)objInst.getSkyWalkingDynamicField()); + ContextManager.getRuntimeContext().put(REQUEST_KEY_IN_RUNTIME_CONTEXT, ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest()); } return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java index 899d22584..f1762a934 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java @@ -19,6 +19,7 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; +import org.apache.skywalking.apm.agent.core.context.ContextManager; 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; @@ -26,6 +27,8 @@ import org.springframework.web.context.request.NativeWebRequest; import java.lang.reflect.Method; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; + /** * {@link InvokeForRequestInterceptor} pass the {@link NativeWebRequest} object into the {@link * org.springframework.stereotype.Controller} object. @@ -36,7 +39,7 @@ public class InvokeForRequestInterceptor implements InstanceMethodsAroundInterce @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - objInst.setSkyWalkingDynamicField(allArguments[0]); + ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, ((NativeWebRequest)allArguments[0]).getNativeResponse()); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java index c2d555608..33fbec273 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java @@ -18,20 +18,22 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; 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.mvc.commons.EnhanceRequireObjectCache; -import javax.servlet.http.HttpServletResponse; -import java.lang.reflect.Method; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { if (allArguments[2] instanceof EnhancedInstance) { - ((EnhanceRequireObjectCache)((EnhancedInstance)allArguments[2]).getSkyWalkingDynamicField()).setHttpResponse((HttpServletResponse)allArguments[1]); + ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, allArguments[1]); + ContextManager.getRuntimeContext().put(REQUEST_KEY_IN_RUNTIME_CONTEXT, allArguments[0]); } }