Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Make telemetry metrics not count in telemetry (fix #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
verdie-g committed Jul 24, 2020
1 parent 9ba0bdc commit ded74a3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
56 changes: 56 additions & 0 deletions DatadogStatsD.Test/TelemetryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DatadogStatsD.Telemetering;
using DatadogStatsD.Transport;
using Moq;
using NUnit.Framework;

namespace DatadogStatsD.Test
{
public class TelemetryTest
{
private static readonly string Version = FileVersionInfo.GetVersionInfo(typeof(Telemetry).Assembly.Location).ProductVersion;

[Test]
public void Test()
{
var transport = new Mock<ITransport>();
var timer = new ManualTimer();
var telemetry = new Telemetry("udp", transport.Object, timer, Array.Empty<string>());

telemetry.MetricSent();
telemetry.EventSent();
telemetry.ServiceCheckSent();
telemetry.PacketSent(11);
telemetry.PacketDropped(3, true);
telemetry.PacketDropped(7, false);

timer.TriggerElapsed();

VerifySent(transport, "metrics", 1);
VerifySent(transport, "events", 1);
VerifySent(transport, "service_checks", 1);
VerifySent(transport, "bytes_sent", 11);
VerifySent(transport, "bytes_dropped", 10);
VerifySent(transport, "bytes_dropped_queue", 3);
VerifySent(transport, "bytes_dropped_writer", 7);
VerifySent(transport, "packets_sent", 1);
VerifySent(transport, "packets_dropped", 2);
VerifySent(transport, "packets_dropped_queue", 1);
VerifySent(transport, "packets_dropped_writer", 1);

timer.TriggerElapsed();

transport.VerifyNoOtherCalls();
}

private void VerifySent(Mock<ITransport> transport, string metricName, int val)
{
string expectedMetric = $"datadog.dogstatsd.client.{metricName}:{val}|c|#client:cs,client_version:{Version},client_transport:udp";
byte[] expectedMetricBytes = Encoding.UTF8.GetBytes(expectedMetric);
transport.Verify(t => t.Send(It.Is<ArraySegment<byte>>(a => a.ToArray().SequenceEqual(expectedMetricBytes))));
}
}
}
26 changes: 14 additions & 12 deletions DatadogStatsD/Telemetering/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ public Telemetry(string transportName, ITransport transport, ITimer tickTimer, I
ClientTransportKey + ":" + transportName,
}).ToArray();

_metricsCount = new Count(transport, this, tickTimer, MetricPrefix + "metrics", tags);
_eventsCount = new Count(transport, this, tickTimer, MetricPrefix + "events", tags);
_serviceChecksCount = new Count(transport, this, tickTimer, MetricPrefix + "service_checks", tags);
_bytesSentCount = new Count(transport, this, tickTimer, MetricPrefix + "bytes_sent", tags);
_bytesDroppedCount = new Count(transport, this, tickTimer, MetricPrefix + "bytes_dropped", tags);
_bytesDroppedQueueCount = new Count(transport, this, tickTimer, MetricPrefix + "bytes_dropped_queue", tags);
_bytesDroppedWriterCount = new Count(transport, this, tickTimer, MetricPrefix + "bytes_dropped_writer", tags);
_packetsSentCount = new Count(transport, this, tickTimer, MetricPrefix + "packets_sent", tags);
_packetsDroppedCount = new Count(transport, this, tickTimer, MetricPrefix + "packets_dropped", tags);
_packetsDroppedQueueCount = new Count(transport, this, tickTimer, MetricPrefix + "packets_dropped_queue", tags);
_packetsDroppedWriterCount = new Count(transport, this, tickTimer, MetricPrefix + "packets_dropped_writer", tags);
var noopTelemetry = new NoopTelemetry(); // so that telemetry metrics aren't counted in telemetry

_metricsCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "metrics", tags);
_eventsCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "events", tags);
_serviceChecksCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "service_checks", tags);
_bytesSentCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "bytes_sent", tags);
_bytesDroppedCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "bytes_dropped", tags);
_bytesDroppedQueueCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "bytes_dropped_queue", tags);
_bytesDroppedWriterCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "bytes_dropped_writer", tags);
_packetsSentCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "packets_sent", tags);
_packetsDroppedCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "packets_dropped", tags);
_packetsDroppedQueueCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "packets_dropped_queue", tags);
_packetsDroppedWriterCount = new Count(transport, noopTelemetry, tickTimer, MetricPrefix + "packets_dropped_writer", tags);
}

public void MetricSent() => _metricsCount.Increment();
Expand Down Expand Up @@ -136,4 +138,4 @@ public void Dispose()
_packetsDroppedWriterCount.Dispose();
}
}
}
}

0 comments on commit ded74a3

Please sign in to comment.