Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿信在这里
SkyWalking
提交
3b30935e
S
SkyWalking
项目概览
阿信在这里
/
SkyWalking
与 Fork 源项目一致
Fork自
山不在高_有仙则灵 / SkyWalking
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyWalking
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3b30935e
编写于
4月 20, 2017
作者:
wu-sheng
提交者:
GitHub
4月 20, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #148 from bai-yang/master
Support plugin for MongoDB
上级
f62110d4
78c330a8
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
818 addition
and
0 deletion
+818
-0
skywalking-sniffer/skywalking-agent/pom.xml
skywalking-sniffer/skywalking-agent/pom.xml
+5
-0
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java
...i/src/main/java/com/a/eye/skywalking/api/conf/Config.java
+10
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml
...-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml
+47
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java
...kywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java
+190
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java
...king/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java
+49
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteBindingInterceptor.java
...ing/plugin/mongodb/v3/MongoDBWriteBindingInterceptor.java
+44
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/define/MongoDBInstrumentation.java
...king/plugin/mongodb/v3/define/MongoDBInstrumentation.java
+76
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def
...ngodb-3.x-plugin/src/main/resources/skywalking-plugin.def
+1
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
...lking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
+131
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
.../plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
+61
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
...plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
+61
-0
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
.../plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
+142
-0
skywalking-sniffer/skywalking-sdk-plugin/pom.xml
skywalking-sniffer/skywalking-sdk-plugin/pom.xml
+1
-0
未找到文件。
skywalking-sniffer/skywalking-agent/pom.xml
浏览文件 @
3b30935e
...
@@ -55,6 +55,11 @@
...
@@ -55,6 +55,11 @@
<artifactId>
motan-plugin
</artifactId>
<artifactId>
motan-plugin
</artifactId>
<version>
${project.version}
</version>
<version>
${project.version}
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.a.eye
</groupId>
<artifactId>
skywalking-mongodb-3.x-plugin
</artifactId>
<version>
${project.version}
</version>
</dependency>
<!-- activation -->
<!-- activation -->
<dependency>
<dependency>
...
...
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java
浏览文件 @
3b30935e
...
@@ -81,4 +81,14 @@ public class Config {
...
@@ -81,4 +81,14 @@ public class Config {
*/
*/
public
static
LogLevel
LEVEL
=
LogLevel
.
DEBUG
;
public
static
LogLevel
LEVEL
=
LogLevel
.
DEBUG
;
}
}
public
static
class
Plugin
{
public
static
class
MongoDB
{
/**
* If true, trace all the parameters, default is false.
* Only trace the operation, not include parameters.
*/
public
static
boolean
TRACE_PARAM
=
false
;
}
}
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml
0 → 100644
浏览文件 @
3b30935e
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<artifactId>
skywalking-sdk-plugin
</artifactId>
<groupId>
com.a.eye
</groupId>
<version>
3.0.1-2017
</version>
</parent>
<artifactId>
skywalking-mongodb-3.x-plugin
</artifactId>
<packaging>
jar
</packaging>
<name>
mongodb-plugin
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
org.mongodb
</groupId>
<artifactId>
mongo-java-driver
</artifactId>
<version>
3.4.2
</version>
<scope>
provided
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 源码插件 -->
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-source-plugin
</artifactId>
<!-- 发布时自动将源码同时发布的配置 -->
<executions>
<execution>
<id>
attach-sources
</id>
<goals>
<goal>
jar
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
java.util.List
;
import
org.bson.BsonDocument
;
import
com.a.eye.skywalking.api.conf.Config
;
import
com.a.eye.skywalking.api.context.ContextManager
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult
;
import
com.a.eye.skywalking.trace.Span
;
import
com.a.eye.skywalking.trace.tag.Tags
;
import
com.mongodb.ReadPreference
;
import
com.mongodb.bulk.DeleteRequest
;
import
com.mongodb.bulk.InsertRequest
;
import
com.mongodb.bulk.UpdateRequest
;
import
com.mongodb.bulk.WriteRequest
;
import
com.mongodb.operation.CountOperation
;
import
com.mongodb.operation.CreateCollectionOperation
;
import
com.mongodb.operation.CreateIndexesOperation
;
import
com.mongodb.operation.CreateViewOperation
;
import
com.mongodb.operation.DeleteOperation
;
import
com.mongodb.operation.DistinctOperation
;
import
com.mongodb.operation.FindAndDeleteOperation
;
import
com.mongodb.operation.FindAndReplaceOperation
;
import
com.mongodb.operation.FindAndUpdateOperation
;
import
com.mongodb.operation.FindOperation
;
import
com.mongodb.operation.GroupOperation
;
import
com.mongodb.operation.InsertOperation
;
import
com.mongodb.operation.ListCollectionsOperation
;
import
com.mongodb.operation.MapReduceToCollectionOperation
;
import
com.mongodb.operation.MapReduceWithInlineResultsOperation
;
import
com.mongodb.operation.MixedBulkWriteOperation
;
import
com.mongodb.operation.ReadOperation
;
import
com.mongodb.operation.UpdateOperation
;
import
com.mongodb.operation.WriteOperation
;
/**
* {@link MongoDBMethodInterceptor} intercept method of {@link com.mongodb.Mongo#execute(ReadOperation, ReadPreference)}
* or {@link com.mongodb.Mongo#execute(WriteOperation)}. record the mongoDB host, operation name and the key of the
* operation.
*
* @author baiyang
*/
public
class
MongoDBMethodInterceptor
implements
InstanceMethodsAroundInterceptor
{
/**
* The key name that MongoDB host in {@link EnhancedClassInstanceContext#context}.
*/
static
final
String
MONGODB_HOST
=
"MONGODB_HOST"
;
/**
* The key name that MongoDB port in {@link EnhancedClassInstanceContext#context}.
*/
static
final
String
MONGODB_PORT
=
"MONGODB_PORT"
;
private
static
final
String
MONGODB_COMPONENT
=
"MongoDB"
;
private
static
final
String
METHOD
=
"MongoDB/"
;
private
static
final
int
FILTER_LENGTH_LIMIT
=
256
;
private
static
final
String
EMPTY
=
""
;
@Override
public
void
beforeMethod
(
final
EnhancedClassInstanceContext
context
,
final
InstanceMethodInvokeContext
interceptorContext
,
final
MethodInterceptResult
result
)
{
Object
[]
arguments
=
interceptorContext
.
allArguments
();
String
methodName
=
arguments
[
0
].
getClass
().
getSimpleName
();
Span
span
=
ContextManager
.
createSpan
(
METHOD
+
methodName
);
Tags
.
COMPONENT
.
set
(
span
,
MONGODB_COMPONENT
);
Tags
.
DB_TYPE
.
set
(
span
,
MONGODB_COMPONENT
);
Tags
.
SPAN_KIND
.
set
(
span
,
Tags
.
SPAN_KIND_CLIENT
);
Tags
.
SPAN_LAYER
.
asDB
(
span
);
if
(
Config
.
Plugin
.
MongoDB
.
TRACE_PARAM
)
{
Tags
.
DB_STATEMENT
.
set
(
span
,
methodName
+
" "
+
this
.
getTraceParam
(
arguments
[
0
]));
}
}
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
,
Object
ret
)
{
Span
span
=
ContextManager
.
activeSpan
();
Tags
.
PEER_HOST
.
set
(
span
,
context
.
get
(
MONGODB_HOST
,
String
.
class
));
Tags
.
PEER_PORT
.
set
(
span
,
(
Integer
)
context
.
get
(
MONGODB_PORT
));
ContextManager
.
stopSpan
();
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
)
{
ContextManager
.
activeSpan
().
log
(
t
);
}
/**
* Convert ReadOperation interface or WriteOperation interface to the implementation class. Get the method name and
* filter info.
*/
@SuppressWarnings
(
"rawtypes"
)
private
String
getTraceParam
(
Object
obj
)
{
if
(
obj
instanceof
CountOperation
)
{
BsonDocument
filter
=
((
CountOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
DistinctOperation
)
{
BsonDocument
filter
=
((
DistinctOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
FindOperation
)
{
BsonDocument
filter
=
((
FindOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
GroupOperation
)
{
BsonDocument
filter
=
((
GroupOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
ListCollectionsOperation
)
{
BsonDocument
filter
=
((
ListCollectionsOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
MapReduceWithInlineResultsOperation
)
{
BsonDocument
filter
=
((
ListCollectionsOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
DeleteOperation
)
{
List
<
DeleteRequest
>
writeRequestList
=
((
DeleteOperation
)
obj
).
getDeleteRequests
();
return
getFilter
(
writeRequestList
);
}
else
if
(
obj
instanceof
InsertOperation
)
{
List
<
InsertRequest
>
writeRequestList
=
((
InsertOperation
)
obj
).
getInsertRequests
();
return
getFilter
(
writeRequestList
);
}
else
if
(
obj
instanceof
UpdateOperation
)
{
List
<
UpdateRequest
>
writeRequestList
=
((
UpdateOperation
)
obj
).
getUpdateRequests
();
return
getFilter
(
writeRequestList
);
}
else
if
(
obj
instanceof
CreateCollectionOperation
)
{
String
filter
=
((
CreateCollectionOperation
)
obj
).
getCollectionName
();
return
limitFilter
(
filter
);
}
else
if
(
obj
instanceof
CreateIndexesOperation
)
{
List
<
String
>
filter
=
((
CreateIndexesOperation
)
obj
).
getIndexNames
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
CreateViewOperation
)
{
String
filter
=
((
CreateViewOperation
)
obj
).
getViewName
();
return
limitFilter
(
filter
);
}
else
if
(
obj
instanceof
FindAndDeleteOperation
)
{
BsonDocument
filter
=
((
FindAndDeleteOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
FindAndReplaceOperation
)
{
BsonDocument
filter
=
((
FindAndReplaceOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
FindAndUpdateOperation
)
{
BsonDocument
filter
=
((
FindAndUpdateOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
MapReduceToCollectionOperation
)
{
BsonDocument
filter
=
((
MapReduceToCollectionOperation
)
obj
).
getFilter
();
return
limitFilter
(
filter
.
toString
());
}
else
if
(
obj
instanceof
MixedBulkWriteOperation
)
{
List
<?
extends
WriteRequest
>
writeRequestList
=
((
MixedBulkWriteOperation
)
obj
).
getWriteRequests
();
return
getFilter
(
writeRequestList
);
}
else
{
return
EMPTY
;
}
}
private
String
getFilter
(
List
<?
extends
WriteRequest
>
writeRequestList
)
{
StringBuilder
params
=
new
StringBuilder
();
for
(
WriteRequest
request
:
writeRequestList
)
{
if
(
request
instanceof
InsertRequest
)
{
params
.
append
(((
InsertRequest
)
request
).
getDocument
().
toString
()).
append
(
","
);
}
else
if
(
request
instanceof
DeleteRequest
)
{
params
.
append
(((
DeleteRequest
)
request
).
getFilter
()).
append
(
","
);
}
else
if
(
request
instanceof
UpdateRequest
)
{
params
.
append
(((
UpdateRequest
)
request
).
getFilter
()).
append
(
","
);
}
if
(
params
.
length
()
>
FILTER_LENGTH_LIMIT
)
{
params
.
append
(
"..."
);
break
;
}
}
return
params
.
toString
();
}
private
String
limitFilter
(
String
filter
)
{
final
StringBuilder
params
=
new
StringBuilder
();
if
(
filter
.
length
()
>
FILTER_LENGTH_LIMIT
)
{
return
params
.
append
(
filter
.
substring
(
0
,
FILTER_LENGTH_LIMIT
)).
append
(
"..."
).
toString
();
}
else
{
return
filter
;
}
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult
;
import
com.mongodb.ReadPreference
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.ReadBinding
;
/**
* {@link MongoDBReadBindingInterceptor} record the host and port information
* from {@link EnhancedClassInstanceContext#context},
*
* @author baiyang
*/
public
class
MongoDBReadBindingInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
,
MethodInterceptResult
result
)
{
}
/**
* Execute after {@link com.mongodb.Mongo#getReadBinding(ReadPreference)},
* record the host and port information
*/
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
,
Object
ret
)
{
ReadBinding
readBinding
=
(
ReadBinding
)
ret
;
ServerAddress
serverAddress
=
readBinding
.
getReadConnectionSource
()
.
getServerDescription
().
getAddress
();
String
host
=
serverAddress
.
getHost
();
Integer
port
=
serverAddress
.
getPort
();
context
.
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
host
);
context
.
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
port
);
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
)
{
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteBindingInterceptor.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.WriteBinding
;
/**
* {@link MongoDBWriteBindingInterceptor} record the host and port information from {@link
* EnhancedClassInstanceContext#context}
*
* @author baiyang
*/
public
class
MongoDBWriteBindingInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
,
MethodInterceptResult
result
)
{
}
/**
* Execute after {@link com.mongodb.Mongo#getWriteBinding()},
* record the host and port information
*/
@Override
public
Object
afterMethod
(
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
,
Object
ret
)
{
WriteBinding
writeBinding
=
(
WriteBinding
)
ret
;
ServerAddress
serverAddress
=
writeBinding
.
getWriteConnectionSource
().
getServerDescription
().
getAddress
();
String
host
=
serverAddress
.
getHost
();
Integer
port
=
serverAddress
.
getPort
();
context
.
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
host
);
context
.
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
port
);
return
ret
;
}
@Override
public
void
handleMethodException
(
Throwable
t
,
EnhancedClassInstanceContext
context
,
InstanceMethodInvokeContext
interceptorContext
)
{
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/define/MongoDBInstrumentation.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3.define
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint
;
import
com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
com.a.eye.skywalking.plugin.mongodb.v3.MongoDBMethodInterceptor
;
/**
* {@link MongoDBInstrumentation} presents that skywalking intercepts {@link com.mongodb.Mongo#execute(ReadOperation,
* ReadPreference)},{@link com.mongodb.Mongo#execute(WriteOperation)} by using {@link MongoDBMethodInterceptor}.
*
* @author baiyang
*/
public
class
MongoDBInstrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
private
static
final
String
ENHANCE_CLASS
=
"com.mongodb.Mongo"
;
private
static
final
String
MONGDB_READ_BINDING_CLASS
=
"com.a.eye.skywalking.plugin.mongodb.v3.MongoDBReadBindingInterceptor"
;
private
static
final
String
MONGDB_WRITE_BINDING_CLASS
=
"com.a.eye.skywalking.plugin.mongodb.v3.MongoDBWriteBindingInterceptor"
;
private
static
final
String
MONGDB_METHOD_INTERCET_CLASS
=
"com.a.eye.skywalking.plugin.mongodb.v3.MongoDBMethodInterceptor"
;
@Override
protected
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
null
;
}
@Override
protected
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]
{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
"execute"
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
MONGDB_METHOD_INTERCET_CLASS
;
}
},
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
"getReadBinding"
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
MONGDB_READ_BINDING_CLASS
;
}
},
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
"getWriteBinding"
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
MONGDB_WRITE_BINDING_CLASS
;
}
}
};
}
@Override
protected
String
enhanceClassName
()
{
return
ENHANCE_CLASS
;
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def
0 → 100644
浏览文件 @
3b30935e
com.a.eye.skywalking.plugin.mongodb.v3.define.MongoDBInstrumentation
\ No newline at end of file
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
org.bson.BsonDocument
;
import
org.bson.BsonString
;
import
org.bson.codecs.Decoder
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.powermock.api.mockito.PowerMockito
;
import
com.a.eye.skywalking.api.boot.ServiceManager
;
import
com.a.eye.skywalking.api.conf.Config
;
import
com.a.eye.skywalking.api.context.TracerContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener
;
import
com.a.eye.skywalking.sniffer.mock.context.SegmentAssert
;
import
com.a.eye.skywalking.trace.LogData
;
import
com.a.eye.skywalking.trace.Span
;
import
com.a.eye.skywalking.trace.TraceSegment
;
import
com.a.eye.skywalking.trace.tag.Tags
;
import
com.mongodb.MongoNamespace
;
import
com.mongodb.operation.FindOperation
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
MongoDBMethodInterceptorTest
{
private
MongoDBMethodInterceptor
interceptor
;
private
MockTracerContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
interceptor
=
new
MongoDBMethodInterceptor
();
mockTracerContextListener
=
new
MockTracerContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
Config
.
Plugin
.
MongoDB
.
TRACE_PARAM
=
true
;
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
String
.
class
)).
thenReturn
(
"127.0.0.1"
);
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_PORT
)).
thenReturn
(
27017
);
when
(
methodInvokeContext
.
methodName
()).
thenReturn
(
"find"
);
BsonDocument
document
=
new
BsonDocument
();
document
.
append
(
"name"
,
new
BsonString
(
"by"
));
MongoNamespace
mongoNamespace
=
new
MongoNamespace
(
"test.user"
);
Decoder
decoder
=
PowerMockito
.
mock
(
Decoder
.
class
);
FindOperation
findOperation
=
new
FindOperation
(
mongoNamespace
,
decoder
);
findOperation
.
filter
(
document
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
findOperation
});
}
@Test
public
void
testIntercept
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
}
});
}
@Test
public
void
testInterceptWithException
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
assertThat
(
span
.
getLogs
().
size
(),
is
(
1
));
assertLogData
(
span
.
getLogs
().
get
(
0
));
}
});
}
private
void
assertLogData
(
LogData
logData
)
{
MatcherAssert
.
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
RuntimeException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
}
private
void
assertRedisSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"MongoDB/FindOperation"
));
assertThat
(
Tags
.
PEER_HOST
.
get
(
span
),
is
(
"127.0.0.1"
));
assertThat
(
Tags
.
PEER_PORT
.
get
(
span
),
is
(
27017
));
assertThat
(
Tags
.
COMPONENT
.
get
(
span
),
is
(
"MongoDB"
));
assertThat
(
Tags
.
DB_STATEMENT
.
get
(
span
),
is
(
"FindOperation { \"name\" : \"by\" }"
));
assertThat
(
Tags
.
DB_TYPE
.
get
(
span
),
is
(
"MongoDB"
));
assertTrue
(
Tags
.
SPAN_LAYER
.
isDB
(
span
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptorTest.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.ConnectionSource
;
import
com.mongodb.binding.ReadBinding
;
import
com.mongodb.connection.ServerConnectionState
;
import
com.mongodb.connection.ServerDescription
;
@RunWith
(
PowerMockRunner
.
class
)
public
class
MongoDBReadBindingInterceptorTest
{
private
MongoDBReadBindingInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
ReadBinding
readBinding
;
@Mock
private
ConnectionSource
connectionSource
;
private
ServerAddress
address
=
new
ServerAddress
(
"127.0.0.1"
,
27017
);
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
MongoDBReadBindingInterceptor
();
ServerDescription
serverDescription
=
ServerDescription
.
builder
().
address
(
address
).
state
(
ServerConnectionState
.
CONNECTED
).
build
();
PowerMockito
.
when
(
connectionSource
.
getServerDescription
()).
thenReturn
(
serverDescription
);
PowerMockito
.
when
(
readBinding
.
getReadConnectionSource
()).
thenReturn
(
connectionSource
);
}
@Test
public
void
afterMethodTest
()
throws
Exception
{
interceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
readBinding
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
27017
);
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteBindingInterceptorTest.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.binding.ConnectionSource
;
import
com.mongodb.binding.WriteBinding
;
import
com.mongodb.connection.ServerConnectionState
;
import
com.mongodb.connection.ServerDescription
;
@RunWith
(
PowerMockRunner
.
class
)
public
class
MongoDBWriteBindingInterceptorTest
{
private
MongoDBWriteBindingInterceptor
interceptor
;
@Mock
private
EnhancedClassInstanceContext
instanceContext
;
@Mock
private
InstanceMethodInvokeContext
interceptorContext
;
@Mock
private
WriteBinding
writeBinding
;
@Mock
private
ConnectionSource
connectionSource
;
private
ServerAddress
address
=
new
ServerAddress
(
"127.0.0.1"
,
27017
);
@Before
public
void
setUp
()
throws
Exception
{
interceptor
=
new
MongoDBWriteBindingInterceptor
();
ServerDescription
serverDescription
=
ServerDescription
.
builder
().
address
(
address
).
state
(
ServerConnectionState
.
CONNECTED
).
build
();
PowerMockito
.
when
(
connectionSource
.
getServerDescription
()).
thenReturn
(
serverDescription
);
PowerMockito
.
when
(
writeBinding
.
getWriteConnectionSource
()).
thenReturn
(
connectionSource
);
}
@Test
public
void
afterMethodTest
()
throws
Exception
{
interceptor
.
afterMethod
(
instanceContext
,
interceptorContext
,
writeBinding
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
"127.0.0.1"
);
verify
(
instanceContext
,
times
(
1
)).
set
(
MongoDBMethodInterceptor
.
MONGODB_PORT
,
27017
);
}
}
skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java
0 → 100644
浏览文件 @
3b30935e
package
com.a.eye.skywalking.plugin.mongodb.v3
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.bson.BsonDocument
;
import
org.bson.BsonString
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.MatcherAssert
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.powermock.api.mockito.PowerMockito
;
import
com.a.eye.skywalking.api.boot.ServiceManager
;
import
com.a.eye.skywalking.api.conf.Config
;
import
com.a.eye.skywalking.api.context.TracerContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext
;
import
com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext
;
import
com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener
;
import
com.a.eye.skywalking.sniffer.mock.context.SegmentAssert
;
import
com.a.eye.skywalking.trace.LogData
;
import
com.a.eye.skywalking.trace.Span
;
import
com.a.eye.skywalking.trace.TraceSegment
;
import
com.a.eye.skywalking.trace.tag.Tags
;
import
com.mongodb.MongoNamespace
;
import
com.mongodb.WriteConcern
;
import
com.mongodb.bulk.DeleteRequest
;
import
com.mongodb.operation.DeleteOperation
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
MongoDBWriteMethodInterceptorTest
{
private
MongoDBMethodInterceptor
interceptor
;
private
MockTracerContextListener
mockTracerContextListener
;
@Mock
private
EnhancedClassInstanceContext
classInstanceContext
;
@Mock
private
InstanceMethodInvokeContext
methodInvokeContext
;
@Before
public
void
setUp
()
throws
Exception
{
ServiceManager
.
INSTANCE
.
boot
();
interceptor
=
new
MongoDBMethodInterceptor
();
mockTracerContextListener
=
new
MockTracerContextListener
();
TracerContext
.
ListenerManager
.
add
(
mockTracerContextListener
);
Config
.
Plugin
.
MongoDB
.
TRACE_PARAM
=
true
;
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_HOST
,
String
.
class
)).
thenReturn
(
"127.0.0.1"
);
when
(
classInstanceContext
.
get
(
MongoDBMethodInterceptor
.
MONGODB_PORT
)).
thenReturn
(
27017
);
when
(
methodInvokeContext
.
methodName
()).
thenReturn
(
"find"
);
BsonDocument
document
=
new
BsonDocument
();
document
.
append
(
"name"
,
new
BsonString
(
"by"
));
List
<
DeleteRequest
>
requestList
=
new
ArrayList
<
DeleteRequest
>();
DeleteRequest
deleteRequest
=
new
DeleteRequest
(
document
);
requestList
.
add
(
deleteRequest
);
MongoNamespace
mongoNamespace
=
new
MongoNamespace
(
"test.user"
);
WriteConcern
writeConcern
=
PowerMockito
.
mock
(
WriteConcern
.
class
);
DeleteOperation
deleteOperation
=
new
DeleteOperation
(
mongoNamespace
,
false
,
writeConcern
,
requestList
);
when
(
methodInvokeContext
.
allArguments
()).
thenReturn
(
new
Object
[]
{
deleteOperation
});
}
@Test
public
void
testIntercept
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
}
});
}
private
void
assertRedisSpan
(
Span
span
)
{
assertThat
(
span
.
getOperationName
(),
is
(
"MongoDB/DeleteOperation"
));
assertThat
(
Tags
.
PEER_HOST
.
get
(
span
),
is
(
"127.0.0.1"
));
assertThat
(
Tags
.
PEER_PORT
.
get
(
span
),
is
(
27017
));
assertThat
(
Tags
.
COMPONENT
.
get
(
span
),
is
(
"MongoDB"
));
assertThat
(
Tags
.
DB_STATEMENT
.
get
(
span
),
is
(
"DeleteOperation { \"name\" : \"by\" },"
));
assertThat
(
Tags
.
DB_TYPE
.
get
(
span
),
is
(
"MongoDB"
));
assertTrue
(
Tags
.
SPAN_LAYER
.
isDB
(
span
));
}
@Test
public
void
testInterceptWithException
()
{
interceptor
.
beforeMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
interceptor
.
handleMethodException
(
new
RuntimeException
(),
classInstanceContext
,
methodInvokeContext
);
interceptor
.
afterMethod
(
classInstanceContext
,
methodInvokeContext
,
null
);
mockTracerContextListener
.
assertSize
(
1
);
mockTracerContextListener
.
assertTraceSegment
(
0
,
new
SegmentAssert
()
{
@Override
public
void
call
(
TraceSegment
traceSegment
)
{
assertThat
(
traceSegment
.
getSpans
().
size
(),
is
(
1
));
Span
span
=
traceSegment
.
getSpans
().
get
(
0
);
assertRedisSpan
(
span
);
assertThat
(
span
.
getLogs
().
size
(),
is
(
1
));
assertLogData
(
span
.
getLogs
().
get
(
0
));
}
});
}
private
void
assertLogData
(
LogData
logData
)
{
MatcherAssert
.
assertThat
(
logData
.
getFields
().
size
(),
is
(
4
));
MatcherAssert
.
assertThat
(
logData
.
getFields
().
get
(
"event"
),
CoreMatchers
.<
Object
>
is
(
"error"
));
assertEquals
(
logData
.
getFields
().
get
(
"error.kind"
),
RuntimeException
.
class
.
getName
());
assertNull
(
logData
.
getFields
().
get
(
"message"
));
}
@After
public
void
tearDown
()
throws
Exception
{
TracerContext
.
ListenerManager
.
remove
(
mockTracerContextListener
);
}
}
skywalking-sniffer/skywalking-sdk-plugin/pom.xml
浏览文件 @
3b30935e
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
<module>
jedis-2.x-plugin
</module>
<module>
jedis-2.x-plugin
</module>
<module>
tomcat-7.x-8.x-plugin
</module>
<module>
tomcat-7.x-8.x-plugin
</module>
<module>
motan-plugin
</module>
<module>
motan-plugin
</module>
<module>
mongodb-3.x-plugin
</module>
</modules>
</modules>
<packaging>
pom
</packaging>
<packaging>
pom
</packaging>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录