提交 a835fe4e 编写于 作者: X Xin,Zhang 提交者: wu-sheng

Fix occure the NPE when SpringMVC and hystrix work together (#1256)

上级 29f6f6e5
/*
* 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";
}
......@@ -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
......
......@@ -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());
}
}
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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());
}
}
......@@ -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;
......
......@@ -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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
List<AbstractTracingSpan> 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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException());
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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;
}
......
......@@ -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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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);
interceptor.beforeMethod(enhancedInstance, m, arguments, argumentType, methodInterceptResult);
interceptor.handleMethodException(enhancedInstance, m, arguments, argumentType, new RuntimeException());
interceptor.afterMethod(enhancedInstance, m, arguments, argumentType, null);
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);
}
});
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;
}
......
......@@ -16,44 +16,25 @@
*
*/
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};
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();
}
@Test
public void testPassNativeWebRequest() throws Throwable {
interceptor.beforeMethod(enhancedInstance, null, argument, new Class[] {NativeWebRequest.class}, null);
verify(enhancedInstance, times(1)).setSkyWalkingDynamicField(Matchers.any());
public interface CaseHandler {
void handleCase() throws Throwable;
}
}
......@@ -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";
}
......@@ -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> nativeWebRequest = new ThreadLocal<NativeWebRequest>();
private ThreadLocal<HttpServletResponse> httpResponse = new ThreadLocal<HttpServletResponse>();
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);
}
}
......@@ -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
......
......@@ -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;
}
......
......@@ -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
......
......@@ -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]);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册