143 lines
4.5 KiB
Groovy
143 lines
4.5 KiB
Groovy
package datadog.trace
|
|
|
|
import datadog.opentracing.DDTracer
|
|
import datadog.trace.api.Config
|
|
import datadog.trace.common.sampling.AllSampler
|
|
import datadog.trace.common.sampling.RateByServiceSampler
|
|
import datadog.trace.common.writer.DDAgentWriter
|
|
import datadog.trace.common.writer.ListWriter
|
|
import datadog.trace.common.writer.LoggingWriter
|
|
import org.junit.Rule
|
|
import org.junit.contrib.java.lang.system.EnvironmentVariables
|
|
import org.junit.contrib.java.lang.system.RestoreSystemProperties
|
|
import spock.lang.Specification
|
|
|
|
import static datadog.trace.api.Config.DEFAULT_SERVICE_NAME
|
|
import static datadog.trace.api.Config.HEADER_TAGS
|
|
import static datadog.trace.api.Config.PREFIX
|
|
import static datadog.trace.api.Config.PRIORITY_SAMPLING
|
|
import static datadog.trace.api.Config.SERVICE_MAPPING
|
|
import static datadog.trace.api.Config.SPAN_TAGS
|
|
import static datadog.trace.api.Config.WRITER_TYPE
|
|
|
|
class DDTracerTest extends Specification {
|
|
@Rule
|
|
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties()
|
|
@Rule
|
|
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
|
|
|
|
def setupSpec() {
|
|
// assert that a trace agent isn't running locally as that messes up the test.
|
|
try {
|
|
(new Socket("localhost", 8126)).close()
|
|
throw new IllegalStateException("Trace Agent unexpectedly running locally.")
|
|
} catch (final ConnectException ioe) {
|
|
// trace agent is not running locally.
|
|
}
|
|
}
|
|
|
|
def "verify defaults on tracer"() {
|
|
when:
|
|
def tracer = new DDTracer()
|
|
|
|
then:
|
|
tracer.serviceName == "unnamed-java-app"
|
|
tracer.sampler instanceof AllSampler
|
|
tracer.writer.toString() == "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:8126/v0.3/traces } }"
|
|
|
|
tracer.spanContextDecorators.size() == 12
|
|
}
|
|
|
|
def "verify overriding sampler"() {
|
|
setup:
|
|
System.setProperty(PREFIX + PRIORITY_SAMPLING, "true")
|
|
|
|
when:
|
|
def tracer = new DDTracer(new Config())
|
|
|
|
then:
|
|
tracer.sampler instanceof RateByServiceSampler
|
|
}
|
|
|
|
def "verify overriding writer"() {
|
|
setup:
|
|
System.setProperty(PREFIX + WRITER_TYPE, "LoggingWriter")
|
|
|
|
when:
|
|
def tracer = new DDTracer(new Config())
|
|
|
|
then:
|
|
tracer.writer instanceof LoggingWriter
|
|
}
|
|
|
|
def "verify mapping configs on tracer"() {
|
|
setup:
|
|
System.setProperty(PREFIX + SERVICE_MAPPING, mapString)
|
|
System.setProperty(PREFIX + SPAN_TAGS, mapString)
|
|
System.setProperty(PREFIX + HEADER_TAGS, mapString)
|
|
|
|
when:
|
|
def config = new Config()
|
|
def tracer = new DDTracer(config)
|
|
def taggedHeaders = tracer.registry.codecs.values().first().taggedHeaders
|
|
|
|
then:
|
|
tracer.defaultSpanTags == map
|
|
tracer.serviceNameMappings == map
|
|
taggedHeaders == map
|
|
|
|
where:
|
|
mapString | map
|
|
"a:1, a:2, a:3" | [a: "3"]
|
|
"a:b,c:d,e:" | [a: "b", c: "d"]
|
|
}
|
|
|
|
def "verify single override on #source for #key"() {
|
|
when:
|
|
System.setProperty(PREFIX + key, value)
|
|
def tracer = new DDTracer(new Config())
|
|
|
|
then:
|
|
tracer."$source".toString() == expected
|
|
|
|
where:
|
|
|
|
source | key | value | expected
|
|
"writer" | "default" | "default" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:8126/v0.3/traces } }"
|
|
"writer" | "writer.type" | "LoggingWriter" | "LoggingWriter { }"
|
|
"writer" | "agent.host" | "somethingelse" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://somethingelse:8126/v0.3/traces } }"
|
|
"writer" | "agent.port" | "777" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:777/v0.3/traces } }"
|
|
"writer" | "trace.agent.port" | "9999" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:9999/v0.3/traces } }"
|
|
}
|
|
|
|
def "verify sampler/writer constructor"() {
|
|
setup:
|
|
def writer = new ListWriter()
|
|
def sampler = new RateByServiceSampler()
|
|
|
|
when:
|
|
def tracer = new DDTracer(DEFAULT_SERVICE_NAME, writer, sampler)
|
|
|
|
then:
|
|
tracer.serviceName == DEFAULT_SERVICE_NAME
|
|
tracer.sampler == sampler
|
|
tracer.writer == writer
|
|
tracer.runtimeId.length() > 0
|
|
}
|
|
|
|
def "Shares TraceCount with DDApi with #key = #value"() {
|
|
setup:
|
|
System.setProperty(PREFIX + key, value)
|
|
final DDTracer tracer = new DDTracer(new Config())
|
|
|
|
expect:
|
|
tracer.writer instanceof DDAgentWriter
|
|
tracer.traceCount.is(((DDAgentWriter) tracer.writer).getApi().traceCount)
|
|
|
|
where:
|
|
key | value
|
|
Config.PRIORITY_SAMPLING | "true"
|
|
Config.PRIORITY_SAMPLING | "false"
|
|
}
|
|
}
|