Adding StatsD tests
Adding a happy & comm failure test case for StatsDMonitor.
This checks some but not all of the counters but also makes sure the exceptional path branches are exercised.
To make this test possible, I added an additional constructor to StatsDMonitor that takes a StatsDClient.
MODIFY
dd-trace-ot/src/main/java/datadog/trace/common/writer/DDAgentWriter.java
dd-trace-ot/src/main/java/datadog/trace/common/writer/DDAgentWriter.java
@@ -466,21 +466,23 @@ public class DDAgentWriter implements Writer {
public static final String LANG_INTERPRETER_VENDOR_TAG = "lang_interpreter_vendor";
public static final String TRACER_VERSION_TAG = "tracer_version";
- private final String host;
- private final int port;
+ private final String hostInfo;
private final StatsDClient statsd;
// DQH - Made a conscious choice to not take a Config object here.
// Letting the creating of the Monitor take the Config,
// so it can decide which Monitor variant to create.
-
public StatsDMonitor(final String host, final int port) {
- this.host = host;
- this.port = port;
-
+ hostInfo = host + ":" + port;
statsd = new NonBlockingStatsDClient(PREFIX, host, port, getDefaultTags());
}
+ // Currently, intended for testing
+ private StatsDMonitor(final StatsDClient statsd) {
+ hostInfo = null;
+ this.statsd = statsd;
+ }
+
protected static final String[] getDefaultTags() {
return new String[] {
tag(LANG_TAG, "java"),
@@ -574,7 +576,11 @@ public class DDAgentWriter implements Writer {
}
public String toString() {
- return "StatsD { host=" + host + ":" + port + " }";
+ if (hostInfo == null) {
+ return "StatsD";
+ } else {
+ return "StatsD { host=" + hostInfo + " }";
+ }
}
}
}

MODIFY
dd-trace-ot/src/test/groovy/datadog/trace/api/writer/DDAgentWriterTest.groovy
dd-trace-ot/src/test/groovy/datadog/trace/api/writer/DDAgentWriterTest.groovy
package datadog.trace.api.writer
+import com.timgroup.statsd.StatsDClient
import datadog.opentracing.DDSpan
import datadog.opentracing.DDSpanContext
import datadog.opentracing.DDTracer
@@ -485,4 +486,99 @@ class DDAgentWriterTest extends DDSpecification {
writer.close()
agent.close()
}
+
+ def "statsd success"() {
+ def numTracesAccepted = 0
+ def numRequests = 0
+ def numResponses = 0
+
+ setup:
+ def minimalTrace = createMinimalTrace()
+
+ // Need to set-up a dummy agent for the final send callback to work
+ def agent = httpServer {
+ handlers {
+ put("v0.4/traces") {
+ response.status(200).send()
+ }
+ }
+ }
+ def api = new DDApi("localhost", agent.address.port, null)
+
+ def statsd = Stub(StatsDClient)
+ statsd.incrementCounter("queue.accepted") >> { stat ->
+ numTracesAccepted += 1
+ }
+ statsd.incrementCounter("api.requests") >> { stat ->
+ numRequests += 1
+ }
+ statsd.incrementCounter("api.responses", _) >> { stat, tags ->
+ numResponses += 1
+ }
+
+ def monitor = new DDAgentWriter.StatsDMonitor(statsd)
+ def writer = new DDAgentWriter(api, monitor)
+ writer.start()
+
+ when:
+ writer.write(minimalTrace)
+ writer.flush()
+
+ then:
+ numTracesAccepted == 1
+ numRequests == 1
+ numResponses == 1
+
+ cleanup:
+ agent.close()
+ writer.close()
+ }
+
+ def "statsd comm failure"() {
+ def numRequests = 0
+ def numResponses = 0
+ def numErrors = 0
+
+ setup:
+ def minimalTrace = createMinimalTrace()
+
+ // DQH -- need to set-up a dummy agent for the final send callback to work
+ def api = new DDApi("localhost", 8192, null) {
+ DDApi.Response sendSerializedTraces(
+ int representativeCount,
+ Integer sizeInBytes,
+ List<byte[]> traces)
+ {
+ // simulating a communication failure to a server
+ return DDApi.Response.failed(new IOException("comm error"))
+ }
+ }
+
+ def statsd = Stub(StatsDClient)
+ statsd.incrementCounter("api.requests") >> { stat ->
+ numRequests += 1
+ }
+ statsd.incrementCounter("api.responses", _) >> { stat, tags ->
+ numResponses += 1
+ }
+ statsd.incrementCounter("api.errors", _) >> { stat ->
+ numErrors += 1
+ }
+
+ def monitor = new DDAgentWriter.StatsDMonitor(statsd)
+ def writer = new DDAgentWriter(api, monitor)
+ writer.start()
+
+ when:
+ writer.write(minimalTrace)
+ writer.flush()
+
+ then:
+ numRequests == 1
+ numResponses == 0
+ numErrors == 1
+
+ cleanup:
+ writer.close()
+ }
}

Adding StatsD tests

Adding a happy & comm failure test case for StatsDMonitor.

This checks some but not all of the counters but also makes sure the exceptional path branches are exercised.

To make this test possible, I added an additional constructor to StatsDMonitor that takes a StatsDClient.