Move MongoClientTracerTest to same module as MongoClientTracer (#1804)
* Move MongoClientTracerTest to mongo-common * Remove multi-version support * Use library dependency
This commit is contained in:
parent
c07da390c0
commit
01b4345742
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import static java.util.Arrays.asList
|
||||
|
||||
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
|
||||
import org.bson.BsonArray
|
||||
import org.bson.BsonDocument
|
||||
import org.bson.BsonInt32
|
||||
import org.bson.BsonString
|
||||
import spock.lang.Specification
|
||||
|
||||
class MongoClientTracerTest extends Specification {
|
||||
def 'should normalize queries to json'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer()
|
||||
|
||||
expect:
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonInt32(1))) ==
|
||||
'{"cmd": "?"}'
|
||||
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonInt32(1))
|
||||
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
||||
'{"cmd": "?", "sub": {"a": "?"}}'
|
||||
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonInt32(1))
|
||||
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
||||
'{"cmd": "?", "sub": ["?"]}'
|
||||
}
|
||||
|
||||
def 'should only preserve string value if it is the value of the first top-level key'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer()
|
||||
|
||||
expect:
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f", new BsonString("c"))
|
||||
.append("sub", new BsonString("c"))) ==
|
||||
'{"cmd": "c", "f": "?", "sub": "?"}'
|
||||
}
|
||||
|
||||
def 'should truncate simple command'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer(20)
|
||||
|
||||
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f1", new BsonString("c1"))
|
||||
.append("f2", new BsonString("c2")))
|
||||
expect:
|
||||
// this can vary because of different whitespace for different mongo versions
|
||||
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
|
||||
}
|
||||
|
||||
def 'should truncate array'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer(27)
|
||||
|
||||
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
|
||||
.append("f2", new BsonString("c3")))
|
||||
expect:
|
||||
// this can vary because of different whitespace for different mongo versions
|
||||
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
|
||||
}
|
||||
|
||||
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
|
||||
return normalizeAcrossVersions(tracer.normalizeQuery(query))
|
||||
}
|
||||
|
||||
def normalizeAcrossVersions(String json) {
|
||||
json = json.replaceAll('\\{ ', '{')
|
||||
json = json.replaceAll(' }', '}')
|
||||
json = json.replaceAll(' :', ':')
|
||||
return json
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
apply from: "$rootDir/gradle/instrumentation.gradle"
|
||||
|
||||
dependencies {
|
||||
compileOnly group: 'org.mongodb', name: 'mongo-java-driver', version: '3.1.0'
|
||||
library group: 'org.mongodb', name: 'mongo-java-driver', version: '3.1.0'
|
||||
}
|
||||
|
|
|
@ -18,19 +18,19 @@ class MongoClientTracerTest extends Specification {
|
|||
def tracer = new MongoClientTracer()
|
||||
|
||||
expect:
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonInt32(1))) ==
|
||||
'{"cmd": "?"}'
|
||||
'{ "cmd" : "?" }'
|
||||
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonInt32(1))
|
||||
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
||||
'{"cmd": "?", "sub": {"a": "?"}}'
|
||||
'{ "cmd" : "?", "sub" : { "a" : "?" } }'
|
||||
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonInt32(1))
|
||||
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
||||
'{"cmd": "?", "sub": ["?"]}'
|
||||
'{ "cmd" : "?", "sub" : ["?"] }'
|
||||
}
|
||||
|
||||
def 'should only preserve string value if it is the value of the first top-level key'() {
|
||||
|
@ -38,47 +38,34 @@ class MongoClientTracerTest extends Specification {
|
|||
def tracer = new MongoClientTracer()
|
||||
|
||||
expect:
|
||||
normalizeQueryAcrossVersions(tracer,
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f", new BsonString("c"))
|
||||
.append("sub", new BsonString("c"))) ==
|
||||
'{"cmd": "c", "f": "?", "sub": "?"}'
|
||||
'{ "cmd" : "c", "f" : "?", "sub" : "?" }'
|
||||
}
|
||||
|
||||
def 'should truncate simple command'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer(20)
|
||||
|
||||
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||
expect:
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f1", new BsonString("c1"))
|
||||
.append("f2", new BsonString("c2")))
|
||||
expect:
|
||||
// this can vary because of different whitespace for different mongo versions
|
||||
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
|
||||
.append("f2", new BsonString("c2"))) ==
|
||||
'{ "cmd" : "c", "f1" '
|
||||
}
|
||||
|
||||
def 'should truncate array'() {
|
||||
setup:
|
||||
def tracer = new MongoClientTracer(27)
|
||||
|
||||
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
|
||||
.append("f2", new BsonString("c3")))
|
||||
expect:
|
||||
// this can vary because of different whitespace for different mongo versions
|
||||
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
|
||||
}
|
||||
|
||||
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
|
||||
return normalizeAcrossVersions(tracer.normalizeQuery(query))
|
||||
}
|
||||
|
||||
def normalizeAcrossVersions(String json) {
|
||||
json = json.replaceAll('\\{ ', '{')
|
||||
json = json.replaceAll(' }', '}')
|
||||
json = json.replaceAll(' :', ':')
|
||||
return json
|
||||
tracer.normalizeQuery(
|
||||
new BsonDocument("cmd", new BsonString("c"))
|
||||
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
|
||||
.append("f2", new BsonString("c3"))) ==
|
||||
'{ "cmd" : "c", "f1" : ["?",'
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue