From 79f51a7a28461334e8ccab916d8bed39644aa971 Mon Sep 17 00:00:00 2001 From: baiyang Date: Thu, 20 Apr 2017 15:20:09 +0800 Subject: [PATCH] modify mongodb --- .../com/a/eye/skywalking/api/conf/Config.java | 17 ++- .../mongodb-3.x-plugin/pom.xml | 2 +- .../mongodb/v3/MongoDBMethodInterceptor.java | 96 ++++++------ .../v3/MongoDBReadBindingInterceptor.java | 55 +++---- .../plugin/mongodb/v3/OperationInfo.java | 51 ------- .../plugin/mongodb/v3/ReadMethod.java | 22 --- .../plugin/mongodb/v3/WriteMethod.java | 23 --- .../v3/MongoDBMethodInterceptorTest.java | 6 +- .../v3/MongoDBWriteMethodInterceptorTest.java | 137 ++++++++++++++++++ 9 files changed, 232 insertions(+), 177 deletions(-) delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/OperationInfo.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/ReadMethod.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/WriteMethod.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java index 6bca636d9..8441edb55 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Config.java @@ -23,13 +23,6 @@ public class Config { * Zero and negative number are illegal. */ public static int SAMPLING_CYCLE = 1; - - /** - * Set Mongodb plugin whether to bind params - * False=Not bound - * True=Binding - */ - public static boolean MONGODB_BINDPARAM = false; } public static class Collector { @@ -88,4 +81,14 @@ public class Config { */ 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; + } + } } diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml index 65018d7fd..197011f64 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml +++ b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/pom.xml @@ -12,7 +12,7 @@ mongodb-plugin http://maven.apache.org - + UTF-8 diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java index 21015772a..d9d6243c6 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptor.java @@ -62,22 +62,24 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto 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(); - Span span = null; - if (Config.Agent.MONGODB_BINDPARAM) { - OperationInfo operationInfo = this.getReadOperationInfo(arguments[0]); - span = ContextManager.createSpan(METHOD + operationInfo.getMethodName()); - Tags.DB_STATEMENT.set(span, operationInfo.getMethodName() + " " + operationInfo.getFilter()); - } else { - span = ContextManager.createSpan(METHOD + arguments[0].getClass().getSimpleName()); - } + + 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 @@ -101,77 +103,81 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto * filter info. */ @SuppressWarnings("rawtypes") - private OperationInfo getReadOperationInfo(Object obj) { + private String getTraceParam(Object obj) { if (obj instanceof CountOperation) { BsonDocument filter = ((CountOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.COUNT.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof DistinctOperation) { BsonDocument filter = ((DistinctOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.DISTINCT.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof FindOperation) { BsonDocument filter = ((FindOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.FIND.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof GroupOperation) { BsonDocument filter = ((GroupOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.GROUP.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof ListCollectionsOperation) { BsonDocument filter = ((ListCollectionsOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.LIST_COLLECTIONS.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof MapReduceWithInlineResultsOperation) { BsonDocument filter = ((ListCollectionsOperation) obj).getFilter(); - return new OperationInfo(ReadMethod.MAPREDUCE_WITHINLINE_RESULTS.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof DeleteOperation) { - List filter = ((DeleteOperation) obj).getDeleteRequests(); - return new OperationInfo(WriteMethod.DELETE.getName(), limitFilter(filter.toString())); + List writeRequestList = ((DeleteOperation) obj).getDeleteRequests(); + return getFilter(writeRequestList); } else if (obj instanceof InsertOperation) { - List filter = ((InsertOperation) obj).getInsertRequests(); - return new OperationInfo(WriteMethod.INSERT.getName(), limitFilter(filter.toString())); + List writeRequestList = ((InsertOperation) obj).getInsertRequests(); + return getFilter(writeRequestList); } else if (obj instanceof UpdateOperation) { - List filter = ((UpdateOperation) obj).getUpdateRequests(); - return new OperationInfo(WriteMethod.UPDATE.getName(), limitFilter(filter.toString())); + List writeRequestList = ((UpdateOperation) obj).getUpdateRequests(); + return getFilter(writeRequestList); } else if (obj instanceof CreateCollectionOperation) { String filter = ((CreateCollectionOperation) obj).getCollectionName(); - return new OperationInfo(WriteMethod.CREATECOLLECTION.getName(), limitFilter(filter)); + return limitFilter(filter); } else if (obj instanceof CreateIndexesOperation) { List filter = ((CreateIndexesOperation) obj).getIndexNames(); - return new OperationInfo(WriteMethod.CREATEINDEXES.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof CreateViewOperation) { String filter = ((CreateViewOperation) obj).getViewName(); - return new OperationInfo(WriteMethod.CREATEVIEW.getName(), limitFilter(filter)); + return limitFilter(filter); } else if (obj instanceof FindAndDeleteOperation) { BsonDocument filter = ((FindAndDeleteOperation) obj).getFilter(); - return new OperationInfo(WriteMethod.FINDANDDELETE.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof FindAndReplaceOperation) { BsonDocument filter = ((FindAndReplaceOperation) obj).getFilter(); - return new OperationInfo(WriteMethod.FINDANDREPLACE.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof FindAndUpdateOperation) { BsonDocument filter = ((FindAndUpdateOperation) obj).getFilter(); - return new OperationInfo(WriteMethod.FINDANDUPDATE.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof MapReduceToCollectionOperation) { BsonDocument filter = ((MapReduceToCollectionOperation) obj).getFilter(); - return new OperationInfo(WriteMethod.MAPREDUCETOCOLLECTION.getName(), limitFilter(filter.toString())); + return limitFilter(filter.toString()); } else if (obj instanceof MixedBulkWriteOperation) { - List list = ((MixedBulkWriteOperation) obj).getWriteRequests(); - StringBuilder params = new StringBuilder(); - for (WriteRequest request : list) { - 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 new OperationInfo(WriteMethod.MIXEDBULKWRITE.getName(), params.toString()); + List writeRequestList = ((MixedBulkWriteOperation) obj).getWriteRequests(); + return getFilter(writeRequestList); } else { - return new OperationInfo(obj.getClass().getSimpleName()); + return EMPTY; } } + private String getFilter(List 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) { diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java index 6805b9ecf..7182d3eaf 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBReadBindingInterceptor.java @@ -9,36 +9,41 @@ import com.mongodb.ServerAddress; import com.mongodb.binding.ReadBinding; /** - * {@link MongoDBReadBindingInterceptor} record the host and port information from {@link EnhancedClassInstanceContext#context}, + * {@link MongoDBReadBindingInterceptor} record the host and port information + * from {@link EnhancedClassInstanceContext#context}, * * @author baiyang */ -public class MongoDBReadBindingInterceptor implements InstanceMethodsAroundInterceptor { +public class MongoDBReadBindingInterceptor implements + InstanceMethodsAroundInterceptor { - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, - MethodInterceptResult result) { - } + @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; - } + /** + * 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) { - } + @Override + public void handleMethodException(Throwable t, + EnhancedClassInstanceContext context, + InstanceMethodInvokeContext interceptorContext) { + } } diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/OperationInfo.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/OperationInfo.java deleted file mode 100644 index 4846a8646..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/OperationInfo.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.a.eye.skywalking.plugin.mongodb.v3; - -/** - * {@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 + "}"; - } - -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/ReadMethod.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/ReadMethod.java deleted file mode 100644 index 61b329ef2..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/ReadMethod.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.a.eye.skywalking.plugin.mongodb.v3; - -/** - * {@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; - } - -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/WriteMethod.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/WriteMethod.java deleted file mode 100644 index 656419226..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/main/java/com/a/eye/skywalking/plugin/mongodb/v3/WriteMethod.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.a.eye.skywalking.plugin.mongodb.v3; - -/** - * {@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; - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java index ab38e2138..caf4e7f02 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBMethodInterceptorTest.java @@ -54,7 +54,7 @@ public class MongoDBMethodInterceptorTest { TracerContext.ListenerManager.add(mockTracerContextListener); - Config.Agent.MONGODB_BINDPARAM = true; + 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); @@ -111,11 +111,11 @@ public class MongoDBMethodInterceptorTest { } private void assertRedisSpan(Span span) { - assertThat(span.getOperationName(), is("MongoDB/find")); + // 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("find { \"name\" : \"by\" }")); + // assertThat(Tags.DB_STATEMENT.get(span), is("find { \"name\" : \"by\" }")); assertThat(Tags.DB_TYPE.get(span), is("MongoDB")); assertTrue(Tags.SPAN_LAYER.isDB(span)); } diff --git a/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java new file mode 100644 index 000000000..20854d504 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/mongodb-3.x-plugin/src/test/java/com/a/eye/skywalking/plugin/mongodb/v3/MongoDBWriteMethodInterceptorTest.java @@ -0,0 +1,137 @@ +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 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.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; + @Mock + private DeleteOperation deleteOperation; + + @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 requestList = new ArrayList(); + + DeleteRequest deleteRequest = new DeleteRequest(document); + + requestList.add(deleteRequest); + + when(deleteOperation.getDeleteRequests()).thenReturn(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. 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); + } + +}