From b63c0ac5725f96d89deaf8cfc5a6c51a24526834 Mon Sep 17 00:00:00 2001 From: lytscu Date: Thu, 9 Nov 2017 19:23:44 +0800 Subject: [PATCH] =?UTF-8?q?=201=E3=80=81Modify=20the=20code=20review.=20?= =?UTF-8?q?=202=E3=80=81Modify=20V3=20plugin=20to=20support=20all=20versio?= =?UTF-8?q?n=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()); } }