Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
月轩居士
SkyWalking
提交
e78e7e4d
S
SkyWalking
项目概览
月轩居士
/
SkyWalking
与 Fork 源项目一致
Fork自
apache / SkyWalking
通知
4
Star
0
Fork
0
代码
文件
提交
分支
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,发现更多精彩内容 >>
提交
e78e7e4d
编写于
9月 19, 2017
作者:
A
ascrutae
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support struts 2 plugin
上级
7834d7bd
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
321 addition
and
1 deletion
+321
-1
apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java
...walking/apm/network/trace/component/ComponentsDefine.java
+4
-1
apm-sniffer/apm-agent/pom.xml
apm-sniffer/apm-agent/pom.xml
+5
-0
apm-sniffer/apm-sdk-plugin/pom.xml
apm-sniffer/apm-sdk-plugin/pom.xml
+1
-0
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/pom.xml
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/pom.xml
+58
-0
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/java/org/skywalking/apm/plugin/struts2/Struts2Interceptor.java
...org/skywalking/apm/plugin/struts2/Struts2Interceptor.java
+50
-0
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/java/org/skywalking/apm/plugin/struts2/define/Struts2Instrumentation.java
...ing/apm/plugin/struts2/define/Struts2Instrumentation.java
+51
-0
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/resources/skywalking-plugin.def
...ruts2-2.x-plugin/src/main/resources/skywalking-plugin.def
+1
-0
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/test/java/org/skywalking/apm/plugin/struts2/Struts2InterceptorTest.java
...skywalking/apm/plugin/struts2/Struts2InterceptorTest.java
+151
-0
未找到文件。
apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java
浏览文件 @
e78e7e4d
...
...
@@ -35,6 +35,8 @@ public class ComponentsDefine {
public
static
final
OfficialComponent
SPRING_MVC_ANNOTATION
=
new
OfficialComponent
(
14
,
"SpringMVCAnnotation"
);
public
static
final
OfficialComponent
STRUTS2
=
new
OfficialComponent
(
14
,
"Struts2"
);
private
static
ComponentsDefine
instance
=
new
ComponentsDefine
();
private
String
[]
components
;
...
...
@@ -44,7 +46,7 @@ public class ComponentsDefine {
}
public
ComponentsDefine
()
{
components
=
new
String
[
1
5
];
components
=
new
String
[
1
6
];
addComponent
(
TOMCAT
);
addComponent
(
HTTPCLIENT
);
addComponent
(
DUBBO
);
...
...
@@ -59,6 +61,7 @@ public class ComponentsDefine {
addComponent
(
OKHTTP
);
addComponent
(
SPRING_REST_TEMPLATE
);
addComponent
(
SPRING_MVC_ANNOTATION
);
addComponent
(
STRUTS2
);
}
private
void
addComponent
(
OfficialComponent
component
)
{
...
...
apm-sniffer/apm-agent/pom.xml
浏览文件 @
e78e7e4d
...
...
@@ -100,6 +100,11 @@
<artifactId>
apm-spring-cloud-feign-1.x-plugin
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.skywalking
</groupId>
<artifactId>
apm-struts2-2.x-plugin
</artifactId>
<version>
${project.version}
</version>
</dependency>
<!-- activation -->
<dependency>
...
...
apm-sniffer/apm-sdk-plugin/pom.xml
浏览文件 @
e78e7e4d
...
...
@@ -23,6 +23,7 @@
<module>
resin-3.x-plugin
</module>
<module>
resin-4.x-plugin
</module>
<module>
spring-plugins
</module>
<module>
struts2-2.x-plugin
</module>
</modules>
<packaging>
pom
</packaging>
...
...
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/pom.xml
0 → 100644
浏览文件 @
e78e7e4d
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
apm-sdk-plugin
</artifactId>
<groupId>
org.skywalking
</groupId>
<version>
3.2.1-2017
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
apm-struts2-2.x-plugin
</artifactId>
<packaging>
jar
</packaging>
<name>
struts2-2.x-plugin
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
org.apache.struts
</groupId>
<artifactId>
struts2-core
</artifactId>
<version>
2.3.1
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
javax.servlet-api
</artifactId>
<version>
3.1.0
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
jsp-api
</artifactId>
<version>
2.0
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 源码插件 -->
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-source-plugin
</artifactId>
<!-- 发布时自动将源码同时发布的配置 -->
<executions>
<execution>
<id>
attach-sources
</id>
<goals>
<goal>
jar
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/java/org/skywalking/apm/plugin/struts2/Struts2Interceptor.java
0 → 100644
浏览文件 @
e78e7e4d
package
org.skywalking.apm.plugin.struts2
;
import
java.lang.reflect.Method
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.struts2.ServletActionContext
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.context.ContextCarrier
;
import
org.skywalking.apm.agent.core.context.ContextManager
;
import
org.skywalking.apm.agent.core.context.tag.Tags
;
import
org.skywalking.apm.agent.core.context.trace.AbstractSpan
;
import
org.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.network.trace.component.ComponentsDefine
;
public
class
Struts2Interceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
HttpServletRequest
request
=
ServletActionContext
.
getRequest
();
String
tracingHeaderValue
=
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
);
ContextCarrier
contextCarrier
=
new
ContextCarrier
().
deserialize
(
tracingHeaderValue
);
AbstractSpan
span
=
ContextManager
.
createEntrySpan
(
request
.
getRequestURI
(),
contextCarrier
);
Tags
.
URL
.
set
(
span
,
request
.
getRequestURL
().
toString
());
Tags
.
HTTP
.
METHOD
.
set
(
span
,
request
.
getMethod
());
span
.
setComponent
(
ComponentsDefine
.
STRUTS2
);
SpanLayer
.
asHttp
(
span
);
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
HttpServletResponse
response
=
ServletActionContext
.
getResponse
();
AbstractSpan
span
=
ContextManager
.
activeSpan
();
if
(
response
.
getStatus
()
>=
400
)
{
span
.
errorOccurred
();
Tags
.
STATUS_CODE
.
set
(
span
,
Integer
.
toString
(
response
.
getStatus
()));
}
ContextManager
.
stopSpan
();
return
ret
;
}
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
errorOccurred
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/java/org/skywalking/apm/plugin/struts2/define/Struts2Instrumentation.java
0 → 100644
浏览文件 @
e78e7e4d
package
org.skywalking.apm.plugin.struts2.define
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
org.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
/**
* {@link Struts2Instrumentation} enhance the <code>invokeAction</code> method
* in <code>com.opensymphony.xwork2.DefaultActionInvocation</code> class by
* <code>org.skywalking.apm.plugin.struts2.Struts2Interceptor</code> class
*
* @author zhangxin
*/
public
class
Struts2Instrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_CLASS
=
"com.opensymphony.xwork2.DefaultActionInvocation"
;
private
static
final
String
ENHANCE_METHOD
=
"invokeAction"
;
private
static
final
String
INTERCEPT_CLASS
=
"org.skywalking.apm.plugin.struts2.Struts2Interceptor"
;
@Override
protected
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
new
ConstructorInterceptPoint
[
0
];
}
@Override
protected
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
ENHANCE_METHOD
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
INTERCEPT_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
}
}
};
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
ENHANCE_CLASS
);
}
}
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/main/resources/skywalking-plugin.def
0 → 100644
浏览文件 @
e78e7e4d
struts2-2.x=org.skywalking.apm.plugin.struts2.define.Struts2Instrumentation
apm-sniffer/apm-sdk-plugin/struts2-2.x-plugin/src/test/java/org/skywalking/apm/plugin/struts2/Struts2InterceptorTest.java
0 → 100644
浏览文件 @
e78e7e4d
package
org.skywalking.apm.plugin.struts2
;
import
com.opensymphony.xwork2.ActionContext
;
import
java.util.List
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.struts2.ServletActionContext
;
import
org.apache.struts2.StrutsStatics
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
import
org.skywalking.apm.agent.core.context.trace.LogDataEntity
;
import
org.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegmentRef
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
import
org.skywalking.apm.agent.test.helper.SegmentRefHelper
;
import
org.skywalking.apm.agent.test.helper.SpanHelper
;
import
org.skywalking.apm.agent.test.tools.AgentServiceRule
;
import
org.skywalking.apm.agent.test.tools.SegmentStorage
;
import
org.skywalking.apm.agent.test.tools.SegmentStoragePoint
;
import
org.skywalking.apm.agent.test.tools.TracingSegmentRunner
;
import
org.skywalking.apm.network.trace.component.ComponentsDefine
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
mock
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
mockStatic
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertComponent
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertException
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertLayer
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertTag
;
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
@PrepareForTest
({
ActionContext
.
class
})
public
class
Struts2InterceptorTest
{
private
Struts2Interceptor
struts2Interceptor
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
private
ActionContext
actionContext
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
HttpServletRequest
request
;
@Mock
private
HttpServletResponse
response
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Mock
private
EnhancedInstance
enhancedInstance
;
private
Object
[]
arguments
;
private
Class
[]
argumentType
;
private
Object
[]
exceptionArguments
;
private
Class
[]
exceptionArgumentType
;
@Before
public
void
setUp
()
throws
Exception
{
struts2Interceptor
=
new
Struts2Interceptor
();
when
(
request
.
getRequestURI
()).
thenReturn
(
"/test/testRequestURL"
);
when
(
request
.
getRequestURL
()).
thenReturn
(
new
StringBuffer
(
"http://localhost:8080/test/testRequestURL"
));
when
(
response
.
getStatus
()).
thenReturn
(
200
);
mockStatic
(
ActionContext
.
class
);
actionContext
=
mock
(
ActionContext
.
class
);
when
(
actionContext
.
get
(
org
.
apache
.
struts2
.
StrutsStatics
.
HTTP_RESPONSE
)).
thenReturn
(
response
);
when
(
actionContext
.
get
(
StrutsStatics
.
HTTP_REQUEST
)).
thenReturn
(
request
);
when
(
ActionContext
.
getContext
()).
thenReturn
(
actionContext
);
arguments
=
new
Object
[]
{
request
,
response
};
argumentType
=
new
Class
[]
{
request
.
getClass
(),
response
.
getClass
()};
exceptionArguments
=
new
Object
[]
{
request
,
response
,
new
RuntimeException
()};
exceptionArgumentType
=
new
Class
[]
{
request
.
getClass
(),
response
.
getClass
(),
new
RuntimeException
().
getClass
()};
}
@Test
public
void
testWithoutSerializedContextData
()
throws
Throwable
{
struts2Interceptor
.
beforeMethod
(
enhancedInstance
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
struts2Interceptor
.
afterMethod
(
enhancedInstance
,
null
,
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
testWithSerializedContextData
()
throws
Throwable
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"#AQA*#AQA*4WcWe0tQNQA*|3|1|1|#192.168.1.8:18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*"
);
struts2Interceptor
.
beforeMethod
(
enhancedInstance
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
struts2Interceptor
.
afterMethod
(
enhancedInstance
,
null
,
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
));
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
@Test
public
void
testWithOccurException
()
throws
Throwable
{
struts2Interceptor
.
beforeMethod
(
enhancedInstance
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
struts2Interceptor
.
handleMethodException
(
enhancedInstance
,
null
,
arguments
,
argumentType
,
new
RuntimeException
());
struts2Interceptor
.
afterMethod
(
enhancedInstance
,
null
,
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
));
List
<
LogDataEntity
>
logDataEntities
=
SpanHelper
.
getLogs
(
spans
.
get
(
0
));
assertThat
(
logDataEntities
.
size
(),
is
(
1
));
assertException
(
logDataEntities
.
get
(
0
),
RuntimeException
.
class
);
}
private
void
assertTraceSegmentRef
(
TraceSegmentRef
ref
)
{
assertThat
(
SegmentRefHelper
.
getEntryApplicationInstanceId
(
ref
),
is
(
1
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
).
toString
(),
is
(
"1.1.15006458883500001"
));
}
private
void
assertHttpSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test/testRequestURL"
));
assertComponent
(
span
,
ComponentsDefine
.
STRUTS2
);
assertTag
(
span
,
0
,
"http://localhost:8080/test/testRequestURL"
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录