From 13417d618944a7342137e67fe4a61c4b4fc3a763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Irmak?= Date: Thu, 31 Mar 2022 12:31:12 +0300 Subject: [PATCH] Stats: Append scrape duration to the end of `/metrics` response --- source/agora/node/Runner.d | 2 +- source/agora/stats/CollectorRegistry.d | 16 +++++++++++----- source/agora/stats/Stats.d | 7 ++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/source/agora/node/Runner.d b/source/agora/node/Runner.d index 261d87bf1fc..5de6a514b57 100644 --- a/source/agora/node/Runner.d +++ b/source/agora/node/Runner.d @@ -106,7 +106,7 @@ public void runNode (Config config, ref Listeners result) scope HTTPServerRequest, scope HTTPServerResponse res) { res.writeBody(cast(const(ubyte[])) Utils.getCollectorRegistry().collect(), - "text/plain"); + "text/plain; charset=utf-8"); } if (hasStatsInterface) diff --git a/source/agora/stats/CollectorRegistry.d b/source/agora/stats/CollectorRegistry.d index a69162d8db5..f1fe9f76983 100644 --- a/source/agora/stats/CollectorRegistry.d +++ b/source/agora/stats/CollectorRegistry.d @@ -17,6 +17,8 @@ module agora.stats.CollectorRegistry; import agora.stats.Collector : Collector; +version (unittest) import std.algorithm; + /// ditto public class CollectorRegistry { @@ -76,6 +78,9 @@ public class CollectorRegistry public const(char)[] collect ( ) { + import core.time; + import std.format; + auto start = MonoTime.currTime; this.collector.reset(); try @@ -90,7 +95,8 @@ public class CollectorRegistry throw ex; } - return this.collector.getCollection(); + return this.collector.getCollection() ~ + format("scrape_duration \"%s\"", MonoTime.currTime - start); } } @@ -139,10 +145,10 @@ unittest stats.setTestStats(Statistics(3600, 347, 3.14, 6.023, 0.43)); - assert(registry.collect() == + assert(registry.collect().startsWith( "up_time_s {id=\"123.034\"} 3600\ncount {id=\"123.034\"} 347\n" ~ "ratio {id=\"123.034\"} 3.14\nfraction {id=\"123.034\"} 6.023\n" ~ - "very_real {id=\"123.034\"} 0.43\n"); + "very_real {id=\"123.034\"} 0.43\n")); } /// Test collection from more than one delegates @@ -155,7 +161,7 @@ unittest stats.setTestStats(Statistics(3600, 347, 3.14, 6.023, 0.43)); stats.setTestLabels(Labels(1_235_813, "ocean", 3.14159)); - assert(registry.collect() == + assert(registry.collect().startsWith( "up_time_s {id=\"123.034\"} 3600\ncount {id=\"123.034\"} 347\n" ~ "ratio {id=\"123.034\"} 3.14\nfraction {id=\"123.034\"} 6.023\n" ~ "very_real {id=\"123.034\"} 0.43\n" ~ @@ -164,7 +170,7 @@ unittest "count {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 347\n" ~ "ratio {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 3.14\n" ~ "fraction {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 6.023\n" ~ - "very_real {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 0.43\n"); + "very_real {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 0.43\n")); } /// Test that the collections are not accumulated upon more than one call to diff --git a/source/agora/stats/Stats.d b/source/agora/stats/Stats.d index ab47c736a11..9ccd5c9d114 100644 --- a/source/agora/stats/Stats.d +++ b/source/agora/stats/Stats.d @@ -170,6 +170,7 @@ version (unittest) unittest { import std.functional : toDelegate; + import std.algorithm; // stats with one label CollectorRegistry collector_registry_with_label = new CollectorRegistry([]); @@ -178,14 +179,14 @@ unittest test_stats_with_label.increaseMetricBy!"test_metric_value"(1, "interesting_label1"); test_stats_with_label.setMetricTo!"test_metric_value"(5, "interesting_label2"); test_stats_with_label.increaseMetricBy!"test_metric_value"(2, "interesting_label2"); - assert(collector_registry_with_label.collect() == + assert(collector_registry_with_label.collect().startsWith( "test_metric_value {test_metric_label=\"interesting_label1\"} 42\n" ~ - "test_metric_value {test_metric_label=\"interesting_label2\"} 7\n"); + "test_metric_value {test_metric_label=\"interesting_label2\"} 7\n")); // stats without label CollectorRegistry collector_registry_without_label = new CollectorRegistry([]); collector_registry_without_label.addCollector(toDelegate(&collectTestStatWithoutLabel)); test_stats_without_label.setMetricTo!"test_metric_value"(41); test_stats_without_label.increaseMetricBy!"test_metric_value"(1); - assert(collector_registry_without_label.collect() == "test_metric_value 42\n"); + assert(collector_registry_without_label.collect().startsWith("test_metric_value 42\n")); }