fix bugs
This commit is contained in:
parent
085f9b69fd
commit
e8084e548d
|
|
@ -57,7 +57,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.a.eye</groupId>
|
||||
<artifactId>skywalking-mongodb-plugin</artifactId>
|
||||
<artifactId>skywalking-mongodb-3.x-plugin</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,28 +1,23 @@
|
|||
<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>
|
||||
<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>
|
||||
<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>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
|
|
@ -30,7 +25,7 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.BsonDocument;
|
||||
|
||||
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;
|
||||
|
|
@ -9,6 +11,7 @@ import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAround
|
|||
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;
|
||||
|
|
@ -29,11 +32,14 @@ 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.
|
||||
* or {@link com.mongodb.Mongo#execute(WriteOperation)}. record the mongoDB host, operation name and the key of the
|
||||
* operation.
|
||||
*
|
||||
* @author baiyang
|
||||
*/
|
||||
|
|
@ -42,21 +48,25 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
|
|||
/**
|
||||
* The key name that MongoDB host in {@link EnhancedClassInstanceContext#context}.
|
||||
*/
|
||||
protected static final String MONGODB_HOST = "MONGODB_HOST";
|
||||
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";
|
||||
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;
|
||||
|
||||
@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());
|
||||
Span span = ContextManager.createSpan(METHOD + operationInfo.getMethodName());
|
||||
Tags.COMPONENT.set(span, MONGODB_COMPONENT);
|
||||
Tags.DB_TYPE.set(span, MONGODB_COMPONENT);
|
||||
Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT);
|
||||
|
|
@ -81,58 +91,87 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
|
|||
}
|
||||
|
||||
/**
|
||||
* Convert ReadOperation interface or WriteOperation interface to the implementation class.
|
||||
* Get the method name and filter info.
|
||||
* 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());
|
||||
BsonDocument filter = ((CountOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.COUNT.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof DistinctOperation) {
|
||||
return new OperationInfo(ReadMethod.DISTINCT.getName(), ((DistinctOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((DistinctOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.DISTINCT.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof FindOperation) {
|
||||
return new OperationInfo(ReadMethod.FIND.getName(), ((FindOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((FindOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.FIND.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof GroupOperation) {
|
||||
return new OperationInfo(ReadMethod.GROUP.getName(), ((GroupOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((GroupOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.GROUP.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof ListCollectionsOperation) {
|
||||
return new OperationInfo(ReadMethod.LIST_COLLECTIONS.getName(), ((ListCollectionsOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((ListCollectionsOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.LIST_COLLECTIONS.getName(), limitFilter(filter.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());
|
||||
BsonDocument filter = ((ListCollectionsOperation) obj).getFilter();
|
||||
return new OperationInfo(ReadMethod.MAPREDUCE_WITHINLINE_RESULTS.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof DeleteOperation) {
|
||||
List<DeleteRequest> filter = ((DeleteOperation) obj).getDeleteRequests();
|
||||
return new OperationInfo(WriteMethod.DELETE.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof InsertOperation) {
|
||||
return new OperationInfo(WriteMethod.INSERT.getName(), ((InsertOperation) obj).getInsertRequests().toString());
|
||||
List<InsertRequest> filter = ((InsertOperation) obj).getInsertRequests();
|
||||
return new OperationInfo(WriteMethod.INSERT.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof UpdateOperation) {
|
||||
return new OperationInfo(WriteMethod.UPDATE.getName(), ((UpdateOperation) obj).getUpdateRequests().toString());
|
||||
List<UpdateRequest> filter = ((UpdateOperation) obj).getUpdateRequests();
|
||||
return new OperationInfo(WriteMethod.UPDATE.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof CreateCollectionOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATECOLLECTION.getName(), ((CreateCollectionOperation) obj).getCollectionName());
|
||||
String filter = ((CreateCollectionOperation) obj).getCollectionName();
|
||||
return new OperationInfo(WriteMethod.CREATECOLLECTION.getName(), limitFilter(filter));
|
||||
} else if (obj instanceof CreateIndexesOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATEINDEXES.getName(), ((CreateIndexesOperation) obj).getIndexNames().toString());
|
||||
List<String> filter = ((CreateIndexesOperation) obj).getIndexNames();
|
||||
return new OperationInfo(WriteMethod.CREATEINDEXES.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof CreateViewOperation) {
|
||||
return new OperationInfo(WriteMethod.CREATEVIEW.getName(), ((CreateViewOperation) obj).getViewName());
|
||||
String filter = ((CreateViewOperation) obj).getViewName();
|
||||
return new OperationInfo(WriteMethod.CREATEVIEW.getName(), limitFilter(filter));
|
||||
} else if (obj instanceof FindAndDeleteOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDDELETE.getName(), ((FindAndDeleteOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((FindAndDeleteOperation) obj).getFilter();
|
||||
return new OperationInfo(WriteMethod.FINDANDDELETE.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof FindAndReplaceOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDREPLACE.getName(), ((FindAndReplaceOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((FindAndReplaceOperation) obj).getFilter();
|
||||
return new OperationInfo(WriteMethod.FINDANDREPLACE.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof FindAndUpdateOperation) {
|
||||
return new OperationInfo(WriteMethod.FINDANDUPDATE.getName(), ((FindAndUpdateOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((FindAndUpdateOperation) obj).getFilter();
|
||||
return new OperationInfo(WriteMethod.FINDANDUPDATE.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof MapReduceToCollectionOperation) {
|
||||
return new OperationInfo(WriteMethod.MAPREDUCETOCOLLECTION.getName(), ((MapReduceToCollectionOperation) obj).getFilter().toString());
|
||||
BsonDocument filter = ((MapReduceToCollectionOperation) obj).getFilter();
|
||||
return new OperationInfo(WriteMethod.MAPREDUCETOCOLLECTION.getName(), limitFilter(filter.toString()));
|
||||
} else if (obj instanceof MixedBulkWriteOperation) {
|
||||
List<? extends WriteRequest> list = ((MixedBulkWriteOperation) obj).getWriteRequests();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder params = new StringBuilder();
|
||||
for (WriteRequest request : list) {
|
||||
if (request instanceof InsertRequest) {
|
||||
sb.append(((InsertRequest) request).getDocument().toString()).append(",");
|
||||
params.append(((InsertRequest) request).getDocument().toString()).append(",");
|
||||
} else if (request instanceof DeleteRequest) {
|
||||
sb.append(((DeleteRequest) request).getFilter()).append(",");
|
||||
params.append(((DeleteRequest) request).getFilter()).append(",");
|
||||
} else if (request instanceof UpdateRequest) {
|
||||
sb.append(((UpdateRequest) request).getFilter()).append(",");
|
||||
params.append(((UpdateRequest) request).getFilter()).append(",");
|
||||
}
|
||||
if (params.length() > FILTER_LENGTH_LIMIT) {
|
||||
params.append("...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new OperationInfo(WriteMethod.MIXEDBULKWRITE.getName(), sb.toString());
|
||||
return new OperationInfo(WriteMethod.MIXEDBULKWRITE.getName(), params.toString());
|
||||
} else {
|
||||
return new OperationInfo(WriteMethod.UNKNOW.getName());
|
||||
return new OperationInfo(obj.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
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.ReadPreference;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.binding.ReadBinding;
|
||||
|
||||
|
|
@ -18,7 +18,6 @@ public class MongoDBReadBindingInterceptor implements InstanceMethodsAroundInter
|
|||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -40,7 +39,6 @@ public class MongoDBReadBindingInterceptor implements InstanceMethodsAroundInter
|
|||
@Override
|
||||
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
|
||||
InstanceMethodInvokeContext interceptorContext) {
|
||||
ContextManager.activeSpan().log(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
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;
|
||||
|
|
@ -9,7 +8,7 @@ import com.mongodb.ServerAddress;
|
|||
import com.mongodb.binding.WriteBinding;
|
||||
|
||||
/**
|
||||
* {@link MongoDBWriteBindingInterceptor} record the host and port information from {@link EnhancedClassInstanceContext#context},
|
||||
* {@link MongoDBWriteBindingInterceptor} record the host and port information from {@link EnhancedClassInstanceContext#context}
|
||||
*
|
||||
* @author baiyang
|
||||
*/
|
||||
|
|
@ -18,7 +17,6 @@ public class MongoDBWriteBindingInterceptor implements InstanceMethodsAroundInte
|
|||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -40,7 +38,6 @@ public class MongoDBWriteBindingInterceptor implements InstanceMethodsAroundInte
|
|||
@Override
|
||||
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
|
||||
InstanceMethodInvokeContext interceptorContext) {
|
||||
ContextManager.activeSpan().log(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
/**
|
||||
* {@link OperationInfo} record the methodName and filter information
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
/**
|
||||
* {@link ReadMethod} mongoDB read method enum
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3;
|
||||
|
||||
/**
|
||||
* {@link WriteMethod} mongoDB write method enum
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.a.eye.skywalking.plugin.mongodb.define;
|
||||
package com.a.eye.skywalking.plugin.mongodb.v3.define;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
|
|
@ -18,11 +18,11 @@ public class MongoDBInstrumentation extends ClassInstanceMethodsEnhancePluginDef
|
|||
|
||||
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_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.MongoDBWriteBindingInterceptor";
|
||||
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.MongoDBMethodInterceptor";
|
||||
private static final String MONGDB_METHOD_INTERCET_CLASS = "com.a.eye.skywalking.plugin.mongodb.v3.MongoDBMethodInterceptor";
|
||||
|
||||
@Override
|
||||
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
|
||||
|
|
@ -0,0 +1 @@
|
|||
com.a.eye.skywalking.plugin.mongodb.v3.define.MongoDBInstrumentation
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
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.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 com.a.eye.skywalking.api.boot.ServiceManager;
|
||||
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.operation.FindOperation;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MongoDBMethodInterceptorTest {
|
||||
|
||||
private MongoDBMethodInterceptor interceptor;
|
||||
private MockTracerContextListener mockTracerContextListener;
|
||||
|
||||
@Mock
|
||||
private EnhancedClassInstanceContext classInstanceContext;
|
||||
@Mock
|
||||
private InstanceMethodInvokeContext methodInvokeContext;
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Mock
|
||||
private FindOperation findOperation;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
|
||||
interceptor = new MongoDBMethodInterceptor();
|
||||
mockTracerContextListener = new MockTracerContextListener();
|
||||
|
||||
TracerContext.ListenerManager.add(mockTracerContextListener);
|
||||
|
||||
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"));
|
||||
|
||||
when(findOperation.getFilter()).thenReturn(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/find"));
|
||||
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("find { \"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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
com.a.eye.skywalking.plugin.mongodb.define.MongoDBInstrumentation
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
<module>jedis-2.x-plugin</module>
|
||||
<module>tomcat-7.x-8.x-plugin</module>
|
||||
<module>motan-plugin</module>
|
||||
<module>mongodb-plugin</module>
|
||||
<module>mongodb-3.x-plugin</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue