Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
润土的好友猹
SkyWalking
提交
4c1963cb
S
SkyWalking
项目概览
润土的好友猹
/
SkyWalking
与 Fork 源项目一致
Fork自
apache / SkyWalking
通知
9
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,发现更多精彩内容 >>
提交
4c1963cb
编写于
7月 09, 2017
作者:
A
ascrutae
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support plugin testcase
上级
f64d4142
变更
40
展开全部
隐藏空白更改
内联
并排
Showing
40 changed file
with
1702 addition
and
2079 deletion
+1702
-2079
apm-application-toolkit/apm-toolkit-opentracing/src/main/java/org/skywalking/apm/toolkit/opentracing/SkywalkingSpan.java
...rg/skywalking/apm/toolkit/opentracing/SkywalkingSpan.java
+1
-2
apm-application-toolkit/apm-toolkit-opentracing/src/test/java/org/skywalking/apm/toolkit/opentracing/SkyWalkingTracerTest.java
...walking/apm/toolkit/opentracing/SkyWalkingTracerTest.java
+0
-39
apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/test/java/org/skywalking/apm/plugin/dubbo/DubboInterceptorTest.java
...org/skywalking/apm/plugin/dubbo/DubboInterceptorTest.java
+115
-127
apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java
...lugin/feign/http/v9/DefaultHttpClientInterceptorTest.java
+98
-68
apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
...pm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
+1
-0
apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/test/java/org/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
...lugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
+95
-90
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/StatementTracing.java
...java/org/skywalking/apm/plugin/jdbc/StatementTracing.java
+1
-1
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/AbstractStatementTest.java
...org/skywalking/apm/plugin/jdbc/AbstractStatementTest.java
+25
-27
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWCallableStatementTest.java
...g/skywalking/apm/plugin/jdbc/SWCallableStatementTest.java
+119
-162
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWConnectionTest.java
...java/org/skywalking/apm/plugin/jdbc/SWConnectionTest.java
+129
-172
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWStatementTest.java
.../java/org/skywalking/apm/plugin/jdbc/SWStatementTest.java
+83
-131
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SwPreparedStatementTest.java
...g/skywalking/apm/plugin/jdbc/SwPreparedStatementTest.java
+113
-159
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java
...sClusterConstructorWithHostAndPortArgInterceptorTest.java
+4
-10
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java
...sterConstructorWithListHostAndPortArgInterceptorTest.java
+10
-17
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
...s/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
+6
-11
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java
...edis/v2/JedisConstructorWithStringArgInterceptorTest.java
+8
-15
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
...n/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
+7
-12
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisMethodInterceptorTest.java
...lking/apm/plugin/jedis/v2/JedisMethodInterceptorTest.java
+77
-114
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
...g/apm/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
+63
-72
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
.../plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
+0
-58
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
...plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
+0
-58
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
.../plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
+0
-135
apm-sniffer/apm-sdk-plugin/motan-plugin/src/test/java/org/skywalking/apm/plugin/motan/MotanConsumerInterceptorTest.java
...alking/apm/plugin/motan/MotanConsumerInterceptorTest.java
+69
-81
apm-sniffer/apm-sdk-plugin/motan-plugin/src/test/java/org/skywalking/apm/plugin/motan/MotanProviderInterceptorTest.java
...alking/apm/plugin/motan/MotanProviderInterceptorTest.java
+80
-94
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java
.../skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java
+5
-3
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java
...walking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java
+91
-76
apm-sniffer/apm-sdk-plugin/pom.xml
apm-sniffer/apm-sdk-plugin/pom.xml
+6
-0
apm-sniffer/apm-sdk-plugin/resin-3.x-plugin/src/test/java/org/skywalking/apm/plugin/resin/v3/ResinV3InterceptorTest.java
...kywalking/apm/plugin/resin/v3/ResinV3InterceptorTest.java
+77
-91
apm-sniffer/apm-sdk-plugin/resin-4.x-plugin/src/test/java/org/skywalking/apm/plugin/resin/v4/ResinV4InterceptorTest.java
...kywalking/apm/plugin/resin/v4/ResinV4InterceptorTest.java
+78
-90
apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/org/skywalking/apm/plugin/tomcat78x/TomcatInterceptorTest.java
...kywalking/apm/plugin/tomcat78x/TomcatInterceptorTest.java
+76
-91
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/AbstractTracingSpanHelper.java
...king/apm/agent/test/helper/AbstractTracingSpanHelper.java
+0
-26
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/FieldSetter.java
...ava/org/skywalking/apm/agent/test/helper/FieldSetter.java
+25
-0
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SegmentHelper.java
...a/org/skywalking/apm/agent/test/helper/SegmentHelper.java
+1
-1
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SegmentRefHelper.java
...rg/skywalking/apm/agent/test/helper/SegmentRefHelper.java
+32
-0
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SpanHelper.java
...java/org/skywalking/apm/agent/test/helper/SpanHelper.java
+73
-0
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/AgentServiceRule.java
...org/skywalking/apm/agent/test/tools/AgentServiceRule.java
+34
-0
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/SegmentStorage.java
...a/org/skywalking/apm/agent/test/tools/SegmentStorage.java
+1
-1
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/SpanAssert.java
.../java/org/skywalking/apm/agent/test/tools/SpanAssert.java
+53
-0
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/TracingSegmentRunner.java
...skywalking/apm/agent/test/tools/TracingSegmentRunner.java
+32
-45
apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/pom.xml
...olkit-activation/apm-toolkit-log4j-1.x-activation/pom.xml
+14
-0
未找到文件。
apm-application-toolkit/apm-toolkit-opentracing/src/main/java/org/skywalking/apm/toolkit/opentracing/SkywalkingSpan.java
浏览文件 @
4c1963cb
...
...
@@ -21,8 +21,7 @@ public class SkywalkingSpan implements Span {
* @param tracer
*/
@NeedSnifferActivation
(
"1. set the span reference to the dynamic field of enhanced SkywalkingSpan"
)
"1. set the span reference to the dynamic field of enhanced SkywalkingSpan"
)
public
SkywalkingSpan
(
SkywalkingTracer
tracer
)
{
}
...
...
apm-application-toolkit/apm-toolkit-opentracing/src/test/java/org/skywalking/apm/toolkit/opentracing/SkyWalkingTracerTest.java
已删除
100644 → 0
浏览文件 @
f64d4142
package
org.skywalking.apm.toolkit.opentracing
;
import
io.opentracing.Span
;
import
io.opentracing.SpanContext
;
import
io.opentracing.Tracer
;
import
io.opentracing.propagation.TextMap
;
import
org.junit.Test
;
import
java.util.Iterator
;
import
java.util.Map
;
/**
* Created by wusheng on 2016/12/21.
*/
public
class
SkyWalkingTracerTest
{
@Test
public
void
testBuildSpan
()
{
Tracer
tracer
=
SkyWalkingTracer
.
INSTANCE
;
Tracer
.
SpanBuilder
spanBuilder
=
tracer
.
buildSpan
(
"/http/serviceName"
);
SpanContext
context
=
new
TextMapContext
(
new
TextMap
()
{
@Override
public
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
()
{
throw
new
UnsupportedOperationException
(
"TextMapInjectAdapter should only be used with Tracer.inject()"
);
}
@Override
public
void
put
(
String
key
,
String
value
)
{
}
});
spanBuilder
.
asChildOf
(
context
).
withTag
(
"example.tag"
,
"testtag"
);
Span
span
=
spanBuilder
.
start
();
span
.
finish
();
}
}
apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/test/java/org/skywalking/apm/plugin/dubbo/DubboInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -5,42 +5,60 @@ import com.alibaba.dubbo.rpc.Invocation;
import
com.alibaba.dubbo.rpc.Invoker
;
import
com.alibaba.dubbo.rpc.Result
;
import
com.alibaba.dubbo.rpc.RpcContext
;
import
java.util.List
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.
Mockito
;
import
org.
powermock.api.mockito.Power
Mockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.context.ContextCarrier
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.plugin.dubbox.BugFixActive
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.context.tag.Tags
;
import
org.skywalking.apm.agent.core.context.util.KeyValuePair
;
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.FieldSetter
;
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.plugin.dubbox.BugFixActive
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
mockStatic
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
when
;
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
@PrepareForTest
({
RpcContext
.
class
,
BugFixActive
.
class
})
public
class
DubboInterceptorTest
{
private
MockTracingContextListener
mockTracerContextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
agentServiceRule
=
new
AgentServiceRule
();
@Mock
private
EnhancedInstance
enhancedInstance
;
private
DubboInterceptor
dubboInterceptor
;
private
RequestParamForTestBelow283
testParam
;
@Mock
private
RpcContext
rpcContext
;
...
...
@@ -49,182 +67,152 @@ public class DubboInterceptorTest {
@Mock
private
Invocation
invocation
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Mock
private
Result
result
;
private
Object
[]
allArguments
;
private
Class
[]
argumentTypes
;
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
dubboInterceptor
=
new
DubboInterceptor
();
testParam
=
new
RequestParamForTestBelow283
();
mockTracerContextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
mockStatic
(
RpcContext
.
class
);
mockStatic
(
BugFixActive
.
class
);
PowerMockito
.
mockStatic
(
RpcContext
.
class
);
when
(
invoker
.
getUrl
()).
thenReturn
(
URL
.
valueOf
(
"dubbo://127.0.0.1:20880/org.skywalking.apm.test.TestDubboService"
));
when
(
invocation
.
getMethodName
()).
thenReturn
(
"test"
);
when
(
invocation
.
getParameterTypes
()).
thenReturn
(
new
Class
[]
{
String
.
class
});
when
(
invocation
.
getArguments
()).
thenReturn
(
new
Object
[]
{
testParam
});
Mockito
.
when
(
RpcContext
.
getContext
()).
thenReturn
(
rpcContext
);
Power
Mockito
.
when
(
RpcContext
.
getContext
()).
thenReturn
(
rpcContext
);
when
(
rpcContext
.
isConsumerSide
()).
thenReturn
(
true
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
invoker
,
invocation
});
allArguments
=
new
Object
[]
{
invoker
,
invocation
};
argumentTypes
=
new
Class
[]
{
invoker
.
getClass
(),
invocation
.
getClass
()};
Config
.
Agent
.
APPLICATION_CODE
=
"DubboTestCases-APP"
;
FieldSetter
.
setStaticValue
(
BugFixActive
.
class
,
"ACTIVE"
,
false
);
}
@Test
public
void
testConsumerBelow283
()
{
when
(
BugFixActive
.
isActive
()).
thenReturn
(
true
);
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertConsumerSpan
(
traceSegment
.
getSpans
().
get
(
0
));
assertNotNull
(
testParam
.
getTraceContext
());
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
contextCarrier
.
deserialize
(
testParam
.
getTraceContext
());
Assert
.
assertTrue
(
contextCarrier
.
isValid
());
}
});
public
void
testConsumerBelow283
()
throws
Throwable
{
BugFixActive
.
active
();
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
assertThat
(
SegmentHelper
.
getSpans
(
traceSegment
).
size
(),
is
(
1
));
assertConsumerSpan
(
SegmentHelper
.
getSpans
(
traceSegment
).
get
(
0
));
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
contextCarrier
.
deserialize
(
testParam
.
getTraceContext
());
assertTrue
(
contextCarrier
.
isValid
());
}
@Test
public
void
testConsumerWithAttachment
()
{
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertConsumerSpan
(
traceSegment
.
getSpans
().
get
(
0
));
}
});
public
void
testConsumerWithAttachment
()
throws
Throwable
{
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertConsumerSpan
(
spans
.
get
(
0
));
}
@Test
public
void
testConsumerWithException
()
{
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertConsumerTraceSegmentInErrorCase
(
traceSegment
);
}
});
public
void
testConsumerWithException
()
throws
Throwable
{
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
handleMethodException
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
new
RuntimeException
());
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
assertConsumerTraceSegmentInErrorCase
(
traceSegment
);
}
@Test
public
void
testConsumerWithResultHasException
()
{
public
void
testConsumerWithResultHasException
()
throws
Throwable
{
when
(
result
.
getException
()).
thenReturn
(
new
RuntimeException
());
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertConsumerTraceSegmentInErrorCase
(
traceSegment
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
assertConsumerTraceSegmentInErrorCase
(
traceSegment
);
}
@Test
public
void
testProviderWithAttachment
()
{
public
void
testProviderWithAttachment
()
throws
Throwable
{
when
(
rpcContext
.
isConsumerSide
()).
thenReturn
(
false
);
when
(
rpcContext
.
getAttachment
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"
302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123
"
);
when
(
rpcContext
.
getAttachment
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"
S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2
"
);
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
assertProvider
();
}
@Test
public
void
testProviderBelow283
()
{
public
void
testProviderBelow283
()
throws
Throwable
{
when
(
rpcContext
.
isConsumerSide
()).
thenReturn
(
false
);
when
(
BugFixActive
.
isActive
()).
thenReturn
(
true
);
FieldSetter
.
setStaticValue
(
BugFixActive
.
class
,
"ACTIVE"
,
true
);
testParam
.
setTraceContext
(
"
302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123
"
);
testParam
.
setTraceContext
(
"
S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2
"
);
dubboInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
result
);
dubboInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
methodInterceptResult
);
dubboInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
allArguments
,
argumentTypes
,
result
);
assertProvider
();
}
private
void
assertConsumerTraceSegmentInErrorCase
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertConsumerSpan
(
traceSegment
.
getSpans
().
get
(
0
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertErrorLog
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertConsumerSpan
(
spans
.
get
(
0
));
AbstractTracingSpan
span
=
spans
.
get
(
0
);
assertThat
(
SpanHelper
.
getLogs
(
span
).
size
(),
is
(
1
));
assertErrorLog
(
SpanHelper
.
getLogs
(
span
).
get
(
0
));
}
private
void
assertErrorLog
(
LogData
logData
)
{
assertThat
(
logData
.
get
Field
s
().
size
(),
is
(
4
));
assertThat
(
logData
.
get
Fields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logData
.
get
Fields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
get
Fields
().
get
(
"message"
));
private
void
assertErrorLog
(
LogData
Entity
logData
)
{
assertThat
(
logData
.
get
Log
s
().
size
(),
is
(
4
));
assertThat
(
logData
.
get
Logs
().
get
(
0
).
getValue
(
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logData
.
get
Logs
().
get
(
1
).
getValue
(
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
get
Logs
().
get
(
2
).
getValue
(
));
}
private
void
assertProvider
()
{
final
TraceSegmentRef
expect
=
new
TraceSegmentRef
();
expect
.
setSpanId
(
1
);
expect
.
setTraceSegmentId
(
"302017.1487666919810.624424584.17332.1.1"
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertProviderSpan
(
traceSegment
.
getSpans
().
get
(
0
));
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
),
expect
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
assertThat
(
SegmentHelper
.
getSpans
(
traceSegment
).
size
(),
is
(
1
));
assertProviderSpan
(
SegmentHelper
.
getSpans
(
traceSegment
).
get
(
0
));
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
private
void
assertTraceSegmentRef
(
TraceSegmentRef
actual
,
TraceSegmentRef
expect
)
{
assertThat
(
actual
.
getSpanId
(),
is
(
expect
.
getSpanId
()
));
assertThat
(
actual
.
getTraceSegmentId
(),
is
(
expect
.
getTraceSegmentId
()
));
private
void
assertTraceSegmentRef
(
TraceSegmentRef
actual
)
{
assertThat
(
SegmentRefHelper
.
getSpanId
(
actual
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
actual
),
is
(
"S.1499176688384.581928182.80935.69.1"
));
}
private
void
assertProviderSpan
(
Span
span
)
{
private
void
assertProviderSpan
(
AbstractTracing
Span
span
)
{
assertCommonsAttribute
(
span
);
assertT
hat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_SERVER
));
assertT
rue
(
span
.
isEntry
(
));
}
private
void
assertConsumerSpan
(
Span
span
)
{
private
void
assertConsumerSpan
(
AbstractTracing
Span
span
)
{
assertCommonsAttribute
(
span
);
assertT
hat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_CLIENT
));
assertT
rue
(
span
.
isExit
(
));
}
private
void
assertCommonsAttribute
(
Span
span
)
{
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"rpc"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
DubboInterceptor
.
DUBBO_COMPONENT
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"dubbo://127.0.0.1:20880/org.skywalking.apm.test.TestDubboService.test(String)"
));
private
void
assertCommonsAttribute
(
AbstractTracingSpan
span
)
{
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
size
(),
is
(
1
));
assertThat
(
SpanHelper
.
getLayer
(
span
),
is
(
SpanLayer
.
RPC_FRAMEWORK
));
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
3
));
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"dubbo://127.0.0.1:20880/org.skywalking.apm.test.TestDubboService.test(String)"
));
assertThat
(
span
.
getOperationName
(),
is
(
"org.skywalking.apm.test.TestDubboService.test(String)"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -5,23 +5,35 @@ import feign.Response;
import
java.nio.charset.Charset
;
import
java.util.Collection
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.BooleanTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
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.tag.Tags
;
import
org.skywalking.apm.agent.core.context.util.KeyValuePair
;
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.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
static
junit
.
framework
.
TestCase
.
assertNotNull
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
...
@@ -29,106 +41,124 @@ import static org.mockito.Mockito.when;
* @author pengys5
*/
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
@PrepareForTest
({
Response
.
class
})
public
class
DefaultHttpClientInterceptorTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
agentServiceRule
=
new
AgentServiceRule
();
private
DefaultHttpClientInterceptor
defaultHttpClientInterceptor
;
private
MockTracingContextListener
mockTracerContextListener
;
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Mock
private
InstanceMethodInvokeContext
instanceMethodInvokeContex
t
;
private
MethodInterceptResult
resul
t
;
private
Request
request
;
private
Object
[]
allArguments
;
private
Class
[]
argumentTypes
;
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracingContextListener
();
classInstanceContext
=
new
EnhancedClassInstanceContext
();
Map
<
String
,
Collection
<
String
>>
headers
=
new
LinkedHashMap
<
String
,
Collection
<
String
>>();
request
=
Request
.
create
(
"GET"
,
"http://skywalking.org"
,
headers
,
"Test"
.
getBytes
(),
Charset
.
forName
(
"UTF-8"
));
Request
.
Options
options
=
new
Request
.
Options
();
Object
[]
allArguments
=
{
request
,
options
};
when
(
instanceMethodInvokeContext
.
allArguments
()).
thenReturn
(
allArguments
);
ServiceManager
.
INSTANCE
.
boot
();
allArguments
=
new
Object
[]
{
request
,
options
};
argumentTypes
=
new
Class
[]
{
request
.
getClass
(),
options
.
getClass
()};
defaultHttpClientInterceptor
=
new
DefaultHttpClientInterceptor
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
}
@Test
public
void
testMethodsAround
()
throws
Throwable
{
defaultHttpClientInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
status
()).
thenReturn
(
200
);
defaultHttpClientInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
false
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
}
});
defaultHttpClientInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
result
);
defaultHttpClientInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
Assert
.
assertEquals
(
1
,
SegmentHelper
.
getSpans
(
traceSegment
).
size
());
AbstractTracingSpan
finishedSpan
=
SegmentHelper
.
getSpans
(
traceSegment
).
get
(
0
);
assertSpan
(
finishedSpan
);
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
finishedSpan
);
assertThat
(
tags
.
size
(),
is
(
2
));
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"GET"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
""
));
Assert
.
assertEquals
(
false
,
SpanHelper
.
getErrorOccurred
(
finishedSpan
));
}
@Test
public
void
testMethodsAroundError
()
throws
Throwable
{
defaultHttpClientInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
defaultHttpClientInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
result
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
status
()).
thenReturn
(
404
);
defaultHttpClientInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
true
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
}
});
defaultHttpClientInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
Assert
.
assertEquals
(
1
,
SegmentHelper
.
getSpans
(
traceSegment
).
size
());
AbstractTracingSpan
finishedSpan
=
SegmentHelper
.
getSpans
(
traceSegment
).
get
(
0
);
assertSpan
(
finishedSpan
);
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
finishedSpan
);
assertThat
(
tags
.
size
(),
is
(
3
));
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"GET"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
""
));
assertThat
(
tags
.
get
(
2
).
getValue
(),
is
(
"404"
));
Assert
.
assertEquals
(
true
,
SpanHelper
.
getErrorOccurred
(
finishedSpan
));
}
private
void
assertSpan
(
Span
span
)
{
Assert
.
assertEquals
(
"http"
,
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
));
Assert
.
assertEquals
(
"GET"
,
StringTagReader
.
get
(
span
,
Tags
.
HTTP
.
METHOD
));
Assert
.
assertEquals
(
"skywalking.org"
,
span
.
getPeerHost
());
Assert
.
assertEquals
(-
1
,
span
.
getPort
());
Assert
.
assertEquals
(
"FeignDefaultHttp"
,
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
));
Assert
.
assertEquals
(
"discovery"
,
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
));
Assert
.
assertEquals
(
""
,
StringTagReader
.
get
(
span
,
Tags
.
URL
));
private
void
assertSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
SpanHelper
.
getLayer
(
span
),
is
(
SpanLayer
.
HTTP
));
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
11
));
}
@Test
public
void
testException
()
throws
Throwable
{
defaultHttpClientInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
defaultHttpClientInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
result
);
defaultHttpClientInterceptor
.
handleMethodException
(
new
NullPointerException
(
"testException"
),
classInstanceContext
,
null
);
defaultHttpClientInterceptor
.
handleMethodException
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
new
NullPointerException
(
"testException"
)
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
status
()).
thenReturn
(
200
);
defaultHttpClientInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
true
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
Assert
.
assertEquals
(
1
,
SpanLogReader
.
getLogs
(
finishedSegment
.
getSpans
().
get
(
0
)).
size
());
Assert
.
assertEquals
(
true
,
SpanLogReader
.
getLogs
(
finishedSegment
.
getSpans
().
get
(
0
)).
get
(
0
).
getFields
().
containsKey
(
"stack"
));
Assert
.
assertEquals
(
"testException"
,
SpanLogReader
.
getLogs
(
finishedSegment
.
getSpans
().
get
(
0
)).
get
(
0
).
getFields
().
get
(
"message"
));
}
});
defaultHttpClientInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
Assert
.
assertEquals
(
1
,
SegmentHelper
.
getSpans
(
traceSegment
).
size
());
AbstractTracingSpan
finishedSpan
=
SegmentHelper
.
getSpans
(
traceSegment
).
get
(
0
);
assertSpan
(
finishedSpan
);
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
finishedSpan
);
assertThat
(
tags
.
size
(),
is
(
2
));
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"GET"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
""
));
Assert
.
assertEquals
(
true
,
SpanHelper
.
getErrorOccurred
(
finishedSpan
));
Assert
.
assertEquals
(
1
,
SpanHelper
.
getLogs
(
finishedSpan
).
size
());
LogDataEntity
logDataEntity
=
SpanHelper
.
getLogs
(
finishedSpan
).
get
(
0
);
assertThat
(
logDataEntity
.
getLogs
().
size
(),
is
(
4
));
assertThat
(
logDataEntity
.
getLogs
().
get
(
0
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logDataEntity
.
getLogs
().
get
(
1
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
NullPointerException
.
class
.
getName
()));
assertThat
(
logDataEntity
.
getLogs
().
get
(
2
).
getValue
(),
is
(
"testException"
));
assertNotNull
(
logDataEntity
.
getLogs
().
get
(
3
).
getValue
());
}
}
apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
浏览文件 @
4c1963cb
...
...
@@ -38,6 +38,7 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
span
.
setComponent
(
ComponentsDefine
.
HTTPCLIENT
);
Tags
.
URL
.
set
(
span
,
httpRequest
.
getRequestLine
().
getUri
());
Tags
.
HTTP
.
METHOD
.
set
(
span
,
httpRequest
.
getRequestLine
().
getMethod
());
SpanLayer
.
asHttp
(
span
);
httpRequest
.
setHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
,
contextCarrier
.
serialize
());
...
...
apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/test/java/org/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.httpClient.v4
;
import
java.lang.reflect.Field
;
import
org.apache.http.*
;
import
org.junit.After
;
import
java.util.List
;
import
org.apache.http.HttpHost
;
import
org.apache.http.HttpRequest
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.ProtocolVersion
;
import
org.apache.http.RequestLine
;
import
org.apache.http.StatusLine
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.Assert
;
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.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.tags.BooleanTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.TraceSegment
;
import
org.skywalking.apm.agent.core.context.tag.Tags
;
import
java.util.List
;
import
org.skywalking.apm.agent.core.context.util.KeyValuePair
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
junit
.
framework
.
TestCase
.
assertNotNull
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
@PrepareForTest
(
HttpHost
.
class
)
public
class
HttpClientExecuteInterceptorTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
agentServiceRule
=
new
AgentServiceRule
();
private
HttpClientExecuteInterceptor
httpClientExecuteInterceptor
;
private
MockTracingContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
instanceMethodInvokeContext
;
@Mock
private
HttpHost
httpHost
;
@Mock
...
...
@@ -44,16 +60,20 @@ public class HttpClientExecuteInterceptorTest {
@Mock
private
StatusLine
statusLine
;
private
Object
[]
allArguments
;
private
Class
[]
argumentsType
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracingContextListener
();
ServiceManager
.
INSTANCE
.
boot
();
httpClientExecuteInterceptor
=
new
HttpClientExecuteInterceptor
();
PowerMockito
.
mock
(
HttpHost
.
class
);
when
(
statusLine
.
getStatusCode
()).
thenReturn
(
200
);
when
(
instanceMethodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
httpHost
,
request
});
when
(
httpResponse
.
getStatusLine
()).
thenReturn
(
statusLine
);
when
(
httpHost
.
getHostName
()).
thenReturn
(
"127.0.0.1"
);
when
(
httpHost
.
getSchemeName
()).
thenReturn
(
"http"
);
...
...
@@ -75,95 +95,80 @@ public class HttpClientExecuteInterceptorTest {
});
when
(
httpHost
.
getPort
()).
thenReturn
(
8080
);
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
allArguments
=
new
Object
[]
{
httpHost
,
request
};
argumentsType
=
new
Class
[]
{
httpHost
.
getClass
(),
request
.
getClass
()};
}
@Test
public
void
testHttpClient
()
{
httpClientExecuteInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
httpClientExecuteInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
httpResponse
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertHttpSpan
(
traceSegment
.
getSpans
().
get
(
0
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
});
}
public
void
testHttpClient
()
throws
Throwable
{
httpClientExecuteInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
null
);
httpClientExecuteInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
httpResponse
);
@Test
public
void
testStatusCodeNotEquals200
()
{
when
(
statusLine
.
getStatusCode
()).
thenReturn
(
500
);
httpClientExecuteInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
httpClientExecuteInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
httpResponse
);
Assert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertHttpSpan
(
traceSegment
.
getSpans
().
get
(
0
));
assertThat
(
BooleanTagReader
.
get
(
traceSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
),
is
(
true
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
});
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertHttpSpan
(
spans
.
get
(
0
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
@Test
public
void
test
HttpClientWithException
()
{
httpClientExecuteInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
httpClientExecuteInterceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
instanceMethodInvokeContext
);
httpClientExecuteInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
httpResponse
);
public
void
test
StatusCodeNotEquals200
()
throws
Throwable
{
when
(
statusLine
.
getStatusCode
()).
thenReturn
(
500
);
httpClientExecuteInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
null
);
httpClientExecuteInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
httpResponse
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
BooleanTagReader
.
get
(
span
,
Tags
.
ERROR
),
is
(
true
));
try
{
assertHttpSpanErrorLog
(
getLogs
(
span
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
Assert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
}
assertThat
(
spans
.
size
(),
is
(
1
));
private
void
assertHttpSpanErrorLog
(
List
<
LogData
>
logs
)
{
assertThat
(
logs
.
size
(),
is
(
1
));
LogData
logData
=
logs
.
get
(
0
);
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
}
});
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
spans
.
get
(
0
));
assertThat
(
tags
.
size
(),
is
(
3
));
assertThat
(
tags
.
get
(
2
).
getValue
(),
is
(
"500"
));
assertHttpSpan
(
spans
.
get
(
0
));
assertThat
(
SpanHelper
.
getErrorOccurred
(
spans
.
get
(
0
)),
is
(
true
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
private
void
assertHttpSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test-web/test"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"HttpClient"
));
assertThat
(
span
.
getPeerHost
(),
is
(
"127.0.0.1"
));
assertThat
(
span
.
getPort
(),
is
(
8080
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"http://127.0.0.1:8080/test-web/test"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_CLIENT
));
@Test
public
void
testHttpClientWithException
()
throws
Throwable
{
httpClientExecuteInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
null
);
httpClientExecuteInterceptor
.
handleMethodException
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
new
RuntimeException
(
"testException"
));
httpClientExecuteInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentsType
,
httpResponse
);
Assert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
AbstractTracingSpan
span
=
spans
.
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
SpanHelper
.
getErrorOccurred
(
span
),
is
(
true
));
assertHttpSpanErrorLog
(
SpanHelper
.
getLogs
(
span
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
private
void
assertHttpSpanErrorLog
(
List
<
LogDataEntity
>
logs
)
{
assertThat
(
logs
.
size
(),
is
(
1
));
LogDataEntity
logData
=
logs
.
get
(
0
);
Assert
.
assertThat
(
logData
.
getLogs
().
size
(),
is
(
4
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
0
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
"error"
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
1
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
2
).
getValue
(),
is
(
"testException"
));
assertNotNull
(
logData
.
getLogs
().
get
(
3
).
getValue
());
}
protected
List
<
LogData
>
getLogs
(
Span
span
)
throws
NoSuchFieldException
,
IllegalAccessException
{
Field
logs
=
Span
.
class
.
getDeclaredField
(
"logs"
);
logs
.
setAccessible
(
true
);
return
(
List
<
LogData
>)
logs
.
get
(
span
);
private
void
assertHttpSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test-web/test"
));
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
2
));
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"http://127.0.0.1:8080/test-web/test"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
"GET"
));
assertThat
(
span
.
isExit
(),
is
(
true
));
}
}
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/StatementTracing.java
浏览文件 @
4c1963cb
...
...
@@ -24,7 +24,7 @@ public class StatementTracing {
Tags
.
DB_TYPE
.
set
(
span
,
"sql"
);
Tags
.
DB_INSTANCE
.
set
(
span
,
connectInfo
.
getDatabaseName
());
Tags
.
DB_STATEMENT
.
set
(
span
,
sql
);
span
.
setComponent
(
connectInfo
.
get
DBType
());
span
.
setComponent
(
connectInfo
.
get
Component
());
SpanLayer
.
asDB
(
span
);
return
exec
.
exe
(
realStatement
,
sql
);
}
catch
(
SQLException
e
)
{
...
...
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/AbstractStatementTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jdbc
;
import
java.
lang.reflect.Field
;
import
java.
sql.SQLException
;
import
java.util.List
;
import
org.hamcrest.CoreMatchers
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
import
org.skywalking.apm.agent.core.context.tag.Tags
;
import
java.sql.SQLException
;
import
org.junit.Assert
;
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.util.KeyValuePair
;
import
org.skywalking.apm.agent.test.helper.SpanHelper
;
import
static
junit
.
framework
.
TestCase
.
assertNotNull
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
junit
.
Assert
.*;
public
abstract
class
AbstractStatementTest
{
protected
MockTracingContextListener
mockTracerContextListener
;
protected
void
assertDBSpanLog
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
SQLException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
protected
void
assertDBSpanLog
(
LogDataEntity
logData
)
{
Assert
.
assertThat
(
logData
.
getLogs
().
size
(),
is
(
4
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
0
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
"error"
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
1
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
SQLException
.
class
.
getName
()));
Assert
.
assertNull
(
logData
.
getLogs
().
get
(
2
).
getValue
());
assertNotNull
(
logData
.
getLogs
().
get
(
3
).
getValue
());
}
protected
void
assertDBSpan
(
Span
span
,
String
exceptOperationName
,
String
exceptDBStatement
)
{
protected
void
assertDBSpan
(
AbstractTracing
Span
span
,
String
exceptOperationName
,
String
exceptDBStatement
)
{
assertDBSpan
(
span
,
exceptOperationName
);
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_STATEMENT
),
is
(
exceptDBStatement
));
assertThat
(
span
.
isExit
(),
is
(
true
));
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
get
(
2
).
getValue
(),
is
(
exceptDBStatement
));
}
protected
void
assertDBSpan
(
Span
span
,
String
exceptOperationName
)
{
protected
void
assertDBSpan
(
AbstractTracing
Span
span
,
String
exceptOperationName
)
{
assertThat
(
span
.
getOperationName
(),
is
(
exceptOperationName
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Mysql"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_INSTANCE
),
is
(
"test"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"db"
));
}
protected
List
<
LogData
>
getLogs
(
Span
span
)
throws
NoSuchFieldException
,
IllegalAccessException
{
Field
logs
=
Span
.
class
.
getDeclaredField
(
"logs"
);
logs
.
setAccessible
(
true
);
return
(
List
<
LogData
>)
logs
.
get
(
span
);
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
5
));
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"sql"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
"test"
));
assertThat
(
SpanHelper
.
getLayer
(
span
),
is
(
SpanLayer
.
DB
));
}
}
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWCallableStatementTest.java
浏览文件 @
4c1963cb
此差异已折叠。
点击以展开。
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWConnectionTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jdbc
;
import
com.mysql.cj.api.jdbc.JdbcConnection
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
java.sql.PreparedStatement
;
import
java.sql.SQLException
;
import
java.sql.Savepoint
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.concurrent.Executor
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
...
...
@@ -25,10 +30,21 @@ import static org.mockito.Matchers.any;
import
static
org
.
mockito
.
Matchers
.
anyBoolean
;
import
static
org
.
mockito
.
Matchers
.
anyInt
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
mockito
.
Mockito
.
doThrow
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
SWConnectionTest
extends
AbstractStatementTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
com
.
mysql
.
cj
.
jdbc
.
PreparedStatement
mysqlPreparedStatement
;
@Mock
...
...
@@ -42,13 +58,8 @@ public class SWConnectionTest extends AbstractStatementTest {
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
mockTracerContextListener
=
new
MockTracingContextListener
();
swConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306/test"
,
new
Properties
(),
jdbcConnection
);
multiHostConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306,127.0.0.1:3309/test"
,
new
Properties
(),
jdbcConnection
);
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
when
(
jdbcConnection
.
prepareStatement
(
anyString
())).
thenReturn
(
mysqlPreparedStatement
);
}
...
...
@@ -58,14 +69,12 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
commit
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
}
@Test
...
...
@@ -73,14 +82,12 @@ public class SWConnectionTest extends AbstractStatementTest {
PreparedStatement
preparedStatement
=
swConnection
.
prepareStatement
(
"SELECT * FROM test"
,
new
String
[]
{
"1"
});
multiHostConnection
.
commit
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -90,21 +97,14 @@ public class SWConnectionTest extends AbstractStatementTest {
try
{
swConnection
.
commit
();
}
finally
{
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
try
{
assertDBSpanLog
(
getLogs
(
traceSegment
.
getSpans
().
get
(
0
)).
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/commit"
);
assertThat
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
size
(),
is
(
1
));
assertDBSpanLog
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
));
}
}
...
...
@@ -113,15 +113,12 @@ public class SWConnectionTest extends AbstractStatementTest {
PreparedStatement
preparedStatement
=
swConnection
.
prepareStatement
(
"SELECT * FROM test"
,
1
,
1
);
swConnection
.
rollback
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
}
});
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
}
@Test
...
...
@@ -129,14 +126,12 @@ public class SWConnectionTest extends AbstractStatementTest {
PreparedStatement
preparedStatement
=
swConnection
.
prepareStatement
(
"SELECT * FROM test"
,
1
,
1
,
1
);
multiHostConnection
.
rollback
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -145,42 +140,38 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
rollback
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback"
);
}
@Test
public
void
testRollBackWithSavePoint
()
throws
SQLException
{
swConnection
.
rollback
(
savepoint
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
}
@Test
public
void
testMultiHostRollBackWithSavePoint
()
throws
SQLException
{
multiHostConnection
.
rollback
(
savepoint
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -189,21 +180,14 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
rollback
(
savepoint
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
try
{
assertDBSpanLog
(
getLogs
(
traceSegment
.
getSpans
().
get
(
0
)).
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/rollback to savepoint"
);
assertDBSpanLog
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
));
}
@Test
...
...
@@ -211,28 +195,26 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
close
();
swConnection
.
clearWarnings
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/close"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/close"
);
}
@Test
public
void
testMultiHostClose
()
throws
SQLException
{
multiHostConnection
.
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/close"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/close"
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -241,21 +223,13 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/close"
);
try
{
assertDBSpanLog
(
getLogs
(
traceSegment
.
getSpans
().
get
(
0
)).
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/close"
);
assertDBSpanLog
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
));
}
@Test
...
...
@@ -263,28 +237,25 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
releaseSavepoint
(
savepoint
);
swConnection
.
clearWarnings
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
}
@Test
public
void
testMultiHostReleaseSavePoint
()
throws
SQLException
{
multiHostConnection
.
releaseSavepoint
(
savepoint
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -293,21 +264,13 @@ public class SWConnectionTest extends AbstractStatementTest {
swConnection
.
releaseSavepoint
(
savepoint
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
assertDBSpan
(
traceSegment
.
getSpans
().
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
try
{
assertDBSpanLog
(
getLogs
(
traceSegment
.
getSpans
().
get
(
0
)).
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Connection/releaseSavepoint savepoint"
);
assertDBSpanLog
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
));
}
@Test
...
...
@@ -370,10 +333,4 @@ public class SWConnectionTest extends AbstractStatementTest {
verify
(
jdbcConnection
,
times
(
1
)).
setTypeMap
(
any
(
HashMap
.
class
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SWStatementTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jdbc
;
import
com.mysql.cj.api.jdbc.JdbcConnection
;
import
java.net.MalformedURLException
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.List
;
import
java.util.Properties
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManag
er
;
import
org.
skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.
sniffer.mock.context.SegmentAssert
;
import
org.
powermock.modules.junit4.PowerMockRunn
er
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.
agent.core.context.trace.AbstractTracingSpan
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
java.net.MalformedURLException
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Properties
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
anyBoolean
;
import
static
org
.
mockito
.
Matchers
.
anyInt
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
SWStatementTest
extends
AbstractStatementTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
com
.
mysql
.
cj
.
jdbc
.
StatementImpl
mysqlStatement
;
@Mock
...
...
@@ -39,14 +54,9 @@ public class SWStatementTest extends AbstractStatementTest {
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracingContextListener
();
ServiceManager
.
INSTANCE
.
boot
();
swConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306/test"
,
new
Properties
(),
jdbcConnection
);
multiHostConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306,127.0.0.1:3309/test"
,
new
Properties
(),
jdbcConnection
);
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
when
(
jdbcConnection
.
createStatement
()).
thenReturn
(
mysqlStatement
);
when
(
jdbcConnection
.
createStatement
(
anyInt
(),
anyInt
())).
thenReturn
(
mysqlStatement
);
when
(
jdbcConnection
.
createStatement
(
anyInt
(),
anyInt
(),
anyInt
())).
thenReturn
(
mysqlStatement
);
...
...
@@ -111,15 +121,10 @@ public class SWStatementTest extends AbstractStatementTest {
verify
(
mysqlStatement
,
times
(
1
)).
getResultSet
();
assertThat
(
connection
,
CoreMatchers
.<
Connection
>
is
(
swConnection
));
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/execute"
,
"SELECT * FROM test"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/execute"
,
"SELECT * FROM test"
);
}
@Test
...
...
@@ -127,15 +132,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
);
boolean
executeSuccess
=
statement
.
execute
(
"SELECT * FROM test"
,
1
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/execute"
,
"SELECT * FROM test"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/execute"
,
"SELECT * FROM test"
);
}
@Test
...
...
@@ -143,15 +144,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
ResultSet
executeSuccess
=
statement
.
executeQuery
(
"SELECT * FROM test"
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeQuery"
,
"SELECT * FROM test"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeQuery"
,
"SELECT * FROM test"
);
}
@Test
...
...
@@ -159,15 +156,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
int
executeSuccess
=
statement
.
executeUpdate
(
"UPDATE test SET a = 1"
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -177,15 +170,11 @@ public class SWStatementTest extends AbstractStatementTest {
statement
.
getGeneratedKeys
();
verify
(
mysqlStatement
,
times
(
1
)).
getGeneratedKeys
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -193,15 +182,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
int
executeSuccess
=
statement
.
executeUpdate
(
"UPDATE test SET a = 1"
,
new
int
[]
{
1
});
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -209,15 +194,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
int
executeSuccess
=
statement
.
executeUpdate
(
"UPDATE test SET a = 1"
,
new
String
[]
{
"1"
});
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -225,15 +206,11 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
boolean
executeSuccess
=
statement
.
execute
(
"UPDATE test SET a = 1"
,
new
int
[]
{
1
});
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -241,15 +218,10 @@ public class SWStatementTest extends AbstractStatementTest {
Statement
statement
=
swConnection
.
createStatement
(
1
,
1
,
1
);
boolean
executeSuccess
=
statement
.
execute
(
"UPDATE test SET a = 1"
,
new
String
[]
{
"1"
});
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -263,15 +235,11 @@ public class SWStatementTest extends AbstractStatementTest {
verify
(
mysqlStatement
,
times
(
1
)).
addBatch
(
anyString
());
verify
(
mysqlStatement
,
times
(
1
)).
clearBatch
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/executeBatch"
,
""
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/executeBatch"
,
""
);
}
@Test
(
expected
=
SQLException
.
class
)
...
...
@@ -282,30 +250,14 @@ public class SWStatementTest extends AbstractStatementTest {
statement
.
execute
(
"UPDATE test SET a = 1 WHERE b = 2"
);
}
finally
{
verify
(
mysqlStatement
,
times
(
1
)).
execute
(
anyString
());
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1 WHERE b = 2"
);
List
<
LogData
>
logs
=
null
;
try
{
logs
=
getLogs
(
span
);
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
assertThat
(
logs
.
size
(),
is
(
1
));
assertDBSpanLog
(
logs
.
get
(
0
));
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/Statement/execute"
,
"UPDATE test SET a = 1 WHERE b = 2"
);
assertThat
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
size
(),
is
(
1
));
assertDBSpanLog
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
));
}
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/SwPreparedStatementTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jdbc
;
import
com.mysql.cj.api.jdbc.JdbcConnection
;
import
java.lang.reflect.Field
;
import
java.io.InputStream
;
import
java.io.Reader
;
import
java.math.BigDecimal
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.sql.Array
;
import
java.sql.Blob
;
import
java.sql.Clob
;
import
java.sql.Connection
;
import
java.sql.Date
;
import
java.sql.NClob
;
import
java.sql.PreparedStatement
;
import
java.sql.Ref
;
import
java.sql.ResultSet
;
import
java.sql.RowId
;
import
java.sql.SQLException
;
import
java.sql.SQLXML
;
import
java.sql.Time
;
import
java.sql.Timestamp
;
import
java.util.Calendar
;
import
java.util.List
;
import
java.util.Properties
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Matchers
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
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.TraceSegment
;
import
java.io.InputStream
;
import
java.io.Reader
;
import
java.math.BigDecimal
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.sql.*
;
import
java.util.Calendar
;
import
java.util.Properties
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
...
...
@@ -37,10 +55,18 @@ import static org.mockito.Matchers.anyInt;
import
static
org
.
mockito
.
Matchers
.
anyLong
;
import
static
org
.
mockito
.
Matchers
.
anyShort
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
SwPreparedStatementTest
extends
AbstractStatementTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
Array
array
;
...
...
@@ -70,14 +96,9 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracingContextListener
();
ServiceManager
.
INSTANCE
.
boot
();
swConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306/test"
,
new
Properties
(),
jdbcConnection
);
multiHostConnection
=
new
SWConnection
(
"jdbc:mysql://127.0.0.1:3306,127.0.0.1:3309/test"
,
new
Properties
(),
jdbcConnection
);
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
when
(
jdbcConnection
.
prepareStatement
(
anyString
())).
thenReturn
(
mysqlPreparedStatement
);
when
(
jdbcConnection
.
prepareStatement
(
anyString
(),
anyInt
(),
anyInt
(),
anyInt
())).
thenReturn
(
mysqlPreparedStatement
);
when
(
jdbcConnection
.
prepareStatement
(
anyString
(),
anyInt
(),
anyInt
())).
thenReturn
(
mysqlPreparedStatement
);
...
...
@@ -98,14 +119,14 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
setCharacterStream
(
4
,
reader
);
preparedStatement
.
setCharacterStream
(
4
,
reader
,
10
);
preparedStatement
.
setCharacterStream
(
5
,
reader
,
10L
);
preparedStatement
.
setShort
(
6
,
(
short
)
12
);
preparedStatement
.
setShort
(
6
,
(
short
)
12
);
preparedStatement
.
setInt
(
7
,
1
);
preparedStatement
.
setString
(
8
,
"test"
);
preparedStatement
.
setBoolean
(
9
,
true
);
preparedStatement
.
setLong
(
10
,
100L
);
preparedStatement
.
setDouble
(
11
,
12.0
);
preparedStatement
.
setFloat
(
12
,
12.0f
);
preparedStatement
.
setByte
(
13
,
(
byte
)
1
);
preparedStatement
.
setByte
(
13
,
(
byte
)
1
);
preparedStatement
.
setBytes
(
14
,
bytesParam
);
preparedStatement
.
setDate
(
15
,
new
Date
(
System
.
currentTimeMillis
()));
preparedStatement
.
setNull
(
16
,
1
);
...
...
@@ -271,15 +292,10 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
executeQuery
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test"
);
}
@Test
...
...
@@ -292,15 +308,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
executeQuery
(
anyString
());
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test"
);
}
@Test
...
...
@@ -311,15 +323,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
execute
(
anyString
(),
anyInt
());
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
@Test
...
...
@@ -329,15 +337,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
close
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
@Test
...
...
@@ -347,15 +351,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
close
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/execute"
,
"INSERT INTO test VALUES(1)"
);
}
@Test
...
...
@@ -368,15 +368,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
execute
(
anyString
());
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/execute"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/execute"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -389,15 +385,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
executeUpdate
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = ?"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = ?"
);
}
@Test
...
...
@@ -410,15 +402,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
executeUpdate
(
anyString
());
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -430,15 +418,10 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
close
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -450,15 +433,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
close
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
...
...
@@ -470,21 +449,16 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
close
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
close
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeUpdate"
,
"UPDATE test SET a = 1"
);
}
@Test
public
void
testBatch
()
throws
SQLException
,
MalformedURLException
{
PreparedStatement
preparedStatement
=
multiHostConnection
.
prepareStatement
(
"UPDATE test SET a = ? WHERE b = ?"
);
preparedStatement
.
setShort
(
1
,
(
short
)
12
);
preparedStatement
.
setShort
(
1
,
(
short
)
12
);
preparedStatement
.
setTime
(
2
,
new
Time
(
System
.
currentTimeMillis
()));
preparedStatement
.
addBatch
();
int
[]
resultSet
=
preparedStatement
.
executeBatch
();
...
...
@@ -494,15 +468,11 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
addBatch
();
verify
(
mysqlPreparedStatement
,
times
(
1
)).
clearBatch
();
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeBatch"
,
""
);
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeBatch"
,
""
);
}
@Test
...
...
@@ -528,7 +498,7 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
preparedStatement
.
setBigDecimal
(
1
,
new
BigDecimal
(
10000
));
preparedStatement
.
setBlob
(
2
,
inputStream
);
preparedStatement
.
setBlob
(
3
,
inputStream
,
1000000L
);
preparedStatement
.
setByte
(
3
,
(
byte
)
1
);
preparedStatement
.
setByte
(
3
,
(
byte
)
1
);
preparedStatement
.
setBytes
(
4
,
new
byte
[]
{
1
,
2
});
preparedStatement
.
setLong
(
5
,
100L
);
...
...
@@ -543,33 +513,17 @@ public class SwPreparedStatementTest extends AbstractStatementTest {
verify
(
mysqlPreparedStatement
,
times
(
1
)).
setBlob
(
anyInt
(),
any
(
InputStream
.
class
),
anyLong
());
verify
(
mysqlPreparedStatement
,
times
(
1
)).
setByte
(
anyInt
(),
anyByte
());
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertDBSpan
(
span
,
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test WHERE a = ? or b = ? or c=? or d = ? or e=?"
);
try
{
Field
logs
=
Span
.
class
.
getDeclaredField
(
"logs"
);
logs
.
setAccessible
(
true
);
List
<
LogData
>
logData
=
(
List
<
LogData
>)
logs
.
get
(
span
);
Assert
.
assertThat
(
logData
.
size
(),
is
(
1
));
assertThat
(
logData
.
size
(),
is
(
1
));
assertDBSpanLog
(
logData
.
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertDBSpan
(
spans
.
get
(
0
),
"Mysql/JDBI/PreparedStatement/executeQuery"
,
"SELECT * FROM test WHERE a = ? or b = ? or c=? or d = ? or e=?"
);
List
<
LogDataEntity
>
logData
=
SpanHelper
.
getLogs
(
spans
.
get
(
0
));
Assert
.
assertThat
(
logData
.
size
(),
is
(
1
));
assertThat
(
logData
.
size
(),
is
(
1
));
assertDBSpanLog
(
logData
.
get
(
0
));
}
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -6,6 +6,7 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
redis.clients.jedis.HostAndPort
;
import
static
org
.
mockito
.
Mockito
.*;
...
...
@@ -17,15 +18,11 @@ public class JedisClusterConstructorWithHostAndPortArgInterceptorTest {
private
JedisClusterConstructorWithHostAndPortArgInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
ConstructorInvokeContext
invokeContext
;
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
JedisClusterConstructorWithHostAndPortArgInterceptor
();
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
new
HostAndPort
(
"127.0.0.1"
,
6379
)});
}
@After
...
...
@@ -35,11 +32,8 @@ public class JedisClusterConstructorWithHostAndPortArgInterceptorTest {
@Test
public
void
onConstruct
()
throws
Exception
{
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_CONN_INFO
,
"127.0.0.1:6379"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_PORT
,
6379
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
new
HostAndPort
(
"127.0.0.1"
,
6379
)});
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:6379"
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jedis.v2
;
import
java.util.HashSet
;
import
java.util.Set
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
redis.clients.jedis.HostAndPort
;
import
java.util.HashSet
;
import
java.util.Set
;
import
static
org
.
mockito
.
Matchers
.
contains
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
skywalking
.
apm
.
plugin
.
jedis
.
v2
.
JedisMethodInterceptor
.
KEY_OF_REDIS_CONN_INFO
;
import
static
org
.
skywalking
.
apm
.
plugin
.
jedis
.
v2
.
JedisMethodInterceptor
.
KEY_OF_REDIS_HOSTS
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
JedisClusterConstructorWithListHostAndPortArgInterceptorTest
{
private
JedisClusterConstructorWithListHostAndPortArgInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
ConstructorInvokeContext
invokeContext
;
private
Set
<
HostAndPort
>
hostAndPortSet
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
hostAndPortSet
=
new
HashSet
<
HostAndPort
>();
interceptor
=
new
JedisClusterConstructorWithListHostAndPortArgInterceptor
();
hostAndPortSet
.
add
(
new
HostAndPort
(
"127.0.0.1"
,
6379
));
hostAndPortSet
.
add
(
new
HostAndPort
(
"127.0.0.1"
,
16379
));
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
hostAndPortSet
});
}
@After
...
...
@@ -45,10 +39,9 @@ public class JedisClusterConstructorWithListHostAndPortArgInterceptorTest {
@Test
public
void
onConstruct
()
throws
Exception
{
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
hostAndPortSet
}
);
verify
(
instanceContext
,
times
(
1
)).
set
(
eq
(
KEY_OF_REDIS_CONN_INFO
),
contains
(
"127.0.0.1:6379;"
));
verify
(
instanceContext
,
times
(
1
)).
set
(
eq
(
KEY_OF_REDIS_HOSTS
),
contains
(
"127.0.0.1:16379;"
));
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:6379;127.0.0.1:16379;"
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -6,24 +6,21 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
redis.clients.jedis.JedisShardInfo
;
import
static
org
.
mockito
.
Mockito
.
*
;
import
static
org
.
skywalking
.
apm
.
plugin
.
jedis
.
v2
.
JedisMethodInterceptor
.*
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
JedisConstructorWithShardInfoArgInterceptorTest
{
private
JedisConstructorWithShardInfoArgInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
ConstructorInvokeContext
invokeContext
;
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
JedisConstructorWithShardInfoArgInterceptor
();
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
new
JedisShardInfo
(
"127.0.0.1"
,
6379
)});
}
@After
...
...
@@ -34,10 +31,8 @@ public class JedisConstructorWithShardInfoArgInterceptorTest {
@Test
public
void
onConstruct
()
throws
Exception
{
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_CONN_INFO
,
"127.0.0.1:6379"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_PORT
,
6379
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
new
JedisShardInfo
(
"127.0.0.1"
,
6379
)});
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:6379"
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -5,9 +5,10 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
static
org
.
mockito
.
Mockito
.
*
;
import
static
org
.
skywalking
.
apm
.
plugin
.
jedis
.
v2
.
JedisMethodInterceptor
.*
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
JedisConstructorWithStringArgInterceptorTest
{
...
...
@@ -15,33 +16,25 @@ public class JedisConstructorWithStringArgInterceptorTest {
private
JedisConstructorWithStringArgInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
ConstructorInvokeContext
invokeContext
;
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
JedisConstructorWithStringArgInterceptor
();
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
"127.0.0.1"
});
}
@Test
public
void
onConstruct
()
throws
Exception
{
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
"127.0.0.1"
}
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_CONN_INFO
,
"127.0.0.1:6379"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_PORT
,
6379
);
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:6379"
);
}
@Test
public
void
onConstructWithPort
()
{
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
"127.0.0.1"
,
16379
});
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
"127.0.0.1"
,
16379
});
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_CONN_INFO
,
"127.0.0.1:16379"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
KEY_OF_REDIS_PORT
,
16379
);
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:16379"
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jedis.v2
;
import
java.net.URI
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
java.net.URI
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
(
URI
.
class
)
...
...
@@ -18,24 +19,18 @@ public class JedisConstructorWithUriArgInterceptorTest {
private
JedisConstructorWithUriArgInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
ConstructorInvokeContext
invokeContext
;
private
EnhancedInstance
enhancedInstance
;
private
URI
uri
=
URI
.
create
(
"http://127.0.0.1:6379"
);
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
JedisConstructorWithUriArgInterceptor
();
when
(
invokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
uri
});
}
@Test
public
void
onConstruct
()
throws
Exception
{
interceptor
.
onConstruct
(
instanceContext
,
invokeContext
);
interceptor
.
onConstruct
(
enhancedInstance
,
new
Object
[]
{
uri
}
);
verify
(
instanceContext
,
times
(
1
)).
set
(
JedisMethodInterceptor
.
KEY_OF_REDIS_CONN_INFO
,
"127.0.0.1:6379"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
JedisMethodInterceptor
.
KEY_OF_REDIS_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
JedisMethodInterceptor
.
KEY_OF_REDIS_PORT
,
6379
);
verify
(
enhancedInstance
,
times
(
1
)).
setSkyWalkingDynamicField
(
"127.0.0.1:6379"
);
}
}
apm-sniffer/apm-sdk-plugin/jedis-2.x-plugin/src/test/java/org/skywalking/apm/plugin/jedis/v2/JedisMethodInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.jedis.v2
;
import
java.lang.reflect.Field
;
import
java.util.List
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.runners.MockitoJUnit
Runner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.
sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.
sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.
sniffer.mock.trace.tags.StringTagRead
er
;
import
org.
powermock.modules.junit4.PowerMock
Runner
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
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.SpanLay
er
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.skywalking.apm.agent.core.context.tag.Tags
;
import
org.skywalking.apm.agent.core.context.util.KeyValuePair
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
junit
.
framework
.
TestCase
.
assertNotNull
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.*
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
skywalking
.
apm
.
plugin
.
jedis
.
v2
.
JedisMethodInterceptor
.*;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
JedisMethodInterceptorTest
{
private
JedisMethodInterceptor
interceptor
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
private
MockTracingContextListener
mockTracerContextListener
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
private
EnhancedInstance
enhancedInstance
;
private
JedisMethodInterceptor
interceptor
;
private
Object
[]
allArgument
;
private
Class
[]
argumentType
;
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
allArgument
=
new
Object
[]
{
"OperationKey"
,
"OperationValue"
};
argumentType
=
new
Class
[]
{
String
.
class
,
String
.
class
};
interceptor
=
new
JedisMethodInterceptor
();
mockTracerContextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
when
(
classInstanceContext
.
get
(
KEY_OF_REDIS_HOST
)).
thenReturn
(
"127.0.0.1"
);
when
(
classInstanceContext
.
get
(
KEY_OF_REDIS_PORT
)).
thenReturn
(
6379
);
when
(
methodInvokeContext
.
methodName
()).
thenReturn
(
"set"
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
"OperationKey"
});
when
(
classInstanceContext
.
isContain
(
"__$invokeCounterKey"
)).
thenReturn
(
true
);
when
(
enhancedInstance
.
getSkyWalkingDynamicField
()).
thenReturn
(
"127.0.0.1:6379"
);
}
@Test
public
void
testIntercept
()
{
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
0
);
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
1
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
}
});
public
void
testIntercept
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
interceptor
.
afterMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertRedisSpan
(
spans
.
get
(
0
));
}
@Test
public
void
testInterceptWithMultiHost
()
{
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
0
);
when
(
classInstanceContext
.
get
(
KEY_OF_REDIS_HOST
)).
thenReturn
(
null
);
when
(
classInstanceContext
.
get
(
KEY_OF_REDIS_HOSTS
)).
thenReturn
(
"127.0.0.1:6379;127.0.0.1:16379;"
);
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
1
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
,
"127.0.0.1:6379;127.0.0.1:16379;"
);
}
});
public
void
testInterceptWithMultiHost
()
throws
Throwable
{
when
(
enhancedInstance
.
getSkyWalkingDynamicField
()).
thenReturn
(
"127.0.0.1:6379;127.0.0.1:16379;"
);
interceptor
.
beforeMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
interceptor
.
afterMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertRedisSpan
(
spans
.
get
(
0
));
}
@Test
public
void
testInterceptWithException
()
{
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
0
);
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
when
(
classInstanceContext
.
get
(
"__$invokeCounterKey"
)).
thenReturn
(
1
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
try
{
Field
logs
=
Span
.
class
.
getDeclaredField
(
"logs"
);
logs
.
setAccessible
(
true
);
List
<
LogData
>
logData
=
(
List
<
LogData
>)
logs
.
get
(
span
);
assertThat
(
logData
.
size
(),
is
(
1
));
assertLogData
(
logData
.
get
(
0
));
}
catch
(
NoSuchFieldException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
});
}
public
void
testInterceptWithException
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
interceptor
.
handleMethodException
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
new
RuntimeException
());
interceptor
.
afterMethod
(
enhancedInstance
,
"set"
,
allArgument
,
argumentType
,
null
);
private
void
assertLogData
(
LogData
logData
)
{
MatcherAssert
.
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
RuntimeException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
}
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
assertRedisSpan
(
spans
.
get
(
0
));
private
void
assertRedisSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"Jedis/set"
));
assertThat
(
span
.
getPeerHost
(),
is
(
"127.0.0.1"
));
assertThat
(
span
.
getPort
(),
is
(
6379
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Redis"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_STATEMENT
),
is
(
"set OperationKey"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_TYPE
),
is
(
"Redis"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"db"
));
assertLogData
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)));
}
private
void
assertRedisSpan
(
Span
span
,
String
exceptedPeerHosts
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"Jedis/set"
));
assertThat
(
span
.
getPeers
(),
is
(
exceptedPeerHosts
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Redis"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_STATEMENT
),
is
(
"set OperationKey"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_TYPE
),
is
(
"Redis"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"db"
));
private
void
assertLogData
(
List
<
LogDataEntity
>
logDataEntities
)
{
assertThat
(
logDataEntities
.
size
(),
is
(
1
));
LogDataEntity
logData
=
logDataEntities
.
get
(
0
);
Assert
.
assertThat
(
logData
.
getLogs
().
size
(),
is
(
4
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
0
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
"error"
));
Assert
.
assertThat
(
logData
.
getLogs
().
get
(
1
).
getValue
(),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
Assert
.
assertNull
(
logData
.
getLogs
().
get
(
2
).
getValue
());
assertNotNull
(
logData
.
getLogs
().
get
(
3
).
getValue
());
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
private
void
assertRedisSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"Jedis/set"
));
assertThat
(
span
.
isExit
(),
is
(
true
));
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
7
));
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"Redis"
));
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
"set OperationKey"
));
assertThat
(
SpanHelper
.
getLayer
(
span
),
is
(
SpanLayer
.
DB
));
}
}
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -2,57 +2,68 @@ package org.skywalking.apm.plugin.mongodb.v3;
import
com.mongodb.MongoNamespace
;
import
com.mongodb.operation.FindOperation
;
import
java.util.List
;
import
org.bson.BsonDocument
;
import
org.bson.BsonString
;
import
org.bson.codecs.Decoder
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.A
fter
;
import
org.junit.A
ssert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.tag.Tags
;
import
org.skywalking.apm.agent.core.context.util.KeyValuePair
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
static
junit
.
framework
.
TestCase
.
assertNotNull
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
*
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertException
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
MongoDBMethodInterceptorTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
private
MongoDBMethodInterceptor
interceptor
;
private
MockTracingContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
private
EnhancedInstance
enhancedInstance
;
private
Object
[]
arguments
;
private
Class
[]
argumentTypes
;
@SuppressWarnings
(
{
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
interceptor
=
new
MongoDBMethodInterceptor
();
mockTracerContextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
Config
.
Plugin
.
MongoDB
.
TRACE_PARAM
=
true
;
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_HOST
)).
thenReturn
(
"127.0.0.1"
);
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_PORT
)).
thenReturn
(
27017
);
when
(
methodInvokeContext
.
methodName
()).
thenReturn
(
"find"
);
when
(
enhancedInstance
.
getSkyWalkingDynamicField
()).
thenReturn
(
"127.0.0.1:27017"
);
BsonDocument
document
=
new
BsonDocument
();
document
.
append
(
"name"
,
new
BsonString
(
"by"
));
...
...
@@ -61,64 +72,44 @@ public class MongoDBMethodInterceptorTest {
FindOperation
findOperation
=
new
FindOperation
(
mongoNamespace
,
decoder
);
findOperation
.
filter
(
document
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
findOperation
});
arguments
=
new
Object
[]
{
findOperation
};
argumentTypes
=
new
Class
[]
{
findOperation
.
getClass
()};
}
@Test
public
void
testIntercept
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
}
});
public
void
testIntercept
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"FindOperation"
,
arguments
,
argumentTypes
,
null
);
interceptor
.
afterMethod
(
enhancedInstance
,
"FindOperation"
,
arguments
,
argumentTypes
,
null
);
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertRedisSpan
(
spans
.
get
(
0
));
}
@Test
public
void
testInterceptWithException
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertLogData
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
}
});
}
private
void
assertLogData
(
LogData
logData
)
{
MatcherAssert
.
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
RuntimeException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
public
void
testInterceptWithException
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"FindOperation"
,
arguments
,
argumentTypes
,
null
);
interceptor
.
handleMethodException
(
enhancedInstance
,
"FindOperation"
,
arguments
,
argumentTypes
,
new
RuntimeException
());
interceptor
.
afterMethod
(
enhancedInstance
,
"FindOperation"
,
arguments
,
argumentTypes
,
null
);
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertRedisSpan
(
spans
.
get
(
0
));
List
<
LogDataEntity
>
logDataEntities
=
SpanHelper
.
getLogs
(
spans
.
get
(
0
));
assertThat
(
logDataEntities
.
size
(),
is
(
1
));
assertException
(
logDataEntities
.
get
(
0
),
RuntimeException
.
class
);
}
private
void
assertRedisSpan
(
Span
span
)
{
private
void
assertRedisSpan
(
AbstractTracing
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"MongoDB/FindOperation"
));
assertThat
(
span
.
getPeerHost
(),
is
(
"127.0.0.1"
));
assertThat
(
span
.
getPort
(),
is
(
27017
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"MongoDB"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_STATEMENT
),
is
(
"FindOperation { \"name\" : \"by\" }"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_TYPE
),
is
(
"MongoDB"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"db"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
assertThat
(
SpanHelper
.
getComponentId
(
span
),
is
(
9
));
List
<
KeyValuePair
>
tags
=
SpanHelper
.
getTags
(
span
);
assertThat
(
tags
.
get
(
1
).
getValue
(),
is
(
"FindOperation { \"name\" : \"by\" }"
));
assertThat
(
tags
.
get
(
0
).
getValue
(),
is
(
"MongoDB"
));
assertThat
(
span
.
isExit
(),
is
(
true
));
assertThat
(
SpanHelper
.
getLayer
(
span
),
is
(
SpanLayer
.
DB
));
}
}
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
已删除
100644 → 0
浏览文件 @
f64d4142
package
org.skywalking.apm.plugin.mongodb.v3
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.ConnectionSource
;
import
com.mongodb.binding.ReadBinding
;
import
com.mongodb.connection.ServerConnectionState
;
import
com.mongodb.connection.ServerDescription
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
PowerMockRunner
.
class
)
public
class
MongoDBReadBindingInterceptorTest
{
private
MongoDBReadBindingInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
ReadBinding
readBinding
;
@Mock
private
ConnectionSource
connectionSource
;
private
ServerAddress
address
=
new
ServerAddress
(
"127.0.0.1"
,
27017
);
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
MongoDBReadBindingInterceptor
();
ServerDescription
serverDescription
=
ServerDescription
.
builder
().
address
(
address
).
state
(
ServerConnectionState
.
CONNECTED
).
build
();
PowerMockito
.
when
(
connectionSource
.
getServerDescription
()).
thenReturn
(
serverDescription
);
PowerMockito
.
when
(
readBinding
.
getReadConnectionSource
()).
thenReturn
(
connectionSource
);
}
@Test
public
void
afterMethodTest
()
throws
Exception
{
interceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
readBinding
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
27017
);
}
}
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
已删除
100644 → 0
浏览文件 @
f64d4142
package
org.skywalking.apm.plugin.mongodb.v3
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.ConnectionSource
;
import
com.mongodb.binding.WriteBinding
;
import
com.mongodb.connection.ServerConnectionState
;
import
com.mongodb.connection.ServerDescription
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
@RunWith
(
PowerMockRunner
.
class
)
public
class
MongoDBWriteBindingInterceptorTest
{
private
MongoDBWriteBindingInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
WriteBinding
writeBinding
;
@Mock
private
ConnectionSource
connectionSource
;
private
ServerAddress
address
=
new
ServerAddress
(
"127.0.0.1"
,
27017
);
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
MongoDBWriteBindingInterceptor
();
ServerDescription
serverDescription
=
ServerDescription
.
builder
().
address
(
address
).
state
(
ServerConnectionState
.
CONNECTED
).
build
();
PowerMockito
.
when
(
connectionSource
.
getServerDescription
()).
thenReturn
(
serverDescription
);
PowerMockito
.
when
(
writeBinding
.
getWriteConnectionSource
()).
thenReturn
(
connectionSource
);
}
@Test
public
void
afterMethodTest
()
throws
Exception
{
interceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
writeBinding
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
27017
);
}
}
apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
已删除
100644 → 0
浏览文件 @
f64d4142
package
org.skywalking.apm.plugin.mongodb.v3
;
import
com.mongodb.MongoNamespace
;
import
com.mongodb.WriteConcern
;
import
com.mongodb.bulk.DeleteRequest
;
import
com.mongodb.operation.DeleteOperation
;
import
org.bson.BsonDocument
;
import
org.bson.BsonString
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
import
org.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.skywalking.apm.agent.core.context.tag.Tags
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
MongoDBWriteMethodInterceptorTest
{
private
MongoDBMethodInterceptor
interceptor
;
private
MockTracingContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
interceptor
=
new
MongoDBMethodInterceptor
();
mockTracerContextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
Config
.
Plugin
.
MongoDB
.
TRACE_PARAM
=
true
;
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_HOST
)).
thenReturn
(
"127.0.0.1"
);
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_PORT
)).
thenReturn
(
27017
);
when
(
methodInvokeContext
.
methodName
()).
thenReturn
(
"find"
);
BsonDocument
document
=
new
BsonDocument
();
document
.
append
(
"name"
,
new
BsonString
(
"by"
));
List
<
DeleteRequest
>
requestList
=
new
ArrayList
<
DeleteRequest
>();
DeleteRequest
deleteRequest
=
new
DeleteRequest
(
document
);
requestList
.
add
(
deleteRequest
);
MongoNamespace
mongoNamespace
=
new
MongoNamespace
(
"test.user"
);
WriteConcern
writeConcern
=
PowerMockito
.
mock
(
WriteConcern
.
class
);
DeleteOperation
deleteOperation
=
new
DeleteOperation
(
mongoNamespace
,
false
,
writeConcern
,
requestList
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
deleteOperation
});
}
@Test
public
void
testIntercept
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
}
});
}
private
void
assertRedisSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"MongoDB/DeleteOperation"
));
assertThat
(
span
.
getPeerHost
(),
is
(
"127.0.0.1"
));
assertThat
(
span
.
getPort
(),
is
(
27017
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"MongoDB"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_STATEMENT
),
is
(
"DeleteOperation { \"name\" : \"by\" },"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
DB_TYPE
),
is
(
"MongoDB"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"db"
));
}
@Test
public
void
testInterceptWithException
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertLogData
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
}
});
}
private
void
assertLogData
(
LogData
logData
)
{
MatcherAssert
.
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
RuntimeException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
apm-sniffer/apm-sdk-plugin/motan-plugin/src/test/java/org/skywalking/apm/plugin/motan/MotanConsumerInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -3,134 +3,122 @@ package org.skywalking.apm.plugin.motan;
import
com.weibo.api.motan.rpc.Request
;
import
com.weibo.api.motan.rpc.Response
;
import
com.weibo.api.motan.rpc.URL
;
import
org.hamcrest.CoreMatchers
;
import
java.util.List
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
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.tag.Tags
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
.
junit
.
Assert
.
*
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.*;
@RunWith
(
MockitoJUnitRunner
.
class
)
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
)
public
class
MotanConsumerInterceptorTest
{
private
MockTracingContextListener
contextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
private
MotanConsumerInterceptor
invokeInterceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
Response
response
;
@Mock
private
Request
request
;
private
URL
url
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
{
ServiceManager
.
INSTANCE
.
boot
();
contextListener
=
new
MockTracingContextListener
();
invokeInterceptor
=
new
MotanConsumerInterceptor
();
url
=
URL
.
valueOf
(
"motan://127.0.0.1:34000/org.skywalking.apm.test.TestService"
);
TracerContext
.
ListenerManager
.
add
(
contextListener
);
when
(
instanceContext
.
get
(
"REQUEST_URL"
)).
thenReturn
(
url
);
when
(
interceptorContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
request
});
when
(
enhancedInstance
.
getSkyWalkingDynamicField
()).
thenReturn
(
url
);
when
(
request
.
getMethodName
()).
thenReturn
(
"test"
);
when
(
request
.
getInterfaceName
()).
thenReturn
(
"org.skywalking.apm.test.TestService"
);
when
(
request
.
getParamtersDesc
()).
thenReturn
(
"java.lang.String, java.lang.Object"
);
}
@Test
public
void
testInvokeInterceptor
()
{
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertMotanConsumerSpan
(
span
);
verify
(
request
,
times
(
1
)).
setAttachment
(
anyString
(),
anyString
());
}
});
public
void
testInvokeInterceptor
()
throws
Throwable
{
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()},
null
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()},
response
);
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertMotanConsumerSpan
(
spans
.
get
(
0
));
verify
(
request
,
times
(
1
)).
setAttachment
(
anyString
(),
anyString
());
}
@Test
public
void
testResponseWithException
()
{
public
void
testResponseWithException
()
throws
Throwable
{
when
(
response
.
getException
()).
thenReturn
(
new
RuntimeException
());
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()}
,
null
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()}
,
response
);
contextListener
.
assertSize
(
1
);
assertTraceSegmentWhenOccurException
();
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertTraceSegmentWhenOccurException
(
spans
.
get
(
0
));
}
private
void
assertTraceSegmentWhenOccurException
()
{
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertMotanConsumerSpan
(
span
);
verify
(
request
,
times
(
1
)).
setAttachment
(
anyString
(),
anyString
());
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
LogData
logData
=
SpanLogReader
.
getLogs
(
span
).
get
(
0
);
assertLogData
(
logData
);
}
});
private
void
assertTraceSegmentWhenOccurException
(
AbstractTracingSpan
tracingSpan
)
{
assertMotanConsumerSpan
(
tracingSpan
);
verify
(
request
,
times
(
1
)).
setAttachment
(
anyString
(),
anyString
());
List
<
LogDataEntity
>
logDataEntities
=
SpanHelper
.
getLogs
(
tracingSpan
);
assertThat
(
logDataEntities
.
size
(),
is
(
1
));
assertException
(
logDataEntities
.
get
(
0
),
RuntimeException
.
class
);
}
@Test
public
void
testInvokeInterceptorWithException
()
{
public
void
testInvokeInterceptorWithException
()
throws
Throwable
{
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
handleMethodException
(
new
RuntimeException
(),
instanceContext
,
interceptorContext
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()}
,
null
);
invokeInterceptor
.
handleMethodException
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()},
new
RuntimeException
()
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
new
Object
[]
{
request
},
new
Class
[]
{
request
.
getClass
()}
,
response
);
contextListener
.
assertSize
(
1
);
assertTraceSegmentWhenOccurException
();
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertTraceSegmentWhenOccurException
(
spans
.
get
(
0
));
}
private
void
assertLogData
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
getFields
().
get
(
"message"
));
}
private
void
assertMotanConsumerSpan
(
Span
span
)
{
private
void
assertMotanConsumerSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"org.skywalking.apm.test.TestService.test(java.lang.String, java.lang.Object)"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Motan"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_CLIENT
));
assertThat
(
span
.
getPeerHost
(),
is
(
"127.0.0.1"
));
assertThat
(
span
.
getPort
(),
is
(
34000
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"rpc"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"motan://127.0.0.1:34000/default_rpc/org.skywalking.apm.test.TestService/1.0/service"
));
assertComponent
(
span
,
ComponentsDefine
.
MOTAN
);
assertLayer
(
span
,
SpanLayer
.
RPC_FRAMEWORK
);
assertTag
(
span
,
0
,
"motan://127.0.0.1:34000/default_rpc/org.skywalking.apm.test.TestService/1.0/service"
);
}
@After
public
void
tearDown
()
{
TracerContext
.
ListenerManager
.
remove
(
contextListener
);
}
}
apm-sniffer/apm-sdk-plugin/motan-plugin/src/test/java/org/skywalking/apm/plugin/motan/MotanProviderInterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -3,162 +3,148 @@ package org.skywalking.apm.plugin.motan;
import
com.weibo.api.motan.rpc.Request
;
import
com.weibo.api.motan.rpc.Response
;
import
com.weibo.api.motan.rpc.URL
;
import
org.hamcrest.CoreMatchers
;
import
java.util.HashMap
;
import
java.util.List
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.runners.MockitoJUnit
Runner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.
powermock.modules.junit4.PowerMock
Runner
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.context.tag.Tags
;
import
java.util.HashMap
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
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
.
junit
.
Assert
.*;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
.
assertLogSize
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
MotanProviderInterceptorTest
{
private
MockTracingContextListener
contextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
private
MotanProviderInterceptor
invokeInterceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
ConstructorInvokeContext
constructorInvokeContext
;
@Mock
private
Response
response
;
@Mock
private
Request
request
;
private
URL
url
;
@Mock
private
EnhancedInstance
enhancedInstance
;
private
Object
[]
arguments
;
private
Class
[]
argumentType
;
@Before
public
void
setUp
()
{
ServiceManager
.
INSTANCE
.
boot
();
invokeInterceptor
=
new
MotanProviderInterceptor
();
contextListener
=
new
MockTracingContextListener
();
url
=
URL
.
valueOf
(
"motan://127.0.0.1:34000/org.skywalking.apm.test.TestService"
);
TracerContext
.
ListenerManager
.
add
(
contextListener
);
when
(
instanceContext
.
get
(
"REQUEST_URL"
)).
thenReturn
(
url
);
when
(
interceptorContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
request
});
when
(
enhancedInstance
.
getSkyWalkingDynamicField
()).
thenReturn
(
url
);
arguments
=
new
Object
[]
{
request
};
argumentType
=
new
Class
[]
{
request
.
getClass
()};
when
(
request
.
getMethodName
()).
thenReturn
(
"test"
);
when
(
request
.
getInterfaceName
()).
thenReturn
(
"org.skywalking.apm.test.TestService"
);
when
(
request
.
getParamtersDesc
()).
thenReturn
(
"java.lang.String, java.lang.Object"
);
when
(
constructorInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
url
});
}
@Test
public
void
testInvokerWithoutRefSegment
()
{
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertMotanProviderSpan
(
span
);
assertTrue
(
traceSegment
.
getRefs
()
==
null
);
}
});
public
void
testInvokerWithoutRefSegment
()
throws
Throwable
{
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
null
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
response
);
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertMotanProviderSpan
(
spans
.
get
(
0
));
assertTrue
(
traceSegment
.
getRefs
()
==
null
);
}
@Test
public
void
testInvokerWithRefSegment
()
{
public
void
testInvokerWithRefSegment
()
throws
Throwable
{
HashMap
attachments
=
new
HashMap
();
attachments
.
put
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
,
"
302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123
"
);
attachments
.
put
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
,
"
S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2
"
);
when
(
request
.
getAttachments
()).
thenReturn
(
attachments
);
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertMotanProviderSpan
(
span
);
assertRefSegment
(
traceSegment
.
getRefs
().
get
(
0
));
}
});
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
null
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
response
);
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertMotanProviderSpan
(
spans
.
get
(
0
));
assertRefSegment
(
traceSegment
.
getRefs
().
get
(
0
));
}
@Test
public
void
testResponseWithException
()
{
public
void
testResponseWithException
()
throws
Throwable
{
when
(
response
.
getException
()).
thenReturn
(
new
RuntimeException
());
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
null
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
response
);
assertTraceSegmentWhenOccurException
();
}
@Test
public
void
testOccurException
()
{
public
void
testOccurException
()
throws
Throwable
{
invokeInterceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
,
null
);
invokeInterceptor
.
handleMethodException
(
new
RuntimeException
(),
instanceContext
,
interceptorContext
);
invokeInterceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
response
);
invokeInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
null
);
invokeInterceptor
.
handleMethodException
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
new
RuntimeException
()
);
invokeInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
arguments
,
argumentType
,
response
);
assertTraceSegmentWhenOccurException
();
}
private
void
assertTraceSegmentWhenOccurException
()
{
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertMotanProviderSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
LogData
logData
=
SpanLogReader
.
getLogs
(
span
).
get
(
0
);
assertLogData
(
logData
);
}
});
}
private
void
assertLogData
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
getFields
().
get
(
"message"
));
MatcherAssert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertMotanProviderSpan
(
spans
.
get
(
0
));
assertLogSize
(
spans
.
get
(
0
),
1
);
List
<
LogDataEntity
>
logDataEntities
=
SpanHelper
.
getLogs
(
spans
.
get
(
0
));
assertException
(
logDataEntities
.
get
(
0
),
RuntimeException
.
class
);
}
private
void
assertRefSegment
(
TraceSegmentRef
primaryRef
)
{
assertThat
(
primaryRef
.
getTraceSegmentId
(),
is
(
"302017.1487666919810.624424584.17332.1
.1"
));
assertThat
(
primaryRef
.
getSpanId
(),
is
(
1
));
assertThat
(
primaryRef
.
getPeerHost
(),
is
(
"127.0.0.1
"
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
primaryRef
),
is
(
"S.1499176688384.581928182.80935.69
.1"
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
primaryRef
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getPeerHost
(
primaryRef
),
is
(
"192.168.1.8:18002
"
));
}
private
void
assertMotanProviderSpan
(
Span
span
)
{
private
void
assertMotanProviderSpan
(
AbstractTracing
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"org.skywalking.apm.test.TestService.test(java.lang.String, java.lang.Object)"
));
assert
That
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Motan"
)
);
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_SERVER
));
assert
That
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"rpc"
)
);
assert
Component
(
span
,
ComponentsDefine
.
MOTAN
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
assert
Layer
(
span
,
SpanLayer
.
RPC_FRAMEWORK
);
}
@After
public
void
tearDown
()
{
TracerContext
.
ListenerManager
.
remove
(
contextListener
);
}
}
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java
浏览文件 @
4c1963cb
...
...
@@ -52,10 +52,10 @@ public class RealCallInterceptor implements InstanceMethodsAroundInterceptor, In
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
HttpUrl
requestUrl
=
request
.
url
();
AbstractSpan
span
=
ContextManager
.
createExitSpan
(
requestUrl
.
uri
().
toString
(),
contextCarrier
,
requestUrl
.
host
()
+
":"
+
requestUrl
.
port
());
AbstractSpan
span
=
ContextManager
.
createExitSpan
(
requestUrl
.
uri
().
getPath
(),
contextCarrier
,
requestUrl
.
host
()
+
":"
+
requestUrl
.
port
());
span
.
setComponent
(
ComponentsDefine
.
OKHTTP
);
Tags
.
HTTP
.
METHOD
.
set
(
span
,
request
.
method
());
Tags
.
URL
.
set
(
span
,
requestUrl
.
ur
l
().
getPath
());
Tags
.
URL
.
set
(
span
,
requestUrl
.
ur
i
().
toString
());
SpanLayer
.
asHttp
(
span
);
Field
headersField
=
Request
.
class
.
getDeclaredField
(
"headers"
);
...
...
@@ -96,6 +96,8 @@ public class RealCallInterceptor implements InstanceMethodsAroundInterceptor, In
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
String
methodName
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
AbstractSpan
abstractSpan
=
ContextManager
.
activeSpan
();
abstractSpan
.
errorOccurred
();
abstractSpan
.
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.okhttp.v3
;
import
java.util.List
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.BooleanTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
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.tag.Tags
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.skywalking.apm.agent.test.helper.SegmentHelper
;
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
.
mock
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
.
assertLogSize
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertOccurException
;
import
static
org
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertTag
;
/**
* @author pengys5
*/
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
@PrepareForTest
({
Response
.
class
})
public
class
RealCallInterceptorTest
{
private
RealCallInterceptor
realCallInterceptor
;
private
MockTracingContextListener
mockTracerContextListener
;
private
EnhancedClassInstanceContext
classInstanceContext
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@
Mock
p
rivate
ConstructorInvokeContext
constructorInvokeContext
;
@
Rule
p
ublic
AgentServiceRule
serviceRule
=
new
AgentServiceRule
()
;
@Mock
private
InstanceMethodInvokeContext
instanceMethodInvokeContext
;
private
RealCallInterceptor
realCallInterceptor
;
@Mock
private
OkHttpClient
client
;
private
Request
request
;
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracingContextListener
();
private
Object
[]
allArguments
;
private
Class
[]
argumentTypes
;
classInstanceContext
=
new
EnhancedClassInstanceContext
();
private
EnhancedInstance
enhancedInstance
=
new
EnhancedInstance
()
{
request
=
new
Request
.
Builder
().
url
(
"http://skywalking.org"
).
build
();
Object
[]
allArguments
=
{
client
,
request
,
false
};
when
(
constructorInvokeContext
.
allArguments
()).
thenReturn
(
allArguments
);
private
Object
object
;
ServiceManager
.
INSTANCE
.
boot
();
realCallInterceptor
=
new
RealCallInterceptor
();
@Override
public
Object
getSkyWalkingDynamicField
()
{
return
object
;
}
@Override
public
void
setSkyWalkingDynamicField
(
Object
value
)
{
this
.
object
=
value
;
}
};
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
@Before
public
void
setUp
()
throws
Exception
{
request
=
new
Request
.
Builder
().
url
(
"http://skywalking.org"
).
build
();
allArguments
=
new
Object
[]
{
client
,
request
,
false
};
argumentTypes
=
new
Class
[]
{
client
.
getClass
(),
request
.
getClass
(),
Boolean
.
class
};
realCallInterceptor
=
new
RealCallInterceptor
();
}
@Test
public
void
testOnConstruct
()
{
realCallInterceptor
.
onConstruct
(
classInstanceContext
,
constructorInvokeContext
);
Assert
.
assertEquals
(
request
,
classInstanceContext
.
get
(
"SWRequestContextKey"
));
realCallInterceptor
.
onConstruct
(
enhancedInstance
,
allArguments
);
assertThat
(
enhancedInstance
.
getSkyWalkingDynamicField
(),
is
(
allArguments
[
1
]
));
}
@Test
public
void
testMethodsAround
()
throws
Throwable
{
realCallInterceptor
.
onConstruct
(
classInstanceContext
,
constructorInvokeContext
);
realCallInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
realCallInterceptor
.
onConstruct
(
enhancedInstance
,
allArguments
);
realCallInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
null
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
code
()).
thenReturn
(
200
);
realCallInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
false
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
}
});
realCallInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertSpan
(
spans
.
get
(
0
));
assertOccurException
(
spans
.
get
(
0
),
false
);
}
@Test
public
void
testMethodsAroundError
()
throws
Throwable
{
realCallInterceptor
.
onConstruct
(
classInstanceContext
,
constructorInvokeContext
);
realCallInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
realCallInterceptor
.
onConstruct
(
enhancedInstance
,
allArguments
);
realCallInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
null
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
code
()).
thenReturn
(
404
);
realCallInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
true
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
}
});
realCallInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertSpan
(
spans
.
get
(
0
));
assertOccurException
(
spans
.
get
(
0
),
true
);
}
private
void
assertSpan
(
Span
span
)
{
Assert
.
assertEquals
(
"http"
,
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
));
Assert
.
assertEquals
(
"GET"
,
StringTagReader
.
get
(
span
,
Tags
.
HTTP
.
METHOD
));
Assert
.
assertEquals
(
"skywalking.org"
,
span
.
getPeerHost
());
Assert
.
assertEquals
(
80
,
span
.
getPort
());
Assert
.
assertEquals
(
"OKHttp"
,
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
));
Assert
.
assertEquals
(
"discovery"
,
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
));
Assert
.
assertEquals
(
"/"
,
StringTagReader
.
get
(
span
,
Tags
.
URL
));
private
void
assertSpan
(
AbstractTracingSpan
span
)
{
assertComponent
(
span
,
ComponentsDefine
.
OKHTTP
);
assertLayer
(
span
,
SpanLayer
.
HTTP
);
assertTag
(
span
,
0
,
"GET"
);
assertTag
(
span
,
1
,
"http://skywalking.org/"
);
assertThat
(
span
.
isExit
(),
is
(
true
));
assertThat
(
span
.
getOperationName
(),
is
(
"/"
));
}
@Test
public
void
testException
()
throws
Throwable
{
realCallInterceptor
.
onConstruct
(
classInstanceContext
,
constructorInvokeContext
);
realCallInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
realCallInterceptor
.
onConstruct
(
enhancedInstance
,
allArguments
);
realCallInterceptor
.
beforeMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
null
);
realCallInterceptor
.
handleMethodException
(
new
NullPointerException
(
"testException"
),
classInstanceContext
,
null
);
realCallInterceptor
.
handleMethodException
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
new
NullPointerException
(
"testException"
)
);
Response
response
=
mock
(
Response
.
class
);
when
(
response
.
code
()).
thenReturn
(
200
);
realCallInterceptor
.
afterMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
response
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
finishedSegment
)
{
Assert
.
assertEquals
(
1
,
finishedSegment
.
getSpans
().
size
());
assertSpan
(
finishedSegment
.
getSpans
().
get
(
0
));
Assert
.
assertEquals
(
true
,
BooleanTagReader
.
get
(
finishedSegment
.
getSpans
().
get
(
0
),
Tags
.
ERROR
));
Assert
.
assertEquals
(
1
,
SpanLogReader
.
getLogs
(
finishedSegment
.
getSpans
().
get
(
0
)).
size
());
Assert
.
assertEquals
(
true
,
SpanLogReader
.
getLogs
(
finishedSegment
.
getSpans
().
get
(
0
)).
get
(
0
).
getFields
().
containsKey
(
"stack"
));
}
});
realCallInterceptor
.
afterMethod
(
enhancedInstance
,
"execute"
,
allArguments
,
argumentTypes
,
response
);
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertSpan
(
spans
.
get
(
0
));
assertOccurException
(
spans
.
get
(
0
),
true
);
assertLogSize
(
spans
.
get
(
0
),
1
);
assertException
(
SpanHelper
.
getLogs
(
spans
.
get
(
0
)).
get
(
0
),
NullPointerException
.
class
,
"testException"
);
}
}
apm-sniffer/apm-sdk-plugin/pom.xml
浏览文件 @
4c1963cb
...
...
@@ -38,6 +38,12 @@
<artifactId>
apm-agent-core
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.skywalking
</groupId>
<artifactId>
apm-test-tools
</artifactId>
<version>
${project.version}
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
...
...
apm-sniffer/apm-sdk-plugin/resin-3.x-plugin/src/test/java/org/skywalking/apm/plugin/resin/v3/ResinV3InterceptorTest.java
浏览文件 @
4c1963cb
...
...
@@ -2,148 +2,134 @@ package org.skywalking.apm.plugin.resin.v3;
import
com.caucho.server.connection.CauchoRequest
;
import
com.caucho.server.http.HttpResponse
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.runners.MockitoJUnit
Runner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.
powermock.modules.junit4.PowerMock
Runner
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.IntTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.context.tag.Tags
;
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
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
;
/**
* ResinInterceptorTest
*
* @author baiyang
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
ResinV3InterceptorTest
{
private
ResinV3Interceptor
interceptor
;
private
MockTracingContextListener
contextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
CauchoRequest
request
;
@Mock
private
HttpResponse
response
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Before
p
ublic
void
setUp
()
throws
Exception
{
private
Object
[]
arguments
;
p
rivate
Class
[]
argumentType
;
ServiceManager
.
INSTANCE
.
boot
();
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
ResinV3Interceptor
();
contextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
contextListener
);
when
(
request
.
getPageURI
()).
thenReturn
(
"/test/testRequestURL"
);
when
(
request
.
getScheme
()).
thenReturn
(
"http"
);
when
(
request
.
getServerName
()).
thenReturn
(
"localhost"
);
when
(
request
.
getServerPort
()).
thenReturn
(
8080
);
when
(
request
.
getRequestURL
()).
thenReturn
(
new
StringBuffer
(
"http://localhost:8080/test/testRequestURL"
));
when
(
response
.
getStatusCode
()).
thenReturn
(
200
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
request
,
response
});
arguments
=
new
Object
[]
{
request
,
response
};
argumentType
=
new
Class
[]
{
request
.
getClass
(),
response
.
getClass
()};
}
@Test
public
void
testWithoutSerializedContextData
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
}
});
}
public
void
testWithoutSerializedContextData
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
null
);
@Test
public
void
testWithSerializedContextData
()
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"
);
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
});
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
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertSpanLog
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
}
});
public
void
testWithSerializedContextData
()
throws
Throwable
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2"
);
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
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
));
}
private
void
assertSpanLog
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logData
.
getFields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
getFields
().
get
(
"message"
));
@Test
public
void
testWithOccurException
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
handleMethodException
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
new
RuntimeException
());
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
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
(
ref
.
getSpanId
(),
is
(
1
));
assertThat
(
ref
.
getTraceSegmentId
(),
is
(
"302017.1487666919810.624424584.17332.1
.1"
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
),
is
(
"S.1499176688384.581928182.80935.69
.1"
));
}
private
void
assertHttpSpan
(
Span
span
)
{
private
void
assertHttpSpan
(
AbstractTracing
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test/testRequestURL"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Resin"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"http://localhost:8080/test/testRequestURL"
));
assertThat
(
IntTagReader
.
get
(
span
,
Tags
.
STATUS_CODE
),
is
(
200
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_SERVER
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"http"
));
assertComponent
(
span
,
ComponentsDefine
.
RESIN
);
assertTag
(
span
,
0
,
"http://localhost:8080/test/testRequestURL"
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
new
MockTracingContextListener
());
}
}
apm-sniffer/apm-sdk-plugin/resin-4.x-plugin/src/test/java/org/skywalking/apm/plugin/resin/v4/ResinV4InterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.resin.v4
;
import
com.caucho.server.http.CauchoRequest
;
import
java.util.List
;
import
javax.servlet.http.HttpServletResponse
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.runners.MockitoJUnit
Runner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.
powermock.modules.junit4.PowerMock
Runner
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.IntTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.context.tag.Tags
;
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
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
;
/**
* Created by Baiyang on 2017/5/6.
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
ResinV4InterceptorTest
{
private
ResinV4Interceptor
interceptor
;
private
MockTracingContextListener
contextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
CauchoRequest
request
;
@Mock
private
HttpServletResponse
response
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
private
Object
[]
arguments
;
private
Class
[]
argumentType
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
interceptor
=
new
ResinV4Interceptor
();
contextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
contextListener
);
when
(
request
.
getPageURI
()).
thenReturn
(
"/test/testRequestURL"
);
when
(
request
.
getScheme
()).
thenReturn
(
"http"
);
...
...
@@ -63,86 +74,63 @@ public class ResinV4InterceptorTest {
when
(
request
.
getRequestURI
()).
thenReturn
(
"/test/testRequestURL"
);
when
(
request
.
getRequestURL
()).
thenReturn
(
new
StringBuffer
(
"http://localhost:8080/test/testRequestURL"
));
when
(
response
.
getStatus
()).
thenReturn
(
200
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
request
,
response
});
arguments
=
new
Object
[]
{
request
,
response
};
argumentType
=
new
Class
[]
{
request
.
getClass
(),
response
.
getClass
()};
}
@Test
public
void
testWithoutSerializedContextData
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
}
});
}
public
void
testWithoutSerializedContextData
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
null
);
@Test
public
void
testWithSerializedContextData
()
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"
);
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
});
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
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertSpanLog
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
}
});
public
void
testWithSerializedContextData
()
throws
Throwable
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2"
);
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
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
));
}
private
void
assertSpanLog
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logData
.
getFields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
getFields
().
get
(
"message"
));
@Test
public
void
testWithOccurException
()
throws
Throwable
{
interceptor
.
beforeMethod
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
methodInterceptResult
);
interceptor
.
handleMethodException
(
enhancedInstance
,
"service"
,
arguments
,
argumentType
,
new
RuntimeException
());
interceptor
.
afterMethod
(
enhancedInstance
,
"service"
,
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
(
ref
.
getSpanId
(),
is
(
1
));
assertThat
(
ref
.
getTraceSegmentId
(),
is
(
"302017.1487666919810.624424584.17332.1
.1"
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
),
is
(
"S.1499176688384.581928182.80935.69
.1"
));
}
private
void
assertHttpSpan
(
Span
span
)
{
private
void
assertHttpSpan
(
AbstractTracing
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test/testRequestURL"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Resin"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"http://localhost:8080/test/testRequestURL"
));
assertThat
(
IntTagReader
.
get
(
span
,
Tags
.
STATUS_CODE
),
is
(
200
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_SERVER
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"http"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
new
MockTracingContextListener
());
assertComponent
(
span
,
ComponentsDefine
.
RESIN
);
assertTag
(
span
,
0
,
"http://localhost:8080/test/testRequestURL"
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
}
apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/org/skywalking/apm/plugin/tomcat78x/TomcatInterceptorTest.java
浏览文件 @
4c1963cb
package
org.skywalking.apm.plugin.tomcat78x
;
import
java.util.List
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.
mockito.runners.MockitoJUnit
Runner
;
import
org.
skywalking.apm.agent.core.boot.ServiceManager
;
import
org.
powermock.modules.junit4.PowerMock
Runner
;
import
org.
powermock.modules.junit4.PowerMockRunnerDelegate
;
import
org.skywalking.apm.agent.core.conf.Config
;
import
org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.skywalking.apm.sniffer.mock.context.MockTracingContextListener
;
import
org.skywalking.apm.sniffer.mock.context.SegmentAssert
;
import
org.skywalking.apm.sniffer.mock.trace.SpanLogReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.IntTagReader
;
import
org.skywalking.apm.sniffer.mock.trace.tags.StringTagReader
;
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.context.tag.Tags
;
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
.
junit
.
Assert
.
assertNull
;
import
static
org
.
mockito
.
Mockito
.
when
;
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
(
MockitoJUnitRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
TomcatInterceptorTest
{
private
TomcatInterceptor
tomcatInterceptor
;
private
MockTracingContextListener
contextListener
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
HttpServletRequest
request
;
@Mock
private
HttpServletResponse
response
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@
Before
p
ublic
void
setUp
()
throws
Exception
{
@
Mock
p
rivate
EnhancedInstance
enhancedInstance
;
ServiceManager
.
INSTANCE
.
boot
();
private
Object
[]
arguments
;
private
Class
[]
argumentType
;
@Before
public
void
setUp
()
throws
Exception
{
tomcatInterceptor
=
new
TomcatInterceptor
();
contextListener
=
new
MockTracingContextListener
();
TracerContext
.
ListenerManager
.
add
(
contextListener
);
when
(
request
.
getRequestURI
()).
thenReturn
(
"/test/testRequestURL"
);
when
(
request
.
getRequestURL
()).
thenReturn
(
new
StringBuffer
(
"http://localhost:8080/test/testRequestURL"
));
when
(
response
.
getStatus
()).
thenReturn
(
200
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
request
,
response
});
arguments
=
new
Object
[]
{
request
,
response
};
argumentType
=
new
Class
[]
{
request
.
getClass
(),
response
.
getClass
()};
}
@Test
public
void
testWithoutSerializedContextData
()
{
tomcatInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
tomcatInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
}
});
public
void
testWithoutSerializedContextData
()
throws
Throwable
{
tomcatInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
arguments
,
argumentType
,
methodInterceptResult
);
tomcatInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
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
()
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"
);
tomcatInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
tomcatInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
});
}
public
void
testWithSerializedContextData
()
throws
Throwable
{
when
(
request
.
getHeader
(
Config
.
Plugin
.
Propagation
.
HEADER_NAME
)).
thenReturn
(
"S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2"
);
@Test
public
void
testWithOccurException
()
{
tomcatInterceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
methodInterceptResult
);
tomcatInterceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
tomcatInterceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
contextListener
.
assertSize
(
1
);
contextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
SpanLogReader
.
getLogs
(
span
).
size
(),
is
(
1
));
assertSpanLog
(
SpanLogReader
.
getLogs
(
span
).
get
(
0
));
}
});
tomcatInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
arguments
,
argumentType
,
methodInterceptResult
);
tomcatInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
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
));
}
private
void
assertSpanLog
(
LogData
logData
)
{
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertThat
(
logData
.
getFields
().
get
(
"error.kind"
),
CoreMatchers
.<
Object
>
is
(
RuntimeException
.
class
.
getName
()));
assertNull
(
logData
.
getFields
().
get
(
"message"
));
@Test
public
void
testWithOccurException
()
throws
Throwable
{
tomcatInterceptor
.
beforeMethod
(
enhancedInstance
,
"invoke"
,
arguments
,
argumentType
,
methodInterceptResult
);
tomcatInterceptor
.
handleMethodException
(
enhancedInstance
,
"invoke"
,
arguments
,
argumentType
,
new
RuntimeException
());
tomcatInterceptor
.
afterMethod
(
enhancedInstance
,
"invoke"
,
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
(
ref
.
getSpanId
(),
is
(
1
));
assertThat
(
ref
.
getTraceSegmentId
(),
is
(
"302017.1487666919810.624424584.17332.1
.1"
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
),
is
(
"S.1499176688384.581928182.80935.69
.1"
));
}
private
void
assertHttpSpan
(
Span
span
)
{
private
void
assertHttpSpan
(
AbstractTracing
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test/testRequestURL"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
COMPONENT
),
is
(
"Tomcat"
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
URL
),
is
(
"http://localhost:8080/test/testRequestURL"
));
assertThat
(
IntTagReader
.
get
(
span
,
Tags
.
STATUS_CODE
),
is
(
200
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_KIND
),
is
(
Tags
.
SPAN_KIND_SERVER
));
assertThat
(
StringTagReader
.
get
(
span
,
Tags
.
SPAN_LAYER
.
SPAN_LAYER_TAG
),
is
(
"http"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
new
MockTracingContextListener
());
assertComponent
(
span
,
ComponentsDefine
.
TOMCAT
);
assertTag
(
span
,
0
,
"http://localhost:8080/test/testRequestURL"
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/AbstractTracingSpanHelper.java
已删除
100644 → 0
浏览文件 @
f64d4142
package
org.skywalking.apm.agent.test.helper
;
import
java.util.Collections
;
import
java.util.List
;
import
org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
import
org.skywalking.apm.agent.core.context.trace.LogDataEntity
;
public
class
AbstractTracingSpanHelper
{
public
static
int
getParentSpanId
(
AbstractTracingSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"parentSpanId"
);
}
catch
(
Exception
e
)
{
}
return
-
9999
;
}
public
static
List
<
LogDataEntity
>
getLogs
(
AbstractTracingSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"logs"
);
}
catch
(
Exception
e
)
{
}
return
Collections
.
emptyList
();
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/FieldSetter.java
0 → 100644
浏览文件 @
4c1963cb
package
org.skywalking.apm.agent.test.helper
;
import
java.lang.reflect.Field
;
/**
* Created by xin on 2017/7/9.
*/
public
class
FieldSetter
{
public
static
<
T
>
void
setValue
(
Object
instance
,
String
fieldName
,
T
value
)
throws
IllegalAccessException
,
NoSuchFieldException
{
Field
field
=
instance
.
getClass
().
getDeclaredField
(
fieldName
);
field
.
setAccessible
(
true
);
field
.
set
(
instance
,
value
);
}
public
static
<
T
>
void
setStaticValue
(
Class
instance
,
String
fieldName
,
T
value
)
throws
IllegalAccessException
,
NoSuchFieldException
{
Field
field
=
instance
.
getDeclaredField
(
fieldName
);
field
.
setAccessible
(
true
);
field
.
set
(
instance
,
value
);
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SegmentHelper.java
浏览文件 @
4c1963cb
...
...
@@ -6,7 +6,7 @@ import org.skywalking.apm.agent.core.context.trace.TraceSegment;
public
class
SegmentHelper
{
public
static
List
<
AbstractTracingSpan
>
getSpan
(
TraceSegment
traceSegment
)
{
public
static
List
<
AbstractTracingSpan
>
getSpan
s
(
TraceSegment
traceSegment
)
{
try
{
return
FieldGetter
.
getValue
(
traceSegment
,
"spans"
);
}
catch
(
Exception
e
)
{
...
...
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/
Trace
SegmentRefHelper.java
→
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SegmentRefHelper.java
浏览文件 @
4c1963cb
...
...
@@ -2,7 +2,7 @@ package org.skywalking.apm.agent.test.helper;
import
org.skywalking.apm.agent.core.context.trace.TraceSegmentRef
;
public
class
Trace
SegmentRefHelper
{
public
class
SegmentRefHelper
{
public
static
String
getPeerHost
(
TraceSegmentRef
ref
)
{
try
{
return
FieldGetter
.
getValue
(
ref
,
"peerHost"
);
...
...
@@ -11,4 +11,22 @@ public class TraceSegmentRefHelper {
return
null
;
}
public
static
String
getTraceSegmentId
(
TraceSegmentRef
ref
)
{
try
{
return
FieldGetter
.
getValue
(
ref
,
"traceSegmentId"
);
}
catch
(
Exception
e
)
{
}
return
null
;
}
public
static
int
getSpanId
(
TraceSegmentRef
ref
)
{
try
{
return
FieldGetter
.
getValue
(
ref
,
"spanId"
);
}
catch
(
Exception
e
)
{
}
return
-
1
;
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/helper/SpanHelper.java
0 → 100644
浏览文件 @
4c1963cb
package
org.skywalking.apm.agent.test.helper
;
import
java.util.Collections
;
import
java.util.List
;
import
org.skywalking.apm.agent.core.context.trace.AbstractSpan
;
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.util.KeyValuePair
;
public
class
SpanHelper
{
public
static
int
getParentSpanId
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"parentSpanId"
);
}
catch
(
Exception
e
)
{
}
return
-
9999
;
}
public
static
List
<
LogDataEntity
>
getLogs
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"logs"
);
}
catch
(
Exception
e
)
{
}
return
Collections
.
emptyList
();
}
public
static
List
<
KeyValuePair
>
getTags
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"tags"
);
}
catch
(
Exception
e
)
{
}
return
Collections
.
emptyList
();
}
public
static
SpanLayer
getLayer
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"layer"
);
}
catch
(
Exception
e
)
{
}
return
null
;
}
public
static
String
getComponentName
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"componentName"
);
}
catch
(
Exception
e
)
{
}
return
null
;
}
public
static
int
getComponentId
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"componentId"
);
}
catch
(
Exception
e
)
{
}
return
-
1
;
}
public
static
boolean
getErrorOccurred
(
AbstractSpan
tracingSpan
)
{
try
{
return
FieldGetter
.
getParentFieldValue
(
tracingSpan
,
"errorOccurred"
);
}
catch
(
Exception
e
)
{
}
return
false
;
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/AgentServiceRule.java
0 → 100644
浏览文件 @
4c1963cb
package
org.skywalking.apm.agent.test.tools
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
org.junit.rules.ExternalResource
;
import
org.skywalking.apm.agent.core.boot.BootService
;
import
org.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig
;
import
org.skywalking.apm.agent.core.context.IgnoredTracerContext
;
import
org.skywalking.apm.agent.core.context.TracingContext
;
import
org.skywalking.apm.agent.core.context.TracingContextListener
;
import
org.skywalking.apm.agent.test.helper.FieldSetter
;
public
class
AgentServiceRule
extends
ExternalResource
{
@Override
protected
void
after
()
{
super
.
after
();
try
{
FieldSetter
.
setValue
(
ServiceManager
.
INSTANCE
.
getClass
(),
"bootedServices"
,
new
HashMap
<
Class
,
BootService
>());
FieldSetter
.
setValue
(
IgnoredTracerContext
.
ListenerManager
.
class
,
"LISTENERS"
,
new
LinkedList
<
TracingContextListener
>());
FieldSetter
.
setValue
(
TracingContext
.
ListenerManager
.
class
,
"LISTENERS"
,
new
LinkedList
<
TracingContextListener
>());
}
catch
(
Exception
e
)
{
}
}
@Override
protected
void
before
()
throws
Throwable
{
super
.
before
();
ServiceManager
.
INSTANCE
.
boot
();
RemoteDownstreamConfig
.
Agent
.
APPLICATION_ID
=
1
;
RemoteDownstreamConfig
.
Agent
.
APPLICATION_INSTANCE_ID
=
1
;
}
}
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/SegmentStorage.java
浏览文件 @
4c1963cb
...
...
@@ -9,7 +9,7 @@ public class SegmentStorage {
private
LinkedList
<
TraceSegment
>
traceSegments
;
private
LinkedList
<
IgnoredTracerContext
>
ignoredTracerContexts
;
SegmentStorage
()
{
public
SegmentStorage
()
{
traceSegments
=
new
LinkedList
<
TraceSegment
>();
ignoredTracerContexts
=
new
LinkedList
<
IgnoredTracerContext
>();
}
...
...
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/SpanAssert.java
0 → 100644
浏览文件 @
4c1963cb
此差异已折叠。
点击以展开。
apm-sniffer/apm-test-tools/src/main/java/org/skywalking/apm/agent/test/tools/TracingSegmentRunner.java
浏览文件 @
4c1963cb
此差异已折叠。
点击以展开。
apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-1.x-activation/pom.xml
浏览文件 @
4c1963cb
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录