Commit fc66c8ac authored by Dominik Charousset's avatar Dominik Charousset

Fix resolution of Prometheus timestamps

parent 8c82c734
...@@ -34,6 +34,20 @@ namespace caf::telemetry::collector { ...@@ -34,6 +34,20 @@ namespace caf::telemetry::collector {
namespace { namespace {
/// Milliseconds since epoch.
struct ms_timestamp {
int64_t value;
/// Converts seconds-since-epoch to milliseconds-since-epoch
explicit ms_timestamp(time_t from) : value(from * int64_t{1000}) {
// nop
}
ms_timestamp(const ms_timestamp&) = default;
ms_timestamp& operator=(const ms_timestamp&) = default;
};
void append(prometheus::char_buffer&) { void append(prometheus::char_buffer&) {
// End of recursion. // End of recursion.
} }
...@@ -57,6 +71,9 @@ void append(prometheus::char_buffer&, const metric_family*, Ts&&...); ...@@ -57,6 +71,9 @@ void append(prometheus::char_buffer&, const metric_family*, Ts&&...);
template <class... Ts> template <class... Ts>
void append(prometheus::char_buffer&, const metric*, Ts&&...); void append(prometheus::char_buffer&, const metric*, Ts&&...);
template <class... Ts>
void append(prometheus::char_buffer&, ms_timestamp, Ts&&...);
template <class... Ts> template <class... Ts>
void append(prometheus::char_buffer& buf, string_view str, Ts&&... xs) { void append(prometheus::char_buffer& buf, string_view str, Ts&&... xs) {
buf.insert(buf.end(), str.begin(), str.end()); buf.insert(buf.end(), str.begin(), str.end());
...@@ -116,6 +133,12 @@ void append(prometheus::char_buffer& buf, const metric* instance, Ts&&... xs) { ...@@ -116,6 +133,12 @@ void append(prometheus::char_buffer& buf, const metric* instance, Ts&&... xs) {
append(buf, std::forward<Ts>(xs)...); append(buf, std::forward<Ts>(xs)...);
} }
template <class... Ts>
void append(prometheus::char_buffer& buf, ms_timestamp ts, Ts&&... xs) {
append(buf, ts.value);
append(buf, std::forward<Ts>(xs)...);
}
} // namespace } // namespace
string_view prometheus::collect_from(const metric_registry& registry, string_view prometheus::collect_from(const metric_registry& registry,
...@@ -136,13 +159,15 @@ string_view prometheus::collect_from(const metric_registry& registry) { ...@@ -136,13 +159,15 @@ string_view prometheus::collect_from(const metric_registry& registry) {
void prometheus::operator()(const metric_family* family, const metric* instance, void prometheus::operator()(const metric_family* family, const metric* instance,
const dbl_gauge* gauge) { const dbl_gauge* gauge) {
set_current_family(family, "gauge"); set_current_family(family, "gauge");
append(buf_, family, instance, ' ', gauge->value(), ' ', now_, '\n'); append(buf_, family, instance, ' ', gauge->value(), ' ', ms_timestamp{now_},
'\n');
} }
void prometheus::operator()(const metric_family* family, const metric* instance, void prometheus::operator()(const metric_family* family, const metric* instance,
const int_gauge* gauge) { const int_gauge* gauge) {
set_current_family(family, "gauge"); set_current_family(family, "gauge");
append(buf_, family, instance, ' ', gauge->value(), ' ', now_, '\n'); append(buf_, family, instance, ' ', gauge->value(), ' ', ms_timestamp{now_},
'\n');
} }
void prometheus::set_current_family(const metric_family* family, void prometheus::set_current_family(const metric_family* family,
......
...@@ -55,13 +55,13 @@ CAF_TEST(the Prometheus collector generates text output) { ...@@ -55,13 +55,13 @@ CAF_TEST(the Prometheus collector generates text output) {
CAF_CHECK_EQUAL(exporter.collect_from(registry, 42), CAF_CHECK_EQUAL(exporter.collect_from(registry, 42),
R"(# HELP foo_bar_seconds Some value without labels. R"(# HELP foo_bar_seconds Some value without labels.
# TYPE foo_bar_seconds gauge # TYPE foo_bar_seconds gauge
foo_bar_seconds 123 42 foo_bar_seconds 123 42000
# HELP some_value_total Some (total) value with two labels. # HELP some_value_total Some (total) value with two labels.
# TYPE some_value_total gauge # TYPE some_value_total gauge
some_value_total{a="1",b="2"} 12 42 some_value_total{a="1",b="2"} 12 42000
some_value_total{a="2",b="1"} 21 42 some_value_total{a="2",b="1"} 21 42000
# TYPE other_value_seconds_total gauge # TYPE other_value_seconds_total gauge
other_value_seconds_total{x="true"} 31337 42 other_value_seconds_total{x="true"} 31337 42000
)"_sv); )"_sv);
CAF_MESSAGE("multiple runs generate the same output"); CAF_MESSAGE("multiple runs generate the same output");
std::string res1; std::string res1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment