Support plugin for MongoDB
This commit is contained in:
parent
bd465ad97e
commit
085f9b69fd
|
|
@ -55,6 +55,11 @@
|
|||
<artifactId>motan-plugin</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.a.eye</groupId>
|
||||
<artifactId>skywalking-mongodb-plugin</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- activation -->
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
<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-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>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<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>
|
||||
|
|
@ -0,0 +1,139 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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.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.UpdateOperation;
|
||||
|
||||
/**
|
||||
* {@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}.
|
||||
*/
|
||||
protected static final String MONGODB_HOST = "MONGODB_HOST";
|
||||
|
||||
/**
|
||||
* The key name that MongoDB port in {@link EnhancedClassInstanceContext#context}.
|
||||
*/
|
||||
protected static final String MONGODB_PORT = "MONGODB_PORT";
|
||||
|
||||
private static final String MONGODB_COMPONENT = "MongoDB";
|
||||
|
||||
@Override
|
||||
public void beforeMethod(final EnhancedClassInstanceContext context,
|
||||
final InstanceMethodInvokeContext interceptorContext, final MethodInterceptResult result) {
|
||||
Object[] arguments = interceptorContext.allArguments();
|
||||
OperationInfo operationInfo = this.getReadOperationInfo(arguments[0]);
|
||||
Span span = ContextManager.createSpan("MongoDB/" + operationInfo.getMethodName());
|
||||
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);
|
||||
Tags.DB_STATEMENT.set(span, operationInfo.getMethodName() + " " + operationInfo.getFilter());
|
||||
}
|
||||
|
||||
@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 OperationInfo getReadOperationInfo(Object obj) {
|
||||
if (obj instanceof CountOperation) {
|
||||
return new OperationInfo(ReadMethod.COUNT.getName(), ((CountOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof DistinctOperation) {
|
||||
return new OperationInfo(ReadMethod.DISTINCT.getName(), ((DistinctOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof FindOperation) {
|
||||
return new OperationInfo(ReadMethod.FIND.getName(), ((FindOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof GroupOperation) {
|
||||
return new OperationInfo(ReadMethod.GROUP.getName(), ((GroupOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof ListCollectionsOperation) {
|
||||
return new OperationInfo(ReadMethod.LIST_COLLECTIONS.getName(), ((ListCollectionsOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof MapReduceWithInlineResultsOperation) {
|
||||
return new OperationInfo(ReadMethod.MAPREDUCE_WITHINLINE_RESULTS.getName(), ((ListCollectionsOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof DeleteOperation) {
|
||||
return new OperationInfo(WriteMethod.DELETE.getName(), ((DeleteOperation) obj).getDeleteRequests().toString());
|
||||
} else if (obj instanceof InsertOperation) {
|
||||
return new OperationInfo(WriteMethod.INSERT.getName(), ((InsertOperation) obj).getInsertRequests().toString());
|
||||
} else if (obj instanceof UpdateOperation) {
|
||||
return new OperationInfo(WriteMethod.UPDATE.getName(), ((UpdateOperation) obj).getUpdateRequests().toString());
|
||||
} else if (obj instanceof CreateCollectionOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATECOLLECTION.getName(), ((CreateCollectionOperation) obj).getCollectionName());
|
||||
} else if (obj instanceof CreateIndexesOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATEINDEXES.getName(), ((CreateIndexesOperation) obj).getIndexNames().toString());
|
||||
} else if (obj instanceof CreateViewOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATEVIEW.getName(), ((CreateViewOperation) obj).getViewName());
|
||||
} else if (obj instanceof FindAndDeleteOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDDELETE.getName(), ((FindAndDeleteOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof FindAndReplaceOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDREPLACE.getName(), ((FindAndReplaceOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof FindAndUpdateOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDUPDATE.getName(), ((FindAndUpdateOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof MapReduceToCollectionOperation) {
|
||||
return new OperationInfo(WriteMethod.MAPREDUCETOCOLLECTION.getName(), ((MapReduceToCollectionOperation) obj).getFilter().toString());
|
||||
} else if (obj instanceof MixedBulkWriteOperation) {
|
||||
List<? extends WriteRequest> list = ((MixedBulkWriteOperation) obj).getWriteRequests();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (WriteRequest request : list) {
|
||||
if (request instanceof InsertRequest) {
|
||||
sb.append(((InsertRequest) request).getDocument().toString()).append(",");
|
||||
} else if (request instanceof DeleteRequest) {
|
||||
sb.append(((DeleteRequest) request).getFilter()).append(",");
|
||||
} else if (request instanceof UpdateRequest) {
|
||||
sb.append(((UpdateRequest) request).getFilter()).append(",");
|
||||
}
|
||||
}
|
||||
return new OperationInfo(WriteMethod.MIXEDBULKWRITE.getName(), sb.toString());
|
||||
} else {
|
||||
return new OperationInfo(WriteMethod.UNKNOW.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
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.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) {
|
||||
ContextManager.activeSpan().log(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
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.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) {
|
||||
ContextManager.activeSpan().log(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
/**
|
||||
* {@link OperationInfo} record the methodName and filter information
|
||||
*
|
||||
* @author baiyang
|
||||
*/
|
||||
public class OperationInfo {
|
||||
|
||||
private String methodName;
|
||||
|
||||
private String filter;
|
||||
|
||||
public OperationInfo() {
|
||||
|
||||
}
|
||||
|
||||
public OperationInfo(String methodName) {
|
||||
super();
|
||||
this.methodName = methodName;
|
||||
this.filter = "";
|
||||
}
|
||||
|
||||
public OperationInfo(String methodName, String filter) {
|
||||
super();
|
||||
this.methodName = methodName;
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
public String getMethodName() {
|
||||
return methodName;
|
||||
}
|
||||
|
||||
public void setMethodName(String methodName) {
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
public String getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public void setFilter(String filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{methodName=" + methodName + ", filter=" + filter + "}";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
/**
|
||||
* {@link ReadMethod} mongoDB read method enum
|
||||
*
|
||||
* @author baiyang
|
||||
*/
|
||||
public enum ReadMethod {
|
||||
COUNT("count"), DISTINCT("distinct"), FIND("find"), GROUP("group"), LIST_COLLECTIONS("listCollections"),
|
||||
MAPREDUCE_WITHINLINE_RESULTS("mapReduceWithInlineResults");
|
||||
|
||||
private String name;
|
||||
|
||||
private ReadMethod(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
|
||||
/**
|
||||
* {@link WriteMethod} mongoDB write method enum
|
||||
*
|
||||
* @author baiyang
|
||||
*/
|
||||
public enum WriteMethod {
|
||||
DELETE("delete"), INSERT("insert"), UPDATE("update"), CREATECOLLECTION("createCollection"), CREATEINDEXES(
|
||||
"createIndexess"), CREATEVIEW("createView"), FINDANDDELETE("findAndDelete"), FINDANDREPLACE(
|
||||
"findAndReplace"), FINDANDUPDATE("findAndUpdate"), MAPREDUCETOCOLLECTION("mapReduceToCollection"),
|
||||
MIXEDBULKWRITE("mixedBulkWrite"), UNKNOW("unknow");
|
||||
|
||||
private String name;
|
||||
|
||||
private WriteMethod(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb.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;
|
||||
|
||||
/**
|
||||
* {@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.MongoDBReadBindingInterceptor";
|
||||
|
||||
private static final String MONGDB_WRITE_BINDING_CLASS = "com.a.eye.skywalking.plugin.mongodb.MongoDBWriteBindingInterceptor";
|
||||
|
||||
private static final String MONGDB_METHOD_INTERCET_CLASS = "com.a.eye.skywalking.plugin.mongodb.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
com.a.eye.skywalking.plugin.mongodb.define.MongoDBInstrumentation
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
<module>jedis-2.x-plugin</module>
|
||||
<module>tomcat-7.x-8.x-plugin</module>
|
||||
<module>motan-plugin</module>
|
||||
<module>mongodb-plugin</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue