private static async Task AssertMetrics(StatsDConfiguration config, IStatsDPublisher publisher) { // Act - Create a counter publisher.Increment("apple"); // Act - Create and change a counter publisher.Increment("bear"); // 1 publisher.Increment(10, "bear"); // 11 publisher.Increment(10, 0, "bear"); // 11 publisher.Decrement("bear"); // 10 publisher.Decrement(5, "bear"); // 5 publisher.Decrement(5, 0, "bear"); // 5 // Act - Mark an event (which is a counter) publisher.Increment("fish"); // Act - Create a gauge publisher.Gauge(3.141, "circle"); // Act - Create and change a gauge publisher.Gauge(10, "dog"); publisher.Gauge(42, "dog"); // Act - Create a timer publisher.Timing(123, "elephant"); publisher.Timing(TimeSpan.FromSeconds(2), "fox"); publisher.Timing(456, 1, "goose"); publisher.Timing(TimeSpan.FromSeconds(3.5), 1, "hen"); // Act - Increment multiple counters publisher.Increment(7, 1, "green", "red"); // 7 publisher.Increment(2, 0, "green", "red"); // 7 publisher.Decrement(1, 0, "red", "green"); // 7 publisher.Decrement(4, 1, "red", "green"); // 3 // Allow enough time for metrics to be registered await Task.Delay(TimeSpan.FromSeconds(1.0)); // Assert var result = await SendCommandAsync("counters"); result.Value <int>(config.Prefix + ".apple").ShouldBe(1, result.ToString()); result.Value <int>(config.Prefix + ".bear").ShouldBe(5, result.ToString()); result.Value <int>(config.Prefix + ".fish").ShouldBe(1, result.ToString()); result.Value <int>(config.Prefix + ".green").ShouldBe(3, result.ToString()); result.Value <int>(config.Prefix + ".red").ShouldBe(3, result.ToString()); result = await SendCommandAsync("gauges"); result.Value <double>(config.Prefix + ".circle").ShouldBe(3.141, result.ToString()); result.Value <int>(config.Prefix + ".dog").ShouldBe(42, result.ToString()); result = await SendCommandAsync("timers"); result[config.Prefix + ".elephant"].Values <int>().ShouldBe(new[] { 123 }, result.ToString()); result[config.Prefix + ".fox"].Values <int>().ShouldBe(new[] { 2000 }, result.ToString()); result[config.Prefix + ".goose"].Values <int>().ShouldBe(new[] { 456 }, result.ToString()); result[config.Prefix + ".hen"].Values <int>().ShouldBe(new[] { 3500 }, result.ToString()); }
public override async Task <FactorialReply> Factorial(FactorialRequest request, ServerCallContext context) { // Obtain the number of available threads in ThreadPool ThreadPool.GetAvailableThreads(out var availableThreads, out _); // The number of available threads is the example of Gauge metric // Send gauge metric to StatsD (using JustEat.StatsD nuget) _stats.Gauge(availableThreads, "GaugeAvailableThreads"); // Increment a counter metric for incoming requests _stats.Increment("CountRequests"); // The method _stats.Time() will calculate the time while the _semaphoreSlim.WaitAsync() were waiting // and send the metric to StatsD await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync()); try { // Again measure time length of calculation and send it to StatsD var result = await _stats.Time("TimeCalculation", async t => await CalculateFactorialAsync(request.Factor)); // Increment a counter of processed requests _stats.Increment("CountProcessed"); return(await Task.FromResult(new FactorialReply { Result = result })); } finally { _semaphoreSlim.Release(); } }
public ActionResult <IEnumerable <string> > Get() { string[] res = new string[] { "value1", "value2" }; _statsPublisher.Gauge(res.Length, "Values Count"); return(res); }
public Task ReportMetricAsync(string metricName, string metricDescription, double metricValue, Dictionary <string, string> labels) { Guard.NotNullOrEmpty(metricName, nameof(metricName)); _statsDPublisher.Gauge(metricValue, metricName); _logger.LogTrace("Metric {MetricName} with value {MetricValue} was written to StatsD server", metricName, metricValue); return(Task.CompletedTask); }
public Task ReportMetricAsync(string metricName, string metricDescription, MeasuredMetric measuredMetric) { Guard.NotNullOrEmpty(metricName, nameof(metricName)); Guard.NotNull(measuredMetric, nameof(measuredMetric)); var metricValue = measuredMetric.Value ?? 0; _statsDPublisher.Gauge(metricValue, metricName); _logger.LogTrace("Metric {MetricName} with value {MetricValue} was written to StatsD server", metricName, metricValue); return(Task.CompletedTask); }
public async Task Run() { await Task.Yield(); var sw = new Stopwatch(); sw.Start(); do { await Task.Delay(100); ThreadPool.QueueUserWorkItem(async _ => { ThreadPool.GetAvailableThreads(out var availableThreads, out var _); _stats.Gauge(availableThreads, "GaugeAvailableThreads"); _stats.Increment("CountRequests"); await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync()); try { var parameter = new Random(DateTime.Now.Millisecond).Next(20); using var channel = GrpcChannel.ForAddress(_settings.WorkerServiceEndpoint); var client = new Worker.WorkerClient(channel); var reply = await client.FactorialAsync(new FactorialRequest { Factor = parameter }); _stats.Increment("CountProcessed"); } finally { _semaphoreSlim.Release(); } }); } while (_settings.LoadSecondsInterval == 0 || (sw.ElapsedMilliseconds <= _settings.LoadSecondsInterval * 1000)); sw.Stop(); }
public Task Gauge(double value, string bucket) { _stats.Gauge(value, bucket); return(Task.CompletedTask); }
/// <inheritdoc /> public void Gauge(double value, string bucket, Operation operation = Operation.Set, IDictionary <string, string>?tags = null) { _inner.Gauge(value, bucket, operation, tags); }
private static async Task AssertMetrics(StatsDConfiguration config, IStatsDPublisher publisher) { // Act - Create a counter publisher.Increment("apple"); // Act - Create and change a counter publisher.Increment("bear"); // 1 publisher.Increment(10, "bear"); // 11 publisher.Increment(10, 0, "bear", null); // 11 publisher.Decrement("bear"); // 10 publisher.Decrement(5, "bear"); // 5 publisher.Decrement(5, 0, "bear"); // 5 // Act - Create and change a counter with tags Dictionary<string, string> tags = new Dictionary<string, string>(); tags.Add("key", "value"); tags.Add("key2", "value2"); publisher.Increment("ant", tags); // 1 publisher.Increment(15, "ant", tags); // 16 publisher.Decrement(5,"ant", tags); // 11 // Act - Create multiple counters with tags and change them tags = new Dictionary<string, string>(); tags.Add("key", "value"); tags.Add("key2", "value2"); publisher.Increment(100, 1, tags, new string[] { "gmc", "ford" }); // 100 publisher.Decrement(5, 1, tags, new string[] { "gmc", "ford" }); // 95 // Act - Create multiple counters without tags and change them publisher.Increment(150, 1, null, new string[] { "jaguar", "kia" }); // 100 publisher.Decrement(20, 1, null, new string[] { "jaguar", "kia" }); // 130 // Act - Create a counter with tags tags = new Dictionary<string, string>(); tags.Add("key", "value"); tags.Add("key2", "value2"); publisher.Increment("orange", tags); // 1 publisher.Increment(50, "orange", tags); // 51 // Act - Create multiple counters with tags tags = new Dictionary<string, string>(); tags.Add("type", "vehicle"); publisher.Increment(60, 1, tags, new string[] { "mazda", "fiat" }); // Act - Create multiple counters without tags publisher.Increment(25, 1, null, new string[] { "ferrari", "jeep" }); // Act - Mark an event (which is a counter) publisher.Increment("fish"); // Act - Create a gauge publisher.Gauge(3.141, "circle", Operation.Set, null); // Act - Create and change a gauge publisher.Gauge(10, "dog", Operation.Set, null); publisher.Gauge(42, "dog", Operation.Set, null); // Act - Create a gauge with tags tags = new Dictionary<string, string>(); tags.Add("foo", "bar"); tags.Add("lorem", "ipsum"); publisher.Gauge(5.5, "square", Operation.Set, tags); // Act - Create a gauge and decrement it publisher.Gauge(2020, "year", Operation.Set, null); publisher.Gauge(10, "year", Operation.Decrement, null); // Act - Create a gauge and increment it publisher.Gauge(15, "score", Operation.Set, null); publisher.Gauge(2, "score", Operation.Increment, null); // Act - Create a timer publisher.Timing(123, "elephant"); publisher.Timing(TimeSpan.FromSeconds(2), "fox"); publisher.Timing(456, 1, "goose", null); publisher.Timing(TimeSpan.FromSeconds(3.5), 1, "hen"); // Act - Create a timer with tags tags = new Dictionary<string, string>(); tags.Add("class", "mammal"); tags.Add("genus", "panthera"); publisher.Timing(128, "leopard", tags); // Act - Increment multiple counters publisher.Increment(7, 1, new string[] { "green", "red" }); // 7 publisher.Increment(2, 0, new List<string>() { "green", "red" }); // 7 publisher.Decrement(1, 0, new string[] { "red", "green" }); // 7 publisher.Decrement(4, 1, new List<string>() { "red", "green" }); // 3 // Allow enough time for metrics to be registered await Task.Delay(TimeSpan.FromSeconds(1.0)); // Assert var result = await SendCommandAsync("counters"); result.Value<int>(config.Prefix + ".apple").ShouldBe(1, result.ToString()); result.Value<int>(config.Prefix + ".bear").ShouldBe(5, result.ToString()); result.Value<int>(config.Prefix + ".ant;key=value;key2=value2").ShouldBe(11, result.ToString()); result.Value<int>(config.Prefix + ".gmc;key=value;key2=value2").ShouldBe(95, result.ToString()); result.Value<int>(config.Prefix + ".ford;key=value;key2=value2").ShouldBe(95, result.ToString()); result.Value<int>(config.Prefix + ".jaguar").ShouldBe(130, result.ToString()); result.Value<int>(config.Prefix + ".kia").ShouldBe(130, result.ToString()); result.Value<int>(config.Prefix + ".orange;key=value;key2=value2").ShouldBe(51, result.ToString()); result.Value<int>(config.Prefix + ".mazda;type=vehicle").ShouldBe(60, result.ToString()); result.Value<int>(config.Prefix + ".fiat;type=vehicle").ShouldBe(60, result.ToString()); result.Value<int>(config.Prefix + ".ferrari").ShouldBe(25, result.ToString()); result.Value<int>(config.Prefix + ".jeep").ShouldBe(25, result.ToString()); result.Value<int>(config.Prefix + ".fish").ShouldBe(1, result.ToString()); result.Value<int>(config.Prefix + ".green").ShouldBe(3, result.ToString()); result.Value<int>(config.Prefix + ".red").ShouldBe(3, result.ToString()); result = await SendCommandAsync("gauges"); result.Value<double>(config.Prefix + ".circle").ShouldBe(3.141, result.ToString()); result.Value<int>(config.Prefix + ".dog").ShouldBe(42, result.ToString()); result.Value<double>(config.Prefix + ".square;foo=bar;lorem=ipsum").ShouldBe(5.5, result.ToString()); result.Value<int>(config.Prefix + ".year").ShouldBe(2010, result.ToString()); result.Value<int>(config.Prefix + ".score").ShouldBe(17, result.ToString()); result = await SendCommandAsync("timers"); result[config.Prefix + ".elephant"].Values<int>().ShouldBe(new[] { 123 }, result.ToString()); result[config.Prefix + ".leopard;class=mammal;genus=panthera"].Values<int>().ShouldBe(new[] { 128 }, result.ToString()); result[config.Prefix + ".fox"].Values<int>().ShouldBe(new[] { 2000 }, result.ToString()); result[config.Prefix + ".goose"].Values<int>().ShouldBe(new[] { 456 }, result.ToString()); result[config.Prefix + ".hen"].Values<int>().ShouldBe(new[] { 3500 }, result.ToString()); }
/// <inheritdoc /> public void Gauge(double value, string bucket) { _inner.Gauge(value, bucket); }