opentelemetry-java-instrume.../dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanTest.groovy

157 lines
4.2 KiB
Groovy

package datadog.opentracing
import datadog.trace.common.sampling.PrioritySampling
import datadog.trace.common.writer.ListWriter
import spock.lang.Specification
import spock.lang.Timeout
import java.util.concurrent.TimeUnit
@Timeout(10)
class DDSpanTest extends Specification {
def writer = new ListWriter()
def tracer = new DDTracer(writer)
def "getters and setters"() {
setup:
final DDSpanContext context =
new DDSpanContext(
1L,
1L,
0L,
"fakeService",
"fakeOperation",
"fakeResource",
PrioritySampling.UNSET,
Collections.<String, String> emptyMap(),
false,
"fakeType",
null,
null,
null)
final DDSpan span = new DDSpan(1L, context)
when:
span.setServiceName("service")
then:
span.getServiceName() == "service"
when:
span.setOperationName("operation")
then:
span.getOperationName() == "operation"
when:
span.setResourceName("resource")
then:
span.getResourceName() == "resource"
when:
span.setSpanType("type")
then:
span.getType() == "type"
when:
span.setSamplingPriority(PrioritySampling.UNSET)
then:
span.getSamplingPriority() == null
when:
span.setSamplingPriority(PrioritySampling.SAMPLER_KEEP)
then:
span.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
when:
context.lockSamplingPriority()
span.setSamplingPriority(PrioritySampling.USER_KEEP)
then:
span.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
}
def "resource name equals operation name if null"() {
setup:
final String opName = "operationName"
DDSpan span
when:
span = tracer.buildSpan(opName).start()
then:
span.getResourceName() == opName
span.getServiceName() == DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME
when:
final String resourceName = "fake"
final String serviceName = "myService"
span = new DDTracer()
.buildSpan(opName)
.withResourceName(resourceName)
.withServiceName(serviceName)
.start()
then:
span.getResourceName() == resourceName
span.getServiceName() == serviceName
}
def "duration measured in nanoseconds"() {
setup:
def mod = TimeUnit.MILLISECONDS.toNanos(1)
def builder = tracer.buildSpan("test")
def start = System.nanoTime()
def span = builder.start()
def between = System.nanoTime()
def betweenDur = System.nanoTime() - between
span.finish()
def total = System.nanoTime() - start
expect:
span.durationNano > betweenDur
span.durationNano < total
span.durationNano % mod > 0 // Very slim chance of a false negative.
}
def "starting with a timestamp disables nanotime"() {
setup:
def mod = TimeUnit.MILLISECONDS.toNanos(1)
def start = System.currentTimeMillis()
def builder = tracer.buildSpan("test")
.withStartTimestamp(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()))
def span = builder.start()
def between = System.currentTimeMillis()
def betweenDur = System.currentTimeMillis() - between
span.finish()
def total = Math.max(1, System.currentTimeMillis() - start)
expect:
span.durationNano >= TimeUnit.MILLISECONDS.toNanos(betweenDur)
span.durationNano <= TimeUnit.MILLISECONDS.toNanos(total)
span.durationNano % mod == 0
}
def "stopping with a timestamp disables nanotime"() {
setup:
def mod = TimeUnit.MILLISECONDS.toNanos(1)
def builder = tracer.buildSpan("test")
def start = System.currentTimeMillis()
def span = builder.start()
def between = System.currentTimeMillis()
def betweenDur = System.currentTimeMillis() - between
span.finish(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis() + 1))
def total = System.currentTimeMillis() - start + 1
expect:
span.durationNano >= TimeUnit.MILLISECONDS.toNanos(betweenDur)
span.durationNano <= TimeUnit.MILLISECONDS.toNanos(total)
span.durationNano % mod == 0
}
def "stopping with a timestamp after start time yeilds a min duration of 1"() {
setup:
def span = tracer.buildSpan("test").start()
span.finish(span.startTimeMicro - 10)
expect:
span.durationNano == 1
}
}