From 06c017ff9736183e1334e4e8f18112faee6eb2e8 Mon Sep 17 00:00:00 2001 From: lytscu Date: Tue, 7 Nov 2017 16:52:15 +0800 Subject: [PATCH 01/22] add mongodb version 2.14.2 plugin --- .../apm-sdk-plugin/mongodb-2.x-plugin/pom.xml | 71 ++++++++++ .../v2/MongoDBV2MethodInterceptor.java | 82 ++++++++++++ .../v2/define/MongoDBV2Instrumentation.java | 103 +++++++++++++++ .../src/main/resources/skywalking-plugin.def | 1 + .../v2/MongoDBV2MethodInterceptorTest.java | 125 ++++++++++++++++++ apm-sniffer/apm-sdk-plugin/pom.xml | 1 + 6 files changed, 383 insertions(+) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml new file mode 100644 index 000000000..3c347b75b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml @@ -0,0 +1,71 @@ + + + + + + apm-sdk-plugin + org.skywalking + 3.2.4-2017 + + 4.0.0 + + mongodb-2.x-plugin + jar + + + UTF-8 + + + + + org.mongodb + mongo-java-driver + 2.14.2 + provided + + + org.mongodb + bson + 2.14.2 + provided + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + attach-sources + + jar + + + + + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java new file mode 100644 index 000000000..d55980a4c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -0,0 +1,82 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2; + +import com.mongodb.ServerAddress; +import com.mongodb.DB; +import java.lang.reflect.Method; +import java.util.List; +import org.skywalking.apm.agent.core.context.ContextCarrier; +import org.skywalking.apm.agent.core.context.ContextManager; +import org.skywalking.apm.agent.core.context.tag.Tags; +import org.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.network.trace.component.ComponentsDefine; + +/** + * @Auther liyuntao + */ + +public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { + + private static final String DB_TYPE = "MongoDB"; + + private static final String MONGO_DB_OP_PREFIX = "MongoDB/"; + + @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + String remotePeer = (String)objInst.getSkyWalkingDynamicField(); + AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + method.getName(), new ContextCarrier(), remotePeer); + span.setComponent(ComponentsDefine.MONGODB); + Tags.DB_TYPE.set(span, DB_TYPE); + SpanLayer.asDB(span); + + } + + @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + AbstractSpan activeSpan = ContextManager.activeSpan(); + activeSpan.errorOccurred(); + activeSpan.log(t); + } + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + DB db = (DB)allArguments[0]; + List servers = db.getMongo().getAllAddress(); + + StringBuilder peers = new StringBuilder(); + for (ServerAddress address : servers) { + peers.append(address.getHost() + ":" + address.getPort() + ";"); + } + + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java new file mode 100644 index 000000000..b2ba7d097 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * @Auther liyuntao + */ +public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.DBCollection"; + + private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; + + + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + } + }; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("getWriteConcern"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("getReadPreference"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 000000000..0544be0b5 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1 @@ +mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBV2Instrumentation diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java new file mode 100644 index 000000000..1352982ff --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java @@ -0,0 +1,125 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2; + +import com.mongodb.Mongo; +import java.lang.reflect.Method; +import java.util.List; +import org.hamcrest.MatcherAssert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +import org.skywalking.apm.agent.core.conf.Config; +import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.skywalking.apm.agent.core.context.trace.LogDataEntity; +import org.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.skywalking.apm.agent.core.context.util.KeyValuePair; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.test.helper.SegmentHelper; +import org.skywalking.apm.agent.test.helper.SpanHelper; +import org.skywalking.apm.agent.test.tools.AgentServiceRule; +import org.skywalking.apm.agent.test.tools.SegmentStorage; +import org.skywalking.apm.agent.test.tools.SegmentStoragePoint; +import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; +import static org.skywalking.apm.agent.test.tools.SpanAssert.assertException; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class MongoDBV2MethodInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private MongoDBV2MethodInterceptor interceptor; + + @Mock + private EnhancedInstance enhancedInstance; + + private Object[] arguments; + private Class[] argumentTypes; + + @SuppressWarnings({"rawtypes", "unchecked"}) + @Before + public void setUp() throws Exception { + + interceptor = new MongoDBV2MethodInterceptor(); + + Config.Plugin.MongoDB.TRACE_PARAM = true; + + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); + + } + + @Test + public void testIntercept() throws Throwable { + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); + interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoSpan(spans.get(0)); + } + + @Test + public void testInterceptWithException() throws Throwable { + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); + interceptor.handleMethodException(enhancedInstance, getExecuteMethod(), null, null, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoSpan(spans.get(0)); + List logDataEntities = SpanHelper.getLogs(spans.get(0)); + assertThat(logDataEntities.size(), is(1)); + assertException(logDataEntities.get(0), RuntimeException.class); + } + + private void assertMongoSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/getWriteConcern")); + assertThat(SpanHelper.getComponentId(span), is(9)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), is(SpanLayer.DB)); + } + + private Method getExecuteMethod() { + try { + return Mongo.class.getMethod("getWriteConcern"); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index 8c527c6ed..524cedf2b 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -52,6 +52,7 @@ h2-1.x-plugin postgresql-8.x-plugin oracle-10.x-plugin + mongodb-2.x-plugin pom From eb9b9a12221fa3fede8b9de32348ca818288fe72 Mon Sep 17 00:00:00 2001 From: lytscu Date: Wed, 8 Nov 2017 17:20:16 +0800 Subject: [PATCH 02/22] add mongodb 2.x enhance plugin --- .../apm-sdk-plugin/mongodb-2.x-plugin/pom.xml | 2 +- .../v2/MongoDBV2MethodInterceptor.java | 4 + .../MongoDBCollectionInstrumentation.java | 152 ++++++++++++++++++ .../v2/define/MongoDBV2Instrumentation.java | 103 +++++++++++- .../src/main/resources/skywalking-plugin.def | 1 + 5 files changed, 256 insertions(+), 6 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml index 3c347b75b..ec6bef62c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml @@ -29,7 +29,7 @@ 4.0.0 - mongodb-2.x-plugin + apm-mongodb-2.x-plugin jar diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index d55980a4c..acbeea1e7 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -34,6 +34,9 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** + * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DBCollection#find()} + *or{@link com.mongodb.DBCollectionImpl#insertImpl}.... record the mongoDB host, operation name ... + * * @Auther liyuntao */ @@ -79,4 +82,5 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } + } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java new file mode 100644 index 000000000..3affcc6b9 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -0,0 +1,152 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor; + + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#find()}, + * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}. + * + * @Auther liyuntao + */ +public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.DBCollection"; + + private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return null; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("find"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.MapReduceCommand")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("aggregate").and(takesArgumentWithType(2, "com.mongodb.ReadPreference")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("createIndex").and(takesArgumentWithType(2, "boolean")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + protected String[] witnessClasses() { + /** + * @see {@link com.mongodb.tools.ConnectionPoolStat} + */ + return new String[] { + "com.mongodb.tools.ConnectionPoolStat" + }; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java index b2ba7d097..20de3d763 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java @@ -24,22 +24,25 @@ import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoin import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** + * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollectionImpl#find()}, + * {@link com.mongodb.DBCollectionImpl#insertImpl} by using {@link MongoDBV2MethodInterceptor}. + * * @Auther liyuntao */ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine { - private static final String ENHANCE_CLASS = "com.mongodb.DBCollection"; + private static final String ENHANCE_CLASS = "com.mongodb.DBCollectionImpl"; private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; - - @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[] { @@ -63,7 +66,7 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("getWriteConcern"); + return named("find").and(takesArgumentWithType(8, "com.mongodb.DBEncoder")); } @Override @@ -79,7 +82,87 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("getReadPreference"); + return named("insertImpl"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return takesArgumentWithType(1, "boolean"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("updateImpl"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("findAndModifyImpl"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("drop"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("createIndex"); } @Override @@ -100,4 +183,14 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD return byName(ENHANCE_CLASS); } + @Override + protected String[] witnessClasses() { + /** + * @see {@link com.mongodb.tools.ConnectionPoolStat} + */ + return new String[] { + "com.mongodb.tools.ConnectionPoolStat" + }; + } + } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def index 0544be0b5..7f6089493 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def @@ -1 +1,2 @@ mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBV2Instrumentation +mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBCollectionInstrumentation From b63c0ac5725f96d89deaf8cfc5a6c51a24526834 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 9 Nov 2017 19:23:44 +0800 Subject: [PATCH 03/22] =?UTF-8?q?=201=E3=80=81Modify=20the=20code=20review?= =?UTF-8?q?.=20=202=E3=80=81Modify=20V3=20plugin=20to=20support=20all=20ve?= =?UTF-8?q?rsion=203.x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MongoDBCollectionInstrumentation.java | 53 ++++++++++++++++--- .../v2/define/MongoDBV2Instrumentation.java | 25 ++------- .../v2/MongoDBV2MethodInterceptorTest.java | 1 - .../mongodb/v3/MongoDBMethodInterceptor.java | 11 +++- 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 3affcc6b9..7e27b92bf 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -24,9 +24,8 @@ import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoin import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.skywalking.apm.agent.core.plugin.match.ClassMatch; -import org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor; - +import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; @@ -34,8 +33,6 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#find()}, * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}. - * - * @Auther liyuntao */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -45,7 +42,19 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return null; + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + } + }; } @Override @@ -130,7 +139,39 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc public boolean isOverrideArgs() { return false; } - } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("insert").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("update").and(takesArgumentWithType(5, "com.mongodb.DBEncoder")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, }; } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java index 20de3d763..edddee003 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java @@ -24,7 +24,6 @@ import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoin import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.skywalking.apm.agent.core.plugin.match.ClassMatch; -import org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -32,10 +31,9 @@ import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMat import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** - * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollectionImpl#find()}, - * {@link com.mongodb.DBCollectionImpl#insertImpl} by using {@link MongoDBV2MethodInterceptor}. - * - * @Auther liyuntao + * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link + * com.mongodb.DBCollectionImpl#find()}, {@link com.mongodb.DBCollectionImpl#insertImpl} by using {@link + * MongoDBV2MethodInterceptor}. */ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -63,22 +61,7 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("find").and(takesArgumentWithType(8, "com.mongodb.DBEncoder")); - } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { @@ -98,7 +81,7 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return takesArgumentWithType(1, "boolean"); + return named("remove").and(takesArgumentWithType(1, "boolean")); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java index 1352982ff..869e36172 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java @@ -117,7 +117,6 @@ public class MongoDBV2MethodInterceptorTest { try { return Mongo.class.getMethod("getWriteConcern"); } catch (NoSuchMethodException e) { - e.printStackTrace(); return null; } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java index 614e032d8..749d1b840 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java @@ -200,11 +200,18 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { Cluster cluster = (Cluster)allArguments[0]; StringBuilder peers = new StringBuilder(); - for (ServerDescription description : cluster.getDescription().getServerDescriptions()) { + List servers; + try { + cluster.getDescription().getClass().getMethod("getServerDescriptions", null); + servers = cluster.getDescription().getServerDescriptions(); + + } catch (NoSuchMethodException e) { + servers = cluster.getDescription().getAny(); + } + for (ServerDescription description : servers) { ServerAddress address = description.getAddress(); peers.append(address.getHost() + ":" + address.getPort() + ";"); } - objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } } From c53c4e4b023ed6004adc1668871272a3623d1f7e Mon Sep 17 00:00:00 2001 From: lytscu Date: Mon, 13 Nov 2017 16:44:04 +0800 Subject: [PATCH 04/22] Modify the code review --- .../v2/MongoDBV2MethodInterceptor.java | 35 ++++++-- .../MongoDBCollectionInstrumentation.java | 70 +++++++-------- .../v2/define/MongoDBV2Instrumentation.java | 89 +------------------ 3 files changed, 69 insertions(+), 125 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index acbeea1e7..51542336e 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -18,8 +18,12 @@ package org.skywalking.apm.plugin.mongodb.v2; +import com.mongodb.CommandResult; +import com.mongodb.Mongo; import com.mongodb.ServerAddress; import com.mongodb.DB; +import com.mongodb.WriteResult; +import com.mongodb.AggregationOutput; import java.lang.reflect.Method; import java.util.List; import org.skywalking.apm.agent.core.context.ContextCarrier; @@ -35,9 +39,7 @@ import org.skywalking.apm.network.trace.component.ComponentsDefine; /** * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DBCollection#find()} - *or{@link com.mongodb.DBCollectionImpl#insertImpl}.... record the mongoDB host, operation name ... - * - * @Auther liyuntao + * or{@link com.mongodb.DBCollectionImpl#insertImpl}.... record the mongoDB host, operation name ... */ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { @@ -59,6 +61,23 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + AbstractSpan activeSpan = ContextManager.activeSpan(); + if (ret instanceof WriteResult) { + WriteResult wresult = (WriteResult)ret; + if (!wresult.getCachedLastError().ok()) { + Tags.STATUS_CODE.set(activeSpan, "failed"); + } + } else if (ret instanceof CommandResult) { + CommandResult cresult = (CommandResult)ret; + if (!cresult.ok()) { + Tags.STATUS_CODE.set(activeSpan, "failed"); + } + } else if (ret instanceof AggregationOutput) { + AggregationOutput aresult = (AggregationOutput)ret; + if (!aresult.getCommandResult().ok()) { + Tags.STATUS_CODE.set(activeSpan, "failed"); + } + } ContextManager.stopSpan(); return ret; } @@ -72,8 +91,14 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - DB db = (DB)allArguments[0]; - List servers = db.getMongo().getAllAddress(); + List servers = null; + if (allArguments[0] instanceof DB) { + DB db = (DB)allArguments[0]; + servers = db.getMongo().getAllAddress(); + } else if (allArguments[0] instanceof Mongo) { + Mongo mongo = (Mongo)allArguments[0]; + servers = mongo.getAllAddress(); + } StringBuilder peers = new StringBuilder(); for (ServerAddress address : servers) { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 7e27b92bf..9ac7fd445 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -76,38 +76,6 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc return false; } }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.MapReduceCommand")); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference")); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { @@ -127,7 +95,7 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("createIndex").and(takesArgumentWithType(2, "boolean")); + return named("insert"); } @Override @@ -143,7 +111,7 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("insert").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); + return named("update"); } @Override @@ -159,7 +127,39 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("update").and(takesArgumentWithType(5, "com.mongodb.DBEncoder")); + return named("remove").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("findAndModify"); + } + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("createIndex").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java index edddee003..039297552 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java @@ -31,13 +31,12 @@ import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMat import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** - * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link - * com.mongodb.DBCollectionImpl#find()}, {@link com.mongodb.DBCollectionImpl#insertImpl} by using {@link - * MongoDBV2MethodInterceptor}. + * {@link MongoDBV2Instrumentation} presents that skywalking intercepts {@link com.mongodb.DB#command}, + * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}. */ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine { - private static final String ENHANCE_CLASS = "com.mongodb.DBCollectionImpl"; + private static final String ENHANCE_CLASS = "com.mongodb.DB"; private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; @@ -65,7 +64,7 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("insertImpl"); + return named("command").and(takesArgumentWithType(3, "com.mongodb.DBEncoder")); } @Override @@ -78,86 +77,6 @@ public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginD return false; } }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("remove").and(takesArgumentWithType(1, "boolean")); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("updateImpl"); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("findAndModifyImpl"); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("drop"); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("createIndex"); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - } }; } From 213fa33bd4b644b097797a2514a80b9cb0bfd2c9 Mon Sep 17 00:00:00 2001 From: lytscu Date: Mon, 13 Nov 2017 19:59:20 +0800 Subject: [PATCH 05/22] Modify afterMethod logic --- .../mongodb/v2/MongoDBV2MethodInterceptor.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index 51542336e..ecf061bec 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -62,21 +62,18 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { AbstractSpan activeSpan = ContextManager.activeSpan(); + CommandResult cresult = null; if (ret instanceof WriteResult) { WriteResult wresult = (WriteResult)ret; - if (!wresult.getCachedLastError().ok()) { - Tags.STATUS_CODE.set(activeSpan, "failed"); - } + cresult = wresult.getCachedLastError(); } else if (ret instanceof CommandResult) { - CommandResult cresult = (CommandResult)ret; - if (!cresult.ok()) { - Tags.STATUS_CODE.set(activeSpan, "failed"); - } + cresult = (CommandResult)ret; } else if (ret instanceof AggregationOutput) { AggregationOutput aresult = (AggregationOutput)ret; - if (!aresult.getCommandResult().ok()) { - Tags.STATUS_CODE.set(activeSpan, "failed"); - } + cresult = aresult.getCommandResult(); + } + if (null != cresult && !cresult.ok()) { + activeSpan.tag("CommandError", cresult.getErrorMessage()); } ContextManager.stopSpan(); return ret; From 778d3c6751c3a090c3456a1274060d12a0f71ccc Mon Sep 17 00:00:00 2001 From: lytscu Date: Tue, 14 Nov 2017 15:27:49 +0800 Subject: [PATCH 06/22] Record operation type --- .../mongodb/v2/MongoDBV2MethodInterceptor.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index ecf061bec..b0f0a03c4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -19,6 +19,7 @@ package org.skywalking.apm.plugin.mongodb.v2; import com.mongodb.CommandResult; +import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.ServerAddress; import com.mongodb.DB; @@ -52,7 +53,17 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + method.getName(), new ContextCarrier(), remotePeer); + String carrier = null; + if (method.getName().equals("command")) { + DBObject obj = (DBObject)allArguments[0]; + for (String key : obj.keySet()) { + carrier = key; + break; + } + } else { + carrier = method.getName(); + } + AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + carrier, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGODB); Tags.DB_TYPE.set(span, DB_TYPE); SpanLayer.asDB(span); From ab36a1bb830c19a8398460deb03d233b8f183d83 Mon Sep 17 00:00:00 2001 From: lytscu Date: Wed, 15 Nov 2017 11:49:12 +0800 Subject: [PATCH 07/22] Split interceptor --- .../MongoDBCollectionMethodInterceptor.java | 101 ++++++++++++++ .../v2/MongoDBV2MethodInterceptor.java | 32 +---- .../MongoDBCollectionInstrumentation.java | 2 +- ...ongoDBCollectionMethodInterceptorTest.java | 125 ++++++++++++++++++ .../v2/MongoDBV2MethodInterceptorTest.java | 13 +- 5 files changed, 242 insertions(+), 31 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptorTest.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java new file mode 100644 index 000000000..ac065c8f9 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -0,0 +1,101 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2; + +import com.mongodb.AggregationOutput; +import com.mongodb.CommandResult; +import com.mongodb.DB; +import com.mongodb.ServerAddress; +import com.mongodb.WriteResult; +import java.lang.reflect.Method; +import java.util.List; +import org.skywalking.apm.agent.core.context.ContextCarrier; +import org.skywalking.apm.agent.core.context.ContextManager; +import org.skywalking.apm.agent.core.context.tag.Tags; +import org.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.network.trace.component.ComponentsDefine; + +/** + * {@link MongoDBCollectionMethodInterceptor} intercept method of {@link com.mongodb.DBCollection#find()} + * or{@link com.mongodb.DBCollectionImpl#insert}.... record the mongoDB host, operation name ... + */ + +public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { + + private static final String DB_TYPE = "MongoDB"; + + private static final String MONGO_DB_OP_PREFIX = "MongoDB/"; + + @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + String remotePeer = (String)objInst.getSkyWalkingDynamicField(); + String carrier = null; + carrier = method.getName(); + AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + carrier, new ContextCarrier(), remotePeer); + span.setComponent(ComponentsDefine.MONGODB); + Tags.DB_TYPE.set(span, DB_TYPE); + SpanLayer.asDB(span); + + } + + @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + AbstractSpan activeSpan = ContextManager.activeSpan(); + CommandResult cresult = null; + if (ret instanceof WriteResult) { + WriteResult wresult = (WriteResult)ret; + cresult = wresult.getCachedLastError(); + } else if (ret instanceof AggregationOutput) { + AggregationOutput aresult = (AggregationOutput)ret; + cresult = aresult.getCommandResult(); + } + if (null != cresult && !cresult.ok()) { + activeSpan.tag("CommandError", cresult.getErrorMessage()); + } + ContextManager.stopSpan(); + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + AbstractSpan activeSpan = ContextManager.activeSpan(); + activeSpan.errorOccurred(); + activeSpan.log(t); + } + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + List servers = null; + DB db = (DB)allArguments[0]; + servers = db.getMongo().getAllAddress(); + StringBuilder peers = new StringBuilder(); + for (ServerAddress address : servers) { + peers.append(address.getHost() + ":" + address.getPort() + ";"); + } + + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index b0f0a03c4..0c2844eff 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -19,12 +19,10 @@ package org.skywalking.apm.plugin.mongodb.v2; import com.mongodb.CommandResult; +import com.mongodb.DBEncoder; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.ServerAddress; -import com.mongodb.DB; -import com.mongodb.WriteResult; -import com.mongodb.AggregationOutput; import java.lang.reflect.Method; import java.util.List; import org.skywalking.apm.agent.core.context.ContextCarrier; @@ -39,8 +37,8 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DBCollection#find()} - * or{@link com.mongodb.DBCollectionImpl#insertImpl}.... record the mongoDB host, operation name ... + * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DB#command(DBObject, DBEncoder)} ()} + * record the mongoDB host, operation name ... */ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { @@ -53,15 +51,13 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - String carrier = null; + String carrier = "command"; if (method.getName().equals("command")) { DBObject obj = (DBObject)allArguments[0]; for (String key : obj.keySet()) { carrier = key; break; } - } else { - carrier = method.getName(); } AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + carrier, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGODB); @@ -74,15 +70,7 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep Class[] argumentsTypes, Object ret) throws Throwable { AbstractSpan activeSpan = ContextManager.activeSpan(); CommandResult cresult = null; - if (ret instanceof WriteResult) { - WriteResult wresult = (WriteResult)ret; - cresult = wresult.getCachedLastError(); - } else if (ret instanceof CommandResult) { - cresult = (CommandResult)ret; - } else if (ret instanceof AggregationOutput) { - AggregationOutput aresult = (AggregationOutput)ret; - cresult = aresult.getCommandResult(); - } + cresult = (CommandResult)ret; if (null != cresult && !cresult.ok()) { activeSpan.tag("CommandError", cresult.getErrorMessage()); } @@ -100,14 +88,8 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { List servers = null; - if (allArguments[0] instanceof DB) { - DB db = (DB)allArguments[0]; - servers = db.getMongo().getAllAddress(); - } else if (allArguments[0] instanceof Mongo) { - Mongo mongo = (Mongo)allArguments[0]; - servers = mongo.getAllAddress(); - } - + Mongo mongo = (Mongo)allArguments[0]; + servers = mongo.getAllAddress(); StringBuilder peers = new StringBuilder(); for (ServerAddress address : servers) { peers.append(address.getHost() + ":" + address.getPort() + ";"); diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 9ac7fd445..1f6deb5dd 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -38,7 +38,7 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc private static final String ENHANCE_CLASS = "com.mongodb.DBCollection"; - private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; + private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor"; @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptorTest.java new file mode 100644 index 000000000..28f0805a4 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptorTest.java @@ -0,0 +1,125 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2; + +import com.mongodb.DBCollection; +import com.mongodb.DBObject; +import java.lang.reflect.Method; +import java.util.List; +import org.hamcrest.MatcherAssert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +import org.skywalking.apm.agent.core.conf.Config; +import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.skywalking.apm.agent.core.context.trace.LogDataEntity; +import org.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.skywalking.apm.agent.core.context.util.KeyValuePair; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.test.helper.SegmentHelper; +import org.skywalking.apm.agent.test.helper.SpanHelper; +import org.skywalking.apm.agent.test.tools.AgentServiceRule; +import org.skywalking.apm.agent.test.tools.SegmentStorage; +import org.skywalking.apm.agent.test.tools.SegmentStoragePoint; +import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; +import static org.skywalking.apm.agent.test.tools.SpanAssert.assertException; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class MongoDBCollectionMethodInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private MongoDBCollectionMethodInterceptor interceptor; + + @Mock + private EnhancedInstance enhancedInstance; + + private Object[] arguments = new Object[3]; + private Class[] argumentTypes; + + @SuppressWarnings({"rawtypes", "unchecked"}) + @Before + public void setUp() throws Exception { + + interceptor = new MongoDBCollectionMethodInterceptor(); + + Config.Plugin.MongoDB.TRACE_PARAM = true; + + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); + + } + + @Test + public void testIntercept() throws Throwable { + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); + interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoSpan(spans.get(0)); + } + + @Test + public void testInterceptWithException() throws Throwable { + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); + interceptor.handleMethodException(enhancedInstance, getExecuteMethod(), null, null, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); + + MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertMongoSpan(spans.get(0)); + List logDataEntities = SpanHelper.getLogs(spans.get(0)); + assertThat(logDataEntities.size(), is(1)); + assertException(logDataEntities.get(0), RuntimeException.class); + } + + private void assertMongoSpan(AbstractTracingSpan span) { + assertThat(span.getOperationName(), is("MongoDB/insert")); + assertThat(SpanHelper.getComponentId(span), is(9)); + List tags = SpanHelper.getTags(span); + assertThat(tags.get(0).getValue(), is("MongoDB")); + assertThat(span.isExit(), is(true)); + assertThat(SpanHelper.getLayer(span), is(SpanLayer.DB)); + } + + private Method getExecuteMethod() { + try { + return DBCollection.class.getMethod("insert", DBObject[].class); + } catch (NoSuchMethodException e) { + return null; + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java index 869e36172..d4af507ca 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java @@ -18,7 +18,8 @@ package org.skywalking.apm.plugin.mongodb.v2; -import com.mongodb.Mongo; +import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.DBCollection; import java.lang.reflect.Method; import java.util.List; import org.hamcrest.MatcherAssert; @@ -63,7 +64,7 @@ public class MongoDBV2MethodInterceptorTest { @Mock private EnhancedInstance enhancedInstance; - private Object[] arguments; + private Object[] arguments = new Object[3]; private Class[] argumentTypes; @SuppressWarnings({"rawtypes", "unchecked"}) @@ -76,11 +77,13 @@ public class MongoDBV2MethodInterceptorTest { when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); + arguments[0] = BasicDBObjectBuilder.start().add("getCount", "name").get(); + } @Test public void testIntercept() throws Throwable { - interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); + interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), arguments, null, null); interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); @@ -105,7 +108,7 @@ public class MongoDBV2MethodInterceptorTest { } private void assertMongoSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/getWriteConcern")); + assertThat(span.getOperationName(), is("MongoDB/command")); assertThat(SpanHelper.getComponentId(span), is(9)); List tags = SpanHelper.getTags(span); assertThat(tags.get(0).getValue(), is("MongoDB")); @@ -115,7 +118,7 @@ public class MongoDBV2MethodInterceptorTest { private Method getExecuteMethod() { try { - return Mongo.class.getMethod("getWriteConcern"); + return DBCollection.class.getMethod("getCount"); } catch (NoSuchMethodException e) { return null; } From b7790c5a87df1880019fd064159e490e6b4a7845 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 10:48:03 +0800 Subject: [PATCH 08/22] V3 modify revert --- .../plugin/mongodb/v3/MongoDBMethodInterceptor.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java index 749d1b840..a82f59b4d 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java @@ -200,15 +200,7 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { Cluster cluster = (Cluster)allArguments[0]; StringBuilder peers = new StringBuilder(); - List servers; - try { - cluster.getDescription().getClass().getMethod("getServerDescriptions", null); - servers = cluster.getDescription().getServerDescriptions(); - - } catch (NoSuchMethodException e) { - servers = cluster.getDescription().getAny(); - } - for (ServerDescription description : servers) { + for (ServerDescription description : cluster.getDescription().getServerDescriptions()) { ServerAddress address = description.getAddress(); peers.append(address.getHost() + ":" + address.getPort() + ";"); } From f3699ba2660ef624cbbc96df8fb6498ff7cc2a66 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 14:36:57 +0800 Subject: [PATCH 09/22] 1.carrier->operation 2. Remove duplicate constructors intercept points --- .../MongoDBCollectionMethodInterceptor.java | 20 +++++-------------- .../v2/MongoDBV2MethodInterceptor.java | 6 +++--- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index ac065c8f9..f2050612c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -20,11 +20,8 @@ package org.skywalking.apm.plugin.mongodb.v2; import com.mongodb.AggregationOutput; import com.mongodb.CommandResult; -import com.mongodb.DB; -import com.mongodb.ServerAddress; import com.mongodb.WriteResult; import java.lang.reflect.Method; -import java.util.List; import org.skywalking.apm.agent.core.context.ContextCarrier; import org.skywalking.apm.agent.core.context.ContextManager; import org.skywalking.apm.agent.core.context.tag.Tags; @@ -51,9 +48,9 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - String carrier = null; - carrier = method.getName(); - AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + carrier, new ContextCarrier(), remotePeer); + String opertaion = null; + opertaion = method.getName(); + AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGODB); Tags.DB_TYPE.set(span, DB_TYPE); SpanLayer.asDB(span); @@ -87,15 +84,8 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - List servers = null; - DB db = (DB)allArguments[0]; - servers = db.getMongo().getAllAddress(); - StringBuilder peers = new StringBuilder(); - for (ServerAddress address : servers) { - peers.append(address.getHost() + ":" + address.getPort() + ";"); - } - - objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); + ((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField(); + objInst.setSkyWalkingDynamicField(((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField().toString()); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java index 0c2844eff..a5bf901b4 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java @@ -51,15 +51,15 @@ public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundIntercep Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - String carrier = "command"; + String opertaion = "command"; if (method.getName().equals("command")) { DBObject obj = (DBObject)allArguments[0]; for (String key : obj.keySet()) { - carrier = key; + opertaion = key; break; } } - AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + carrier, new ContextCarrier(), remotePeer); + AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGODB); Tags.DB_TYPE.set(span, DB_TYPE); SpanLayer.asDB(span); From 4eeb5ab8dfc2475bdc7ca96235bbb89f5f7409dc Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 14:46:37 +0800 Subject: [PATCH 10/22] Delete redundant code --- .../plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index f2050612c..92fb75280 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -84,7 +84,6 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - ((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField(); objInst.setSkyWalkingDynamicField(((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField().toString()); } From 78398cc733bb84ff7d8482281c3232030a1b7e6c Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 17:27:24 +0800 Subject: [PATCH 11/22] Reconstruction --- .../MongoDBCollectionMethodInterceptor.java | 16 +- .../v2/MongoDBV2MethodInterceptor.java | 101 ------------ .../mongodb/v2/define/InterceptPoint.java | 38 +++++ .../MongoDBCollectionInstrumentation.java | 153 ++++++++++-------- .../v2/define/MongoDBV2Instrumentation.java | 98 ----------- .../src/main/resources/skywalking-plugin.def | 1 - .../v2/MongoDBV2MethodInterceptorTest.java | 127 --------------- 7 files changed, 133 insertions(+), 401 deletions(-) delete mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java delete mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java delete mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index 92fb75280..9ac04cd88 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -20,8 +20,11 @@ package org.skywalking.apm.plugin.mongodb.v2; import com.mongodb.AggregationOutput; import com.mongodb.CommandResult; +import com.mongodb.DB; +import com.mongodb.ServerAddress; import com.mongodb.WriteResult; import java.lang.reflect.Method; +import java.util.List; import org.skywalking.apm.agent.core.context.ContextCarrier; import org.skywalking.apm.agent.core.context.ContextManager; import org.skywalking.apm.agent.core.context.tag.Tags; @@ -48,8 +51,7 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - String opertaion = null; - opertaion = method.getName(); + String opertaion = method.getName(); AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer); span.setComponent(ComponentsDefine.MONGODB); Tags.DB_TYPE.set(span, DB_TYPE); @@ -70,6 +72,7 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround } if (null != cresult && !cresult.ok()) { activeSpan.tag("CommandError", cresult.getErrorMessage()); + activeSpan.log(cresult.getException()); } ContextManager.stopSpan(); return ret; @@ -84,7 +87,14 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - objInst.setSkyWalkingDynamicField(((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField().toString()); + List servers = null; + DB db = (DB)allArguments[0]; + servers = db.getMongo().getAllAddress(); + StringBuilder peers = new StringBuilder(); + for (ServerAddress address : servers) { + peers.append(address.getHost() + ":" + address.getPort() + ";"); + } + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java deleted file mode 100644 index a5bf901b4..000000000 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptor.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed 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. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.plugin.mongodb.v2; - -import com.mongodb.CommandResult; -import com.mongodb.DBEncoder; -import com.mongodb.DBObject; -import com.mongodb.Mongo; -import com.mongodb.ServerAddress; -import java.lang.reflect.Method; -import java.util.List; -import org.skywalking.apm.agent.core.context.ContextCarrier; -import org.skywalking.apm.agent.core.context.ContextManager; -import org.skywalking.apm.agent.core.context.tag.Tags; -import org.skywalking.apm.agent.core.context.trace.AbstractSpan; -import org.skywalking.apm.agent.core.context.trace.SpanLayer; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.skywalking.apm.network.trace.component.ComponentsDefine; - -/** - * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DB#command(DBObject, DBEncoder)} ()} - * record the mongoDB host, operation name ... - */ - -public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { - - private static final String DB_TYPE = "MongoDB"; - - private static final String MONGO_DB_OP_PREFIX = "MongoDB/"; - - @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, - Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - - String remotePeer = (String)objInst.getSkyWalkingDynamicField(); - String opertaion = "command"; - if (method.getName().equals("command")) { - DBObject obj = (DBObject)allArguments[0]; - for (String key : obj.keySet()) { - opertaion = key; - break; - } - } - AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer); - span.setComponent(ComponentsDefine.MONGODB); - Tags.DB_TYPE.set(span, DB_TYPE); - SpanLayer.asDB(span); - - } - - @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, - Class[] argumentsTypes, Object ret) throws Throwable { - AbstractSpan activeSpan = ContextManager.activeSpan(); - CommandResult cresult = null; - cresult = (CommandResult)ret; - if (null != cresult && !cresult.ok()) { - activeSpan.tag("CommandError", cresult.getErrorMessage()); - } - ContextManager.stopSpan(); - return ret; - } - - @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, - Class[] argumentsTypes, Throwable t) { - AbstractSpan activeSpan = ContextManager.activeSpan(); - activeSpan.errorOccurred(); - activeSpan.log(t); - } - - @Override - public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - List servers = null; - Mongo mongo = (Mongo)allArguments[0]; - servers = mongo.getAllAddress(); - StringBuilder peers = new StringBuilder(); - for (ServerAddress address : servers) { - peers.append(address.getHost() + ":" + address.getPort() + ";"); - } - - objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); - } - -} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java new file mode 100644 index 000000000..8e4488965 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java @@ -0,0 +1,38 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2.define; + +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; + +/** + * @Auther liyuntao + */ +public abstract class InterceptPoint implements InstanceMethodsInterceptPoint { + private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor"; + + @Override + public String getMethodsInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 1f6deb5dd..21ec90243 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -32,7 +32,7 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#find()}, - * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}. + * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBCollectionMethodInterceptor}. */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -60,118 +60,129 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named("find"); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named("aggregate").and(takesArgumentWithType(2, "com.mongodb.ReadPreference")); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("insert"); + return named("insert").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("update"); + return named("update").and(takesArgumentWithType(5, "com.mongodb.DBEncoder")); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named("remove").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named("findAndModify"); } - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } }, - new InstanceMethodsInterceptPoint() { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named("createIndex").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); } + }, + /** + *Involved db_command operation + */ + new InterceptPoint() { @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; + public ElementMatcher getMethodsMatcher() { + return named("getCount").and(takesArgumentWithType(6, "java.util.concurrent.TimeUnit")); } }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("drop"); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("dropIndexes"); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("rename").and(takesArgumentWithType(1, "boolean")); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("group").and(takesArgumentWithType(1, "boolean")); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("group").and(takesArgumentWithType(1, "com.mongodb.DBObject")); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("distinct").and(takesArgumentWithType(2, "com.mongodb.ReadPreference")); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.MapReduceCommand")); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.DBObject")); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference")); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("explainAggregate"); + } + }, + }; } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java deleted file mode 100644 index 039297552..000000000 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBV2Instrumentation.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed 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. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.plugin.mongodb.v2.define; - -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; -import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; -import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; -import org.skywalking.apm.agent.core.plugin.match.ClassMatch; - -import static net.bytebuddy.matcher.ElementMatchers.any; -import static net.bytebuddy.matcher.ElementMatchers.named; -import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; -import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; - -/** - * {@link MongoDBV2Instrumentation} presents that skywalking intercepts {@link com.mongodb.DB#command}, - * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}. - */ -public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine { - - private static final String ENHANCE_CLASS = "com.mongodb.DB"; - - private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor"; - - @Override - protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[] { - new ConstructorInterceptPoint() { - @Override - public ElementMatcher getConstructorMatcher() { - return any(); - } - - @Override - public String getConstructorInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - } - }; - } - - @Override - protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[] { - - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("command").and(takesArgumentWithType(3, "com.mongodb.DBEncoder")); - } - - @Override - public String getMethodsInterceptor() { - return MONGDB_METHOD_INTERCET_CLASS; - } - - @Override - public boolean isOverrideArgs() { - return false; - } - }, - }; - } - - @Override - protected ClassMatch enhanceClass() { - return byName(ENHANCE_CLASS); - } - - @Override - protected String[] witnessClasses() { - /** - * @see {@link com.mongodb.tools.ConnectionPoolStat} - */ - return new String[] { - "com.mongodb.tools.ConnectionPoolStat" - }; - } - -} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def index 7f6089493..fdf8f440c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def @@ -1,2 +1 @@ -mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBV2Instrumentation mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBCollectionInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java deleted file mode 100644 index d4af507ca..000000000 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/test/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBV2MethodInterceptorTest.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed 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. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.plugin.mongodb.v2; - -import com.mongodb.BasicDBObjectBuilder; -import com.mongodb.DBCollection; -import java.lang.reflect.Method; -import java.util.List; -import org.hamcrest.MatcherAssert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.modules.junit4.PowerMockRunnerDelegate; -import org.skywalking.apm.agent.core.conf.Config; -import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; -import org.skywalking.apm.agent.core.context.trace.LogDataEntity; -import org.skywalking.apm.agent.core.context.trace.SpanLayer; -import org.skywalking.apm.agent.core.context.trace.TraceSegment; -import org.skywalking.apm.agent.core.context.util.KeyValuePair; -import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; -import org.skywalking.apm.agent.test.helper.SegmentHelper; -import org.skywalking.apm.agent.test.helper.SpanHelper; -import org.skywalking.apm.agent.test.tools.AgentServiceRule; -import org.skywalking.apm.agent.test.tools.SegmentStorage; -import org.skywalking.apm.agent.test.tools.SegmentStoragePoint; -import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.when; -import static org.skywalking.apm.agent.test.tools.SpanAssert.assertException; - -@RunWith(PowerMockRunner.class) -@PowerMockRunnerDelegate(TracingSegmentRunner.class) -public class MongoDBV2MethodInterceptorTest { - - @SegmentStoragePoint - private SegmentStorage segmentStorage; - - @Rule - public AgentServiceRule serviceRule = new AgentServiceRule(); - - private MongoDBV2MethodInterceptor interceptor; - - @Mock - private EnhancedInstance enhancedInstance; - - private Object[] arguments = new Object[3]; - private Class[] argumentTypes; - - @SuppressWarnings({"rawtypes", "unchecked"}) - @Before - public void setUp() throws Exception { - - interceptor = new MongoDBV2MethodInterceptor(); - - Config.Plugin.MongoDB.TRACE_PARAM = true; - - when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017"); - - arguments[0] = BasicDBObjectBuilder.start().add("getCount", "name").get(); - - } - - @Test - public void testIntercept() throws Throwable { - interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), arguments, null, null); - interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); - - MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); - TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); - List spans = SegmentHelper.getSpans(traceSegment); - assertMongoSpan(spans.get(0)); - } - - @Test - public void testInterceptWithException() throws Throwable { - interceptor.beforeMethod(enhancedInstance, getExecuteMethod(), null, null, null); - interceptor.handleMethodException(enhancedInstance, getExecuteMethod(), null, null, new RuntimeException()); - interceptor.afterMethod(enhancedInstance, getExecuteMethod(), null, null, null); - - MatcherAssert.assertThat(segmentStorage.getTraceSegments().size(), is(1)); - TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); - List spans = SegmentHelper.getSpans(traceSegment); - assertMongoSpan(spans.get(0)); - List logDataEntities = SpanHelper.getLogs(spans.get(0)); - assertThat(logDataEntities.size(), is(1)); - assertException(logDataEntities.get(0), RuntimeException.class); - } - - private void assertMongoSpan(AbstractTracingSpan span) { - assertThat(span.getOperationName(), is("MongoDB/command")); - assertThat(SpanHelper.getComponentId(span), is(9)); - List tags = SpanHelper.getTags(span); - assertThat(tags.get(0).getValue(), is("MongoDB")); - assertThat(span.isExit(), is(true)); - assertThat(SpanHelper.getLayer(span), is(SpanLayer.DB)); - } - - private Method getExecuteMethod() { - try { - return DBCollection.class.getMethod("getCount"); - } catch (NoSuchMethodException e) { - return null; - } - } - -} From 6b9d6caff14fea9abb06961052458cee41b5f62a Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 17:41:30 +0800 Subject: [PATCH 12/22] 3.2.4->3.2.5 --- apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml index ec6bef62c..ae5b8d77e 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/pom.xml @@ -25,7 +25,7 @@ apm-sdk-plugin org.skywalking - 3.2.4-2017 + 3.2.5-2017 4.0.0 From 484d43cbf7a48b0d3ae8b8ff6fca558e0d802484 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 19:51:19 +0800 Subject: [PATCH 13/22] add DBCollectionImpl instrumentation --- .../MongoDBCollectionImplInstrumentation.java | 132 ++++++++++++++++++ .../MongoDBCollectionInstrumentation.java | 44 +----- .../src/main/resources/skywalking-plugin.def | 1 + 3 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java new file mode 100644 index 000000000..b41e5078b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -0,0 +1,132 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.mongodb.v2.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * {@link MongoDBCollectionImplInstrumentation} presents that skywalking intercepts {@link + * com.mongodb.DBCollection#find()}, {@link com.mongodb.DBCollection#mapReduce} by using {@link + * MongoDBCollectionMethodInterceptor}. + */ +public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.mongodb.DBCollectionImpl"; + + private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return MONGDB_METHOD_INTERCET_CLASS; + } + } + }; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("find").and(takesArguments(9)); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("insert").and(takesArguments(4)); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("insertImpl"); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("update"); + } + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("updateImpl"); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("remove").and(takesArguments(4)); + } + + }, + new InterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("createIndex"); + } + + }, + + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + protected String[] witnessClasses() { + /** + * @see {@link com.mongodb.tools.ConnectionPoolStat} + */ + return new String[] { + "com.mongodb.tools.ConnectionPoolStat" + }; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 21ec90243..ee1902339 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -27,6 +27,7 @@ import org.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; @@ -60,54 +61,19 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("find"); + return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference")); } - }, + new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named("aggregate").and(takesArgumentWithType(2, "com.mongodb.ReadPreference")); + return named("findAndModify").and(takesArguments(9)); } - - }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("insert").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); - } - - }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("update").and(takesArgumentWithType(5, "com.mongodb.DBEncoder")); - } - - }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("remove").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); - } - - }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("findAndModify"); - } - - }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("createIndex").and(takesArgumentWithType(2, "com.mongodb.DBEncoder")); - } - }, /** *Involved db_command operation diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def index fdf8f440c..fb8a07eab 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/resources/skywalking-plugin.def @@ -1 +1,2 @@ mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBCollectionInstrumentation +mongodb-2.x=org.skywalking.apm.plugin.mongodb.v2.define.MongoDBCollectionImplInstrumentation From 1182e8a9bf07f744069a04c96ee72a0f5a03af28 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 20:04:46 +0800 Subject: [PATCH 14/22] Comments --- .../plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java | 1 + .../mongodb/v2/define/MongoDBCollectionImplInstrumentation.java | 2 +- .../mongodb/v2/define/MongoDBCollectionInstrumentation.java | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index 9ac04cd88..f79cabfe9 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -94,6 +94,7 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround for (ServerAddress address : servers) { peers.append(address.getHost() + ":" + address.getPort() + ";"); } + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java index b41e5078b..fccdaa6c1 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -32,7 +32,7 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** * {@link MongoDBCollectionImplInstrumentation} presents that skywalking intercepts {@link - * com.mongodb.DBCollection#find()}, {@link com.mongodb.DBCollection#mapReduce} by using {@link + * com.mongodb.DBCollection#find()}, {@link com.mongodb.DBCollection#createIndex} by using {@link * MongoDBCollectionMethodInterceptor}. */ public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index ee1902339..3afcef635 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -32,7 +32,7 @@ import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMat import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** - * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#find()}, + * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#getCount()}, * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBCollectionMethodInterceptor}. */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { From 726d1d6b0c5672d2e435ad0432ec47850099b2f6 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 20:38:30 +0800 Subject: [PATCH 15/22] Comments --- .../MongoDBCollectionMethodInterceptor.java | 3 +-- .../MongoDBCollectionImplInstrumentation.java | 13 +++++++++--- .../MongoDBCollectionInstrumentation.java | 21 ++++++++++++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index f79cabfe9..98ac0842f 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,8 +37,7 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor} intercept method of {@link com.mongodb.DBCollection#find()} - * or{@link com.mongodb.DBCollectionImpl#insert}.... record the mongoDB host, operation name ... + * {@link MongoDBCollectionMethodInterceptor} intercept {@link com.mongodb.DBCollectionImpl}{@link com.mongodb.DBCollection}class */ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java index fccdaa6c1..514eeac9e 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -31,9 +31,16 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** - * {@link MongoDBCollectionImplInstrumentation} presents that skywalking intercepts {@link - * com.mongodb.DBCollection#find()}, {@link com.mongodb.DBCollection#createIndex} by using {@link - * MongoDBCollectionMethodInterceptor}. + * {@link MongoDBCollectionImplInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the following methods in the + * {@link com.mongodb.DBCollectionImpl}class: + * 1. find
+ * 2. insert
+ * 3. insertImpl
+ * 4. update
+ * 5. updateImpl
+ * 6. remove
+ * 7. createIndex
+ * */ public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 3afcef635..805edd622 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -31,9 +31,20 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + /** - * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#getCount()}, - * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBCollectionMethodInterceptor}. + * {@link MongoDBCollectionInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the following methods in the + * {@link com.mongodb.DBCollection}class: + * 1. aggregate
+ * 2. findAndModify
+ * 3. getCount
+ * 4. drop
+ * 5. dropIndexes
+ * 6. rename
+ * 7. group
+ * 8. distinct
+ * 9. mapReduce
+ * */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { @@ -136,12 +147,6 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.DBObject")); } }, - new InterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference")); - } - }, new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { From 96633124dabca0c1fe59f1e0766c3d953ad469f0 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 20:52:09 +0800 Subject: [PATCH 16/22] comments --- .../plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java | 3 ++- .../mongodb/v2/define/MongoDBCollectionInstrumentation.java | 3 --- .../apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index 98ac0842f..dbdcaa6a5 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,7 +37,8 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor} intercept {@link com.mongodb.DBCollectionImpl}{@link com.mongodb.DBCollection}class + * {@link MongoDBCollectionMethodInterceptor} intercept {@link com.mongodb.DBCollectionImpl}{@link com.mongodb.DBCollection}class, + * create the exit span when the client call the interceptor methods. */ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 805edd622..e07dfe232 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -86,9 +86,6 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc return named("findAndModify").and(takesArguments(9)); } }, - /** - *Involved db_command operation - */ new InterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java index a82f59b4d..8b7940618 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java @@ -204,6 +204,7 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto ServerAddress address = description.getAddress(); peers.append(address.getHost() + ":" + address.getPort() + ";"); } + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } } From 4215fc628cad3f2b4c0d6feb08aa2050e13074e3 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 21:04:11 +0800 Subject: [PATCH 17/22] comments --- .../mongodb/v2/MongoDBCollectionMethodInterceptor.java | 7 +++++-- .../apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index dbdcaa6a5..84013cb6c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,8 +37,11 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor} intercept {@link com.mongodb.DBCollectionImpl}{@link com.mongodb.DBCollection}class, - * create the exit span when the client call the interceptor methods. + * {@link MongoDBCollectionMethodInterceptor}create the exit span when the client call the interceptor methods. + *

+ * {@link MongoDBCollectionMethodInterceptor} intercept constructor of {@link com.mongodb.DBCollection}or {@link com.mongodb.DBCollectionImpl} for + * record the ServerAddress. + * */ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java index 8b7940618..614e032d8 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v3/MongoDBMethodInterceptor.java @@ -204,7 +204,7 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto ServerAddress address = description.getAddress(); peers.append(address.getHost() + ":" + address.getPort() + ";"); } - + objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString()); } } From 14bc3d9670508f23856c0ed63aecfaf718ec1183 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 21:14:50 +0800 Subject: [PATCH 18/22] comments --- .../mongodb/v2/MongoDBCollectionMethodInterceptor.java | 6 ++---- .../v2/define/MongoDBCollectionImplInstrumentation.java | 2 +- .../mongodb/v2/define/MongoDBCollectionInstrumentation.java | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index 84013cb6c..8d8f2ee18 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,11 +37,9 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor}create the exit span when the client call the interceptor methods. - *

* {@link MongoDBCollectionMethodInterceptor} intercept constructor of {@link com.mongodb.DBCollection}or {@link com.mongodb.DBCollectionImpl} for - * record the ServerAddress. - * + * record the ServerAddress,and create the exit span. + *@Auther liyuntao */ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java index 514eeac9e..dcff3ac3c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -40,7 +40,7 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; * 5. updateImpl
* 6. remove
* 7. createIndex
- * + *@Auther liyuntao */ public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index e07dfe232..aa96ca7a2 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -44,7 +44,7 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; * 7. group
* 8. distinct
* 9. mapReduce
- * + *@Auther liyuntao */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { From 57ee51f256b89ce6f8d0f91abdd28cc14d55719c Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 16 Nov 2017 21:26:39 +0800 Subject: [PATCH 19/22] comments --- .../v2/MongoDBCollectionMethodInterceptor.java | 7 ++++--- .../MongoDBCollectionImplInstrumentation.java | 15 +++++---------- .../MongoDBCollectionInstrumentation.java | 18 +++++------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index 8d8f2ee18..f061f379c 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,9 +37,10 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor} intercept constructor of {@link com.mongodb.DBCollection}or {@link com.mongodb.DBCollectionImpl} for - * record the ServerAddress,and create the exit span. - *@Auther liyuntao + * {@link MongoDBCollectionMethodInterceptor} intercept constructor of {@link com.mongodb.DBCollection}or {@link + * com.mongodb.DBCollectionImpl} for record the ServerAddress,and create the exit span. + * + * @author liyuntao */ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java index dcff3ac3c..698b24d3e 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -31,16 +31,11 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** - * {@link MongoDBCollectionImplInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the following methods in the - * {@link com.mongodb.DBCollectionImpl}class: - * 1. find
- * 2. insert
- * 3. insertImpl
- * 4. update
- * 5. updateImpl
- * 6. remove
- * 7. createIndex
- *@Auther liyuntao + * {@link MongoDBCollectionImplInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the + * following methods in the {@link com.mongodb.DBCollectionImpl}class: 1. find
2. insert
3. insertImpl
+ * 4. update
5. updateImpl
6. remove
7. createIndex
+ * + * @author liyuntao */ public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index aa96ca7a2..172ae813a 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -31,20 +31,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; - /** - * {@link MongoDBCollectionInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the following methods in the - * {@link com.mongodb.DBCollection}class: - * 1. aggregate
- * 2. findAndModify
- * 3. getCount
- * 4. drop
- * 5. dropIndexes
- * 6. rename
- * 7. group
- * 8. distinct
- * 9. mapReduce
- *@Auther liyuntao + * {@link MongoDBCollectionInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the + * following methods in the {@link com.mongodb.DBCollection}class: 1. aggregate
2. findAndModify
3. getCount + *
4. drop
5. dropIndexes
6. rename
7. group
8. distinct
9. mapReduce
+ * + * @author liyuntao */ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { From b13049a429ecb2dc8dfe26bab0b275270a1d25d2 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sun, 19 Nov 2017 08:54:51 +0800 Subject: [PATCH 20/22] support start script --- .../bin/collectorService.bat | 21 ++++++ .../bin/collectorService.sh | 34 ++++++++++ .../apm-collector-boot/bin/startup.bat | 5 ++ .../apm-collector-boot/bin/startup.sh | 7 ++ apm-collector/apm-collector-boot/pom.xml | 53 ++++++++++++++- .../src/main/assembly/assembly.xml | 67 +++++++++++++++++++ .../src/main/assembly/log4j2.xml | 44 ++++++++++++ 7 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 apm-collector/apm-collector-boot/bin/collectorService.bat create mode 100644 apm-collector/apm-collector-boot/bin/collectorService.sh create mode 100644 apm-collector/apm-collector-boot/bin/startup.bat create mode 100644 apm-collector/apm-collector-boot/bin/startup.sh create mode 100644 apm-collector/apm-collector-boot/src/main/assembly/assembly.xml create mode 100644 apm-collector/apm-collector-boot/src/main/assembly/log4j2.xml diff --git a/apm-collector/apm-collector-boot/bin/collectorService.bat b/apm-collector/apm-collector-boot/bin/collectorService.bat new file mode 100644 index 000000000..08660014c --- /dev/null +++ b/apm-collector/apm-collector-boot/bin/collectorService.bat @@ -0,0 +1,21 @@ +@echo off + +setlocal +set COLLECTOR_PROCESS_TITLE=Skywalking-Collector +set COLLECTOR_HOME=%~dp0%.. +set COLLECTOR_OPTS="-Xms256M -Xmx512M -Dcollector.logDir=%COLLECTOR_HOME%\logs" + +set CLASSPATH=%COLLECTOR_HOME%\config;.; +set CLASSPATH=%COLLECTOR_HOME%\libs\*;%CLASSPATH% + +if defined JAVA_HOME ( + set _EXECJAVA="%JAVA_HOME:"=%"\bin\java +) + +if not defined JAVA_HOME ( + echo "JAVA_HOME not set." + set _EXECJAVA=java +) + +start "%COLLECTOR_PROCESS_TITLE%" %_EXECJAVA% "%COLLECTOR_OPTS%" -cp "%CLASSPATH%" org.skywalking.apm.collector.boot.CollectorBootStartUp +endlocal diff --git a/apm-collector/apm-collector-boot/bin/collectorService.sh b/apm-collector/apm-collector-boot/bin/collectorService.sh new file mode 100644 index 000000000..db5147865 --- /dev/null +++ b/apm-collector/apm-collector-boot/bin/collectorService.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env sh + +PRG="$0" +PRGDIR=`dirname "$PRG"` +[ -z "$COLLECTOR_HOME" ] && COLLECTOR_HOME=`cd "$PRGDIR/.." >/dev/null; pwd` + +COLLECT_LOG_DIR="${COLLECTOR_HOME}/logs" +JAVA_OPTS=" -Xms256M -Xmx512M" + +if [ ! -d "${COLLECTOR_HOME}/logs" ]; then + mkdir -p "${COLLECT_LOG_DIR}" +fi + +_RUNJAVA=${JAVA_HOME}/bin/java +[ -z "$JAVA_HOME" ] && _RUNJAVA=java + +CLASSPATH="$COLLECTOR_HOME/config:$CLASSPATH" +for i in "$COLLECTOR_HOME"/libs/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +WEBUI_OPTIONS=" -Dcollector.logDir=${COLLECT_LOG_DIR}" + +eval exec "\"$_RUNJAVA\" ${JAVA_OPTS} ${WEBUI_OPTIONS} -classpath $CLASSPATH org.skywalking.apm.collector.boot.CollectorBootStartUp \ + 2>${COLLECT_LOG_DIR}/collector.log 1> /dev/null &" + +if [ $? -eq 0 ]; then + sleep 1 + echo "Skywalking Web started successfully!" +else + echo "Skywalking Web started failure!" + exit 1 +fi diff --git a/apm-collector/apm-collector-boot/bin/startup.bat b/apm-collector/apm-collector-boot/bin/startup.bat new file mode 100644 index 000000000..d1f9f401d --- /dev/null +++ b/apm-collector/apm-collector-boot/bin/startup.bat @@ -0,0 +1,5 @@ +@echo off + +setlocal +call "%~dp0"\collectorService.bat start +endlocal diff --git a/apm-collector/apm-collector-boot/bin/startup.sh b/apm-collector/apm-collector-boot/bin/startup.sh new file mode 100644 index 000000000..d8f441aa7 --- /dev/null +++ b/apm-collector/apm-collector-boot/bin/startup.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +PRG="$0" +PRGDIR=`dirname "$PRG"` +EXECUTABLE=collectorService.sh + +exec "$PRGDIR"/"$EXECUTABLE" start diff --git a/apm-collector/apm-collector-boot/pom.xml b/apm-collector/apm-collector-boot/pom.xml index b9873cc2c..ca0aed61c 100644 --- a/apm-collector/apm-collector-boot/pom.xml +++ b/apm-collector/apm-collector-boot/pom.xml @@ -144,4 +144,55 @@ - \ No newline at end of file + + + Skywalking-Collector + + + maven-compiler-plugin + + ${compiler.version} + ${compiler.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.4.3 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.2 + + + application.yml + log4j2.xml + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + assembly + package + + single + + + + src/main/assembly/assembly.xml + + + + + + + + diff --git a/apm-collector/apm-collector-boot/src/main/assembly/assembly.xml b/apm-collector/apm-collector-boot/src/main/assembly/assembly.xml new file mode 100644 index 000000000..683048fbf --- /dev/null +++ b/apm-collector/apm-collector-boot/src/main/assembly/assembly.xml @@ -0,0 +1,67 @@ + + + + + + zip + tar.gz + + + + /libs + runtime + + + + + ${project.basedir}/bin + /bin + + *.sh + *.bat + + 0755 + + + src/main/resources + /config + + application.yml + + + + src/main/assembly + /config + + log4j2.xml + + + + src/main/resources + + application.yml + log4j2.xml + + /config + + + diff --git a/apm-collector/apm-collector-boot/src/main/assembly/log4j2.xml b/apm-collector/apm-collector-boot/src/main/assembly/log4j2.xml new file mode 100644 index 000000000..9c73106af --- /dev/null +++ b/apm-collector/apm-collector-boot/src/main/assembly/log4j2.xml @@ -0,0 +1,44 @@ + + + + + + ${sys:collector.logDir} + + + + + %d - %c -%-4r [%t] %-5p %x - %m%n + + + + + + + + + + + + + + + + From 626ef11fef8441a7d7cdb313368e978b0286656f Mon Sep 17 00:00:00 2001 From: lytscu Date: Mon, 20 Nov 2017 11:20:24 +0800 Subject: [PATCH 21/22] comments --- .../v2/MongoDBCollectionMethodInterceptor.java | 4 ++-- .../plugin/mongodb/v2/define/InterceptPoint.java | 2 +- .../MongoDBCollectionImplInstrumentation.java | 10 ++++++++-- .../v2/define/MongoDBCollectionInstrumentation.java | 13 +++++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index f061f379c..ca09fdced 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -37,8 +37,8 @@ import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptR import org.skywalking.apm.network.trace.component.ComponentsDefine; /** - * {@link MongoDBCollectionMethodInterceptor} intercept constructor of {@link com.mongodb.DBCollection}or {@link - * com.mongodb.DBCollectionImpl} for record the ServerAddress,and create the exit span. + * {@link MongoDBCollectionMethodInterceptor} intercepts constructor of {@link com.mongodb.DBCollection}or {@link + * com.mongodb.DBCollectionImpl} recording the ServerAddress and creating the exit span. * * @author liyuntao */ diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java index 8e4488965..01ddb6081 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/InterceptPoint.java @@ -21,7 +21,7 @@ package org.skywalking.apm.plugin.mongodb.v2.define; import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; /** - * @Auther liyuntao + * @auther liyuntao */ public abstract class InterceptPoint implements InstanceMethodsInterceptPoint { private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor"; diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java index 698b24d3e..47213d615 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionImplInstrumentation.java @@ -32,8 +32,14 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** * {@link MongoDBCollectionImplInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the - * following methods in the {@link com.mongodb.DBCollectionImpl}class: 1. find
2. insert
3. insertImpl
- * 4. update
5. updateImpl
6. remove
7. createIndex
+ * following methods in the {@link com.mongodb.DBCollectionImpl}class: + * 1. find
+ * 2. insert
+ * 3. insertImpl
+ * 4. update
+ * 5. updateImpl
+ * 6. remove
+ * 7. createIndex
* * @author liyuntao */ diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java index 172ae813a..70da4a3b5 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/define/MongoDBCollectionInstrumentation.java @@ -33,8 +33,17 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** * {@link MongoDBCollectionInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the - * following methods in the {@link com.mongodb.DBCollection}class: 1. aggregate
2. findAndModify
3. getCount - *
4. drop
5. dropIndexes
6. rename
7. group
8. distinct
9. mapReduce
+ * following methods in the {@link com.mongodb.DBCollection}class: + * 1. aggregate
+ * 2. findAndModify
+ * 3. getCount + *
+ * 4. drop
+ * 5. dropIndexes
+ * 6. rename
+ * 7. group
+ * 8. distinct
+ * 9. mapReduce
* * @author liyuntao */ From 242ce8009653d69c3ba1c1438aefcca512aac928 Mon Sep 17 00:00:00 2001 From: lytscu Date: Mon, 20 Nov 2017 17:21:13 +0800 Subject: [PATCH 22/22] Remove tag: activeSpan.tag(CommandError, cresult.getErrorMessage()); --- .../plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java index ca09fdced..376d29c90 100644 --- a/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mongodb-2.x-plugin/src/main/java/org/skywalking/apm/plugin/mongodb/v2/MongoDBCollectionMethodInterceptor.java @@ -73,7 +73,6 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround cresult = aresult.getCommandResult(); } if (null != cresult && !cresult.ok()) { - activeSpan.tag("CommandError", cresult.getErrorMessage()); activeSpan.log(cresult.getException()); } ContextManager.stopSpan();