Skip to content

Commit

Permalink
Stats: Append scrape duration to the end of /metrics response
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Mar 31, 2022
1 parent 1730eac commit 13417d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/agora/node/Runner.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions source/agora/stats/CollectorRegistry.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module agora.stats.CollectorRegistry;

import agora.stats.Collector : Collector;

version (unittest) import std.algorithm;

/// ditto
public class CollectorRegistry
{
Expand Down Expand Up @@ -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
Expand All @@ -90,7 +95,8 @@ public class CollectorRegistry
throw ex;
}

return this.collector.getCollection();
return this.collector.getCollection() ~
format("scrape_duration \"%s\"", MonoTime.currTime - start);
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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" ~
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions source/agora/stats/Stats.d
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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"));
}

0 comments on commit 13417d6

Please sign in to comment.