Apply `_sample_rate` metric to allow dd-agent to do proper scaling of metrics when traces are sampled.

This commit is contained in:
Tyler Benson 2020-01-13 15:44:51 -08:00
parent 5ff855737b
commit a4db31cf79
3 changed files with 15 additions and 3 deletions

View File

@ -113,7 +113,13 @@ public class DDAgentWriter implements Writer {
public void write(final List<DDSpan> trace) {
// We can't add events after shutdown otherwise it will never complete shutting down.
if (traceProcessingDisruptor.running) {
final int representativeCount = traceCount.getAndSet(0) + 1;
final int representativeCount;
if (trace.isEmpty() || !(trace.get(0).isRootSpan())) {
// We don't want to reset the count if we can't correctly report the value.
representativeCount = 1;
} else {
representativeCount = traceCount.getAndSet(0) + 1;
}
final boolean published = traceProcessingDisruptor.publish(trace, representativeCount);
if (published) {

View File

@ -63,6 +63,12 @@ public class TraceProcessingDisruptor extends AbstractDisruptor<List<DDSpan>> {
final DisruptorEvent<List<DDSpan>> event, final long sequence, final boolean endOfBatch) {
try {
if (event.data != null) {
if (1 < event.representativeCount && !event.data.isEmpty()) {
// attempt to have agent scale the metrics properly
((DDSpan) event.data.get(0).getLocalRootSpan())
.context()
.setMetric("_sample_rate", 1d / event.representativeCount);
}
try {
final byte[] serializedTrace = api.serializeTrace(event.data);
batchWritingDisruptor.publish(serializedTrace, event.representativeCount);

View File

@ -131,7 +131,7 @@ class DDAgentWriterTest extends DDSpecification {
writer.close()
where:
span = [newSpanOf(0, "fixed-thread-name")]
span = newSpanOf(0, "fixed-thread-name")
trace = (0..10000).collect { span }
}
@ -162,7 +162,7 @@ class DDAgentWriterTest extends DDSpecification {
writer.close()
where:
span = [newSpanOf(0, "fixed-thread-name")]
span = newSpanOf(0, "fixed-thread-name")
trace = (1..10).collect { span }
}