Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#10504] Geometry filter query in the mongodb is not traced #10505

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.trace.ServiceTypeFactory;

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

import static com.navercorp.pinpoint.common.trace.AnnotationKeyProperty.VIEW_IN_RECORD_SET;
import static com.navercorp.pinpoint.common.trace.ServiceTypeProperty.INCLUDE_DESTINATION_ID;
import static com.navercorp.pinpoint.common.trace.ServiceTypeProperty.RECORD_STATISTICS;
Expand All @@ -37,42 +34,42 @@ private MongoConstants() {
static final String MONGO_SCOPE = "MONGO_JAVA_DRIVER";

static final String MONGO_FILTER_NOT = "com.mongodb.client.model.Filters$NotFilter";
static final String MONGO_FILTER_SIMPLEENCODING = "com.mongodb.client.model.Filters$SimpleEncodingFilter";
static final String MONGO_FILTER_ITERABLEOPERATOR = "com.mongodb.client.model.Filters$IterableOperatorFilter";
static final String MONGO_FILTER_SIMPLE_ENCODING = "com.mongodb.client.model.Filters$SimpleEncodingFilter";
static final String MONGO_FILTER_ITERABLE_OPERATOR = "com.mongodb.client.model.Filters$IterableOperatorFilter";
static final String MONGO_FILTER_OR = "com.mongodb.client.model.Filters$OrFilter";
static final String MONGO_FILTER_AND = "com.mongodb.client.model.Filters$AndFilter";
static final String MONGO_FILTER_OPERATOR = "com.mongodb.client.model.Filters$OperatorFilter";
static final String MONGO_FILTER_SIMPLE = "com.mongodb.client.model.Filters$SimpleFilter";
static final String MONGO_FILTER_GEOMETRYOPERATOR = "com.mongodb.client.model.Filters$.GeometryOperatorFilter";
static final String MONGO_FILTER_GEOMETRY_OPERATOR = "com.mongodb.client.model.Filters$GeometryOperatorFilter";
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 = 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[] FILTER_LIST = {
MONGO_FILTER_NOT,
MONGO_FILTER_SIMPLE_ENCODING,
MONGO_FILTER_ITERABLE_OPERATOR,
MONGO_FILTER_OR,
MONGO_FILTER_AND,
MONGO_FILTER_OPERATOR,
MONGO_FILTER_SIMPLE,
MONGO_FILTER_GEOMETRY_OPERATOR,
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 = Arrays.asList(
MONGO_UPDATES_SIMPLE,
MONGO_UPDATES_WITHEACH,
MONGO_UPDATES_PUSH,
MONGO_UPDATES_PULLALL,
MONGO_UPDATES_COMPOSITE
);
static final String[] UPDATES_LIST = {
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
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,13 @@
}

List<InstrumentClass> nestedClasses = target.getNestedClasses(
ClassFilters.name(MongoConstants.FILTERLIST.toArray(new String[0]))
ClassFilters.name(MongoConstants.FILTER_LIST)

Check warning on line 548 in plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/MongoPlugin.java

View check run for this annotation

Codecov / codecov/patch

plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/MongoPlugin.java#L548

Added line #L548 was not covered by tests
);

for (final InstrumentClass nestedClass : nestedClasses) {

//GeometryOperatorFilter 3.1+
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_GEOMETRYOPERATOR)) {
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_GEOMETRY_OPERATOR)) {
instrumentor.transform(loader, nestedClass.getName(), GeometryOperatorTransform.class);
}

Expand All @@ -561,12 +561,12 @@
}

//SimpleEncodingFilter
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_SIMPLEENCODING)) {
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_SIMPLE_ENCODING)) {
instrumentor.transform(loader, nestedClass.getName(), SimpleEncodingFilterTransform.class);
}

//IterableOperatorFilter
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_ITERABLEOPERATOR)) {
if (nestedClass.getName().equals(MongoConstants.MONGO_FILTER_ITERABLE_OPERATOR)) {
instrumentor.transform(loader, nestedClass.getName(), IterableOperatorFilterTransform.class);
}

Expand Down Expand Up @@ -766,7 +766,7 @@
}

List<InstrumentClass> nestedClasses = target.getNestedClasses(
ClassFilters.name(MongoConstants.UPDATESLIST.toArray(new String[0]))
ClassFilters.name(MongoConstants.UPDATES_LIST)

Check warning on line 769 in plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/MongoPlugin.java

View check run for this annotation

Codecov / codecov/patch

plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/MongoPlugin.java#L769

Added line #L769 was not covered by tests
);

for (final InstrumentClass nestedClass : nestedClasses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ private void parseSortObject(Object arg) {
private void parseFilterObject(Object arg) {

String argName = arg.getClass().getName();
logger.debug("filter arg : " + arg.getClass().getName());
logger.debug("filter arg : {}", argName);

//OperatorFilter
switch (argName) {
case MongoConstants.MONGO_FILTER_GEOMETRYOPERATOR:
case MongoConstants.MONGO_FILTER_GEOMETRY_OPERATOR:
logger.debug("writing GeometryOperatorFilter");

bsonWriter.writeStartDocument();
Expand Down Expand Up @@ -289,7 +289,7 @@ private void parseFilterObject(Object arg) {
bsonWriter.writeEndDocument();
break;
//IterableOperatorFilter
case MongoConstants.MONGO_FILTER_ITERABLEOPERATOR:
case MongoConstants.MONGO_FILTER_ITERABLE_OPERATOR:
logger.debug("writing IterableOperatorFilter");

if (arg instanceof FieldNameGetter) {
Expand All @@ -309,7 +309,7 @@ private void parseFilterObject(Object arg) {
}
break;
//SimpleEncodingFilter
case MongoConstants.MONGO_FILTER_SIMPLEENCODING:
case MongoConstants.MONGO_FILTER_SIMPLE_ENCODING:
//else if (arg instanceof FieldNameGetter && arg instanceof ValueGetter) {

logger.debug("writing SimpleEncodingFilter");
Expand Down Expand Up @@ -754,23 +754,27 @@ private void writeNull() {
}

private boolean isSort(Object arg) {
if (MongoConstants.MONGO_SORT_COMPOSITE.equals(arg.getClass().getName())) {
return true;
}
return false;
return MongoConstants.MONGO_SORT_COMPOSITE.equals(arg.getClass().getName());
}

private boolean isUpdates(Object arg) {
if (MongoConstants.UPDATESLIST.contains(arg.getClass().getName())) {
return true;
}
return false;
String name = arg.getClass().getName();
return contains(MongoConstants.UPDATES_LIST, name);
}

private boolean isFilter(Object arg) {
if (MongoConstants.FILTERLIST.contains(arg.getClass().getName())) {
return true;
String name = arg.getClass().getName();
return contains(MongoConstants.FILTER_LIST, name);
}

private static boolean contains(String[] list, String name) {
for (String className : list) {
if (className.equals(name)) {
return true;
}
}
return false;
}


}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.navercorp.pinpoint.plugin.mongo;

import com.mongodb.client.model.Filters;
import com.navercorp.pinpoint.common.util.BytesUtils;
import org.bson.BsonBinary;
import org.bson.BsonDocument;
import org.bson.conversions.Bson;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -35,4 +38,19 @@ void parse_binary() {
byte[] sourceByes = Arrays.copyOf(Base64.getEncoder().encode(bytes), WriteContext.DEFAULT_ABBREVIATE_MAX_WIDTH);
Assertions.assertEquals(BytesUtils.toString(sourceByes), nopadInput);
}


@Test
void parse_geometry() {
List<String> parameter = new ArrayList<>();
WriteContext context = new WriteContext(parameter, true, true);

BsonDocument bson = new BsonDocument();
BsonBinary binary = new BsonBinary("123456789".getBytes(StandardCharsets.UTF_8));
bson.append("bson", binary);

Bson geo = Filters.geoIntersects("geo", bson);
// touch GeometryOperatorFilter line
Assertions.assertThrows(ClassCastException.class, () -> context.parse(Collections.singletonList(geo)));
}
}