From f8bf285b3b225e413b4a804f15ba45059fa0c0ae Mon Sep 17 00:00:00 2001 From: youjie_li Date: Fri, 22 Nov 2024 03:11:18 +0800 Subject: [PATCH 01/10] support mongo db.instance and AggregateOperation --- .../v3/support/MongoOperationHelper.java | 16 ++++++++++ .../mongodb/v3/support/MongoSpanHelper.java | 30 +++++++++++++++++++ .../v4/support/MongoOperationHelper.java | 16 ++++++++++ .../mongodb/v4/support/MongoSpanHelper.java | 22 ++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoOperationHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoOperationHelper.java index 99ab80c051..d68d642ec0 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoOperationHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoOperationHelper.java @@ -22,6 +22,7 @@ import com.mongodb.bulk.InsertRequest; import com.mongodb.bulk.UpdateRequest; import com.mongodb.bulk.WriteRequest; +import com.mongodb.operation.AggregateOperation; import com.mongodb.operation.CountOperation; import com.mongodb.operation.CreateCollectionOperation; import com.mongodb.operation.CreateIndexesOperation; @@ -104,6 +105,9 @@ public static String getTraceParam(Object obj) { } else if (obj instanceof FindAndUpdateOperation) { BsonDocument filter = ((FindAndUpdateOperation) obj).getFilter(); return limitFilter(filter.toString()); + } else if (obj instanceof AggregateOperation) { + List pipelines = ((AggregateOperation) obj).getPipeline(); + return getPipelines(pipelines); } else if (obj instanceof MapReduceToCollectionOperation) { BsonDocument filter = ((MapReduceToCollectionOperation) obj).getFilter(); return limitFilter(filter.toString()); @@ -115,6 +119,18 @@ public static String getTraceParam(Object obj) { } } + private static String getPipelines(List pipelines) { + StringBuilder params = new StringBuilder(); + for (BsonDocument pipeline : pipelines) { + params.append(pipeline.toString()).append(","); + final int filterLengthLimit = MongoPluginConfig.Plugin.MongoDB.FILTER_LENGTH_LIMIT; + if (filterLengthLimit > 0 && params.length() > filterLengthLimit) { + return params.substring(0, filterLengthLimit) + "..."; + } + } + return params.toString(); + } + private static String getFilter(List writeRequestList) { StringBuilder params = new StringBuilder(); for (WriteRequest request : writeRequestList) { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java index 759abc4cd0..2e5f2acafd 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java @@ -18,14 +18,18 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.support; +import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.mongodb.v3.MongoPluginConfig; +import java.lang.reflect.Field; + public class MongoSpanHelper { private MongoSpanHelper() { @@ -38,6 +42,32 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec Tags.DB_TYPE.set(span, MongoConstants.DB_TYPE); SpanLayer.asDB(span); + try { + Field namespaceField = operation.getClass().getDeclaredField("namespace"); + Field.setAccessible(new Field[]{namespaceField}, true); + MongoNamespace namespace = (MongoNamespace) namespaceField.get(operation); + Tags.DB_INSTANCE.set(span, namespace.getFullName()); + } catch (Exception e) { + try { + Field wrappedField = operation.getClass().getDeclaredField("wrapped"); + Field.setAccessible(new Field[]{wrappedField}, true); + Object wrappedOperation = wrappedField.get(operation); + Field wrappedNamespaceField = wrappedOperation.getClass().getDeclaredField("namespace"); + Field.setAccessible(new Field[]{wrappedNamespaceField}, true); + MongoNamespace wrappedNamespace = (MongoNamespace) wrappedNamespaceField.get(wrappedOperation); + Tags.DB_INSTANCE.set(span, wrappedNamespace.getFullName()); + } catch (Exception e2) { + + } + } + + if (operation instanceof EnhancedInstance) { + Object databaseName = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (databaseName != null) { + Tags.DB_INSTANCE.set(span, (String) databaseName); + } + } + if (MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM) { Tags.DB_BIND_VARIABLES.set(span, MongoOperationHelper.getTraceParam(operation)); } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoOperationHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoOperationHelper.java index 45d0481fbc..8a561cc887 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoOperationHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoOperationHelper.java @@ -22,6 +22,7 @@ import com.mongodb.internal.bulk.InsertRequest; import com.mongodb.internal.bulk.UpdateRequest; import com.mongodb.internal.bulk.WriteRequest; +import com.mongodb.internal.operation.AggregateOperation; import com.mongodb.internal.operation.CountOperation; import com.mongodb.internal.operation.CreateCollectionOperation; import com.mongodb.internal.operation.CreateIndexesOperation; @@ -101,6 +102,9 @@ public static String getTraceParam(Object obj) { } else if (obj instanceof FindAndUpdateOperation) { BsonDocument filter = ((FindAndUpdateOperation) obj).getFilter(); return limitFilter(filter.toString()); + } else if (obj instanceof AggregateOperation) { + List pipelines = ((AggregateOperation) obj).getPipeline(); + return getPipelines(pipelines); } else if (obj instanceof MapReduceToCollectionOperation) { BsonDocument filter = ((MapReduceToCollectionOperation) obj).getFilter(); return limitFilter(filter.toString()); @@ -112,6 +116,18 @@ public static String getTraceParam(Object obj) { } } + private static String getPipelines(List pipelines) { + StringBuilder params = new StringBuilder(); + for (BsonDocument pipeline : pipelines) { + params.append(pipeline.toString()).append(","); + final int filterLengthLimit = MongoPluginConfig.Plugin.MongoDB.FILTER_LENGTH_LIMIT; + if (filterLengthLimit > 0 && params.length() > filterLengthLimit) { + return params.substring(0, filterLengthLimit) + "..."; + } + } + return params.toString(); + } + private static String getFilter(List writeRequestList) { StringBuilder params = new StringBuilder(); for (WriteRequest request : writeRequestList) { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java index 4f81a0f058..9b7d26dec2 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java @@ -18,6 +18,7 @@ package org.apache.skywalking.apm.plugin.mongodb.v4.support; +import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.tag.Tags; @@ -26,6 +27,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import java.lang.reflect.Field; + public class MongoSpanHelper { private MongoSpanHelper() { @@ -44,6 +47,25 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec Tags.DB_TYPE.set(span, MongoConstants.DB_TYPE); SpanLayer.asDB(span); + try { + Field namespaceField = operation.getClass().getDeclaredField("namespace"); + Field.setAccessible(new Field[]{namespaceField}, true); + MongoNamespace namespace = (MongoNamespace) namespaceField.get(operation); + Tags.DB_INSTANCE.set(span, namespace.getFullName()); + } catch (Exception e) { + try { + Field wrappedField = operation.getClass().getDeclaredField("wrapped"); + Field.setAccessible(new Field[]{wrappedField}, true); + Object wrappedOperation = wrappedField.get(operation); + Field wrappedNamespaceField = wrappedOperation.getClass().getDeclaredField("namespace"); + Field.setAccessible(new Field[]{wrappedNamespaceField}, true); + MongoNamespace wrappedNamespace = (MongoNamespace) wrappedNamespaceField.get(wrappedOperation); + Tags.DB_INSTANCE.set(span, wrappedNamespace.getFullName()); + } catch (Exception e2) { + + } + } + if (operation instanceof EnhancedInstance) { Object databaseName = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); if (databaseName != null) { From 5801ee2e5331026fa32723052f746f42c43618d7 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Fri, 22 Nov 2024 15:10:30 +0800 Subject: [PATCH 02/10] support mongo db.instance and AggregateOperation unit test --- .../v30/MongoDBInterceptorTest.java | 3 +- ...ngoDBOperationExecutorInterceptorTest.java | 72 ++++++++++++++----- ...ngoDBOperationExecutorInterceptorTest.java | 46 ++++++++++-- 3 files changed, 96 insertions(+), 25 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java index 62d28a6c14..44f405b148 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java @@ -128,7 +128,8 @@ private void assertRedisSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("{\"name\": \"by\"}")); + assertThat(tags.get(1).getValue(), is("test.user")); + assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); assertThat(tags.get(0).getValue(), is("MongoDB")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java index 60de9f5c21..77e1552018 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java @@ -18,12 +18,12 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.v37; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import java.lang.reflect.Method; -import java.util.List; +import com.mongodb.MongoNamespace; +import com.mongodb.ReadConcern; +import com.mongodb.client.internal.OperationExecutor; +import com.mongodb.operation.AggregateOperation; +import com.mongodb.operation.FindOperation; +import com.mongodb.operation.WriteOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -50,11 +50,15 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; -import com.mongodb.MongoNamespace; -import com.mongodb.ReadConcern; -import com.mongodb.client.internal.OperationExecutor; -import com.mongodb.operation.FindOperation; -import com.mongodb.operation.WriteOperation; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(TracingSegmentRunner.class) public class MongoDBOperationExecutorInterceptorTest { @@ -75,6 +79,8 @@ public class MongoDBOperationExecutorInterceptorTest { private Object[] arguments; private Class[] argumentTypes; + private Decoder decoder; + private MongoNamespace mongoNamespace; @Before public void setUp() { @@ -87,11 +93,10 @@ public void setUp() { BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); - MongoNamespace mongoNamespace = new MongoNamespace("test.user"); - Decoder decoder = mock(Decoder.class); + mongoNamespace = new MongoNamespace("test.user"); + decoder = mock(Decoder.class); FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); - arguments = new Object[] {findOperation}; argumentTypes = new Class[] {findOperation.getClass()}; } @@ -104,11 +109,28 @@ public void testIntercept() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertRedisSpan(spans.get(0)); + assertMongoFindOperationSpan(spans.get(0)); } @Test - public void testInterceptWithException() throws Throwable { + public void testAggregateOperationIntercept() throws Throwable { + MongoNamespace mongoNamespace = new MongoNamespace("test.user"); + BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); + List pipeline = Collections.singletonList(matchStage); + AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); + Object[] arguments = {aggregateOperation}; + Class[] argumentTypes = {aggregateOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoAggregateOperationSpan(spans.get(0)); + } + + @Test + public void testInterceptFindOperationWithException() throws Throwable { interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); interceptor.handleMethodException( enhancedInstance, getMethod(), arguments, argumentTypes, new RuntimeException()); @@ -117,17 +139,29 @@ public void testInterceptWithException() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertRedisSpan(spans.get(0)); + assertMongoFindOperationSpan(spans.get(0)); List logDataEntities = SpanHelper.getLogs(spans.get(0)); assertThat(logDataEntities.size(), is(1)); SpanAssert.assertException(logDataEntities.get(0), RuntimeException.class); } - private void assertRedisSpan(AbstractTracingSpan span) { + private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(SpanHelper.getComponentId(span), is(42)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(1).getValue(), is("test.user")); + assertThat(tags.get(2).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); + } + + private void assertMongoFindOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("{\"name\": \"by\"}")); + assertThat(tags.get(1).getValue(), is("test.user")); + assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); assertThat(tags.get(0).getValue(), is("MongoDB")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java index ed2154056e..aa04aba5a4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java @@ -23,7 +23,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.lang.reflect.Method; +import java.util.Collections; import java.util.List; + +import com.mongodb.internal.operation.AggregateOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -77,6 +80,10 @@ public class MongoDBOperationExecutorInterceptorTest { private Class[] argumentTypes; + private Decoder decoder; + + private MongoNamespace mongoNamespace; + @Before public void setUp() { @@ -89,7 +96,7 @@ public void setUp() { BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); MongoNamespace mongoNamespace = new MongoNamespace("test.user"); - Decoder decoder = mock(Decoder.class); + decoder = mock(Decoder.class); FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); @@ -105,7 +112,24 @@ public void testIntercept() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertMongoSpan(spans.get(0)); + assertMongoFindOperationSpan(spans.get(0)); + } + + @Test + public void testAggregateOperationIntercept() throws Throwable { + MongoNamespace mongoNamespace = new MongoNamespace("test.user"); + BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); + List pipeline = Collections.singletonList(matchStage); + AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); + Object[] arguments = {aggregateOperation}; + Class[] argumentTypes = {aggregateOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoAggregateOperationSpan(spans.get(0)); } @Test @@ -118,17 +142,29 @@ public void testInterceptWithException() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertMongoSpan(spans.get(0)); + assertMongoFindOperationSpan(spans.get(0)); List logDataEntities = SpanHelper.getLogs(spans.get(0)); assertThat(logDataEntities.size(), is(1)); SpanAssert.assertException(logDataEntities.get(0), RuntimeException.class); } - private void assertMongoSpan(AbstractTracingSpan span) { + private void assertMongoFindOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("{\"name\": \"by\"}")); + assertThat(tags.get(1).getValue(), is("test.user")); + assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); + } + + private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(SpanHelper.getComponentId(span), is(42)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(1).getValue(), is("test.user")); + assertThat(tags.get(2).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(tags.get(0).getValue(), is("MongoDB")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); From 80b5ac801b7d4585ea7298cb0e0ce259c7b0d72b Mon Sep 17 00:00:00 2001 From: youjie_li Date: Sun, 24 Nov 2024 01:20:35 +0800 Subject: [PATCH 03/10] support mongo db.instance and AggregateOperation test scenarios --- .../mongodb/v3/support/MongoSpanHelper.java | 37 +++++----- .../v30/MongoDBInterceptorTest.java | 41 ++++++++++- ...ngoDBOperationExecutorInterceptorTest.java | 10 +-- .../MongoDBOperationExecutorInterceptor.java | 2 +- ...perationNamespaceConstructInterceptor.java | 3 +- .../mongodb/v4/support/MongoSpanHelper.java | 38 ++++------- ...ngoDBOperationExecutorInterceptorTest.java | 68 +++++++++++++++---- .../config/expectedData.yaml | 35 ++++++++-- .../mongodb/controller/CaseController.java | 14 ++++ .../config/expectedData.yaml | 29 ++++++-- .../mongodb/controller/CaseController.java | 14 ++++ 11 files changed, 217 insertions(+), 74 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java index 2e5f2acafd..89d6efdc82 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java @@ -21,17 +21,20 @@ import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.mongodb.v3.MongoPluginConfig; +import org.apache.skywalking.apm.util.StringUtil; import java.lang.reflect.Field; public class MongoSpanHelper { + private static final AbstractTag DB_COLLECTION_TAG = Tags.ofKey("db.collection"); + private MongoSpanHelper() { } @@ -43,33 +46,35 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec SpanLayer.asDB(span); try { - Field namespaceField = operation.getClass().getDeclaredField("namespace"); - Field.setAccessible(new Field[]{namespaceField}, true); - MongoNamespace namespace = (MongoNamespace) namespaceField.get(operation); - Tags.DB_INSTANCE.set(span, namespace.getFullName()); + MongoNamespace namespace = tryToGetMongoNamespace(operation); + extractTagsFromNamespace(span, namespace); } catch (Exception e) { try { Field wrappedField = operation.getClass().getDeclaredField("wrapped"); Field.setAccessible(new Field[]{wrappedField}, true); Object wrappedOperation = wrappedField.get(operation); - Field wrappedNamespaceField = wrappedOperation.getClass().getDeclaredField("namespace"); - Field.setAccessible(new Field[]{wrappedNamespaceField}, true); - MongoNamespace wrappedNamespace = (MongoNamespace) wrappedNamespaceField.get(wrappedOperation); - Tags.DB_INSTANCE.set(span, wrappedNamespace.getFullName()); + MongoNamespace namespace = tryToGetMongoNamespace(wrappedOperation); + extractTagsFromNamespace(span, namespace); } catch (Exception e2) { } } - if (operation instanceof EnhancedInstance) { - Object databaseName = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); - if (databaseName != null) { - Tags.DB_INSTANCE.set(span, (String) databaseName); - } - } - if (MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM) { Tags.DB_BIND_VARIABLES.set(span, MongoOperationHelper.getTraceParam(operation)); } } + + private static void extractTagsFromNamespace(AbstractSpan span, MongoNamespace namespace) { + Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); + if (StringUtil.isNotEmpty(namespace.getCollectionName())) { + span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); + } + } + + private static MongoNamespace tryToGetMongoNamespace(Object operation) throws IllegalAccessException, NoSuchFieldException { + Field namespaceField = operation.getClass().getDeclaredField("namespace"); + Field.setAccessible(new Field[]{namespaceField}, true); + return (MongoNamespace) namespaceField.get(operation); + } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java index 44f405b148..aa66b4db49 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java @@ -23,7 +23,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.lang.reflect.Method; +import java.util.Collections; import java.util.List; + +import com.mongodb.operation.AggregateOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -70,6 +73,7 @@ public class MongoDBInterceptorTest { @Mock private EnhancedInstance enhancedInstance; + private Decoder decoder; private Object[] arguments; private Class[] argumentTypes; @@ -89,7 +93,7 @@ public void setUp() throws Exception { BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); MongoNamespace mongoNamespace = new MongoNamespace("test.user"); - Decoder decoder = mock(Decoder.class); + decoder = mock(Decoder.class); FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); @@ -108,6 +112,24 @@ public void testIntercept() throws Throwable { assertRedisSpan(spans.get(0)); } + @Test + public void testAggregateOperationIntercept() throws Throwable { + MongoNamespace mongoNamespace = new MongoNamespace("test.user"); + BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); + List pipeline = Collections.singletonList(matchStage); + AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); + Object[] arguments = {aggregateOperation}; + Class[] argumentTypes = {aggregateOperation.getClass()}; + + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), arguments, argumentTypes, null); + interceptor.afterMethod(enhancedInstance, getExecuteMethod(), arguments, argumentTypes, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoAggregateOperationSpan(spans.get(0)); + } + @Test public void testInterceptWithException() throws Throwable { interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), arguments, argumentTypes, null); @@ -128,9 +150,22 @@ private void assertRedisSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("test.user")); - assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"name\": \"by\"}")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); + } + + private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(SpanHelper.getComponentId(span), is(42)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java index 77e1552018..1955a170b4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java @@ -149,9 +149,10 @@ private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("test.user")); - assertThat(tags.get(2).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } @@ -160,9 +161,10 @@ private void assertMongoFindOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("test.user")); - assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"name\": \"by\"}")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java index dbdf7c6926..633b250398 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java @@ -37,7 +37,7 @@ public class MongoDBOperationExecutorInterceptor implements InstanceMethodsAroun @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) { - String executeMethod = allArguments[0].getClass().getSimpleName(); + String executeMethod = argumentsTypes[0].getSimpleName(); // OperationExecutor has included th remotePeer // See: MongoDBClientDelegateInterceptor.afterMethod String remotePeer = (String) objInst.getSkyWalkingDynamicField(); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java index 6641012fbf..3d7846b15e 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java @@ -27,8 +27,7 @@ public class OperationNamespaceConstructInterceptor implements InstanceConstruct @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { MongoNamespace mongoNamespace = (MongoNamespace) allArguments[0]; - String databaseName = mongoNamespace.getDatabaseName(); - objInst.setSkyWalkingDynamicField(databaseName); + objInst.setSkyWalkingDynamicField(mongoNamespace); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java index 9b7d26dec2..fa7b3a96b5 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java @@ -21,16 +21,18 @@ import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; - -import java.lang.reflect.Field; +import org.apache.skywalking.apm.util.StringUtil; public class MongoSpanHelper { + private static final AbstractTag DB_COLLECTION_TAG = Tags.ofKey("db.collection"); + private MongoSpanHelper() { } @@ -42,34 +44,19 @@ private MongoSpanHelper() { */ public static void createExitSpan(String executeMethod, String remotePeer, Object operation) { AbstractSpan span = ContextManager.createExitSpan( - MongoConstants.MONGO_DB_OP_PREFIX + executeMethod, new ContextCarrier(), remotePeer); + MongoConstants.MONGO_DB_OP_PREFIX + executeMethod, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGO_DRIVER); Tags.DB_TYPE.set(span, MongoConstants.DB_TYPE); SpanLayer.asDB(span); - try { - Field namespaceField = operation.getClass().getDeclaredField("namespace"); - Field.setAccessible(new Field[]{namespaceField}, true); - MongoNamespace namespace = (MongoNamespace) namespaceField.get(operation); - Tags.DB_INSTANCE.set(span, namespace.getFullName()); - } catch (Exception e) { - try { - Field wrappedField = operation.getClass().getDeclaredField("wrapped"); - Field.setAccessible(new Field[]{wrappedField}, true); - Object wrappedOperation = wrappedField.get(operation); - Field wrappedNamespaceField = wrappedOperation.getClass().getDeclaredField("namespace"); - Field.setAccessible(new Field[]{wrappedNamespaceField}, true); - MongoNamespace wrappedNamespace = (MongoNamespace) wrappedNamespaceField.get(wrappedOperation); - Tags.DB_INSTANCE.set(span, wrappedNamespace.getFullName()); - } catch (Exception e2) { - - } - } - if (operation instanceof EnhancedInstance) { - Object databaseName = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); - if (databaseName != null) { - Tags.DB_INSTANCE.set(span, (String) databaseName); + Object namespaceObj = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (namespaceObj != null) { + MongoNamespace namespace = (MongoNamespace) namespaceObj; + Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); + if (StringUtil.isNotEmpty(namespace.getCollectionName())) { + span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); + } } } @@ -78,3 +65,4 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec } } } + diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java index aa04aba5a4..7fbcfe9e89 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java @@ -41,17 +41,21 @@ import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.plugin.mongodb.v4.interceptor.MongoDBOperationExecutorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v4.interceptor.operation.OperationNamespaceConstructInterceptor; import org.apache.skywalking.apm.plugin.mongodb.v4.support.MongoPluginConfig; import org.bson.BsonDocument; import org.bson.BsonString; import org.bson.codecs.Decoder; import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; +import org.hamcrest.core.Is; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import com.mongodb.MongoNamespace; @@ -74,8 +78,27 @@ public class MongoDBOperationExecutorInterceptorTest { @Mock private EnhancedInstance enhancedInstance; + private FindOperation enhancedInstanceForFindOperation; + + @Spy + private EnhancedInstance enhancedObjInstance = new EnhancedInstance() { + private MongoNamespace namespace; + + @Override + public Object getSkyWalkingDynamicField() { + return namespace; + } + + @Override + public void setSkyWalkingDynamicField(Object value) { + this.namespace = (MongoNamespace) value; + } + }; + private MongoDBOperationExecutorInterceptor interceptor; + private OperationNamespaceConstructInterceptor constructInterceptor; + private Object[] arguments; private Class[] argumentTypes; @@ -88,22 +111,31 @@ public class MongoDBOperationExecutorInterceptorTest { public void setUp() { interceptor = new MongoDBOperationExecutorInterceptor(); - + constructInterceptor = new OperationNamespaceConstructInterceptor(); + enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM = true; - - when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); - + decoder = mock(Decoder.class); + mongoNamespace = new MongoNamespace("test.user"); BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); - MongoNamespace mongoNamespace = new MongoNamespace("test.user"); - decoder = mock(Decoder.class); + FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); - - arguments = new Object[] {findOperation}; + decoder = mock(Decoder.class); + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); + enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); + arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; } + @Test + public void testConstructIntercept() throws Throwable { + constructInterceptor.onConstruct(enhancedObjInstance, new Object[]{mongoNamespace}); + MatcherAssert.assertThat(enhancedObjInstance.getSkyWalkingDynamicField(), Is.is(mongoNamespace)); + } + @Test public void testIntercept() throws Throwable { interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); @@ -121,8 +153,13 @@ public void testAggregateOperationIntercept() throws Throwable { BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); List pipeline = Collections.singletonList(matchStage); AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); - Object[] arguments = {aggregateOperation}; + + AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); + Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); @@ -135,8 +172,7 @@ public void testAggregateOperationIntercept() throws Throwable { @Test public void testInterceptWithException() throws Throwable { interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); - interceptor.handleMethodException( - enhancedInstance, getMethod(), arguments, argumentTypes, new RuntimeException()); + interceptor.handleMethodException(enhancedInstance, getMethod(), arguments, argumentTypes, new RuntimeException()); interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); @@ -152,9 +188,10 @@ private void assertMongoFindOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("test.user")); - assertThat(tags.get(2).getValue(), is("{\"name\": \"by\"}")); assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"name\": \"by\"}")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } @@ -163,9 +200,10 @@ private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); - assertThat(tags.get(1).getValue(), is("test.user")); - assertThat(tags.get(2).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("{\"$match\": {\"name\": \"by\"}},")); assertThat(span.isExit(), is(true)); assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } diff --git a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml index e17d7162cf..1cbddfae0d 100644 --- a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml @@ -31,6 +31,7 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation @@ -45,6 +46,8 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/FindOperation @@ -59,9 +62,11 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - - operationName: MongoDB/MixedBulkWriteOperation + - operationName: MongoDB/AggregateOperation parentSpanId: 0 spanId: 4 spanLayer: Database @@ -73,11 +78,29 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} + - {key: db.bind_vars, value: '{"$match": {"name": "test"}},'} + skipAnalysis: 'false' + - operationName: MongoDB/MixedBulkWriteOperation + parentSpanId: 0 + spanId: 5 + spanLayer: Database + startTime: nq 0 + endTime: nq 0 + componentId: 42 + isError: false + spanType: Exit + peer: mongodb-server:27017 + tags: + - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/FindOperation parentSpanId: 0 - spanId: 5 + spanId: 6 spanLayer: Database startTime: nq 0 endTime: nq 0 @@ -87,11 +110,13 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation parentSpanId: 0 - spanId: 6 + spanId: 7 spanLayer: Database startTime: nq 0 endTime: nq 0 @@ -101,11 +126,13 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/DropDatabaseOperation parentSpanId: 0 - spanId: 7 + spanId: 8 spanLayer: Database startTime: nq 0 endTime: nq 0 diff --git a/test/plugin/scenarios/mongodb-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java b/test/plugin/scenarios/mongodb-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java index dad835c02a..d26f7d092e 100644 --- a/test/plugin/scenarios/mongodb-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java +++ b/test/plugin/scenarios/mongodb-3.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java @@ -19,16 +19,23 @@ package org.apache.skywalking.apm.testcase.mongodb.controller; import com.mongodb.MongoClient; +import com.mongodb.client.AggregateIterable; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Aggregates; +import com.mongodb.client.model.Filters; import org.bson.BsonDocument; import org.bson.Document; +import org.bson.conversions.Bson; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Arrays; +import java.util.List; + import static com.mongodb.client.model.Filters.eq; @RestController @@ -65,6 +72,13 @@ public String mongoDBCase() { FindIterable findIterable = collection.find(eq("name", "org")); findIterable.first(); + // AggregateOperation + List pipeline = Arrays.asList( + Aggregates.match(Filters.eq("name", "test")) + ); + AggregateIterable aggregateIterable = collection.aggregate(pipeline); + aggregateIterable.first(); + // MixedBulkWriteOperation collection.updateOne(eq("name", "org"), BsonDocument.parse("{ $set : { \"name\": \"testA\"} }")); diff --git a/test/plugin/scenarios/mongodb-4.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/mongodb-4.x-scenario/config/expectedData.yaml index 299d8b45ea..7673d38268 100644 --- a/test/plugin/scenarios/mongodb-4.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/mongodb-4.x-scenario/config/expectedData.yaml @@ -47,6 +47,7 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/FindOperation @@ -62,9 +63,10 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: '{"name": "org"}'} skipAnalysis: 'false' - - operationName: MongoDB/MixedBulkWriteOperation + - operationName: MongoDB/AggregateOperation parentSpanId: 0 spanId: 4 spanLayer: Database @@ -77,11 +79,28 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} + - {key: db.bind_vars, value: '{"$match": {"name": "test"}},'} + skipAnalysis: 'false' + - operationName: MongoDB/MixedBulkWriteOperation + parentSpanId: 0 + spanId: 5 + spanLayer: Database + startTime: nq 0 + endTime: nq 0 + componentId: 42 + isError: false + spanType: Exit + peer: mongodb-server:27017 + tags: + - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: '{"name": "org"},'} skipAnalysis: 'false' - operationName: MongoDB/FindOperation parentSpanId: 0 - spanId: 5 + spanId: 6 spanLayer: Database startTime: nq 0 endTime: nq 0 @@ -92,11 +111,12 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: '{"name": "testA"}'} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation parentSpanId: 0 - spanId: 6 + spanId: 7 spanLayer: Database startTime: nq 0 endTime: nq 0 @@ -107,11 +127,12 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: '{"id": "1"},'} skipAnalysis: 'false' - operationName: MongoDB/DropDatabaseOperation parentSpanId: 0 - spanId: 7 + spanId: 8 spanLayer: Database startTime: nq 0 endTime: nq 0 diff --git a/test/plugin/scenarios/mongodb-4.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java b/test/plugin/scenarios/mongodb-4.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java index 33dfea5768..e50909855c 100644 --- a/test/plugin/scenarios/mongodb-4.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java +++ b/test/plugin/scenarios/mongodb-4.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/mongodb/controller/CaseController.java @@ -18,18 +18,25 @@ package org.apache.skywalking.apm.testcase.mongodb.controller; +import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.FindIterable; +import com.mongodb.client.model.Aggregates; +import com.mongodb.client.model.Filters; import org.bson.BsonDocument; import org.bson.Document; +import org.bson.conversions.Bson; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Arrays; +import java.util.List; + import static com.mongodb.client.model.Filters.eq; @RestController @@ -63,6 +70,13 @@ public String mongoDBCase() { FindIterable findIterable = collection.find(eq("name", "org")); findIterable.first(); + // AggregateOperation + List pipeline = Arrays.asList( + Aggregates.match(Filters.eq("name", "test")) + ); + AggregateIterable aggregateIterable = collection.aggregate(pipeline); + aggregateIterable.first(); + // MixedBulkWriteOperation collection.updateOne(eq("name", "org"), BsonDocument.parse("{ $set : { \"name\": \"testA\"} }")); From 3e648037f0b8eb00c38199564d5e214a469c4bc4 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Mon, 25 Nov 2024 00:58:52 +0800 Subject: [PATCH 04/10] support mongo db.instance and AggregateOperation test scenarios --- .../mongodb/v3/support/MongoSpanHelper.java | 72 ++++++++++++++----- ...ngoDBOperationExecutorInterceptorTest.java | 27 +++++++ .../MongoDBOperationExecutorInterceptor.java | 2 +- .../mongodb/v4/support/MongoSpanHelper.java | 13 ++-- ...ngoDBOperationExecutorInterceptorTest.java | 62 ++++++++++++---- 5 files changed, 138 insertions(+), 38 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java index 89d6efdc82..1aadb8ee76 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java @@ -19,6 +19,7 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.support; import com.mongodb.MongoNamespace; +import lombok.SneakyThrows; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag; @@ -38,26 +39,18 @@ public class MongoSpanHelper { private MongoSpanHelper() { } + @SneakyThrows public static void createExitSpan(String executeMethod, String remotePeer, Object operation) { AbstractSpan span = ContextManager.createExitSpan( - MongoConstants.MONGO_DB_OP_PREFIX + executeMethod, new ContextCarrier(), remotePeer); + MongoConstants.MONGO_DB_OP_PREFIX + executeMethod, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGO_DRIVER); Tags.DB_TYPE.set(span, MongoConstants.DB_TYPE); SpanLayer.asDB(span); - try { - MongoNamespace namespace = tryToGetMongoNamespace(operation); + Field[] declaredFields = operation.getClass().getDeclaredFields(); + MongoNamespace namespace = tryToGetMongoNamespace(operation, declaredFields); + if (namespace != null) { extractTagsFromNamespace(span, namespace); - } catch (Exception e) { - try { - Field wrappedField = operation.getClass().getDeclaredField("wrapped"); - Field.setAccessible(new Field[]{wrappedField}, true); - Object wrappedOperation = wrappedField.get(operation); - MongoNamespace namespace = tryToGetMongoNamespace(wrappedOperation); - extractTagsFromNamespace(span, namespace); - } catch (Exception e2) { - - } } if (MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM) { @@ -72,9 +65,54 @@ private static void extractTagsFromNamespace(AbstractSpan span, MongoNamespace n } } - private static MongoNamespace tryToGetMongoNamespace(Object operation) throws IllegalAccessException, NoSuchFieldException { - Field namespaceField = operation.getClass().getDeclaredField("namespace"); - Field.setAccessible(new Field[]{namespaceField}, true); - return (MongoNamespace) namespaceField.get(operation); + private static MongoNamespace tryToGetMongoNamespace(Object operation, Field[] declaredFields) throws IllegalAccessException { + Field namespaceField = null; + Field wrappedField = null; + Field databaseField = null; + Field collectionField = null; + for (Field field : declaredFields) { + if ("namespace".equals(field.getName())) { + namespaceField = field; + Field.setAccessible(new Field[]{field}, true); + } + if ("wrapped".equals(field.getName())) { + wrappedField = field; + Field.setAccessible(new Field[]{field}, true); + } + if ("databaseName".equals(field.getName())) { + databaseField = field; + Field.setAccessible(new Field[]{field}, true); + } + if ("collectionName".equals(field.getName())) { + collectionField = field; + Field.setAccessible(new Field[]{field}, true); + } + } + if (namespaceField != null) { + return (MongoNamespace) namespaceField.get(operation); + } + String database = null; + String collection = null; + if (databaseField != null) { + database = (String) databaseField.get(operation); + } + if (collectionField != null) { + collection = (String) collectionField.get(operation); + } + if (database != null && collection == null) { + return new MongoNamespace(database); + } + if (database != null && collection != null) { + return new MongoNamespace(database, collection); + } + if (wrappedField != null) { + Object wrapped = wrappedField.get(operation); + if (wrapped != null && wrapped != operation) { + Field[] declaredFieldsInWrapped = wrapped.getClass().getDeclaredFields(); + return tryToGetMongoNamespace(wrapped, declaredFieldsInWrapped); + } + } + return null; + } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java index 1955a170b4..26c805f9d4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java @@ -22,6 +22,7 @@ import com.mongodb.ReadConcern; import com.mongodb.client.internal.OperationExecutor; import com.mongodb.operation.AggregateOperation; +import com.mongodb.operation.CreateCollectionOperation; import com.mongodb.operation.FindOperation; import com.mongodb.operation.WriteOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; @@ -112,6 +113,20 @@ public void testIntercept() throws Throwable { assertMongoFindOperationSpan(spans.get(0)); } + @Test + public void testCreateCollectionOperationIntercept() throws Throwable { + CreateCollectionOperation createCollectionOperation = new CreateCollectionOperation("test", "user"); + Object[] arguments = {createCollectionOperation}; + Class[] argumentTypes = {createCollectionOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoCreateCollectionOperationSpan(spans.get(0)); + } + @Test public void testAggregateOperationIntercept() throws Throwable { MongoNamespace mongoNamespace = new MongoNamespace("test.user"); @@ -145,6 +160,18 @@ public void testInterceptFindOperationWithException() throws Throwable { SpanAssert.assertException(logDataEntities.get(0), RuntimeException.class); } + private void assertMongoCreateCollectionOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/CreateCollectionOperation")); + assertThat(SpanHelper.getComponentId(span), is(42)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(tags.get(3).getValue(), is("user")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); + } + private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java index 633b250398..dbdf7c6926 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/MongoDBOperationExecutorInterceptor.java @@ -37,7 +37,7 @@ public class MongoDBOperationExecutorInterceptor implements InstanceMethodsAroun @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) { - String executeMethod = argumentsTypes[0].getSimpleName(); + String executeMethod = allArguments[0].getClass().getSimpleName(); // OperationExecutor has included th remotePeer // See: MongoDBClientDelegateInterceptor.afterMethod String remotePeer = (String) objInst.getSkyWalkingDynamicField(); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java index fa7b3a96b5..472e3bf9c0 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java @@ -38,9 +38,10 @@ private MongoSpanHelper() { /** * createExitSpan + * * @param executeMethod executeMethod - * @param remotePeer remotePeer - * @param operation operation + * @param remotePeer remotePeer + * @param operation operation */ public static void createExitSpan(String executeMethod, String remotePeer, Object operation) { AbstractSpan span = ContextManager.createExitSpan( @@ -50,13 +51,15 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec SpanLayer.asDB(span); if (operation instanceof EnhancedInstance) { - Object namespaceObj = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); - if (namespaceObj != null) { - MongoNamespace namespace = (MongoNamespace) namespaceObj; + Object dynamicFieldValue = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (dynamicFieldValue != null && dynamicFieldValue instanceof MongoNamespace) { + MongoNamespace namespace = (MongoNamespace) dynamicFieldValue; Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); if (StringUtil.isNotEmpty(namespace.getCollectionName())) { span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); } + } else if (dynamicFieldValue != null && dynamicFieldValue instanceof String) { + Tags.DB_INSTANCE.set(span, (String) dynamicFieldValue); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java index 7fbcfe9e89..0e635388ba 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java @@ -18,15 +18,13 @@ package org.apache.skywalking.apm.plugin.mongodb.v4; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.List; - +import com.mongodb.MongoNamespace; +import com.mongodb.ReadConcern; +import com.mongodb.client.internal.OperationExecutor; import com.mongodb.internal.operation.AggregateOperation; +import com.mongodb.internal.operation.CreateCollectionOperation; +import com.mongodb.internal.operation.FindOperation; +import com.mongodb.internal.operation.WriteOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -58,11 +56,16 @@ import org.mockito.Spy; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; -import com.mongodb.MongoNamespace; -import com.mongodb.ReadConcern; -import com.mongodb.client.internal.OperationExecutor; -import com.mongodb.internal.operation.FindOperation; -import com.mongodb.internal.operation.WriteOperation; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(TracingSegmentRunner.class) public class MongoDBOperationExecutorInterceptorTest { @@ -136,6 +139,24 @@ public void testConstructIntercept() throws Throwable { MatcherAssert.assertThat(enhancedObjInstance.getSkyWalkingDynamicField(), Is.is(mongoNamespace)); } + @Test + public void testCreateCollectionOperationIntercept() throws Throwable { + CreateCollectionOperation createCollectionOperation = new CreateCollectionOperation("test", "user"); + CreateCollectionOperation enhancedInstanceForCreateCollectionOperation = mock(CreateCollectionOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn("test"); + when(enhancedInstanceForCreateCollectionOperation.getCollectionName()).thenReturn("user"); + + Object[] arguments = {enhancedInstanceForCreateCollectionOperation}; + Class[] argumentTypes = {createCollectionOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoCreateCollectionOperationSpan(spans.get(0)); + } + @Test public void testIntercept() throws Throwable { interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); @@ -185,7 +206,7 @@ public void testInterceptWithException() throws Throwable { } private void assertMongoFindOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/FindOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); @@ -196,8 +217,19 @@ private void assertMongoFindOperationSpan(AbstractTracingSpan span) { assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); } + private void assertMongoCreateCollectionOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), startsWith("MongoDB/CreateCollectionOperation")); + assertThat(SpanHelper.getComponentId(span), is(42)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(tags.get(1).getValue(), is("test")); + assertThat(tags.get(2).getValue(), is("user")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), CoreMatchers.is(SpanLayer.DB)); + } + private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); From 4130d1fd7a03f9615a3a3363978c19d808ce8161 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Mon, 25 Nov 2024 01:47:32 +0800 Subject: [PATCH 05/10] support mongo db.instance and AggregateOperation test scenarios --- .../scenarios/mongodb-3.x-scenario/config/expectedData.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml index 1cbddfae0d..36987cac9b 100644 --- a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml @@ -32,6 +32,7 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} + - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation From 12b4b046ecda8d29d22345aec1798cb5b83c88b7 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Tue, 26 Nov 2024 04:00:32 +0800 Subject: [PATCH 06/10] =?UTF-8?q?support=20mongo=20db.instance=20tag=20?= =?UTF-8?q?=E3=80=81db.collection=20and=20AggregateOperation:=20test=20sce?= =?UTF-8?q?narios=20and=20=20remove=20reflect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...regateExplainOperationInstrumentation.java | 64 +++++++++++++++++ ...AggregateOperationImplInstrumentation.java | 64 +++++++++++++++++ .../AggregateOperationInstrumentation.java | 64 +++++++++++++++++ .../ChangeStreamOperationInstrumentation.java | 65 +++++++++++++++++ .../CommandReadOperationInstrumentation.java | 65 +++++++++++++++++ .../CountOperationInstrumentation.java | 65 +++++++++++++++++ .../DistinctOperationInstrumentation.java | 64 +++++++++++++++++ .../FindOperationInstrumentation.java | 64 +++++++++++++++++ ...stCollectionsOperationInstrumentation.java | 63 +++++++++++++++++ .../ListIndexesOperationInstrumentation.java | 64 +++++++++++++++++ ...InlineResultsOperationInstrumentation.java | 64 +++++++++++++++++ ...MapReduceReadOperationInstrumentation.java | 64 +++++++++++++++++ ...MapReduceReadOperationInstrumentation.java | 64 +++++++++++++++++ ...eToCollectionOperationInstrumentation.java | 64 +++++++++++++++++ ...FindAndModifyOperationInstrumentation.java | 64 +++++++++++++++++ .../BaseWriteOperationInstrumentation.java | 64 +++++++++++++++++ .../CommandWriteOperationInstrumentation.java | 63 +++++++++++++++++ ...ateCollectionOperationInstrumentation.java | 63 +++++++++++++++++ ...CreateIndexesOperationInstrumentation.java | 64 +++++++++++++++++ .../CreateViewOperationInstrumentation.java | 63 +++++++++++++++++ ...ropCollectionOperationInstrumentation.java | 64 +++++++++++++++++ .../DropDatabaseOperationInstrumentation.java | 64 +++++++++++++++++ .../DropIndexOperationInstrumentation.java | 64 +++++++++++++++++ ...eToCollectionOperationInstrumentation.java | 64 +++++++++++++++++ ...ixedBulkWriteOperationInstrumentation.java | 64 +++++++++++++++++ ...ameCollectionOperationInstrumentation.java | 64 +++++++++++++++++ ...OperationDatabaseConstructInterceptor.java | 32 +++++++++ ...perationNamespaceConstructInterceptor.java | 33 +++++++++ ...ppedMapReduceReadOperationInterceptor.java | 34 +++++++++ .../mongodb/v3/support/MongoSpanHelper.java | 69 ++++--------------- .../src/main/resources/skywalking-plugin.def | 31 ++++++++- .../v30/MongoDBInterceptorTest.java | 55 +++++++++------ ...ngoDBOperationExecutorInterceptorTest.java | 28 ++++++-- ...ppedMapReduceReadOperationInterceptor.java | 3 +- .../config/expectedData.yaml | 3 +- 35 files changed, 1862 insertions(+), 89 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateExplainOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ChangeStreamOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CommandReadOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/DistinctOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/FindOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListCollectionsOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListIndexesOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/MapReduceWithInlineResultsOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/WrappedMapReduceReadOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/v36/readOperation/WrappedMapReduceReadOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/AggregateToCollectionOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseFindAndModifyOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseWriteOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CommandWriteOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateCollectionOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateIndexesOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateViewOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropCollectionOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropDatabaseOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropIndexOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MapReduceToCollectionOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MixedBulkWriteOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/RenameCollectionOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateExplainOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateExplainOperationInstrumentation.java new file mode 100644 index 0000000000..929935a2c1 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateExplainOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class AggregateExplainOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateExplainOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java new file mode 100644 index 0000000000..de0a7f3548 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class AggregateOperationImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(4); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java new file mode 100644 index 0000000000..6709afde69 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class AggregateOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperationImpl"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(5); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ChangeStreamOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ChangeStreamOperationInstrumentation.java new file mode 100644 index 0000000000..f71ba7e4db --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ChangeStreamOperationInstrumentation.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class ChangeStreamOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.ChangeStreamOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(5); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CommandReadOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CommandReadOperationInstrumentation.java new file mode 100644 index 0000000000..ab317963f2 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CommandReadOperationInstrumentation.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class CommandReadOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CommandReadOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java new file mode 100644 index 0000000000..177d5c31cd --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class CountOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CountOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(2); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/DistinctOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/DistinctOperationInstrumentation.java new file mode 100644 index 0000000000..43cf1f9db3 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/DistinctOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class DistinctOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.DistinctOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/FindOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/FindOperationInstrumentation.java new file mode 100644 index 0000000000..702d78138a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/FindOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class FindOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.FindOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListCollectionsOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListCollectionsOperationInstrumentation.java new file mode 100644 index 0000000000..0eff3237db --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListCollectionsOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class ListCollectionsOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.ListCollectionsOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListIndexesOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListIndexesOperationInstrumentation.java new file mode 100644 index 0000000000..7a1870badc --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ListIndexesOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class ListIndexesOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.ListIndexesOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/MapReduceWithInlineResultsOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/MapReduceWithInlineResultsOperationInstrumentation.java new file mode 100644 index 0000000000..e0fcadfecf --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/MapReduceWithInlineResultsOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class MapReduceWithInlineResultsOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.MapReduceWithInlineResultsOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/WrappedMapReduceReadOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/WrappedMapReduceReadOperationInstrumentation.java new file mode 100644 index 0000000000..4e76a8e06c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/WrappedMapReduceReadOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class WrappedMapReduceReadOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.MapReduceIterableImpl"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/v36/readOperation/WrappedMapReduceReadOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/v36/readOperation/WrappedMapReduceReadOperationInstrumentation.java new file mode 100644 index 0000000000..c3fabf603e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/v36/readOperation/WrappedMapReduceReadOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.v36.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class WrappedMapReduceReadOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.client.internal.MapReduceIterableImpl$WrappedMapReduceReadOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.WrappedMapReduceReadOperationInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/AggregateToCollectionOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/AggregateToCollectionOperationInstrumentation.java new file mode 100644 index 0000000000..62bde0f7f8 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/AggregateToCollectionOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class AggregateToCollectionOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateToCollectionOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(5); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseFindAndModifyOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseFindAndModifyOperationInstrumentation.java new file mode 100644 index 0000000000..bb1c0445ee --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseFindAndModifyOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class BaseFindAndModifyOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.BaseFindAndModifyOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseWriteOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseWriteOperationInstrumentation.java new file mode 100644 index 0000000000..fdb6b8ffe3 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/BaseWriteOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class BaseWriteOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.BaseWriteOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(4); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CommandWriteOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CommandWriteOperationInstrumentation.java new file mode 100644 index 0000000000..77d3d4f165 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CommandWriteOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class CommandWriteOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CommandWriteOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateCollectionOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateCollectionOperationInstrumentation.java new file mode 100644 index 0000000000..a5f45f68e3 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateCollectionOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class CreateCollectionOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CreateCollectionOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(3); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateIndexesOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateIndexesOperationInstrumentation.java new file mode 100644 index 0000000000..556205173a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateIndexesOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class CreateIndexesOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CreateIndexesOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(3); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateViewOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateViewOperationInstrumentation.java new file mode 100644 index 0000000000..e16c434818 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/CreateViewOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class CreateViewOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.CreateViewOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropCollectionOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropCollectionOperationInstrumentation.java new file mode 100644 index 0000000000..88a04f6900 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropCollectionOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class DropCollectionOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.DropCollectionOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(2); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropDatabaseOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropDatabaseOperationInstrumentation.java new file mode 100644 index 0000000000..88450fec86 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropDatabaseOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class DropDatabaseOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.DropDatabaseOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(2); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropIndexOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropIndexOperationInstrumentation.java new file mode 100644 index 0000000000..8be077745b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/DropIndexOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class DropIndexOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.DropIndexOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(3); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MapReduceToCollectionOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MapReduceToCollectionOperationInstrumentation.java new file mode 100644 index 0000000000..cc72bd315b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MapReduceToCollectionOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class MapReduceToCollectionOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.MapReduceToCollectionOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(5); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MixedBulkWriteOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MixedBulkWriteOperationInstrumentation.java new file mode 100644 index 0000000000..44a90e3001 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/MixedBulkWriteOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class MixedBulkWriteOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.MixedBulkWriteOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/RenameCollectionOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/RenameCollectionOperationInstrumentation.java new file mode 100644 index 0000000000..95c9a4fdb9 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/writeOperation/RenameCollectionOperationInstrumentation.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class RenameCollectionOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.RenameCollectionOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(3); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java new file mode 100644 index 0000000000..2bed7eb491 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; + +public class OperationDatabaseConstructInterceptor implements InstanceConstructorInterceptor { + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + String databaseName = (String) allArguments[0]; + objInst.setSkyWalkingDynamicField(databaseName); + } + +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java new file mode 100644 index 0000000000..5c7c307906 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation; + +import com.mongodb.MongoNamespace; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; + +public class OperationNamespaceConstructInterceptor implements InstanceConstructorInterceptor { + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + MongoNamespace mongoNamespace = (MongoNamespace) allArguments[0]; + objInst.setSkyWalkingDynamicField(mongoNamespace); + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java new file mode 100644 index 0000000000..84bd5b34d8 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; + +public class WrappedMapReduceReadOperationInterceptor implements InstanceConstructorInterceptor { + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + if (allArguments[0] instanceof EnhancedInstance) { + EnhancedInstance enhancedInstance = (EnhancedInstance) allArguments[0]; + objInst.setSkyWalkingDynamicField(enhancedInstance.getSkyWalkingDynamicField()); + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java index 1aadb8ee76..e5bfcf7a43 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java @@ -26,12 +26,11 @@ import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.mongodb.v3.MongoPluginConfig; import org.apache.skywalking.apm.util.StringUtil; -import java.lang.reflect.Field; - public class MongoSpanHelper { private static final AbstractTag DB_COLLECTION_TAG = Tags.ofKey("db.collection"); @@ -47,10 +46,17 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec Tags.DB_TYPE.set(span, MongoConstants.DB_TYPE); SpanLayer.asDB(span); - Field[] declaredFields = operation.getClass().getDeclaredFields(); - MongoNamespace namespace = tryToGetMongoNamespace(operation, declaredFields); - if (namespace != null) { - extractTagsFromNamespace(span, namespace); + if (operation instanceof EnhancedInstance) { + Object dynamicFieldValue = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (dynamicFieldValue != null && dynamicFieldValue instanceof MongoNamespace) { + MongoNamespace namespace = (MongoNamespace) dynamicFieldValue; + Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); + if (StringUtil.isNotEmpty(namespace.getCollectionName())) { + span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); + } + } else if (dynamicFieldValue != null && dynamicFieldValue instanceof String) { + Tags.DB_INSTANCE.set(span, (String) dynamicFieldValue); + } } if (MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM) { @@ -64,55 +70,4 @@ private static void extractTagsFromNamespace(AbstractSpan span, MongoNamespace n span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); } } - - private static MongoNamespace tryToGetMongoNamespace(Object operation, Field[] declaredFields) throws IllegalAccessException { - Field namespaceField = null; - Field wrappedField = null; - Field databaseField = null; - Field collectionField = null; - for (Field field : declaredFields) { - if ("namespace".equals(field.getName())) { - namespaceField = field; - Field.setAccessible(new Field[]{field}, true); - } - if ("wrapped".equals(field.getName())) { - wrappedField = field; - Field.setAccessible(new Field[]{field}, true); - } - if ("databaseName".equals(field.getName())) { - databaseField = field; - Field.setAccessible(new Field[]{field}, true); - } - if ("collectionName".equals(field.getName())) { - collectionField = field; - Field.setAccessible(new Field[]{field}, true); - } - } - if (namespaceField != null) { - return (MongoNamespace) namespaceField.get(operation); - } - String database = null; - String collection = null; - if (databaseField != null) { - database = (String) databaseField.get(operation); - } - if (collectionField != null) { - collection = (String) collectionField.get(operation); - } - if (database != null && collection == null) { - return new MongoNamespace(database); - } - if (database != null && collection != null) { - return new MongoNamespace(database, collection); - } - if (wrappedField != null) { - Object wrapped = wrappedField.get(operation); - if (wrapped != null && wrapped != operation) { - Field[] declaredFieldsInWrapped = wrapped.getClass().getDeclaredFields(); - return tryToGetMongoNamespace(wrapped, declaredFieldsInWrapped); - } - } - return null; - - } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def index 8416fd8e52..dd8c6fda5f 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -19,8 +19,37 @@ mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v30.MongoDBInstru # v3.7.x~ mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v37.MongoDBClientDelegateInstrumentation mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v37.MongoDBOperationExecutorInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v36.readOperation.WrappedMapReduceReadOperationInstrumentation # v3.8.x~ mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v38.MongoDBOperationExecutorInstrumentation # v3.6.x mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v36.MongoDBInstrumentation -mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v36.MongoDBOperationExecutorInstrumentation \ No newline at end of file +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.v36.MongoDBOperationExecutorInstrumentation + +# readOperation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.AggregateExplainOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.AggregateOperationImplInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.AggregateOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ChangeStreamOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.CommandReadOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.CountOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.DistinctOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.FindOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ListCollectionsOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ListIndexesOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.MapReduceWithInlineResultsOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.WrappedMapReduceReadOperationInstrumentation +# writeOperation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.AggregateToCollectionOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.BaseFindAndModifyOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.BaseWriteOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.CommandWriteOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.CreateCollectionOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.CreateIndexesOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.CreateViewOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.DropCollectionOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.DropDatabaseOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.DropIndexOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.MapReduceToCollectionOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.MixedBulkWriteOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.RenameCollectionOperationInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java index aa66b4db49..c49a9ae4ea 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java @@ -18,15 +18,10 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.v30; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.List; - +import com.mongodb.Mongo; +import com.mongodb.MongoNamespace; import com.mongodb.operation.AggregateOperation; +import com.mongodb.operation.FindOperation; import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -51,11 +46,19 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; -import com.mongodb.Mongo; -import com.mongodb.MongoNamespace; -import com.mongodb.operation.FindOperation; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(TracingSegmentRunner.class) public class MongoDBInterceptorTest { @@ -70,12 +73,16 @@ public class MongoDBInterceptorTest { private MongoDBInterceptor interceptor; + private FindOperation enhancedInstanceForFindOperation; + @Mock private EnhancedInstance enhancedInstance; - private Decoder decoder; private Object[] arguments; + private Class[] argumentTypes; + private Decoder decoder; + private MongoNamespace mongoNamespace; @SuppressWarnings({ "rawtypes", @@ -92,12 +99,14 @@ public void setUp() throws Exception { BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); - MongoNamespace mongoNamespace = new MongoNamespace("test.user"); + mongoNamespace = new MongoNamespace("test.user"); decoder = mock(Decoder.class); FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); - - arguments = new Object[] {findOperation}; + enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); + arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; } @@ -109,7 +118,7 @@ public void testIntercept() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertRedisSpan(spans.get(0)); + assertFindOperationSpan(spans.get(0)); } @Test @@ -118,7 +127,11 @@ public void testAggregateOperationIntercept() throws Throwable { BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); List pipeline = Collections.singletonList(matchStage); AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); - Object[] arguments = {aggregateOperation}; + + AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); + Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), arguments, argumentTypes, null); @@ -140,14 +153,14 @@ public void testInterceptWithException() throws Throwable { MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); List spans = SegmentHelper.getSpans(traceSegment); - assertRedisSpan(spans.get(0)); + assertFindOperationSpan(spans.get(0)); List logDataEntities = SpanHelper.getLogs(spans.get(0)); assertThat(logDataEntities.size(), is(1)); SpanAssert.assertException(logDataEntities.get(0), RuntimeException.class); } - private void assertRedisSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/FindOperation")); + private void assertFindOperationSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), startsWith("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); @@ -159,7 +172,7 @@ private void assertRedisSpan(AbstractTracingSpan span) { } private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java index 26c805f9d4..f1822d8953 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java @@ -49,6 +49,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; @@ -57,6 +58,7 @@ import java.util.List; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -75,6 +77,8 @@ public class MongoDBOperationExecutorInterceptorTest { @Mock private EnhancedInstance enhancedInstance; + private FindOperation enhancedInstanceForFindOperation; + private MongoDBOperationExecutorInterceptor interceptor; private Object[] arguments; @@ -98,7 +102,10 @@ public void setUp() { decoder = mock(Decoder.class); FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); - arguments = new Object[] {findOperation}; + enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); + arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; } @@ -116,8 +123,12 @@ public void testIntercept() throws Throwable { @Test public void testCreateCollectionOperationIntercept() throws Throwable { CreateCollectionOperation createCollectionOperation = new CreateCollectionOperation("test", "user"); - Object[] arguments = {createCollectionOperation}; + CreateCollectionOperation enhancedInstanceForCreateCollectionOperation = mock(CreateCollectionOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForCreateCollectionOperation.getCollectionName()).thenReturn("user"); + Object[] arguments = {enhancedInstanceForCreateCollectionOperation}; Class[] argumentTypes = {createCollectionOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); @@ -133,8 +144,13 @@ public void testAggregateOperationIntercept() throws Throwable { BsonDocument matchStage = new BsonDocument("$match", new BsonDocument("name", new BsonString("by"))); List pipeline = Collections.singletonList(matchStage); AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); - Object[] arguments = {aggregateOperation}; + + AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); + Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; + interceptor.beforeMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); interceptor.afterMethod(enhancedInstance, getMethod(), arguments, argumentTypes, null); @@ -161,7 +177,7 @@ public void testInterceptFindOperationWithException() throws Throwable { } private void assertMongoCreateCollectionOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/CreateCollectionOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/CreateCollectionOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); @@ -173,7 +189,7 @@ private void assertMongoCreateCollectionOperationSpan(AbstractTracingSpan span) } private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/AggregateOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/AggregateOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); @@ -185,7 +201,7 @@ private void assertMongoAggregateOperationSpan(AbstractTracingSpan span) { } private void assertMongoFindOperationSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/FindOperation")); + assertThat(span.getOperationName(), startsWith("MongoDB/FindOperation")); assertThat(SpanHelper.getComponentId(span), is(42)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java index 39c4699ffb..5e124741eb 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java @@ -27,8 +27,7 @@ public class WrappedMapReduceReadOperationInterceptor implements InstanceConstru public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { if (allArguments[0] instanceof EnhancedInstance) { EnhancedInstance enhancedInstance = (EnhancedInstance) allArguments[0]; - String databaseName = (String) enhancedInstance.getSkyWalkingDynamicField(); - objInst.setSkyWalkingDynamicField(databaseName); + objInst.setSkyWalkingDynamicField(enhancedInstance.getSkyWalkingDynamicField()); } } diff --git a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml index 36987cac9b..e40a4cdb29 100644 --- a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml @@ -32,7 +32,6 @@ segmentItems: tags: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} - - {key: db.collection, value: testCollection} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation @@ -81,7 +80,7 @@ segmentItems: - {key: db.type, value: MongoDB} - {key: db.instance, value: test-database} - {key: db.collection, value: testCollection} - - {key: db.bind_vars, value: '{"$match": {"name": "test"}},'} + - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: MongoDB/MixedBulkWriteOperation parentSpanId: 0 From 13e6c506db0dd8fcecf4ba744f2ea83c8cd60eeb Mon Sep 17 00:00:00 2001 From: youjie_li Date: Tue, 26 Nov 2024 11:24:30 +0800 Subject: [PATCH 07/10] =?UTF-8?q?support=20mongo=20db.instance=20tag=20?= =?UTF-8?q?=E3=80=81db.collection=20and=20AggregateOperation:=20test=20sce?= =?UTF-8?q?narios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AggregateOperationImplInstrumentation.java | 6 +++--- .../readOperation/AggregateOperationInstrumentation.java | 6 +++--- .../define/readOperation/CountOperationInstrumentation.java | 4 ++-- .../AggregateOperationImplInstrumentation.java | 4 ++-- .../readOperation/AggregateOperationInstrumentation.java | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java index de0a7f3548..208d0a2146 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationImplInstrumentation.java @@ -26,11 +26,11 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.any; public class AggregateOperationImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { - private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperation"; + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperationImpl"; private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; @@ -46,7 +46,7 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(4); + return any(); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java index 6709afde69..bffd857fcb 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/AggregateOperationInstrumentation.java @@ -26,11 +26,11 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.any; public class AggregateOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { - private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperationImpl"; + private static final String ENHANCE_CLASS = "com.mongodb.operation.AggregateOperation"; private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; @@ -46,7 +46,7 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(5); + return any(); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java index 177d5c31cd..6ee8e1ea29 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/CountOperationInstrumentation.java @@ -26,7 +26,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.any; public class CountOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -46,7 +46,7 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(2); + return any(); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationImplInstrumentation.java index 49cb53e085..1e50102399 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationImplInstrumentation.java @@ -26,7 +26,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.any; public class AggregateOperationImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -53,7 +53,7 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(4); + return any(); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationInstrumentation.java index b95124195b..6e56abd504 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/define/readOperation/AggregateOperationInstrumentation.java @@ -26,7 +26,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.any; public class AggregateOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -53,7 +53,7 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(5); + return any(); } @Override From 21336992a5e5fb0aa535c12432bf6cd9cd740a7e Mon Sep 17 00:00:00 2001 From: youjie_li Date: Tue, 26 Nov 2024 11:54:56 +0800 Subject: [PATCH 08/10] =?UTF-8?q?support=20mongo=20db.instance=20tag=20?= =?UTF-8?q?=E3=80=81db.collection=20and=20AggregateOperation:=20test=20sce?= =?UTF-8?q?narios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scenarios/mongodb-3.x-scenario/config/expectedData.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml index e40a4cdb29..a050100ce9 100644 --- a/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/mongodb-3.x-scenario/config/expectedData.yaml @@ -142,6 +142,7 @@ segmentItems: peer: mongodb-server:27017 tags: - {key: db.type, value: MongoDB} + - {key: db.instance, value: test-database} - {key: db.bind_vars, value: not null} skipAnalysis: 'false' - operationName: GET:/mongodb-case/case/mongodb From 116c29ae6cdfd10ab6f0dfa1425350b034176aa3 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Thu, 28 Nov 2024 02:45:18 +0800 Subject: [PATCH 09/10] support mongo db.instance tag,db.collection tag and AggregateOperation:code review --- CHANGES.md | 1 + .../GroupOperationInstrumentation.java | 63 +++++++++++++ ...ollectionScanOperationInstrumentation.java | 63 +++++++++++++ .../UserExistsOperationInstrumentation.java | 63 +++++++++++++ ...OperationDatabaseConstructInterceptor.java | 3 +- ...perationNamespaceConstructInterceptor.java | 3 +- ...ppedMapReduceReadOperationInterceptor.java | 2 - .../v3/support/MongoNamespaceInfo.java | 94 +++++++++++++++++++ .../mongodb/v3/support/MongoSpanHelper.java | 22 ++--- .../src/main/resources/skywalking-plugin.def | 3 + .../v30/MongoDBInterceptorTest.java | 5 +- ...ngoDBOperationExecutorInterceptorTest.java | 7 +- ...OperationDatabaseConstructInterceptor.java | 3 +- ...perationNamespaceConstructInterceptor.java | 3 +- ...ppedMapReduceReadOperationInterceptor.java | 2 - .../v4/support/MongoNamespaceInfo.java | 94 +++++++++++++++++++ .../mongodb/v4/support/MongoSpanHelper.java | 16 ++-- ...ngoDBOperationExecutorInterceptorTest.java | 16 ++-- 18 files changed, 420 insertions(+), 43 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/GroupOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ParallelCollectionScanOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/UserExistsOperationInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java diff --git a/CHANGES.md b/CHANGES.md index c2c322b280..c80b2ac858 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Release Notes. * Fix intermittent ClassCircularityError by preloading ThreadLocalRandom since ByteBuddy 1.12.11 * Add witness class/method for resteasy-server plugin(v3/v4/v6) * Add async-profiler feature for performance analysis +* Support db.instance tag,db.collection tag and AggregateOperation span for mongodb plugin(3.x/4.x) All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/222?closed=1) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/GroupOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/GroupOperationInstrumentation.java new file mode 100644 index 0000000000..0ad153cdef --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/GroupOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class GroupOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.GroupOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ParallelCollectionScanOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ParallelCollectionScanOperationInstrumentation.java new file mode 100644 index 0000000000..554d178570 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/ParallelCollectionScanOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class ParallelCollectionScanOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.ParallelCollectionScanOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/UserExistsOperationInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/UserExistsOperationInstrumentation.java new file mode 100644 index 0000000000..393b0096a7 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/define/readOperation/UserExistsOperationInstrumentation.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; + +public class UserExistsOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.operation.UserExistsOperation"; + + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPTOR_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java index 2bed7eb491..a948de5128 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java @@ -20,13 +20,14 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo; public class OperationDatabaseConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { String databaseName = (String) allArguments[0]; - objInst.setSkyWalkingDynamicField(databaseName); + objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(databaseName)); } } \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java index 5c7c307906..f6c97a6d37 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java @@ -21,13 +21,14 @@ import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo; public class OperationNamespaceConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { MongoNamespace mongoNamespace = (MongoNamespace) allArguments[0]; - objInst.setSkyWalkingDynamicField(mongoNamespace); + objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(mongoNamespace)); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java index 84bd5b34d8..3cb37c8cb5 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java @@ -25,10 +25,8 @@ public class WrappedMapReduceReadOperationInterceptor implements InstanceConstru @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - if (allArguments[0] instanceof EnhancedInstance) { EnhancedInstance enhancedInstance = (EnhancedInstance) allArguments[0]; objInst.setSkyWalkingDynamicField(enhancedInstance.getSkyWalkingDynamicField()); - } } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java new file mode 100644 index 0000000000..3330638865 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v3.support; + +import com.mongodb.MongoNamespace; +import com.mongodb.annotations.Immutable; +import org.apache.skywalking.apm.util.StringUtil; + +@Immutable +public class MongoNamespaceInfo { + + private final String databaseName; + private final String collectionName; + + public MongoNamespaceInfo(String databaseName) { + this(databaseName, null); + } + + public MongoNamespaceInfo(MongoNamespace mongoNamespace) { + this(mongoNamespace.getDatabaseName(), mongoNamespace.getCollectionName()); + } + + public MongoNamespaceInfo(String databaseName, String collectionName) { + this.databaseName = databaseName; + this.collectionName = collectionName; + } + + public String getDatabaseName() { + return databaseName; + } + + public String getCollectionName() { + return collectionName; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (o != null && this.getClass() == o.getClass()) { + MongoNamespaceInfo that = (MongoNamespaceInfo) o; + if (!equals(this.databaseName, that.databaseName)) { + return false; + } else { + return equals(this.collectionName, that.collectionName); + } + } else { + return false; + } + } + + private boolean equals(String src, String tgt) { + if (src == null && tgt == null) { + return true; + } + if (src == null && tgt != null) { + return false; + } + if (src != null && tgt == null) { + return false; + } + return src.equals(tgt); + } + + public String toString() { + if (StringUtil.isNotBlank(collectionName)) { + return databaseName + '.' + collectionName; + } else { + return databaseName; + } + } + + public int hashCode() { + int result = this.databaseName.hashCode(); + result = 31 * result + this.collectionName.hashCode(); + return result; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java index e5bfcf7a43..c7a88151a4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.support; -import com.mongodb.MongoNamespace; import lombok.SneakyThrows; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; @@ -47,15 +46,14 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec SpanLayer.asDB(span); if (operation instanceof EnhancedInstance) { - Object dynamicFieldValue = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); - if (dynamicFieldValue != null && dynamicFieldValue instanceof MongoNamespace) { - MongoNamespace namespace = (MongoNamespace) dynamicFieldValue; - Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); - if (StringUtil.isNotEmpty(namespace.getCollectionName())) { - span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); + MongoNamespaceInfo mongoNamespaceInfo = (MongoNamespaceInfo) ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (mongoNamespaceInfo != null) { + if (StringUtil.isNotEmpty(mongoNamespaceInfo.getDatabaseName())) { + Tags.DB_INSTANCE.set(span, mongoNamespaceInfo.getDatabaseName()); + } + if (StringUtil.isNotEmpty(mongoNamespaceInfo.getCollectionName())) { + span.tag(DB_COLLECTION_TAG, mongoNamespaceInfo.getCollectionName()); } - } else if (dynamicFieldValue != null && dynamicFieldValue instanceof String) { - Tags.DB_INSTANCE.set(span, (String) dynamicFieldValue); } } @@ -64,10 +62,4 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec } } - private static void extractTagsFromNamespace(AbstractSpan span, MongoNamespace namespace) { - Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); - if (StringUtil.isNotEmpty(namespace.getCollectionName())) { - span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); - } - } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def index dd8c6fda5f..8e6b05fdf1 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -39,6 +39,9 @@ mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.Lis mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ListIndexesOperationInstrumentation mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.MapReduceWithInlineResultsOperationInstrumentation mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.WrappedMapReduceReadOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.GroupOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ParallelCollectionScanOperationInstrumentation +mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.UserExistsOperationInstrumentation # writeOperation mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.AggregateToCollectionOperationInstrumentation mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.BaseFindAndModifyOperationInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java index c49a9ae4ea..2319ad54e3 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v30/MongoDBInterceptorTest.java @@ -36,6 +36,7 @@ import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.plugin.mongodb.v3.MongoPluginConfig; +import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo; import org.bson.BsonDocument; import org.bson.BsonString; import org.bson.codecs.Decoder; @@ -104,7 +105,7 @@ public void setUp() throws Exception { FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; @@ -129,7 +130,7 @@ public void testAggregateOperationIntercept() throws Throwable { AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java index f1822d8953..8c6112e7fc 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/v37/MongoDBOperationExecutorInterceptorTest.java @@ -39,6 +39,7 @@ import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.plugin.mongodb.v3.MongoPluginConfig; +import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo; import org.bson.BsonDocument; import org.bson.BsonString; import org.bson.codecs.Decoder; @@ -103,7 +104,7 @@ public void setUp() { FindOperation findOperation = new FindOperation(mongoNamespace, decoder); findOperation.filter(document); enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; @@ -124,7 +125,7 @@ public void testIntercept() throws Throwable { public void testCreateCollectionOperationIntercept() throws Throwable { CreateCollectionOperation createCollectionOperation = new CreateCollectionOperation("test", "user"); CreateCollectionOperation enhancedInstanceForCreateCollectionOperation = mock(CreateCollectionOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForCreateCollectionOperation.getCollectionName()).thenReturn("user"); Object[] arguments = {enhancedInstanceForCreateCollectionOperation}; Class[] argumentTypes = {createCollectionOperation.getClass()}; @@ -146,7 +147,7 @@ public void testAggregateOperationIntercept() throws Throwable { AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationDatabaseConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationDatabaseConstructInterceptor.java index a643e3f201..850b625153 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationDatabaseConstructInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationDatabaseConstructInterceptor.java @@ -20,13 +20,14 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v4.support.MongoNamespaceInfo; public class OperationDatabaseConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { String databaseName = (String) allArguments[0]; - objInst.setSkyWalkingDynamicField(databaseName); + objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(databaseName)); } } \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java index 3d7846b15e..9a9fec49f0 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/OperationNamespaceConstructInterceptor.java @@ -21,13 +21,14 @@ import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v4.support.MongoNamespaceInfo; public class OperationNamespaceConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { MongoNamespace mongoNamespace = (MongoNamespace) allArguments[0]; - objInst.setSkyWalkingDynamicField(mongoNamespace); + objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(mongoNamespace)); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java index 5e124741eb..90614d3fc7 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java @@ -25,10 +25,8 @@ public class WrappedMapReduceReadOperationInterceptor implements InstanceConstru @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - if (allArguments[0] instanceof EnhancedInstance) { EnhancedInstance enhancedInstance = (EnhancedInstance) allArguments[0]; objInst.setSkyWalkingDynamicField(enhancedInstance.getSkyWalkingDynamicField()); - } } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java new file mode 100644 index 0000000000..6600b867ba --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.mongodb.v4.support; + +import com.mongodb.MongoNamespace; +import com.mongodb.annotations.Immutable; +import org.apache.skywalking.apm.util.StringUtil; + +@Immutable +public class MongoNamespaceInfo { + + private final String databaseName; + private final String collectionName; + + public MongoNamespaceInfo(String databaseName) { + this(databaseName, null); + } + + public MongoNamespaceInfo(MongoNamespace mongoNamespace) { + this(mongoNamespace.getDatabaseName(), mongoNamespace.getCollectionName()); + } + + public MongoNamespaceInfo(String databaseName, String collectionName) { + this.databaseName = databaseName; + this.collectionName = collectionName; + } + + public String getDatabaseName() { + return databaseName; + } + + public String getCollectionName() { + return collectionName; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (o != null && this.getClass() == o.getClass()) { + MongoNamespaceInfo that = (MongoNamespaceInfo) o; + if (!equals(this.databaseName, that.databaseName)) { + return false; + } else { + return equals(this.collectionName, that.collectionName); + } + } else { + return false; + } + } + + private boolean equals(String src, String tgt) { + if (src == null && tgt == null) { + return true; + } + if (src == null && tgt != null) { + return false; + } + if (src != null && tgt == null) { + return false; + } + return src.equals(tgt); + } + + public String toString() { + if (StringUtil.isNotBlank(collectionName)) { + return databaseName + '.' + collectionName; + } else { + return databaseName; + } + } + + public int hashCode() { + int result = this.databaseName.hashCode(); + result = 31 * result + this.collectionName.hashCode(); + return result; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java index 472e3bf9c0..d87b66a825 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoSpanHelper.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.plugin.mongodb.v4.support; -import com.mongodb.MongoNamespace; import org.apache.skywalking.apm.agent.core.context.ContextCarrier; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag; @@ -51,15 +50,14 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec SpanLayer.asDB(span); if (operation instanceof EnhancedInstance) { - Object dynamicFieldValue = ((EnhancedInstance) operation).getSkyWalkingDynamicField(); - if (dynamicFieldValue != null && dynamicFieldValue instanceof MongoNamespace) { - MongoNamespace namespace = (MongoNamespace) dynamicFieldValue; - Tags.DB_INSTANCE.set(span, namespace.getDatabaseName()); - if (StringUtil.isNotEmpty(namespace.getCollectionName())) { - span.tag(DB_COLLECTION_TAG, namespace.getCollectionName()); + MongoNamespaceInfo mongoNamespaceInfo = (MongoNamespaceInfo) ((EnhancedInstance) operation).getSkyWalkingDynamicField(); + if (mongoNamespaceInfo != null) { + if (StringUtil.isNotEmpty(mongoNamespaceInfo.getDatabaseName())) { + Tags.DB_INSTANCE.set(span, mongoNamespaceInfo.getDatabaseName()); + } + if (StringUtil.isNotEmpty(mongoNamespaceInfo.getCollectionName())) { + span.tag(DB_COLLECTION_TAG, mongoNamespaceInfo.getCollectionName()); } - } else if (dynamicFieldValue != null && dynamicFieldValue instanceof String) { - Tags.DB_INSTANCE.set(span, (String) dynamicFieldValue); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java index 0e635388ba..4bb4b1213b 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/mongodb/v4/MongoDBOperationExecutorInterceptorTest.java @@ -39,6 +39,7 @@ import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.plugin.mongodb.v4.interceptor.MongoDBOperationExecutorInterceptor; +import org.apache.skywalking.apm.plugin.mongodb.v4.support.MongoNamespaceInfo; import org.apache.skywalking.apm.plugin.mongodb.v4.interceptor.operation.OperationNamespaceConstructInterceptor; import org.apache.skywalking.apm.plugin.mongodb.v4.support.MongoPluginConfig; import org.bson.BsonDocument; @@ -85,7 +86,7 @@ public class MongoDBOperationExecutorInterceptorTest { @Spy private EnhancedInstance enhancedObjInstance = new EnhancedInstance() { - private MongoNamespace namespace; + private MongoNamespaceInfo namespace; @Override public Object getSkyWalkingDynamicField() { @@ -94,7 +95,7 @@ public Object getSkyWalkingDynamicField() { @Override public void setSkyWalkingDynamicField(Object value) { - this.namespace = (MongoNamespace) value; + this.namespace = (MongoNamespaceInfo) value; } }; @@ -110,6 +111,8 @@ public void setSkyWalkingDynamicField(Object value) { private MongoNamespace mongoNamespace; + private MongoNamespaceInfo mongoNamespaceInfo; + @Before public void setUp() { @@ -119,6 +122,7 @@ public void setUp() { MongoPluginConfig.Plugin.MongoDB.TRACE_PARAM = true; decoder = mock(Decoder.class); mongoNamespace = new MongoNamespace("test.user"); + mongoNamespaceInfo = new MongoNamespaceInfo(mongoNamespace); BsonDocument document = new BsonDocument(); document.append("name", new BsonString("by")); @@ -127,7 +131,7 @@ public void setUp() { decoder = mock(Decoder.class); when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); enhancedInstanceForFindOperation = mock(FindOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForFindOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespaceInfo); when(enhancedInstanceForFindOperation.getFilter()).thenReturn(findOperation.getFilter()); arguments = new Object[] {enhancedInstanceForFindOperation}; argumentTypes = new Class[] {findOperation.getClass()}; @@ -136,14 +140,14 @@ public void setUp() { @Test public void testConstructIntercept() throws Throwable { constructInterceptor.onConstruct(enhancedObjInstance, new Object[]{mongoNamespace}); - MatcherAssert.assertThat(enhancedObjInstance.getSkyWalkingDynamicField(), Is.is(mongoNamespace)); + MatcherAssert.assertThat(enhancedObjInstance.getSkyWalkingDynamicField(), Is.is(new MongoNamespaceInfo(mongoNamespace))); } @Test public void testCreateCollectionOperationIntercept() throws Throwable { CreateCollectionOperation createCollectionOperation = new CreateCollectionOperation("test", "user"); CreateCollectionOperation enhancedInstanceForCreateCollectionOperation = mock(CreateCollectionOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn("test"); + when(((EnhancedInstance) enhancedInstanceForCreateCollectionOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo("test")); when(enhancedInstanceForCreateCollectionOperation.getCollectionName()).thenReturn("user"); Object[] arguments = {enhancedInstanceForCreateCollectionOperation}; @@ -176,7 +180,7 @@ public void testAggregateOperationIntercept() throws Throwable { AggregateOperation aggregateOperation = new AggregateOperation(mongoNamespace, pipeline, decoder); AggregateOperation enhancedInstanceForAggregateOperation = mock(AggregateOperation.class, Mockito.withSettings().extraInterfaces(EnhancedInstance.class)); - when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(mongoNamespace); + when(((EnhancedInstance) enhancedInstanceForAggregateOperation).getSkyWalkingDynamicField()).thenReturn(new MongoNamespaceInfo(mongoNamespace)); when(enhancedInstanceForAggregateOperation.getPipeline()).thenReturn(aggregateOperation.getPipeline()); Object[] arguments = {enhancedInstanceForAggregateOperation}; Class[] argumentTypes = {aggregateOperation.getClass()}; From 6da22d9e8c74904f3ed1b62e77c9ccbc38faa50e Mon Sep 17 00:00:00 2001 From: youjie_li Date: Thu, 28 Nov 2024 12:11:45 +0800 Subject: [PATCH 10/10] support mongo db.instance tag,db.collection tag and AggregateOperation:code review --- .../v3/support/MongoNamespaceInfo.java | 40 ++----------------- .../v4/support/MongoNamespaceInfo.java | 40 ++----------------- 2 files changed, 8 insertions(+), 72 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java index 3330638865..16ba7f8aae 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoNamespaceInfo.java @@ -19,10 +19,12 @@ package org.apache.skywalking.apm.plugin.mongodb.v3.support; import com.mongodb.MongoNamespace; -import com.mongodb.annotations.Immutable; +import lombok.EqualsAndHashCode; +import lombok.Getter; import org.apache.skywalking.apm.util.StringUtil; -@Immutable +@EqualsAndHashCode +@Getter public class MongoNamespaceInfo { private final String databaseName; @@ -49,34 +51,6 @@ public String getCollectionName() { return collectionName; } - public boolean equals(Object o) { - if (this == o) { - return true; - } else if (o != null && this.getClass() == o.getClass()) { - MongoNamespaceInfo that = (MongoNamespaceInfo) o; - if (!equals(this.databaseName, that.databaseName)) { - return false; - } else { - return equals(this.collectionName, that.collectionName); - } - } else { - return false; - } - } - - private boolean equals(String src, String tgt) { - if (src == null && tgt == null) { - return true; - } - if (src == null && tgt != null) { - return false; - } - if (src != null && tgt == null) { - return false; - } - return src.equals(tgt); - } - public String toString() { if (StringUtil.isNotBlank(collectionName)) { return databaseName + '.' + collectionName; @@ -85,10 +59,4 @@ public String toString() { } } - public int hashCode() { - int result = this.databaseName.hashCode(); - result = 31 * result + this.collectionName.hashCode(); - return result; - } - } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java index 6600b867ba..54d54b4d8a 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v4/support/MongoNamespaceInfo.java @@ -19,10 +19,12 @@ package org.apache.skywalking.apm.plugin.mongodb.v4.support; import com.mongodb.MongoNamespace; -import com.mongodb.annotations.Immutable; +import lombok.EqualsAndHashCode; +import lombok.Getter; import org.apache.skywalking.apm.util.StringUtil; -@Immutable +@EqualsAndHashCode +@Getter public class MongoNamespaceInfo { private final String databaseName; @@ -49,34 +51,6 @@ public String getCollectionName() { return collectionName; } - public boolean equals(Object o) { - if (this == o) { - return true; - } else if (o != null && this.getClass() == o.getClass()) { - MongoNamespaceInfo that = (MongoNamespaceInfo) o; - if (!equals(this.databaseName, that.databaseName)) { - return false; - } else { - return equals(this.collectionName, that.collectionName); - } - } else { - return false; - } - } - - private boolean equals(String src, String tgt) { - if (src == null && tgt == null) { - return true; - } - if (src == null && tgt != null) { - return false; - } - if (src != null && tgt == null) { - return false; - } - return src.equals(tgt); - } - public String toString() { if (StringUtil.isNotBlank(collectionName)) { return databaseName + '.' + collectionName; @@ -85,10 +59,4 @@ public String toString() { } } - public int hashCode() { - int result = this.databaseName.hashCode(); - result = 31 * result + this.collectionName.hashCode(); - return result; - } - }