Skip to content

Commit

Permalink
Convert mongo-common tests from groovy to java (#12382)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiepuhuan authored Oct 17, 2024
1 parent c3d8ed4 commit c743b6d
Show file tree
Hide file tree
Showing 22 changed files with 1,947 additions and 1,555 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.mongo.v3_1;

import com.mongodb.MongoClientOptions;
import io.opentelemetry.instrumentation.mongo.v3_1.AbstractMongo31ClientTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

class MongoClientTest extends AbstractMongo31ClientTest {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Override
protected void configureMongoClientOptions(MongoClientOptions.Builder options) {}

@Override
protected InstrumentationExtension testing() {
return testing;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.mongo.v3_1;

import com.mongodb.MongoClientOptions;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

class MongoClientTest extends AbstractMongo31ClientTest {

@RegisterExtension
private static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();

@Override
protected void configureMongoClientOptions(MongoClientOptions.Builder options) {
options.addCommandListener(
MongoTelemetry.create(testing().getOpenTelemetry()).newCommandListener());
}

@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.mongo.v3_1;

import static io.opentelemetry.instrumentation.mongo.v3_1.MongoTelemetryBuilder.DEFAULT_MAX_NORMALIZED_QUERY_LENGTH;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class MongoDbAttributesGetterTest {

@Test
@DisplayName("should sanitize statements to json")
void shouldSanitizeStatementsToJson() {
MongoDbAttributesGetter extractor =
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH);

assertThat(
sanitizeStatementAcrossVersions(extractor, new BsonDocument("cmd", new BsonInt32(1))))
.isEqualTo("{\"cmd\": \"?\"}");

assertThat(
sanitizeStatementAcrossVersions(
extractor,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonDocument("a", new BsonInt32(1)))))
.isEqualTo("{\"cmd\": \"?\", \"sub\": {\"a\": \"?\"}}");

assertThat(
sanitizeStatementAcrossVersions(
extractor,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonArray(singletonList(new BsonInt32(1))))))
.isEqualTo("{\"cmd\": \"?\", \"sub\": [\"?\"]}");
}

@Test
@DisplayName("should only preserve string value if it is the value of the first top-level key")
void shouldOnlyPreserveStringValueIfItIsTheValueOfTheFirstTopLevelKey() {
MongoDbAttributesGetter extractor =
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH);

assertThat(
sanitizeStatementAcrossVersions(
extractor,
new BsonDocument("cmd", new BsonString("c"))
.append("f", new BsonString("c"))
.append("sub", new BsonString("c"))))
.isEqualTo("{\"cmd\": \"c\", \"f\": \"?\", \"sub\": \"?\"}");
}

@Test
@DisplayName("should truncate simple command")
void shouldTruncateSimpleCommand() {
MongoDbAttributesGetter extractor = new MongoDbAttributesGetter(true, 20);

String normalized =
sanitizeStatementAcrossVersions(
extractor,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonString("c1"))
.append("f2", new BsonString("c2")));

// This can vary because of different whitespace for different MongoDB versions
assertThat(normalized).isIn("{\"cmd\": \"c\", \"f1\": \"", "{\"cmd\": \"c\", \"f1\" ");
}

@Test
@DisplayName("should truncate array")
void shouldTruncateArray() {
MongoDbAttributesGetter extractor = new MongoDbAttributesGetter(true, 27);

String normalized =
sanitizeStatementAcrossVersions(
extractor,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
.append("f2", new BsonString("c3")));

// This can vary because of different whitespace for different MongoDB versions
assertThat(normalized)
.isIn("{\"cmd\": \"c\", \"f1\": [\"?\", \"?", "{\"cmd\": \"c\", \"f1\": [\"?\",");
}

static String sanitizeStatementAcrossVersions(
MongoDbAttributesGetter extractor, BsonDocument query) {
return sanitizeAcrossVersions(extractor.sanitizeStatement(query));
}

static String sanitizeAcrossVersions(String json) {
json = json.replaceAll("\\{ ", "{");
json = json.replaceAll(" }", "}");
json = json.replaceAll(" :", ":");
return json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.mongo.v3_1;

import static io.opentelemetry.instrumentation.mongo.v3_1.MongoTelemetryBuilder.DEFAULT_MAX_NORMALIZED_QUERY_LENGTH;
import static org.assertj.core.api.Assertions.assertThat;

import com.mongodb.event.CommandStartedEvent;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class MongoSpanNameExtractorTest {

@Test
@DisplayName("test span name with no dbName")
void testSpanNameWithNoDbName() {
MongoSpanNameExtractor nameExtractor =
new MongoSpanNameExtractor(
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH),
new MongoAttributesExtractor());

String command = "listDatabases";
CommandStartedEvent event =
new CommandStartedEvent(
0, null, null, command, new BsonDocument(command, new BsonInt32(1)));

String spanName = nameExtractor.extract(event);

assertThat(spanName).isEqualTo(command);
}
}
2 changes: 0 additions & 2 deletions instrumentation/mongo/mongo-3.1/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ dependencies {

compileOnly("org.mongodb:mongo-java-driver:3.1.0")

implementation("org.apache.groovy:groovy")
implementation("io.opentelemetry:opentelemetry-api")
implementation("org.spockframework:spock-core")
}
Loading

0 comments on commit c743b6d

Please sign in to comment.