Skip to content

Commit

Permalink
[#5067] Cleanup mongodb plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad authored and RoySRose committed Feb 12, 2019
1 parent 452a6f2 commit df464da
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 159 deletions.
Expand Up @@ -22,9 +22,9 @@
import com.mongodb.client.result.DeleteResult;
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.common.util.StringStringValue;
import com.navercorp.pinpoint.plugin.mongo.MongoConstants;
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;
import com.navercorp.pinpoint.plugin.mongo.NormalizedBson;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
Expand Down Expand Up @@ -148,7 +148,7 @@ public void insertComlexBsonValueData30(PluginTestVerifier verifier, MongoCollec
Object[] objects = new Object[1];
objects[0] = document;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

Method insertOne;
try {
Expand Down Expand Up @@ -197,7 +197,7 @@ public void insertComlexBsonValueData34(PluginTestVerifier verifier, MongoCollec
Object[] objects = new Object[1];
objects[0] = document;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

Method insertOne;
try {
Expand All @@ -220,7 +220,7 @@ public void insertData(PluginTestVerifier verifier, MongoCollection<Document> co
Object[] objects = new Object[1];
objects[0] = doc;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

Method insertOne;
try {
Expand All @@ -246,7 +246,7 @@ public void updateData(PluginTestVerifier verifier, MongoCollection<Document> co
objects[0] = doc;
objects[1] = doc2;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

Method updateOne;
try {
Expand Down Expand Up @@ -295,7 +295,7 @@ public void deleteData(PluginTestVerifier verifier, MongoCollection<Document> co
Object[] objects = new Object[1];
objects[0] = doc;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

DeleteResult deleteResult = collection.deleteMany(doc);

Expand All @@ -320,7 +320,7 @@ public void filterData(PluginTestVerifier verifier, MongoCollection<Document> co
Object[] objects = new Object[1];
objects[0] = bson;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

MongoCursor<Document> cursor = collection.find(bson).iterator();

Expand Down Expand Up @@ -357,7 +357,7 @@ public void filterData2(PluginTestVerifier verifier, MongoCollection<Document> c
Object[] objects = new Object[1];
objects[0] = bson;

StringStringValue parsedBson = MongoUtil.parseBson(objects, true);
NormalizedBson parsedBson = MongoUtil.parseBson(objects, true);

MongoCursor<Document> cursor = collection.find(bson).iterator();

Expand Down
Expand Up @@ -44,21 +44,4 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StringStringValue)) return false;

StringStringValue that = (StringStringValue) o;

if (!getStringValue1().equals(that.getStringValue1())) return false;
return getStringValue2() != null ? getStringValue2().equals(that.getStringValue2()) : that.getStringValue2() == null;
}

@Override
public int hashCode() {
int result = getStringValue1().hashCode();
result = 31 * result + (getStringValue2() != null ? getStringValue2().hashCode() : 0);
return result;
}
}
Expand Up @@ -19,7 +19,7 @@
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.trace.ServiceTypeFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.navercorp.pinpoint.common.trace.AnnotationKeyProperty.VIEW_IN_RECORD_SET;
Expand All @@ -45,32 +45,32 @@ private MongoConstants() {
static final String MONGO_FILTER_TEXT = "com.mongodb.client.model.Filters$TextFilter";
static final String MONGO_FILTER_ORNOR = "com.mongodb.client.model.Filters$OrNorFilter";

static final List<String> FILTERLIST = new ArrayList<String>() {{
add(MONGO_FILTER_NOT);
add(MONGO_FILTER_SIMPLEENCODING);
add(MONGO_FILTER_ITERABLEOPERATOR);
add(MONGO_FILTER_OR);
add(MONGO_FILTER_AND);
add(MONGO_FILTER_OPERATOR);
add(MONGO_FILTER_SIMPLE);
add(MONGO_FILTER_GEOMETRYOPERATOR);
add(MONGO_FILTER_TEXT);
add(MONGO_FILTER_ORNOR);
}};
static final List<String> FILTERLIST = Arrays.asList(
MONGO_FILTER_NOT,
MONGO_FILTER_SIMPLEENCODING,
MONGO_FILTER_ITERABLEOPERATOR,
MONGO_FILTER_OR,
MONGO_FILTER_AND,
MONGO_FILTER_OPERATOR,
MONGO_FILTER_SIMPLE,
MONGO_FILTER_GEOMETRYOPERATOR,
MONGO_FILTER_TEXT,
MONGO_FILTER_ORNOR
);

static final String MONGO_UPDATES_SIMPLE = "com.mongodb.client.model.Updates$SimpleUpdate";
static final String MONGO_UPDATES_WITHEACH = "com.mongodb.client.model.Updates$WithEachUpdate";
static final String MONGO_UPDATES_PUSH = "com.mongodb.client.model.Updates$PushUpdate";
static final String MONGO_UPDATES_PULLALL = "com.mongodb.client.model.Updates$PullAllUpdate";
static final String MONGO_UPDATES_COMPOSITE = "com.mongodb.client.model.Updates$CompositeUpdate";

static final List<String> UPDATESLIST = new ArrayList<String>() {{
add(MONGO_UPDATES_SIMPLE);
add(MONGO_UPDATES_WITHEACH);
add(MONGO_UPDATES_PUSH);
add(MONGO_UPDATES_PULLALL);
add(MONGO_UPDATES_COMPOSITE);
}};
static final List<String> UPDATESLIST = Arrays.asList(
MONGO_UPDATES_SIMPLE,
MONGO_UPDATES_WITHEACH,
MONGO_UPDATES_PUSH,
MONGO_UPDATES_PULLALL,
MONGO_UPDATES_COMPOSITE
);

static final String MONGO_SORT_COMPOSITE = "com.mongodb.client.model.Sorts$CompoundSort";

Expand Down
Expand Up @@ -692,40 +692,33 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, Strin

private String[] getMethodlistR3_0_x() {

List<String> methodlist = new ArrayList<String>();
methodlist.addAll(Arrays.asList("findOneAndUpdate", "findOneAndReplace", "findOneAndDelete", "find", "count", "distinct", "listIndexes"
final String[] methodList = new String[]{"findOneAndUpdate", "findOneAndReplace", "findOneAndDelete", "find", "count", "distinct", "listIndexes"
// , "watch" ,"aggregate","mapReduce"
));

return methodlist.toArray(new String[0]);
};
return methodList;
}

private String[] getMethodlistCUD3_0_x() {

List<String> methodlist = new ArrayList<String>();
methodlist.addAll(Arrays.asList("dropIndexes", "dropIndex", "createIndexes", "createIndex"
, "updateMany", "updateOne", "replaceOne", "deleteMany", "deleteOne", "insertMany", "insertOne", "bulkWrite"));

return methodlist.toArray(new String[0]);
final String[] methodList = new String[] {"dropIndexes", "dropIndex", "createIndexes", "createIndex"
, "updateMany", "updateOne", "replaceOne", "deleteMany", "deleteOne", "insertMany", "insertOne", "bulkWrite"};
return methodList;
}

private String[] getMethodlistR3_7_x() {

List<String> methodlist = new ArrayList<String>();
methodlist.addAll(Arrays.asList("findOneAndUpdate", "findOneAndReplace", "findOneAndDelete", "find", "count", "distinct", "listIndexes", "countDocuments"
final String[] methodList = new String[]{"findOneAndUpdate", "findOneAndReplace", "findOneAndDelete", "find", "count", "distinct", "listIndexes", "countDocuments"
// , "watch", "aggregate", "mapReduce"
));
};

return methodlist.toArray(new String[0]);
return methodList;
}

private String[] getMethodlistCUD3_7_x() {

List<String> methodlist = new ArrayList<String>();
methodlist.addAll(Arrays.asList("dropIndexes", "dropIndex", "createIndexes", "createIndex"
, "updateMany", "updateOne", "replaceOne", "deleteMany", "deleteOne", "insertMany", "insertOne", "bulkWrite"));
final String[] methodlist = new String[]{"dropIndexes", "dropIndex", "createIndexes", "createIndex"
, "updateMany", "updateOne", "replaceOne", "deleteMany", "deleteOne", "insertMany", "insertOne", "bulkWrite"};

return methodlist.toArray(new String[0]);
return methodlist;
}

@Override
Expand Down
Expand Up @@ -55,13 +55,14 @@ public static String getWriteConcern0(WriteConcern writeConcern) {
return mongoWriteConcernMapper.getName(writeConcern);
}

public static void recordParsedBson(SpanEventRecorder recorder, StringStringValue stringStringValue) {
if (stringStringValue != null) {
public static void recordParsedBson(SpanEventRecorder recorder, NormalizedBson normalizedBson) {
if (normalizedBson != null) {
StringStringValue stringStringValue = new StringStringValue(normalizedBson.getNormalizedBson(), normalizedBson.getParameter());
recorder.recordAttribute(MongoConstants.MONGO_JSON_DATA, stringStringValue);
}
}

public static StringStringValue parseBson(Object[] args, boolean traceBsonBindValue) {
public static NormalizedBson parseBson(Object[] args, boolean traceBsonBindValue) {

if (args == null) {
return null;
Expand All @@ -83,7 +84,7 @@ public static StringStringValue parseBson(Object[] args, boolean traceBsonBindVa

String parsedJsonString = StringJoiner.join(parsedJson, SEPARATOR);
String jsonParameterString = StringJoiner.join(jsonParameter, SEPARATOR);
return new StringStringValue(parsedJsonString, jsonParameterString);
return new NormalizedBson(parsedJsonString, jsonParameterString);
}
}

@@ -0,0 +1,65 @@
/*
* Copyright 2019 NAVER Corp.
*
* 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.
*/

package com.navercorp.pinpoint.plugin.mongo;

/**
* @author Woonduk Kang(emeroad)
*/
public class NormalizedBson {

private final String normalizedBson;
private final String parameter;

public NormalizedBson(String normalizedBson, String parameter) {
this.normalizedBson = normalizedBson;
this.parameter = parameter;
}

public String getNormalizedBson() {
return normalizedBson;
}

public String getParameter() {
return parameter;
}

@Override
public String toString() {
return "NormalizedBson{" +
"normalizedBson='" + normalizedBson + '\'' +
", parameter='" + parameter + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NormalizedBson)) return false;

NormalizedBson that = (NormalizedBson) o;

if (normalizedBson != null ? !normalizedBson.equals(that.normalizedBson) : that.normalizedBson != null) return false;
return parameter != null ? parameter.equals(that.parameter) : that.parameter == null;
}

@Override
public int hashCode() {
int result = normalizedBson != null ? normalizedBson.hashCode() : 0;
result = 31 * result + (parameter != null ? parameter.hashCode() : 0);
return result;
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2019 NAVER Corp.
*
* 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.
*/

package com.navercorp.pinpoint.plugin.mongo.interceptor;

import com.navercorp.pinpoint.bootstrap.context.DatabaseInfo;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo;

/**
* @author Woonduk Kang(emeroad)
*/
public final class DatabaseInfoUtils {
private DatabaseInfoUtils() {
}

public static DatabaseInfo getDatabaseInfo(Object target, DatabaseInfo defaultDatabaseInfo) {
if (target instanceof DatabaseInfoAccessor) {
final DatabaseInfo databaseInfo = ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo();
if (databaseInfo != null) {
return databaseInfo;
}
}
return defaultDatabaseInfo;
}

}
Expand Up @@ -23,12 +23,11 @@
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.MongoDatabaseInfo;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo;
import com.navercorp.pinpoint.bootstrap.util.InterceptorUtils;
import com.navercorp.pinpoint.common.util.StringStringValue;
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;
import com.navercorp.pinpoint.plugin.mongo.NormalizedBson;

/**
* @author Roy Kim
Expand All @@ -48,16 +47,7 @@ public MongoCUDSessionInterceptor(TraceContext traceContext, MethodDescriptor de
@Override
protected void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) {

DatabaseInfo databaseInfo;
if (target instanceof DatabaseInfoAccessor) {
databaseInfo = ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo();
} else {
databaseInfo = null;
}

if (databaseInfo == null) {
databaseInfo = UnKnownDatabaseInfo.MONGO_INSTANCE;
}
final DatabaseInfo databaseInfo = DatabaseInfoUtils.getDatabaseInfo(target, UnKnownDatabaseInfo.MONGO_INSTANCE);

recorder.recordServiceType(databaseInfo.getExecuteQueryType());
recorder.recordEndPoint(databaseInfo.getMultipleHost());
Expand All @@ -77,7 +67,7 @@ public void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] a
final boolean success = InterceptorUtils.isSuccess(throwable);
if (success) {
if (args != null) {
StringStringValue parsedBson = MongoUtil.parseBson(args, traceBsonBindValue);
NormalizedBson parsedBson = MongoUtil.parseBson(args, traceBsonBindValue);
MongoUtil.recordParsedBson(recorder, parsedBson);
}
}
Expand Down

0 comments on commit df464da

Please sign in to comment.