Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
山不在高_有仙则灵
SkyWalking
提交
a835fe4e
S
SkyWalking
项目概览
山不在高_有仙则灵
/
SkyWalking
与 Fork 源项目一致
Fork自
apache / SkyWalking
通知
12
Star
0
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyWalking
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a835fe4e
编写于
5月 24, 2018
作者:
X
Xin,Zhang
提交者:
wu-sheng
5月 24, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix occure the NPE when SpringMVC and hystrix work together (#1256)
上级
29f6f6e5
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
271 addition
and
177 deletion
+271
-177
apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/Constants.java
...rg/apache/skywalking/apm/plugin/hystrix/v1/Constants.java
+24
-0
apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java
...g/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java
+5
-0
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
...plugin/spring/mvc/v3/HandlerMethodInvokerInterceptor.java
+4
-3
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
.../spring/mvc/v3/define/AbstractSpring3Instrumentation.java
+0
-1
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
...lugin/spring/mvc/v3/define/ControllerInstrumentation.java
+1
-1
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
...spring/mvc/v3/define/InvocableHandlerInstrumentation.java
+1
-2
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
...king/apm/plugin/spring/mvc/v3/GetBeanInterceptorTest.java
+1
-10
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
...ring/mvc/v4/define/AbstractControllerInstrumentation.java
+1
-2
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
...in/spring/mvc/v4/RequestMappingMethodInterceptorTest.java
+37
-30
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
...lugin/spring/mvc/v4/RestMappingMethodInterceptorTest.java
+103
-73
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
...alking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java
+40
-0
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java
...e/skywalking/apm/plugin/spring/mvc/commons/Constants.java
+8
-1
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java
.../plugin/spring/mvc/commons/EnhanceRequireObjectCache.java
+0
-23
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java
...ng/mvc/commons/interceptor/AbstractMethodInterceptor.java
+30
-23
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java
...in/spring/mvc/commons/interceptor/GetBeanInterceptor.java
+6
-3
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java
.../mvc/commons/interceptor/InvokeForRequestInterceptor.java
+4
-1
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java
...c/commons/interceptor/InvokeHandlerMethodInterceptor.java
+6
-4
未找到文件。
apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/Constants.java
0 → 100644
浏览文件 @
a835fe4e
/*
* 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"
;
}
apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java
浏览文件 @
a835fe4e
...
...
@@ -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
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
()
);
}
}
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
());
}
}
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
}
...
...
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
}
...
...
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
→
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
浏览文件 @
a835fe4e
...
...
@@ -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
;
}
}
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java
浏览文件 @
a835fe4e
...
...
@@ -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"
;
}
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/EnhanceRequireObjectCache.java
浏览文件 @
a835fe4e
...
...
@@ -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
);
}
}
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java
浏览文件 @
a835fe4e
...
...
@@ -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
...
...
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/GetBeanInterceptor.java
浏览文件 @
a835fe4e
...
...
@@ -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
;
}
...
...
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java
浏览文件 @
a835fe4e
...
...
@@ -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
...
...
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java
浏览文件 @
a835fe4e
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录