public async Task UdpUri_Counter_ReceivesValidStatsd(double value) { var port = (ushort)_rng.Next(1024, 65535); var handler = new StatsdMetricHandler("127.0.0.1", port); var utcNow = DateTime.UtcNow; var reading = new MetricReading("test.metrics", MetricType.Counter, value, new Dictionary <string, string> { ["host"] = "test!" }.ToImmutableDictionary(), utcNow); var listenerTask = ReceiveStatsdOverUdpAsync(port, _output); // expected format: {metric}:{value}|{unit}|#{tag},{tag} // so for this counter: "test.metrics:1|c|#host:test! handler.SerializeMetric(reading); await handler.FlushAsync( TimeSpan.Zero, 0, e => _output.WriteLine($"{e.BytesWritten} bytes written"), ex => _output.WriteLine(ex.ToString()) ); // make sure the data we received matches the statsd for the bytes we sent! var actualBytes = await listenerTask; var expectedBytes = ToStatsd(reading); _output.WriteLine($"Actual:\t\t{string.Join(" ", actualBytes.Select(x => $"{x:x2}"))}"); _output.WriteLine($"Expected:\t{string.Join(" ", expectedBytes.Select(x => $"{x:x2}"))}"); Assert.Equal(expectedBytes, actualBytes); }
public static IEnumerable <MetricReading> GenerateReadings(int count) { var utcNow = DateTime.UtcNow; var readings = new MetricReading[count]; for (var i = 0; i < readings.Length; i++) { readings[i] = new MetricReading("test.metric_" + i, MetricType.Counter, string.Empty, i, new Dictionary <string, string>(), utcNow.AddSeconds(i)); } return(readings); }