Class Collector
This class provides methods to collect metrics with no label, one label, or
multiple labels, using overloaded definitions of the collect
method.
class Collector
;
Once the desired stats have been collected, the accumulated response
string can be collected using the getCollection
method.
Additionally, the reset
method can be used to reset stat
collection, when the desired stats have been collected from a Collector
instance.
Methods
Name | Description |
---|---|
collect
(values)
|
Collect stats from the data members of a struct or a class and prepare
them to be fetched upon the next call to getCollection . The
specifications of the format of the collected stats can be found at
https://prometheus.io/docs/instrumenting/exposition_formats/ .
|
collect
(values, label_val)
|
Collect stats from the data members of a struct or a class, annotate
them with a given label name and value, and prepare them to be fetched
upon the next call to getCollection .
The specifications of the format of the collected stats can be found
at https://prometheus.io/docs/instrumenting/exposition_formats/ .
|
collect
(values, labels)
|
Collect stats from the data members of a struct or a class, annotate
them with labels from the data members of another struct or class, and
prepare them to be fetched upon the next call to getCollection .
The specifications of the format of the collected stats can be found
at https://prometheus.io/docs/instrumenting/exposition_formats/ .
|
getCollection
()
|
|
reset
()
|
Reset the length of the stat collection buffer to 0. |
Example
Test collecting populated stats, but without any label.
auto test_collector = new Collector();
test_collector .collect(Statistics(3600, 347, 3.14, 6.023, 0.43));
assert(test_collector .getCollection() ==
"up_time_s 3600\ncount 347\nratio 3.14\nfraction 6.023\n" ~
"very_real 0.43\n");
Example
Test collecting populated stats with one label
auto test_collector = new Collector();
test_collector .collect!("id")(
Statistics(3600, 347, 3.14, 6.023, 0.43), 123.034);
assert(test_collector .getCollection() ==
"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");
Example
Test collecting stats having initial values with multiple labels
auto test_collector = new Collector();
test_collector .collect(Statistics(3600, 347, 3.14, 6.023, 0.43),
Labels(1_235_813, "ocean", 3.14159));
assert(test_collector .getCollection() ==
"up_time_s {id=\"1235813\",job=\"ocean\",perf=\"3.14159\"} 3600\n" ~
"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");
Example
Test resetting collected stats
auto test_collector = new Collector();
test_collector .collect!("id")(Statistics .init, 123);
assert(test_collector .getCollection() ==
"up_time_s {id=\"123\"} 0\ncount {id=\"123\"} 0\n" ~
"ratio {id=\"123\"} 0\nfraction {id=\"123\"} 0\n" ~
"very_real {id=\"123\"} 0\n");
test_collector .reset();
assert(test_collector .getCollection() == "");