Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿信在这里
SkyWalking
提交
47caee48
S
SkyWalking
项目概览
阿信在这里
/
SkyWalking
与 Fork 源项目一致
Fork自
山不在高_有仙则灵 / SkyWalking
通知
1
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,发现更多精彩内容 >>
提交
47caee48
编写于
2月 28, 2017
作者:
A
ascrutae
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix httpclient issue and add test case
上级
696090b0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
222 addition
and
0 deletion
+222
-0
skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
...ng/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
+3
-0
skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
...lugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
+166
-0
skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/plugin/InstrumentationAssert.java
...skywalking/sniffer/mock/plugin/InstrumentationAssert.java
+31
-0
skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/plugin/InstrumentationAssertTest.java
...alking/sniffer/mock/plugin/InstrumentationAssertTest.java
+22
-0
未找到文件。
skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java
浏览文件 @
47caee48
...
...
@@ -24,6 +24,7 @@ import org.apache.http.StatusLine;
*/
public
class
HttpClientExecuteInterceptor
implements
InstanceMethodsAroundInterceptor
{
public
static
final
String
HEADER_NAME_OF_CONTEXT_DATA
=
"SKYWALKING_CONTEXT_DATA"
;
private
static
final
String
COMPONENT_NAME
=
"Http"
;
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
...
...
@@ -40,6 +41,7 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
Tags
.
PEER_PORT
.
set
(
span
,
httpHost
.
getPort
());
Tags
.
PEER_HOST
.
set
(
span
,
httpHost
.
getHostName
());
Tags
.
SPAN_KIND
.
set
(
span
,
Tags
.
SPAN_KIND_CLIENT
);
Tags
.
COMPONENT
.
set
(
span
,
COMPONENT_NAME
);
Tags
.
URL
.
set
(
span
,
generateURL
(
httpHost
,
httpRequest
));
Tags
.
SPAN_LAYER
.
asHttp
(
span
);
...
...
@@ -79,6 +81,7 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
)
{
Tags
.
ERROR
.
set
(
ContextManager
.
INSTANCE
.
activeSpan
(),
true
);
ContextManager
.
INSTANCE
.
activeSpan
().
log
(
t
);
}
...
...
skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java
0 → 100644
浏览文件 @
47caee48
package
com.a.eye.skywalking.plugin.httpClient.v4
;
import
com.a.eye.skywalking.api.context.TracerContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener
;
import
com.a.eye.skywalking.sniffer.mock.context.SegmentAssert
;
import
com.a.eye.skywalking.trace.LogData
;
import
com.a.eye.skywalking.trace.Span
;
import
com.a.eye.skywalking.trace.TraceSegment
;
import
com.a.eye.skywalking.trace.tag.Tags
;
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.junit.After
;
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.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
java.util.List
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
(
HttpHost
.
class
)
public
class
HttpClientExecuteInterceptorTest
{
private
HttpClientExecuteInterceptor
httpClientExecuteInterceptor
;
private
MockTracerContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
instanceMethodInvokeContext
;
@Mock
private
HttpHost
httpHost
;
@Mock
private
HttpRequest
request
;
@Mock
private
HttpResponse
httpResponse
;
@Mock
private
StatusLine
statusLine
;
@Before
public
void
setUp
()
throws
Exception
{
mockTracerContextListener
=
new
MockTracerContextListener
();
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"
);
when
(
request
.
getRequestLine
()).
thenReturn
(
new
RequestLine
()
{
@Override
public
String
getMethod
()
{
return
"GET"
;
}
@Override
public
ProtocolVersion
getProtocolVersion
()
{
return
new
ProtocolVersion
(
"http"
,
1
,
1
);
}
@Override
public
String
getUri
()
{
return
"/test-web/test"
;
}
});
when
(
httpHost
.
getPort
()).
thenReturn
(
8080
);
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
}
@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
());
}
});
}
@Test
public
void
testStatusCodeNotEquals200
()
{
when
(
statusLine
.
getStatusCode
()).
thenReturn
(
500
);
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
));
assertThat
(
Tags
.
ERROR
.
get
(
traceSegment
.
getSpans
().
get
(
0
)),
is
(
true
));
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
});
}
@Test
public
void
testHttpClientWithException
()
{
httpClientExecuteInterceptor
.
beforeMethod
(
classInstanceContext
,
instanceMethodInvokeContext
,
null
);
httpClientExecuteInterceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
instanceMethodInvokeContext
);
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
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertHttpSpan
(
span
);
assertThat
(
Tags
.
ERROR
.
get
(
span
),
is
(
true
));
assertHttpSpanErrorLog
(
span
.
getLogs
());
verify
(
request
,
times
(
1
)).
setHeader
(
anyString
(),
anyString
());
}
private
void
assertHttpSpanErrorLog
(
List
<
LogData
>
logs
)
{
assertThat
(
logs
.
size
(),
is
(
1
));
LogData
logData
=
logs
.
get
(
0
);
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
}
});
}
private
void
assertHttpSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test-web/test"
));
assertThat
(
Tags
.
COMPONENT
.
get
(
span
),
is
(
"Http"
));
assertThat
(
Tags
.
PEER_HOST
.
get
(
span
),
is
(
"127.0.0.1"
));
assertThat
(
Tags
.
PEER_PORT
.
get
(
span
),
is
(
8080
));
assertThat
(
Tags
.
URL
.
get
(
span
),
is
(
"http://127.0.0.1:8080/test-web/test"
));
assertThat
(
Tags
.
SPAN_KIND
.
get
(
span
),
is
(
Tags
.
SPAN_KIND_CLIENT
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
\ No newline at end of file
skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/plugin/InstrumentationAssert.java
0 → 100644
浏览文件 @
47caee48
package
com.a.eye.skywalking.sniffer.mock.plugin
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassEnhancePluginDefine
;
public
class
InstrumentationAssert
{
private
ClassEnhancePluginDefine
instrumentationClass
;
private
String
enhanceClass
;
public
InstrumentationAssert
(
Class
instrumentationClass
)
{
try
{
this
.
instrumentationClass
=
(
ClassEnhancePluginDefine
)
instrumentationClass
.
newInstance
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
InstrumentationAssert
enhanceClass
(
String
enhanceClass
)
{
this
.
enhanceClass
=
enhanceClass
;
return
this
;
}
public
InstrumentationAssert
constructor
()
{
return
this
;
}
public
void
withParamType
(
Class
...
paramType
)
{
}
}
skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/plugin/InstrumentationAssertTest.java
0 → 100644
浏览文件 @
47caee48
package
com.a.eye.skywalking.sniffer.mock.plugin
;
import
org.junit.Test
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
public
class
InstrumentationAssertTest
{
@Test
public
void
testAssert
()
{
new
InstrumentationAssert
(
Object
.
class
).
enhanceClass
(
""
)
.
constructor
().
withParamType
(
Class
.
class
,
Object
.
class
);
/*.and(assertMethod("").withParamType())*/
// new InstrumentationAssert(Object.class).enhanceClass("").allMethod()
// .and(assertConstructor().withParamType(Class.class, Object.class));
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录