Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
javalover123888
SkyWalking
提交
7eb5694a
S
SkyWalking
项目概览
javalover123888
/
SkyWalking
与 Fork 源项目一致
Fork自
山不在高_有仙则灵 / SkyWalking
通知
2
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,发现更多精彩内容 >>
提交
7eb5694a
编写于
8月 13, 2019
作者:
A
Andy Ai
提交者:
wu-sheng
8月 13, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Java agent support undertow routing handler (#3173)
* Support undertow routing handler
上级
136c7d4f
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
898 addition
and
106 deletion
+898
-106
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/ListenerConfigInterceptor.java
...ng/apm/plugin/undertow/v2x/ListenerConfigInterceptor.java
+60
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/RootHandlerInterceptor.java
...lking/apm/plugin/undertow/v2x/RootHandlerInterceptor.java
+55
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/RoutingHandlerInterceptor.java
...ng/apm/plugin/undertow/v2x/RoutingHandlerInterceptor.java
+53
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/RoutingHandlerInstrumentation.java
...in/undertow/v2x/define/RoutingHandlerInstrumentation.java
+83
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowAddListenerInstrumentation.java
...dertow/v2x/define/UndertowAddListenerInstrumentation.java
+76
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowListenerConfigInstrumentation.java
...tow/v2x/define/UndertowListenerConfigInstrumentation.java
+78
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowRootHandlerInstrumentation.java
...dertow/v2x/define/UndertowRootHandlerInstrumentation.java
+26
-16
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/handler/TracingHandler.java
...lking/apm/plugin/undertow/v2x/handler/TracingHandler.java
+97
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/resources/skywalking-plugin.def
...ertow-2.x-plugin/src/main/resources/skywalking-plugin.def
+4
-1
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/RootHandlerInterceptorTest.java
...g/apm/plugin/undertow/v2x/RootHandlerInterceptorTest.java
+104
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/RoutingHandlerInterceptorTest.java
...pm/plugin/undertow/v2x/RoutingHandlerInterceptorTest.java
+144
-0
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/TracingHandlerTest.java
...kywalking/apm/plugin/undertow/v2x/TracingHandlerTest.java
+51
-89
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/UndertowBuilderMethodMatcherTest.java
...plugin/undertow/v2x/UndertowBuilderMethodMatcherTest.java
+67
-0
未找到文件。
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/ListenerConfigInterceptor.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.Undertow
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.RoutingHandler
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
/**
* @author AI
* 2019-08-10
*/
public
class
ListenerConfigInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
final
Undertow
.
ListenerBuilder
builder
=
(
Undertow
.
ListenerBuilder
)
allArguments
[
0
];
final
Field
rootHandlerField
=
Undertow
.
ListenerBuilder
.
class
.
getDeclaredField
(
"rootHandler"
);
rootHandlerField
.
setAccessible
(
true
);
final
Object
handler
=
rootHandlerField
.
get
(
builder
);
if
(
null
!=
handler
&&
!(
handler
instanceof
RoutingHandler
))
{
rootHandlerField
.
set
(
builder
,
new
TracingHandler
((
HttpHandler
)
handler
));
}
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
return
ret
;
}
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
errorOccurred
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/RootHandlerInterceptor.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.RoutingHandler
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
import
java.lang.reflect.Method
;
/**
* @author chenpengfei
* @author AI
*/
public
class
RootHandlerInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
int
handlerIndex
=
allArguments
.
length
-
1
;
if
(!(
allArguments
[
handlerIndex
]
instanceof
RoutingHandler
))
{
allArguments
[
handlerIndex
]
=
new
TracingHandler
((
HttpHandler
)
allArguments
[
handlerIndex
]);
}
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
return
ret
;
}
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
errorOccurred
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/RoutingHandlerInterceptor.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.server.HttpHandler
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
import
java.lang.reflect.Method
;
/**
* @author AI
* 2019-07-25
*/
public
class
RoutingHandlerInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
final
int
httpHandlerIndex
=
argumentsTypes
.
length
-
1
;
final
HttpHandler
handler
=
(
HttpHandler
)
allArguments
[
httpHandlerIndex
];
final
String
template
=
(
String
)
allArguments
[
1
];
allArguments
[
httpHandlerIndex
]
=
new
TracingHandler
(
template
,
handler
);
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
return
ret
;
}
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
errorOccurred
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/RoutingHandlerInstrumentation.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x.define
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
bytebuddy
.
ArgumentTypeNameMatch
.
takesArgumentWithType
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
/**
* @author AI
* 2019-07-26
*/
public
class
RoutingHandlerInstrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_METHOD
=
"add"
;
private
static
final
String
ENHANCE_CLASS
=
"io.undertow.server.RoutingHandler"
;
private
static
final
String
INTERCEPTOR_CLASS
=
"org.apache.skywalking.apm.plugin.undertow.v2x.RoutingHandlerInterceptor"
;
@Override
public
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
new
ConstructorInterceptPoint
[
0
];
}
@Override
public
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
RoutingHandlerInstrumentation
.
getRoutingHandlerMethodMatcher
();
}
@Override
public
String
getMethodsInterceptor
()
{
return
INTERCEPTOR_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
true
;
}
}
};
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
ENHANCE_CLASS
);
}
public
static
ElementMatcher
<
MethodDescription
>
getRoutingHandlerMethodMatcher
()
{
final
ElementMatcher
.
Junction
<
MethodDescription
>
basicMatcher
=
named
(
ENHANCE_METHOD
)
.
and
(
takesArgumentWithType
(
0
,
"io.undertow.util.HttpString"
))
.
and
(
takesArgumentWithType
(
1
,
"java.lang.String"
));
final
String
httpHandlerClassName
=
"io.undertow.server.HttpHandler"
;
return
(
basicMatcher
.
and
(
takesArgumentWithType
(
2
,
httpHandlerClassName
)))
.
or
(
basicMatcher
.
and
(
takesArgumentWithType
(
3
,
httpHandlerClassName
)));
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowAddListenerInstrumentation.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x.define
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
bytebuddy
.
ArgumentTypeNameMatch
.
takesArgumentWithType
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
/**
* @author AI
* 2019-08-10
*/
public
class
UndertowAddListenerInstrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_CLASS
=
"io.undertow.Undertow$Builder"
;
private
static
final
String
INTERCEPTOR_CLASS
=
"org.apache.skywalking.apm.plugin.undertow.v2x.RootHandlerInterceptor"
;
private
static
final
String
[]
ENHANCE_METHODS
=
new
String
[]{
"addHttpListener"
,
"addHttpsListener"
,
"addAjpListener"
};
@Override
public
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
new
ConstructorInterceptPoint
[
0
];
}
@Override
public
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
final
InstanceMethodsInterceptPoint
[]
points
=
new
InstanceMethodsInterceptPoint
[
ENHANCE_METHODS
.
length
];
for
(
int
i
=
0
;
i
<
ENHANCE_METHODS
.
length
;
i
++)
{
final
String
method
=
ENHANCE_METHODS
[
i
];
final
InstanceMethodsInterceptPoint
point
=
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
method
).
and
(
takesArgumentWithType
(
2
,
"io.undertow.server.HttpHandler"
));
}
@Override
public
String
getMethodsInterceptor
()
{
return
INTERCEPTOR_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
true
;
}
};
points
[
i
]
=
point
;
}
return
points
;
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
ENHANCE_CLASS
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowListenerConfigInstrumentation.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x.define
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
bytebuddy
.
ArgumentTypeNameMatch
.
takesArgumentWithType
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
/**
* @author AI
* 2019-08-10
*/
public
class
UndertowListenerConfigInstrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_METHOD
=
"addListener"
;
private
static
final
String
ENHANCE_CLASS
=
"io.undertow.Undertow$Builder"
;
private
static
final
String
INTERCEPTOR_CLASS
=
"org.apache.skywalking.apm.plugin.undertow.v2x.ListenerConfigInterceptor"
;
@Override
public
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
new
ConstructorInterceptPoint
[
0
];
}
@Override
public
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
UndertowListenerConfigInstrumentation
.
getUndertowBuilderMethodMatcher
();
}
@Override
public
String
getMethodsInterceptor
()
{
return
INTERCEPTOR_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
}
}
};
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
ENHANCE_CLASS
);
}
public
static
ElementMatcher
<
MethodDescription
>
getUndertowBuilderMethodMatcher
()
{
return
named
(
ENHANCE_METHOD
).
and
(
takesArgumentWithType
(
0
,
"io.undertow.Undertow$ListenerBuilder"
));
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/UndertowInstrumentation.java
→
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/define/Undertow
RootHandler
Instrumentation.java
浏览文件 @
7eb5694a
...
...
@@ -20,35 +20,38 @@ package org.apache.skywalking.apm.plugin.undertow.v2x.define;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.
Static
MethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.Class
Static
MethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.
Instance
MethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.Class
Instance
MethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
org.apache.skywalking.apm.agent.core.plugin.match.NameMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
bytebuddy
.
ArgumentTypeNameMatch
.
takesArgumentWithType
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
/**
* @author chenpengfei
* @author AI
*/
public
class
Undertow
Instrumentation
extends
ClassStatic
MethodsEnhancePluginDefine
{
public
class
Undertow
RootHandlerInstrumentation
extends
ClassInstance
MethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_
CLASS
=
"io.undertow.server.Connectors
"
;
private
static
final
String
ENHANCE_
METHOD
=
"executeRootHandl
er"
;
private
static
final
String
INTERCEPTOR_CLASS
=
"org.apache.skywalking.apm.plugin.undertow.v2x.
Execute
RootHandlerInterceptor"
;
private
static
final
String
ENHANCE_
METHOD
=
"setHandler
"
;
private
static
final
String
ENHANCE_
CLASS
=
"io.undertow.Undertow$Build
er"
;
private
static
final
String
INTERCEPTOR_CLASS
=
"org.apache.skywalking.apm.plugin.undertow.v2x.RootHandlerInterceptor"
;
@Override
p
rotected
ClassMatch
enhanceClas
s
()
{
return
NameMatch
.
byName
(
ENHANCE_CLASS
)
;
p
ublic
ConstructorInterceptPoint
[]
getConstructorsInterceptPoint
s
()
{
return
new
ConstructorInterceptPoint
[
0
]
;
}
@Override
public
StaticMethodsInterceptPoint
[]
getStaticMethodsInterceptPoints
()
{
return
new
StaticMethodsInterceptPoint
[]
{
new
StaticMethodsInterceptPoint
()
{
public
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
ENHANCE_METHOD
);
return
named
(
ENHANCE_METHOD
)
.
and
(
takesArgumentWithType
(
0
,
"io.undertow.server.HttpHandler"
))
;
}
@Override
...
...
@@ -56,10 +59,17 @@ public class UndertowInstrumentation extends ClassStaticMethodsEnhancePluginDefi
return
INTERCEPTOR_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
@Override
public
boolean
isOverrideArgs
()
{
return
true
;
}
}
};
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
ENHANCE_CLASS
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/
ExecuteRootHandlerIntercepto
r.java
→
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/undertow/v2x/
handler/TracingHandle
r.java
浏览文件 @
7eb5694a
...
...
@@ -15,63 +15,83 @@
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x.handler
;
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.server.ExchangeCompletionListener
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.HttpServerExchange
;
import
io.undertow.util.HeaderMap
;
import
org.apache.skywalking.apm.agent.core.context.CarrierItem
;
import
org.apache.skywalking.apm.agent.core.context.ContextCarrier
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.context.tag.Tags
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan
;
import
org.apache.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.network.trace.component.ComponentsDefine
;
import
java.lang.reflect.Method
;
import
io.undertow.server.HttpServerExchange
;
import
io.undertow.util.HeaderMap
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.Constants
;
/**
* @author chenpengfei
* @author AI
* 2019-08-06
*/
public
class
ExecuteRootHandlerInterceptor
implements
StaticMethodsAroundInterceptor
{
public
class
TracingHandler
implements
HttpHandler
{
private
final
String
template
;
private
final
HttpHandler
next
;
@Override
public
void
beforeMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
MethodInterceptResult
result
)
{
HttpServerExchange
exchange
=
(
HttpServerExchange
)
allArguments
[
1
];
public
TracingHandler
(
HttpHandler
handler
)
{
this
(
null
,
handler
);
}
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
HeaderMap
headers
=
exchange
.
getRequestHeaders
();
CarrierItem
next
=
contextCarrier
.
items
();
while
(
next
.
hasNext
())
{
next
=
next
.
next
();
next
.
setHeadValue
(
headers
.
getFirst
(
next
.
getHeadKey
()));
public
TracingHandler
(
String
template
,
HttpHandler
handler
)
{
this
.
next
=
handler
;
this
.
template
=
template
;
}
@Override
public
void
handleRequest
(
HttpServerExchange
exchange
)
throws
Exception
{
final
HeaderMap
headers
=
exchange
.
getRequestHeaders
();
final
ContextCarrier
carrier
=
new
ContextCarrier
();
CarrierItem
items
=
carrier
.
items
();
while
(
items
.
hasNext
())
{
items
=
items
.
next
();
items
.
setHeadValue
(
headers
.
getFirst
(
items
.
getHeadKey
()));
}
String
operationName
;
if
(
null
==
template
)
{
operationName
=
exchange
.
getRequestPath
();
}
else
{
operationName
=
template
;
}
AbstractSpan
span
=
ContextManager
.
createEntrySpan
(
exchange
.
getRequestPath
(),
contextC
arrier
);
final
AbstractSpan
span
=
ContextManager
.
createEntrySpan
(
operationName
,
c
arrier
);
Tags
.
URL
.
set
(
span
,
exchange
.
getRequestURL
());
Tags
.
HTTP
.
METHOD
.
set
(
span
,
exchange
.
getRequestMethod
().
toString
());
span
.
setComponent
(
ComponentsDefine
.
UNDERTOW
);
SpanLayer
.
asHttp
(
span
);
}
@Override
public
Object
afterMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Object
ret
)
{
HttpServerExchange
exchange
=
(
HttpServerExchange
)
allArguments
[
1
];
AbstractSpan
span
=
ContextManager
.
activeSpan
();
if
(
exchange
.
getStatusCode
()
>=
400
)
{
span
.
errorOccurred
();
Tags
.
STATUS_CODE
.
set
(
span
,
Integer
.
toString
(
exchange
.
getStatusCode
()));
try
{
span
.
prepareForAsync
();
exchange
.
addExchangeCompleteListener
(
new
ExchangeCompletionListener
()
{
@Override
public
void
exchangeEvent
(
HttpServerExchange
httpServerExchange
,
NextListener
nextListener
)
{
nextListener
.
proceed
();
if
(
httpServerExchange
.
getStatusCode
()
>=
400
)
{
span
.
errorOccurred
();
Tags
.
STATUS_CODE
.
set
(
span
,
Integer
.
toString
(
httpServerExchange
.
getStatusCode
()));
}
span
.
asyncFinish
();
}
});
}
catch
(
Throwable
e
)
{
ContextManager
.
activeSpan
().
log
(
e
);
}
try
{
next
.
handleRequest
(
exchange
);
}
catch
(
Throwable
e
)
{
span
.
errorOccurred
().
log
(
e
);
}
finally
{
ContextManager
.
stopSpan
(
span
);
ContextManager
.
getRuntimeContext
().
remove
(
Constants
.
FORWARD_REQUEST_FLAG
);
}
ContextManager
.
stopSpan
();
ContextManager
.
getRuntimeContext
().
remove
(
Constants
.
FORWARD_REQUEST_FLAG
);
return
ret
;
}
@Override
public
void
handleMethodException
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
errorOccurred
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/main/resources/skywalking-plugin.def
浏览文件 @
7eb5694a
...
...
@@ -14,5 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.RoutingHandlerInstrumentation
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.RequestDispatcherImplInstrumentation
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.UndertowInstrumentation
\ No newline at end of file
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.UndertowRootHandlerInstrumentation
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.UndertowAddListenerInstrumentation
undertow-2.x-plugin=org.apache.skywalking.apm.plugin.undertow.v2x.define.UndertowListenerConfigInstrumentation
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/RootHandlerInterceptorTest.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.Undertow
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.RoutingHandler
;
import
io.undertow.util.Methods
;
import
org.apache.skywalking.apm.agent.core.conf.Config
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.test.tools.AgentServiceRule
;
import
org.apache.skywalking.apm.agent.test.tools.SegmentStorage
;
import
org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint
;
import
org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
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.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
java.lang.reflect.Method
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author chenpengfei
*/
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
RootHandlerInterceptorTest
{
private
RootHandlerInterceptor
rootHandlerInterceptor
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
HttpHandler
httpHandler
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Mock
private
EnhancedInstance
enhancedInstance
;
@Before
public
void
setUp
()
throws
Exception
{
Config
.
Agent
.
ACTIVE_V1_HEADER
=
true
;
rootHandlerInterceptor
=
new
RootHandlerInterceptor
();
}
@After
public
void
clear
()
{
Config
.
Agent
.
ACTIVE_V1_HEADER
=
false
;
}
@Test
public
void
testBindTracingHandler
()
throws
Throwable
{
Object
[]
arguments
=
new
Object
[]{
httpHandler
};
Class
[]
argumentType
=
new
Class
[]{
HttpHandler
.
class
};
final
Method
method
=
Undertow
.
Builder
.
class
.
getMethod
(
"setHandler"
,
argumentType
);
rootHandlerInterceptor
.
beforeMethod
(
enhancedInstance
,
method
,
arguments
,
argumentType
,
methodInterceptResult
);
rootHandlerInterceptor
.
afterMethod
(
enhancedInstance
,
method
,
arguments
,
argumentType
,
null
);
assertTrue
(
arguments
[
0
]
instanceof
TracingHandler
);
}
@Test
public
void
testBindRoutingHandler
()
throws
Throwable
{
RoutingHandler
handler
=
new
RoutingHandler
();
handler
.
add
(
Methods
.
GET
,
"/projects/{projectId}"
,
httpHandler
);
Object
[]
arguments
=
new
Object
[]{
handler
};
Class
[]
argumentType
=
new
Class
[]{
HttpHandler
.
class
};
final
Method
method
=
Undertow
.
Builder
.
class
.
getMethod
(
"setHandler"
,
argumentType
);
rootHandlerInterceptor
.
beforeMethod
(
enhancedInstance
,
method
,
arguments
,
argumentType
,
methodInterceptResult
);
rootHandlerInterceptor
.
afterMethod
(
enhancedInstance
,
method
,
arguments
,
argumentType
,
null
);
assertTrue
(
arguments
[
0
]
instanceof
RoutingHandler
);
}
}
\ No newline at end of file
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/RoutingHandlerInterceptorTest.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.HttpServerExchange
;
import
io.undertow.server.RoutingHandler
;
import
io.undertow.server.ServerConnection
;
import
io.undertow.util.HeaderMap
;
import
io.undertow.util.HttpString
;
import
io.undertow.util.Methods
;
import
io.undertow.util.StatusCodes
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
import
org.apache.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.apache.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.test.helper.SegmentHelper
;
import
org.apache.skywalking.apm.agent.test.helper.SpanHelper
;
import
org.apache.skywalking.apm.agent.test.tools.AgentServiceRule
;
import
org.apache.skywalking.apm.agent.test.tools.SegmentStorage
;
import
org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint
;
import
org.apache.skywalking.apm.agent.test.tools.SpanAssert
;
import
org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner
;
import
org.apache.skywalking.apm.network.trace.component.ComponentsDefine
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
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
java.lang.reflect.Method
;
import
java.net.InetSocketAddress
;
import
java.util.List
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertComponent
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author AI
* 2019-07-29
*/
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
RoutingHandlerInterceptorTest
{
private
RoutingHandlerInterceptor
interceptor
;
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
HttpHandler
httpHandler
;
@Mock
ServerConnection
serverConnection
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Mock
private
EnhancedInstance
enhancedInstance
;
private
String
template
=
"/projects/{projectId}/users"
;
private
String
uri
=
"/projects/{projectId}/users"
;
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
RoutingHandlerInterceptor
();
}
@Test
public
void
testBindArguments
()
throws
Throwable
{
Method
method
=
RoutingHandler
.
class
.
getMethod
(
"add"
,
HttpString
.
class
,
String
.
class
,
HttpHandler
.
class
);
Object
[]
arguments
=
new
Object
[]{
Methods
.
GET
,
template
,
httpHandler
};
interceptor
.
beforeMethod
(
enhancedInstance
,
method
,
arguments
,
method
.
getParameterTypes
(),
methodInterceptResult
);
interceptor
.
afterMethod
(
enhancedInstance
,
method
,
arguments
,
method
.
getParameterTypes
(),
null
);
assertTrue
(
arguments
[
2
]
instanceof
TracingHandler
);
}
@Test
public
void
testStatusCodeIsOk
()
throws
Throwable
{
TracingHandler
handler
=
new
TracingHandler
(
template
,
httpHandler
);
HttpServerExchange
exchange
=
buildExchange
();
handler
.
handleRequest
(
exchange
);
exchange
.
endExchange
();
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
testStatusCodeIsNotOk
()
throws
Throwable
{
TracingHandler
handler
=
new
TracingHandler
(
template
,
httpHandler
);
HttpServerExchange
exchange
=
buildExchange
();
exchange
.
setStatusCode
(
StatusCodes
.
INTERNAL_SERVER_ERROR
);
handler
.
handleRequest
(
exchange
);
exchange
.
endExchange
();
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertHttpSpan
(
spans
.
get
(
0
));
assertThat
(
SpanHelper
.
getErrorOccurred
(
spans
.
get
(
0
)),
is
(
true
));
}
private
HttpServerExchange
buildExchange
()
{
HeaderMap
requestHeaders
=
new
HeaderMap
();
HeaderMap
responseHeaders
=
new
HeaderMap
();
HttpServerExchange
exchange
=
new
HttpServerExchange
(
serverConnection
,
requestHeaders
,
responseHeaders
,
0
);
exchange
.
setRequestURI
(
uri
);
exchange
.
setRequestPath
(
uri
);
exchange
.
setDestinationAddress
(
new
InetSocketAddress
(
"localhost"
,
8080
));
exchange
.
setRequestScheme
(
"http"
);
exchange
.
setRequestMethod
(
Methods
.
GET
);
return
exchange
;
}
private
void
assertHttpSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
template
));
assertComponent
(
span
,
ComponentsDefine
.
UNDERTOW
);
SpanAssert
.
assertTag
(
span
,
0
,
"http://localhost:8080"
+
uri
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
SpanAssert
.
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/
ExecuteRootHandlerIntercepto
rTest.java
→
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/
TracingHandle
rTest.java
浏览文件 @
7eb5694a
...
...
@@ -15,7 +15,6 @@
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.server.HttpHandler
;
...
...
@@ -23,18 +22,14 @@ import io.undertow.server.HttpServerExchange;
import
io.undertow.server.ServerConnection
;
import
io.undertow.util.HeaderMap
;
import
io.undertow.util.HttpString
;
import
java.net.InetSocketAddres
s
;
import
java.util.List
;
import
io.undertow.util.Method
s
;
import
io.undertow.util.StatusCodes
;
import
org.apache.skywalking.apm.agent.core.conf.Config
;
import
org.apache.skywalking.apm.agent.core.context.SW3CarrierItem
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan
;
import
org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity
;
import
org.apache.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.apache.skywalking.apm.agent.core.context.trace.TraceSegment
;
import
org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef
;
import
org.apache.skywalking.apm.agent.core.context.util.TagValuePair
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.test.helper.SegmentHelper
;
import
org.apache.skywalking.apm.agent.test.helper.SegmentRefHelper
;
import
org.apache.skywalking.apm.agent.test.helper.SpanHelper
;
...
...
@@ -44,9 +39,7 @@ import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import
org.apache.skywalking.apm.agent.test.tools.SpanAssert
;
import
org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner
;
import
org.apache.skywalking.apm.network.trace.component.ComponentsDefine
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.handler.TracingHandler
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -54,132 +47,101 @@ import org.mockito.Mock;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
;
import
java.net.InetSocketAddress
;
import
java.util.List
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
test
.
tools
.
SpanAssert
.
assertComponent
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
/**
* @author chenpengfei
* @author AI
* 2019-07-29
*/
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
TracingSegmentRunner
.
class
)
public
class
ExecuteRootHandlerInterceptorTest
{
private
ExecuteRootHandlerInterceptor
executeRootHandlerInterceptor
;
public
class
TracingHandlerTest
{
@SegmentStoragePoint
private
SegmentStorage
segmentStorage
;
@Rule
public
AgentServiceRule
serviceRule
=
new
AgentServiceRule
();
@Mock
private
HttpHandler
httpHandler
;
private
HttpServerExchange
exchange
;
@Mock
ServerConnection
serverConnection
;
private
HeaderMap
requestHeaders
=
new
HeaderMap
();
private
HeaderMap
responseHeaders
=
new
HeaderMap
();
private
Object
[]
arguments
;
private
Class
[]
argumentType
;
private
String
template
=
"/projects/{projectId}/users"
;
private
String
uri
=
"/projects/{projectId}/users"
;
@Mock
private
MethodInterceptResult
methodInterceptResult
;
@Before
public
void
setUp
()
throws
Exception
{
Config
.
Agent
.
ACTIVE_V1_HEADER
=
true
;
executeRootHandlerInterceptor
=
new
ExecuteRootHandlerInterceptor
();
exchange
=
new
HttpServerExchange
(
serverConnection
,
requestHeaders
,
responseHeaders
,
0
);
exchange
.
setRequestURI
(
"/test/testRequestURL"
);
exchange
.
setRequestPath
(
"/test/testRequestURL"
);
exchange
.
setDestinationAddress
(
new
InetSocketAddress
(
"localhost"
,
8080
));
exchange
.
setRequestScheme
(
"http"
);
exchange
.
setRequestMethod
(
HttpString
.
tryFromString
(
"POST"
));
arguments
=
new
Object
[]
{
httpHandler
,
exchange
};
argumentType
=
new
Class
[]
{
httpHandler
.
getClass
(),
exchange
.
getClass
()};
}
@After
public
void
clear
()
{
Config
.
Agent
.
ACTIVE_V1_HEADER
=
false
;
}
@Test
public
void
testWithoutSerializedContextData
()
throws
Throwable
{
executeRootHandlerInterceptor
.
beforeMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
executeRootHandlerInterceptor
.
afterMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
null
);
public
void
testStatusCodeIsOk
()
throws
Throwable
{
TracingHandler
handler
=
new
TracingHandler
(
template
,
httpHandler
);
HttpServerExchange
exchange
=
buildExchange
();
handler
.
handleRequest
(
exchange
);
exchange
.
endExchange
();
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
test
WithSerializedContextData
()
throws
Throwable
{
requestHeaders
.
put
(
HttpString
.
tryFromString
(
SW3CarrierItem
.
HEADER_NAME
),
"1.234.111|3|1|1|#192.168.1.8:18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*"
);
ex
ecuteRootHandlerInterceptor
.
beforeMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
executeRootHandlerInterceptor
.
afterMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
null
);
public
void
test
StatusCodeIsNotOk
()
throws
Throwable
{
TracingHandler
handler
=
new
TracingHandler
(
template
,
httpHandler
);
HttpServerExchange
exchange
=
buildExchange
();
ex
change
.
setStatusCode
(
StatusCodes
.
INTERNAL_SERVER_ERROR
);
handler
.
handleRequest
(
exchange
);
exchange
.
endExchange
();
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertHttpSpan
(
spans
.
get
(
0
));
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
}
@Test
public
void
testStatusCodeNotEquals200
()
throws
Throwable
{
exchange
.
setStatusCode
(
500
);
executeRootHandlerInterceptor
.
beforeMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
executeRootHandlerInterceptor
.
afterMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
null
);
Assert
.
assertThat
(
segmentStorage
.
getTraceSegments
().
size
(),
is
(
1
));
TraceSegment
traceSegment
=
segmentStorage
.
getTraceSegments
().
get
(
0
);
List
<
AbstractTracingSpan
>
spans
=
SegmentHelper
.
getSpans
(
traceSegment
);
assertThat
(
spans
.
size
(),
is
(
1
));
List
<
TagValuePair
>
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
));
}
@Test
public
void
testWithUndertowException
()
throws
Throwable
{
executeRootHandlerInterceptor
.
beforeMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
methodInterceptResult
);
executeRootHandlerInterceptor
.
handleMethodException
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
new
RuntimeException
());
executeRootHandlerInterceptor
.
afterMethod
(
EnhancedInstance
.
class
,
null
,
arguments
,
argumentType
,
null
);
public
void
testWithSerializedContextData
()
throws
Throwable
{
Config
.
Agent
.
ACTIVE_V1_HEADER
=
true
;
TracingHandler
handler
=
new
TracingHandler
(
httpHandler
);
HttpServerExchange
exchange
=
buildExchange
();
exchange
.
getRequestHeaders
().
put
(
HttpString
.
tryFromString
(
SW3CarrierItem
.
HEADER_NAME
),
"1.234.111|3|1|1|#192.168.1.8:18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*"
);
handler
.
handleRequest
(
exchange
);
exchange
.
endExchange
();
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
));
SpanAssert
.
assertException
(
logDataEntities
.
get
(
0
),
RuntimeException
.
class
);
assertTraceSegmentRef
(
traceSegment
.
getRefs
().
get
(
0
));
Config
.
Agent
.
ACTIVE_V1_HEADER
=
false
;
}
private
void
assertTraceSegmentRef
(
TraceSegmentRef
ref
)
{
assertThat
(
SegmentRefHelper
.
getEntryServiceInstanceId
(
ref
),
is
(
1
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
).
toString
(),
is
(
"1.234.111"
));
private
HttpServerExchange
buildExchange
()
{
HeaderMap
requestHeaders
=
new
HeaderMap
();
HeaderMap
responseHeaders
=
new
HeaderMap
();
HttpServerExchange
exchange
=
new
HttpServerExchange
(
serverConnection
,
requestHeaders
,
responseHeaders
,
0
);
exchange
.
setRequestURI
(
uri
);
exchange
.
setRequestPath
(
uri
);
exchange
.
setDestinationAddress
(
new
InetSocketAddress
(
"localhost"
,
8080
));
exchange
.
setRequestScheme
(
"http"
);
exchange
.
setRequestMethod
(
Methods
.
GET
);
return
exchange
;
}
private
void
assertHttpSpan
(
AbstractTracingSpan
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"/test/testRequestURL"
));
assertThat
(
span
.
getOperationName
(),
is
(
template
));
assertComponent
(
span
,
ComponentsDefine
.
UNDERTOW
);
SpanAssert
.
assertTag
(
span
,
0
,
"http://localhost:8080
/test/testRequestURL"
);
SpanAssert
.
assertTag
(
span
,
0
,
"http://localhost:8080
"
+
uri
);
assertThat
(
span
.
isEntry
(),
is
(
true
));
SpanAssert
.
assertLayer
(
span
,
SpanLayer
.
HTTP
);
}
}
\ No newline at end of file
private
void
assertTraceSegmentRef
(
TraceSegmentRef
ref
)
{
assertThat
(
SegmentRefHelper
.
getEntryServiceInstanceId
(
ref
),
is
(
1
));
assertThat
(
SegmentRefHelper
.
getSpanId
(
ref
),
is
(
3
));
assertThat
(
SegmentRefHelper
.
getTraceSegmentId
(
ref
).
toString
(),
is
(
"1.234.111"
));
}
}
apm-sniffer/apm-sdk-plugin/undertow-plugins/undertow-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/undertow/v2x/UndertowBuilderMethodMatcherTest.java
0 → 100644
浏览文件 @
7eb5694a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.undertow.v2x
;
import
io.undertow.Undertow
;
import
io.undertow.predicate.Predicate
;
import
io.undertow.server.HttpHandler
;
import
io.undertow.server.RoutingHandler
;
import
io.undertow.util.HttpString
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.define.RoutingHandlerInstrumentation
;
import
org.apache.skywalking.apm.plugin.undertow.v2x.define.UndertowListenerConfigInstrumentation
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.lang.reflect.Method
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
takesArgument
;
/**
* @author AI
* 2019-08-10
*/
public
class
UndertowBuilderMethodMatcherTest
{
@Test
public
void
testMatch
()
throws
Throwable
{
ElementMatcher
<
MethodDescription
>
matcher
=
named
(
"addHttpListener"
).
and
(
takesArgument
(
2
,
HttpHandler
.
class
));
Method
method
=
Undertow
.
Builder
.
class
.
getMethod
(
"addHttpListener"
,
int
.
class
,
String
.
class
,
HttpHandler
.
class
);
MethodDescription
md
=
new
MethodDescription
.
ForLoadedMethod
(
method
);
boolean
r
=
matcher
.
matches
(
md
);
Assert
.
assertTrue
(
r
);
}
@Test
public
void
testMatchRoutingHandler
()
throws
Throwable
{
ElementMatcher
<
MethodDescription
>
matcher
=
RoutingHandlerInstrumentation
.
getRoutingHandlerMethodMatcher
();
Method
method1
=
RoutingHandler
.
class
.
getMethod
(
"add"
,
HttpString
.
class
,
String
.
class
,
HttpHandler
.
class
);
Method
method2
=
RoutingHandler
.
class
.
getMethod
(
"add"
,
HttpString
.
class
,
String
.
class
,
Predicate
.
class
,
HttpHandler
.
class
);
Assert
.
assertTrue
(
matcher
.
matches
(
new
MethodDescription
.
ForLoadedMethod
(
method1
)));
Assert
.
assertTrue
(
matcher
.
matches
(
new
MethodDescription
.
ForLoadedMethod
(
method2
)));
}
@Test
public
void
testMatcListenerConfig
()
throws
Throwable
{
ElementMatcher
<
MethodDescription
>
matcher
=
UndertowListenerConfigInstrumentation
.
getUndertowBuilderMethodMatcher
();
Method
method
=
Undertow
.
Builder
.
class
.
getMethod
(
"addListener"
,
Undertow
.
ListenerBuilder
.
class
);
Assert
.
assertTrue
(
matcher
.
matches
(
new
MethodDescription
.
ForLoadedMethod
(
method
)));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录