Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
SkyWalking
提交
a49e19c7
S
SkyWalking
项目概览
apache
/
SkyWalking
上一次同步 1 年多
通知
302
Star
21345
Fork
6091
代码
文件
提交
分支
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,体验更适合开发者的 AI 搜索 >>
提交
a49e19c7
编写于
6月 19, 2016
作者:
wu-sheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
提交plugin模式大量重构,此重构会暂时造成dubbo插件失效(后续补充)
上级
da9192cf
变更
35
隐藏空白更改
内联
并排
Showing
35 changed file
with
561 addition
and
134 deletion
+561
-134
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/AbstractClassEnhancePluginDefine.java
.../plugin/interceptor/AbstractClassEnhancePluginDefine.java
+72
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/assist/SimpleObjectFirstInvokeInterceptor.java
...nterceptor/assist/SimpleObjectFirstInvokeInterceptor.java
+2
-2
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassConstructorInterceptor.java
...ugin/interceptor/enhance/ClassConstructorInterceptor.java
+6
-4
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
.../plugin/interceptor/enhance/ClassEnhancePluginDefine.java
+182
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java
...ptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java
+24
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsInterceptor.java
.../interceptor/enhance/ClassInstanceMethodsInterceptor.java
+9
-7
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java
...ceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java
+23
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java
...in/interceptor/enhance/ClassStaticMethodsInterceptor.java
+64
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ConstructorInvokeContext.java
.../plugin/interceptor/enhance/ConstructorInvokeContext.java
+1
-1
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/FieldGetter.java
...ud/skywalking/plugin/interceptor/enhance/FieldGetter.java
+5
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/FieldSetter.java
...ud/skywalking/plugin/interceptor/enhance/FieldSetter.java
+1
-1
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/InstanceMethodInvokeContext.java
...ugin/interceptor/enhance/InstanceMethodInvokeContext.java
+17
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/IntanceMethodsAroundInterceptor.java
.../interceptor/enhance/IntanceMethodsAroundInterceptor.java
+13
-0
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/MethodInvokeContext.java
...lking/plugin/interceptor/enhance/MethodInvokeContext.java
+2
-11
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/StaticMethodsAroundInterceptor.java
...n/interceptor/enhance/StaticMethodsAroundInterceptor.java
+10
-0
skywalking-api/src/test/java/test/ai/cloud/matcher/TestAroundInterceptor.java
...est/java/test/ai/cloud/matcher/TestAroundInterceptor.java
+7
-7
skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java
...rc/test/java/test/ai/cloud/matcher/TestMatcherDefine.java
+12
-14
skywalking-api/src/test/java/test/ai/cloud/plugin/BeInterceptedClass.java
...rc/test/java/test/ai/cloud/plugin/BeInterceptedClass.java
+4
-0
skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java
...pi/src/test/java/test/ai/cloud/plugin/PluginMainTest.java
+2
-0
skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java
...test/java/test/ai/cloud/plugin/TestAroundInterceptor.java
+7
-7
skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java
...test/java/test/ai/cloud/plugin/TestInterceptorDefine.java
+16
-5
skywalking-api/src/test/java/test/ai/cloud/plugin/TestStaticAroundInterceptor.java
...ava/test/ai/cloud/plugin/TestStaticAroundInterceptor.java
+26
-0
skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/ai/cloud/skywalking/plugin/DubboPluginDefine.java
...ava/com/ai/cloud/skywalking/plugin/DubboPluginDefine.java
+18
-37
skywalking-sdk-plugin/httpClient-4.x-plugin-dubbox-rest-attachment/src/main/java/org/skywalking/httpClient/v4/plugin/dubbox/rest/attachment/DubboxRestHeadSetterAttachment.java
...ubbox/rest/attachment/DubboxRestHeadSetterAttachment.java
+5
-5
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/HttpClientExecuteInterceptor.java
...ng/httpClient/v4/plugin/HttpClientExecuteInterceptor.java
+7
-7
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/AbstractHttpClientPluginDefine.java
...ient/v4/plugin/define/AbstractHttpClientPluginDefine.java
+1
-1
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/DefaultRequestDirectorPluginDefine.java
.../v4/plugin/define/DefaultRequestDirectorPluginDefine.java
+1
-1
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/HttpClientPluginDefine.java
...g/httpClient/v4/plugin/define/HttpClientPluginDefine.java
+4
-4
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/InternalHttpClientPluginDefine.java
...ient/v4/plugin/define/InternalHttpClientPluginDefine.java
+1
-1
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/MinimalHttpClientPluginDefine.java
...lient/v4/plugin/define/MinimalHttpClientPluginDefine.java
+1
-1
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisBaseInterceptor.java
...loud/skywalking/jedis/v2/plugin/JedisBaseInterceptor.java
+6
-6
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisClusterInterceptor.java
...d/skywalking/jedis/v2/plugin/JedisClusterInterceptor.java
+1
-1
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisInterceptor.java
...ai/cloud/skywalking/jedis/v2/plugin/JedisInterceptor.java
+1
-1
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/define/JedisClusterPluginDefine.java
...king/jedis/v2/plugin/define/JedisClusterPluginDefine.java
+5
-5
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/define/JedisPluginDefine.java
.../skywalking/jedis/v2/plugin/define/JedisPluginDefine.java
+5
-5
未找到文件。
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
Interceptor
PluginDefine.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
AbstractClassEnhance
PluginDefine.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
import
static
com
.
ai
.
cloud
.
skywalking
.
plugin
.
PluginBootstrap
.
CLASS_TYPE_POOL
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
any
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
not
;
import
net.bytebuddy.ByteBuddy
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.dynamic.ClassFileLocator
;
import
net.bytebuddy.dynamic.DynamicType
;
import
net.bytebuddy.dynamic.loading.ClassLoadingStrategy
;
import
net.bytebuddy.implementation.MethodDelegation
;
import
net.bytebuddy.implementation.SuperMethodCall
;
import
net.bytebuddy.implementation.bind.annotation.FieldProxy
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
net.bytebuddy.matcher.ElementMatchers
;
import
net.bytebuddy.pool.TypePool.Resolution
;
import
org.apache.logging.log4j.LogManager
;
...
...
@@ -22,12 +14,9 @@ import com.ai.cloud.skywalking.plugin.IPlugin;
import
com.ai.cloud.skywalking.plugin.PluginException
;
import
com.ai.cloud.skywalking.util.StringUtil
;
public
abstract
class
Interceptor
PluginDefine
implements
IPlugin
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
Interceptor
PluginDefine
.
class
);
public
abstract
class
AbstractClassEnhance
PluginDefine
implements
IPlugin
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
AbstractClassEnhance
PluginDefine
.
class
);
public
static
final
String
contextAttrName
=
"_$EnhancedClassInstanceContext"
;
@Override
public
void
define
()
throws
PluginException
{
String
interceptorDefineClassName
=
this
.
getClass
().
getName
();
...
...
@@ -55,63 +44,8 @@ public abstract class InterceptorPluginDefine implements IPlugin {
DynamicType
.
Builder
<?>
newClassBuilder
=
new
ByteBuddy
()
.
rebase
(
resolution
.
resolve
(),
ClassFileLocator
.
ForClassLoader
.
ofClassPath
());
/**
* alter class source code.<br/>
*
* new class need:<br/>
* 1.add field '_$EnhancedClassInstanceContext' of type
* EnhancedClassInstanceContext <br/>
*
* 2.intercept constructor by default, and intercept method which it's
* required by interceptorDefineClass. <br/>
*/
IAroundInterceptor
interceptor
=
instance
();
if
(
interceptor
==
null
)
{
throw
new
EnhanceException
(
"no IAroundInterceptor instance. "
);
}
newClassBuilder
=
newClassBuilder
.
defineField
(
contextAttrName
,
EnhancedClassInstanceContext
.
class
)
.
constructor
(
any
())
.
intercept
(
SuperMethodCall
.
INSTANCE
.
andThen
(
MethodDelegation
.
to
(
new
ClassConstructorInterceptor
(
interceptor
))
.
appendParameterBinder
(
FieldProxy
.
Binder
.
install
(
FieldGetter
.
class
,
FieldSetter
.
class
))));
MethodMatcher
[]
methodMatchers
=
getBeInterceptedMethodsMatchers
();
ClassMethodInterceptor
classMethodInterceptor
=
new
ClassMethodInterceptor
(
interceptor
);
StringBuilder
enhanceRules
=
new
StringBuilder
(
"\nprepare to enhance class ["
+
enhanceOriginClassName
+
"] as following rules:\n"
);
int
ruleIdx
=
1
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
enhanceRules
.
append
(
"\t"
+
ruleIdx
++
+
". "
+
methodMatcher
+
"\n"
);
}
logger
.
debug
(
enhanceRules
);
ElementMatcher
.
Junction
<
MethodDescription
>
matcher
=
null
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
logger
.
debug
(
"enhance class {} by rule: {}"
,
enhanceOriginClassName
,
methodMatcher
);
if
(
matcher
==
null
)
{
matcher
=
methodMatcher
.
buildMatcher
();
continue
;
}
matcher
=
matcher
.
or
(
methodMatcher
.
buildMatcher
());
}
/**
* exclude static methods.
*/
matcher
=
matcher
.
and
(
not
(
ElementMatchers
.
isStatic
()));
newClassBuilder
=
newClassBuilder
.
method
(
matcher
).
intercept
(
MethodDelegation
.
to
(
classMethodInterceptor
));
newClassBuilder
=
this
.
enhance
(
enhanceOriginClassName
,
newClassBuilder
);
/**
* naming class as origin class name, make and load class to
...
...
@@ -126,26 +60,13 @@ public abstract class InterceptorPluginDefine implements IPlugin {
logger
.
debug
(
"enhance class {} by {} completely."
,
enhanceOriginClassName
,
interceptorDefineClassName
);
}
protected
abstract
DynamicType
.
Builder
<?>
enhance
(
String
enhanceOriginClassName
,
DynamicType
.
Builder
<?>
newClassBuilder
)
throws
PluginException
;
/**
* 返回要被增强的类,应当返回类全名
*
* @return
*/
public
abstract
String
getBeInterceptedClassName
();
/**
* 返回需要被增强的方法列表
*
* @return
*/
public
abstract
MethodMatcher
[]
getBeInterceptedMethodsMatchers
();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
public
abstract
IAroundInterceptor
instance
();
protected
abstract
String
getBeInterceptedClassName
();
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/assist/FirstInvokeInterceptor.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/assist/
SimpleObject
FirstInvokeInterceptor.java
浏览文件 @
a49e19c7
...
...
@@ -3,8 +3,8 @@ package com.ai.cloud.skywalking.plugin.interceptor.assist;
import
java.util.concurrent.atomic.AtomicInteger
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorException
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
/**
* 用于首次拦截方法调用,避免方法内部的方法调用被多次拦截。
...
...
@@ -12,7 +12,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.InterceptorException;
* @author wusheng
*
*/
public
abstract
class
FirstInvokeInterceptor
implements
I
AroundInterceptor
{
public
abstract
class
SimpleObjectFirstInvokeInterceptor
implements
IntanceMethods
AroundInterceptor
{
protected
String
invokeCounterKey
=
"__$invokeCounterKey"
;
protected
Object
invokeCounterInstLock
=
new
Object
();
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/ClassConstructorInterceptor.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/
ClassConstructorInterceptor.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
import
net.bytebuddy.implementation.bind.annotation.AllArguments
;
import
net.bytebuddy.implementation.bind.annotation.FieldProxy
;
...
...
@@ -8,20 +8,22 @@ import net.bytebuddy.implementation.bind.annotation.This;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
public
class
ClassConstructorInterceptor
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
ClassConstructorInterceptor
.
class
);
private
IAroundInterceptor
interceptor
;
private
I
ntanceMethods
AroundInterceptor
interceptor
;
public
ClassConstructorInterceptor
(
IAroundInterceptor
interceptor
)
{
public
ClassConstructorInterceptor
(
I
ntanceMethods
AroundInterceptor
interceptor
)
{
this
.
interceptor
=
interceptor
;
}
@RuntimeType
public
void
intercept
(
@This
Object
obj
,
@FieldProxy
(
Interceptor
PluginDefine
.
contextAttrName
)
FieldSetter
accessor
,
@FieldProxy
(
ClassEnhance
PluginDefine
.
contextAttrName
)
FieldSetter
accessor
,
@AllArguments
Object
[]
allArguments
)
{
try
{
EnhancedClassInstanceContext
context
=
new
EnhancedClassInstanceContext
();
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
any
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
not
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.dynamic.DynamicType
;
import
net.bytebuddy.implementation.MethodDelegation
;
import
net.bytebuddy.implementation.SuperMethodCall
;
import
net.bytebuddy.implementation.bind.annotation.FieldProxy
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
net.bytebuddy.matcher.ElementMatchers
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
com.ai.cloud.skywalking.plugin.PluginException
;
import
com.ai.cloud.skywalking.plugin.interceptor.AbstractClassEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhanceException
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
public
abstract
class
ClassEnhancePluginDefine
extends
AbstractClassEnhancePluginDefine
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
ClassEnhancePluginDefine
.
class
);
public
static
final
String
contextAttrName
=
"_$EnhancedClassInstanceContext"
;
protected
DynamicType
.
Builder
<?>
enhance
(
String
enhanceOriginClassName
,
DynamicType
.
Builder
<?>
newClassBuilder
)
throws
PluginException
{
newClassBuilder
=
this
.
enhanceClass
(
enhanceOriginClassName
,
newClassBuilder
);
newClassBuilder
=
this
.
enhanceInstance
(
enhanceOriginClassName
,
newClassBuilder
);
return
newClassBuilder
;
}
private
DynamicType
.
Builder
<?>
enhanceInstance
(
String
enhanceOriginClassName
,
DynamicType
.
Builder
<?>
newClassBuilder
)
throws
PluginException
{
MethodMatcher
[]
methodMatchers
=
getInstanceMethodsMatchers
();
if
(
methodMatchers
==
null
){
return
newClassBuilder
;
}
/**
* alter class source code.<br/>
*
* new class need:<br/>
* 1.add field '_$EnhancedClassInstanceContext' of type
* EnhancedClassInstanceContext <br/>
*
* 2.intercept constructor by default, and intercept method which it's
* required by interceptorDefineClass. <br/>
*/
IntanceMethodsAroundInterceptor
interceptor
=
getInstanceMethodsInterceptor
();
if
(
interceptor
==
null
)
{
throw
new
EnhanceException
(
"no IntanceMethodsAroundInterceptor instance. "
);
}
newClassBuilder
=
newClassBuilder
.
defineField
(
contextAttrName
,
EnhancedClassInstanceContext
.
class
)
.
constructor
(
any
())
.
intercept
(
SuperMethodCall
.
INSTANCE
.
andThen
(
MethodDelegation
.
to
(
new
ClassConstructorInterceptor
(
interceptor
))
.
appendParameterBinder
(
FieldProxy
.
Binder
.
install
(
FieldGetter
.
class
,
FieldSetter
.
class
))));
ClassInstanceMethodsInterceptor
classMethodInterceptor
=
new
ClassInstanceMethodsInterceptor
(
interceptor
);
StringBuilder
enhanceRules
=
new
StringBuilder
(
"\nprepare to enhance class ["
+
enhanceOriginClassName
+
"] instance methods as following rules:\n"
);
int
ruleIdx
=
1
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
enhanceRules
.
append
(
"\t"
+
ruleIdx
++
+
". "
+
methodMatcher
+
"\n"
);
}
logger
.
debug
(
enhanceRules
);
ElementMatcher
.
Junction
<
MethodDescription
>
matcher
=
null
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
logger
.
debug
(
"enhance class {} instance methods by rule: {}"
,
enhanceOriginClassName
,
methodMatcher
);
if
(
matcher
==
null
)
{
matcher
=
methodMatcher
.
buildMatcher
();
continue
;
}
matcher
=
matcher
.
or
(
methodMatcher
.
buildMatcher
());
}
/**
* exclude static methods.
*/
matcher
=
matcher
.
and
(
not
(
ElementMatchers
.
isStatic
()));
newClassBuilder
=
newClassBuilder
.
method
(
matcher
).
intercept
(
MethodDelegation
.
to
(
classMethodInterceptor
));
return
newClassBuilder
;
}
/**
* 返回需要被增强的方法列表
*
* @return
*/
protected
abstract
MethodMatcher
[]
getInstanceMethodsMatchers
();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
protected
abstract
IntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
();
private
DynamicType
.
Builder
<?>
enhanceClass
(
String
enhanceOriginClassName
,
DynamicType
.
Builder
<?>
newClassBuilder
)
throws
PluginException
{
MethodMatcher
[]
methodMatchers
=
getStaticMethodsMatchers
();
if
(
methodMatchers
==
null
){
return
newClassBuilder
;
}
StaticMethodsAroundInterceptor
interceptor
=
getStaticMethodsInterceptor
();
if
(
interceptor
==
null
)
{
throw
new
EnhanceException
(
"no StaticMethodsAroundInterceptor instance. "
);
}
ClassStaticMethodsInterceptor
classMethodInterceptor
=
new
ClassStaticMethodsInterceptor
(
interceptor
);
StringBuilder
enhanceRules
=
new
StringBuilder
(
"\nprepare to enhance class ["
+
enhanceOriginClassName
+
"] static methods as following rules:\n"
);
int
ruleIdx
=
1
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
enhanceRules
.
append
(
"\t"
+
ruleIdx
++
+
". "
+
methodMatcher
+
"\n"
);
}
logger
.
debug
(
enhanceRules
);
ElementMatcher
.
Junction
<
MethodDescription
>
matcher
=
null
;
for
(
MethodMatcher
methodMatcher
:
methodMatchers
)
{
logger
.
debug
(
"enhance class {} static methods by rule: {}"
,
enhanceOriginClassName
,
methodMatcher
);
if
(
matcher
==
null
)
{
matcher
=
methodMatcher
.
buildMatcher
();
continue
;
}
matcher
=
matcher
.
or
(
methodMatcher
.
buildMatcher
());
}
/**
* restrict static methods.
*/
matcher
=
matcher
.
and
(
ElementMatchers
.
isStatic
());
newClassBuilder
=
newClassBuilder
.
method
(
matcher
).
intercept
(
MethodDelegation
.
to
(
classMethodInterceptor
));
return
newClassBuilder
;
}
/**
* 返回需要被增强的方法列表
*
* @return
*/
protected
abstract
MethodMatcher
[]
getStaticMethodsMatchers
();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
protected
abstract
StaticMethodsAroundInterceptor
getStaticMethodsInterceptor
();
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
/**
* 仅增强拦截实例方法
*
* @author wusheng
*
*/
public
abstract
class
ClassInstanceMethodsEnhancePluginDefine
extends
ClassEnhancePluginDefine
{
@Override
protected
MethodMatcher
[]
getStaticMethodsMatchers
()
{
return
null
;
}
@Override
protected
StaticMethodsAroundInterceptor
getStaticMethodsInterceptor
()
{
return
null
;
}
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
ClassMethod
Interceptor.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/ClassInstanceMethods
Interceptor.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
import
java.lang.reflect.Method
;
import
java.util.concurrent.Callable
;
...
...
@@ -13,19 +13,21 @@ import net.bytebuddy.implementation.bind.annotation.This;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
/**
* 类方法拦截、控制器
*
* @author wusheng
*
*/
public
class
Class
Method
Interceptor
{
public
class
Class
InstanceMethods
Interceptor
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
Class
Method
Interceptor
.
class
);
.
getLogger
(
Class
InstanceMethods
Interceptor
.
class
);
private
IAroundInterceptor
interceptor
;
private
I
ntanceMethods
AroundInterceptor
interceptor
;
public
Class
MethodInterceptor
(
I
AroundInterceptor
interceptor
)
{
public
Class
InstanceMethodsInterceptor
(
IntanceMethods
AroundInterceptor
interceptor
)
{
this
.
interceptor
=
interceptor
;
}
...
...
@@ -35,9 +37,9 @@ public class ClassMethodInterceptor {
@AllArguments
Object
[]
allArguments
,
@Origin
Method
method
,
@SuperCall
Callable
<?>
zuper
,
@FieldValue
(
Interceptor
PluginDefine
.
contextAttrName
)
EnhancedClassInstanceContext
instanceContext
)
@FieldValue
(
ClassEnhance
PluginDefine
.
contextAttrName
)
EnhancedClassInstanceContext
instanceContext
)
throws
Exception
{
MethodInvokeContext
interceptorContext
=
new
MethodInvokeContext
(
obj
,
InstanceMethodInvokeContext
interceptorContext
=
new
Instance
MethodInvokeContext
(
obj
,
method
.
getName
(),
allArguments
);
try
{
interceptor
.
beforeMethod
(
instanceContext
,
interceptorContext
);
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
/**
* 仅增强拦截类级别静态方法
*
* @author wusheng
*
*/
public
abstract
class
ClassStaticMethodsEnhancePluginDefine
extends
ClassEnhancePluginDefine
{
@Override
protected
MethodMatcher
[]
getInstanceMethodsMatchers
()
{
return
null
;
}
@Override
protected
IntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
null
;
}
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
import
java.lang.reflect.Method
;
import
java.util.concurrent.Callable
;
import
net.bytebuddy.implementation.bind.annotation.AllArguments
;
import
net.bytebuddy.implementation.bind.annotation.Origin
;
import
net.bytebuddy.implementation.bind.annotation.RuntimeType
;
import
net.bytebuddy.implementation.bind.annotation.SuperCall
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
/**
* 类静态方法拦截、控制器
*
* @author wusheng
*
*/
public
class
ClassStaticMethodsInterceptor
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
ClassStaticMethodsInterceptor
.
class
);
private
StaticMethodsAroundInterceptor
interceptor
;
public
ClassStaticMethodsInterceptor
(
StaticMethodsAroundInterceptor
interceptor
)
{
this
.
interceptor
=
interceptor
;
}
@RuntimeType
public
Object
intercept
(
@Origin
Class
<?>
clazz
,
@AllArguments
Object
[]
allArguments
,
@Origin
Method
method
,
@SuperCall
Callable
<?>
zuper
)
throws
Exception
{
MethodInvokeContext
interceptorContext
=
new
MethodInvokeContext
(
method
.
getName
(),
allArguments
);
try
{
interceptor
.
beforeMethod
(
interceptorContext
);
}
catch
(
Throwable
t
)
{
logger
.
error
(
"class[{}] before static method[{}] intercept failue:{}"
,
clazz
,
method
.
getName
(),
t
.
getMessage
(),
t
);
}
Object
ret
=
null
;
try
{
ret
=
zuper
.
call
();
}
catch
(
Throwable
t
)
{
try
{
interceptor
.
handleMethodException
(
t
,
interceptorContext
,
ret
);
}
catch
(
Throwable
t2
)
{
logger
.
error
(
"class[{}] handle static method[{}] exception failue:{}"
,
clazz
,
method
.
getName
(),
t2
.
getMessage
(),
t2
);
}
throw
t
;
}
finally
{
try
{
ret
=
interceptor
.
afterMethod
(
interceptorContext
,
ret
);
}
catch
(
Throwable
t
)
{
logger
.
error
(
"class[{}] after static method[{}] intercept failue:{}"
,
clazz
,
method
.
getName
(),
t
.
getMessage
(),
t
);
}
}
return
ret
;
}
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/ConstructorInvokeContext.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/
ConstructorInvokeContext.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
public
class
ConstructorInvokeContext
{
/**
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/FieldGetter.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/
FieldGetter.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
public
interface
FieldGetter
{
Object
getValue
();
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/FieldSetter.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/
FieldSetter.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
public
interface
FieldSetter
{
void
setValue
(
Object
value
);
...
...
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/InstanceMethodInvokeContext.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
public
class
InstanceMethodInvokeContext
extends
MethodInvokeContext
{
/**
* 代理类实例
*/
private
Object
objInst
;
InstanceMethodInvokeContext
(
Object
objInst
,
String
methodName
,
Object
[]
allArguments
)
{
super
(
methodName
,
allArguments
);
this
.
objInst
=
objInst
;
}
public
Object
inst
(){
return
objInst
;
}
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
I
AroundInterceptor.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/IntanceMethods
AroundInterceptor.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
public
interface
IAroundInterceptor
{
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
public
interface
IntanceMethodsAroundInterceptor
{
public
void
onConstruct
(
EnhancedClassInstanceContext
context
,
ConstructorInvokeContext
interceptorContext
);
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
);
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
);
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
);
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
);
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
);
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
);
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodInvokeContext.java
→
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/
enhance/
MethodInvokeContext.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor
;
package
com.ai.cloud.skywalking.plugin.interceptor
.enhance
;
/**
* 方法执行拦截上下文
...
...
@@ -7,10 +7,6 @@ package com.ai.cloud.skywalking.plugin.interceptor;
*
*/
public
class
MethodInvokeContext
{
/**
* 代理类实例
*/
private
Object
objInst
;
/**
* 方法名称
*/
...
...
@@ -20,8 +16,7 @@ public class MethodInvokeContext {
*/
private
Object
[]
allArguments
;
MethodInvokeContext
(
Object
objInst
,
String
methodName
,
Object
[]
allArguments
)
{
this
.
objInst
=
objInst
;
MethodInvokeContext
(
String
methodName
,
Object
[]
allArguments
)
{
this
.
methodName
=
methodName
;
this
.
allArguments
=
allArguments
;
}
...
...
@@ -33,8 +28,4 @@ public class MethodInvokeContext {
public
String
methodName
(){
return
methodName
;
}
public
Object
inst
(){
return
objInst
;
}
}
skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/StaticMethodsAroundInterceptor.java
0 → 100644
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin.interceptor.enhance
;
public
interface
StaticMethodsAroundInterceptor
{
public
void
beforeMethod
(
MethodInvokeContext
interceptorContext
);
public
Object
afterMethod
(
MethodInvokeContext
interceptorContext
,
Object
ret
);
public
void
handleMethodException
(
Throwable
t
,
MethodInvokeContext
interceptorContext
,
Object
ret
);
}
skywalking-api/src/test/java/test/ai/cloud/matcher/TestAroundInterceptor.java
浏览文件 @
a49e19c7
package
test.ai.cloud.matcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
/**
* Created by xin on 16-6-8.
*/
public
class
TestAroundInterceptor
implements
IAroundInterceptor
{
public
class
TestAroundInterceptor
implements
I
ntanceMethods
AroundInterceptor
{
@Override
public
void
onConstruct
(
EnhancedClassInstanceContext
context
,
ConstructorInvokeContext
interceptorContext
)
{
}
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
)
{
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
)
{
System
.
out
.
println
(
"before method"
);
}
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
System
.
out
.
println
(
"after method"
);
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
}
}
skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java
浏览文件 @
a49e19c7
package
test.ai.cloud.matcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher
;
/**
* Created by xin on 16-6-8.
*/
public
class
TestMatcherDefine
extends
Interceptor
PluginDefine
{
public
class
TestMatcherDefine
extends
ClassInstanceMethodsEnhance
PluginDefine
{
@Override
public
String
getBeInterceptedClassName
()
{
return
"test.ai.cloud.matcher.TestMatcherClass"
;
}
@Override
p
ublic
MethodMatcher
[]
getBeIntercepted
MethodsMatchers
()
{
return
new
MethodMatcher
[]{
new
PrivateMethodMatcher
(),
new
MethodsExclusiveMatcher
(
new
SimpleMethodMatcher
(
"set"
)),
new
SimpleMethodMatcher
(
MethodMatcher
.
Modifier
.
Private
,
"set"
,
1
)
};
//
return new MethodMatcher[] { new SimpleMethodMatcher(Modifier.Public, "printabc", new Class[]{String.class, String.class}) };
//
return new MethodMatcher[] { new PrivateMethodMatcher()};
p
rotected
MethodMatcher
[]
getInstance
MethodsMatchers
()
{
//
return new MethodMatcher[]{
//
new PrivateMethodMatcher(),
//
new MethodsExclusiveMatcher(new SimpleMethodMatcher("set")),
//
new SimpleMethodMatcher(MethodMatcher.Modifier.Private, "set", 1)
//
};
//
return new MethodMatcher[] { new SimpleMethodMatcher(Modifier.Public, "printabc", new Class[]{String.class, String.class}) };
return
new
MethodMatcher
[]
{
new
PrivateMethodMatcher
()};
//return new MethodMatcher[]{new AnyMethodsMatcher()};
//return new MethodMatcher[]{new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get"))};
}
@Override
p
ublic
IAroundInterceptor
instance
()
{
p
rotected
IntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
new
TestAroundInterceptor
();
}
}
skywalking-api/src/test/java/test/ai/cloud/plugin/BeInterceptedClass.java
浏览文件 @
a49e19c7
...
...
@@ -8,4 +8,8 @@ public class BeInterceptedClass {
public
void
printabc
(){
System
.
out
.
println
(
"printabc"
);
}
public
static
void
call
(){
System
.
out
.
println
(
"static call"
);
}
}
skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java
浏览文件 @
a49e19c7
...
...
@@ -22,5 +22,7 @@ public class PluginMainTest {
inst
.
printabc
();
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
end
-
start
+
"ms"
);
BeInterceptedClass
.
call
();
}
}
skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java
浏览文件 @
a49e19c7
package
test.ai.cloud.plugin
;
import
com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
public
class
TestAroundInterceptor
implements
IAroundInterceptor
{
public
class
TestAroundInterceptor
implements
I
ntanceMethods
AroundInterceptor
{
@Override
public
void
onConstruct
(
EnhancedClassInstanceContext
context
,
ConstructorInvokeContext
interceptorContext
)
{
...
...
@@ -14,19 +14,19 @@ public class TestAroundInterceptor implements IAroundInterceptor {
}
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
)
{
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
)
{
System
.
out
.
println
(
"beforeMethod : "
+
context
.
get
(
"test.key"
,
String
.
class
));
}
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
System
.
out
.
println
(
"afterMethod: "
+
context
.
get
(
"test.key"
,
String
.
class
));
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
// TODO Auto-generated method stub
}
...
...
skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java
浏览文件 @
a49e19c7
package
test.ai.cloud.plugin
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher
;
public
class
TestInterceptorDefine
extends
Interceptor
PluginDefine
{
public
class
TestInterceptorDefine
extends
ClassEnhance
PluginDefine
{
@Override
public
String
getBeInterceptedClassName
()
{
...
...
@@ -13,13 +14,23 @@ public class TestInterceptorDefine extends InterceptorPluginDefine {
}
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]
{
new
SimpleMethodMatcher
(
"printabc"
)
};
}
@Override
public
I
AroundInterceptor
instance
()
{
public
I
ntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
new
TestAroundInterceptor
();
}
@Override
protected
MethodMatcher
[]
getStaticMethodsMatchers
()
{
return
new
MethodMatcher
[]
{
new
SimpleMethodMatcher
(
"call"
)
};
}
@Override
protected
StaticMethodsAroundInterceptor
getStaticMethodsInterceptor
()
{
return
new
TestStaticAroundInterceptor
();
}
}
skywalking-api/src/test/java/test/ai/cloud/plugin/TestStaticAroundInterceptor.java
0 → 100644
浏览文件 @
a49e19c7
package
test.ai.cloud.plugin
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.MethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
public
class
TestStaticAroundInterceptor
implements
StaticMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
MethodInvokeContext
interceptorContext
)
{
System
.
out
.
println
(
"beforeMethod : static"
);
}
@Override
public
Object
afterMethod
(
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
System
.
out
.
println
(
"afterMethod: static"
);
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
// TODO Auto-generated method stub
}
}
skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/ai/cloud/skywalking/plugin/DubboPluginDefine.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.plugin
;
import
com.ai.cloud.skywalking.conf.AuthDesc
;
import
com.ai.cloud.skywalking.plugin.boot.BootException
;
import
com.ai.cloud.skywalking.plugin.boot.BootPluginDefine
;
import
com.alibaba.dubbo.rpc.Invoker
;
import
net.bytebuddy.ByteBuddy
;
import
net.bytebuddy.dynamic.ClassFileLocator
;
import
net.bytebuddy.dynamic.DynamicType
;
import
net.bytebuddy.dynamic.loading.ClassLoadingStrategy
;
import
net.bytebuddy.implementation.MethodDelegation
;
import
net.bytebuddy.pool.TypePool
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher
;
import
static
com
.
ai
.
cloud
.
skywalking
.
plugin
.
PluginBootstrap
.
CLASS_TYPE_POOL
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
takesArguments
;
public
class
DubboPluginDefine
extends
ClassStaticMethodsEnhancePluginDefine
{
@Override
protected
MethodMatcher
[]
getStaticMethodsMatchers
()
{
return
new
MethodMatcher
[]
{
new
SimpleMethodMatcher
(
"buildInvokerChain"
)
};
}
public
class
DubboPluginDefine
extends
BootPluginDefine
{
@Override
protected
StaticMethodsAroundInterceptor
getStaticMethodsInterceptor
()
{
// TODO Auto-generated method stub
return
null
;
}
private
static
final
String
interceptorClassName
=
"com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper"
;
@Override
protected
void
boot
()
throws
BootException
{
if
(!
AuthDesc
.
isAuth
())
{
return
;
}
TypePool
.
Resolution
resolution
=
CLASS_TYPE_POOL
.
describe
(
interceptorClassName
);
DynamicType
.
Builder
<?>
newClassBuilder
=
new
ByteBuddy
()
.
rebase
(
resolution
.
resolve
(),
ClassFileLocator
.
ForClassLoader
.
ofClassPath
());
newClassBuilder
=
newClassBuilder
.
method
(
named
(
"buildInvokerChain"
)
.
and
(
takesArguments
(
Invoker
.
class
,
String
.
class
,
String
.
class
)))
.
intercept
(
MethodDelegation
.
to
(
new
DubboFilterBuildInterceptor
()));
newClassBuilder
.
name
(
interceptorClassName
)
.
make
()
.
load
(
ClassLoader
.
getSystemClassLoader
(),
ClassLoadingStrategy
.
Default
.
INJECTION
).
getLoaded
();
}
@Override
protected
String
getBeInterceptedClassName
()
{
return
"com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper"
;
}
}
skywalking-sdk-plugin/httpClient-4.x-plugin-dubbox-rest-attachment/src/main/java/org/skywalking/httpClient/v4/plugin/dubbox/rest/attachment/DubboxRestHeadSetterAttachment.java
浏览文件 @
a49e19c7
...
...
@@ -2,11 +2,11 @@ package org.skywalking.httpClient.v4.plugin.dubbox.rest.attachment;
import
org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
public
class
DubboxRestHeadSetterAttachment
extends
Interceptor
PluginDefine
{
public
class
DubboxRestHeadSetterAttachment
extends
ClassInstanceMethodsEnhance
PluginDefine
{
/**
* this method is called as InterceptorPluginDefine<br/>
...
...
@@ -20,12 +20,12 @@ public class DubboxRestHeadSetterAttachment extends InterceptorPluginDefine {
}
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
null
;
}
@Override
public
I
AroundInterceptor
instance
()
{
public
I
ntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
null
;
}
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/HttpClientExecuteInterceptor.java
浏览文件 @
a49e19c7
...
...
@@ -5,12 +5,12 @@ import org.apache.http.HttpRequest;
import
com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender
;
import
com.ai.cloud.skywalking.model.Identification
;
import
com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
public
class
HttpClientExecuteInterceptor
implements
IAroundInterceptor
{
public
class
HttpClientExecuteInterceptor
implements
I
ntanceMethods
AroundInterceptor
{
/**
* default headname of sky walking context<br/>
*/
...
...
@@ -25,7 +25,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
)
{
Instance
MethodInvokeContext
interceptorContext
)
{
Object
[]
allArguments
=
interceptorContext
.
allArguments
();
if
(
allArguments
[
0
]
==
null
||
allArguments
[
1
]
==
null
)
{
// illegal args, can't trace. ignore.
...
...
@@ -51,7 +51,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
Object
[]
allArguments
=
interceptorContext
.
allArguments
();
if
(
allArguments
[
0
]
==
null
||
allArguments
[
1
]
==
null
)
{
// illegal args, can't trace. ignore.
...
...
@@ -64,7 +64,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
Object
[]
allArguments
=
interceptorContext
.
allArguments
();
if
(
allArguments
[
0
]
==
null
||
allArguments
[
1
]
==
null
)
{
// illegal args, can't trace. ignore.
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/AbstractHttpClientPluginDefine.java
浏览文件 @
a49e19c7
...
...
@@ -17,7 +17,7 @@ public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
*
*/
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]
{
new
SimpleMethodMatcher
(
"doExecute"
)};
}
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/DefaultRequestDirectorPluginDefine.java
浏览文件 @
a49e19c7
...
...
@@ -15,7 +15,7 @@ public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
}
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]
{
new
SimpleMethodMatcher
(
"execute"
)};
}
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/HttpClientPluginDefine.java
浏览文件 @
a49e19c7
...
...
@@ -2,13 +2,13 @@ package org.skywalking.httpClient.v4.plugin.define;
import
org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.
IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.
InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.
enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.
enhance.IntanceMethodsAroundInterceptor
;
public
abstract
class
HttpClientPluginDefine
extends
Interceptor
PluginDefine
{
public
abstract
class
HttpClientPluginDefine
extends
ClassInstanceMethodsEnhance
PluginDefine
{
@Override
public
I
AroundInterceptor
instance
()
{
public
I
ntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
new
HttpClientExecuteInterceptor
();
}
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/InternalHttpClientPluginDefine.java
浏览文件 @
a49e19c7
...
...
@@ -5,7 +5,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public
class
InternalHttpClientPluginDefine
extends
HttpClientPluginDefine
{
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]{
new
SimpleMethodMatcher
(
"doExecute"
)};
}
...
...
skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/skywalking/httpClient/v4/plugin/define/MinimalHttpClientPluginDefine.java
浏览文件 @
a49e19c7
...
...
@@ -5,7 +5,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public
class
MinimalHttpClientPluginDefine
extends
HttpClientPluginDefine
{
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]{
new
SimpleMethodMatcher
(
"doExecute"
)};
}
...
...
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisBaseInterceptor.java
浏览文件 @
a49e19c7
...
...
@@ -3,16 +3,16 @@ package com.ai.cloud.skywalking.jedis.v2.plugin;
import
com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender
;
import
com.ai.cloud.skywalking.model.Identification
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.
MethodInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.
assist.FirstInvokeInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.
assist.SimpleObjectFirstInvokeInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.
enhance.InstanceMethodInvokeContext
;
public
abstract
class
JedisBaseInterceptor
extends
FirstInvokeInterceptor
{
public
abstract
class
JedisBaseInterceptor
extends
SimpleObject
FirstInvokeInterceptor
{
protected
static
final
String
REDIS_CONN_INFO_KEY
=
"redisClusterConnInfo"
;
private
static
RPCBuriedPointSender
sender
=
new
RPCBuriedPointSender
();
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
)
{
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
)
{
if
(
this
.
isFirstBeforeMethod
(
context
))
{
/**
* redis server wouldn't process rpc context. ignore the
...
...
@@ -34,7 +34,7 @@ public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
}
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
if
(
this
.
isLastAfterMethod
(
context
))
{
sender
.
afterSend
();
}
...
...
@@ -42,7 +42,7 @@ public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
Instance
MethodInvokeContext
interceptorContext
,
Object
ret
)
{
sender
.
handleException
(
t
);
}
}
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisClusterInterceptor.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.jedis.v2.plugin
;
import
com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext
;
import
redis.clients.jedis.HostAndPort
;
...
...
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/JedisInterceptor.java
浏览文件 @
a49e19c7
...
...
@@ -4,8 +4,8 @@ import java.net.URI;
import
redis.clients.jedis.JedisShardInfo
;
import
com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext
;
public
class
JedisInterceptor
extends
JedisBaseInterceptor
{
...
...
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/define/JedisClusterPluginDefine.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.jedis.v2.plugin.define
;
import
com.ai.cloud.skywalking.jedis.v2.plugin.JedisClusterInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodsMatcher
;
public
class
JedisClusterPluginDefine
extends
Interceptor
PluginDefine
{
public
class
JedisClusterPluginDefine
extends
ClassInstanceMethodsEnhance
PluginDefine
{
@Override
public
String
getBeInterceptedClassName
()
{
...
...
@@ -14,14 +14,14 @@ public class JedisClusterPluginDefine extends InterceptorPluginDefine {
}
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]{
new
AnyMethodsMatcher
()
};
}
@Override
public
I
AroundInterceptor
instance
()
{
public
I
ntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
new
JedisClusterInterceptor
();
}
}
skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/ai/cloud/skywalking/jedis/v2/plugin/define/JedisPluginDefine.java
浏览文件 @
a49e19c7
package
com.ai.cloud.skywalking.jedis.v2.plugin.define
;
import
com.ai.cloud.skywalking.jedis.v2.plugin.JedisInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher
;
import
com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher
;
public
class
JedisPluginDefine
extends
Interceptor
PluginDefine
{
public
class
JedisPluginDefine
extends
ClassInstanceMethodsEnhance
PluginDefine
{
@Override
public
String
getBeInterceptedClassName
()
{
...
...
@@ -16,7 +16,7 @@ public class JedisPluginDefine extends InterceptorPluginDefine {
}
@Override
public
MethodMatcher
[]
get
BeIntercepted
MethodsMatchers
()
{
public
MethodMatcher
[]
get
Instance
MethodsMatchers
()
{
return
new
MethodMatcher
[]{
new
MethodsExclusiveMatcher
(
new
PrivateMethodMatcher
(),
...
...
@@ -32,7 +32,7 @@ public class JedisPluginDefine extends InterceptorPluginDefine {
}
@Override
public
I
AroundInterceptor
instance
()
{
public
I
ntanceMethodsAroundInterceptor
getInstanceMethodsInterceptor
()
{
return
new
JedisInterceptor
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录