From 21100ccc77f731dc7cba5a496001027e8a73a089 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 12 Jun 2017 21:52:09 +0200 Subject: [PATCH] Expose the start time point instead of the uptime, as per Prometheus recommendations. --- metrics.cpp | 7 +++++++ metrics.h | 6 ++++++ mixer.cpp | 5 +++-- mixer.h | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/metrics.cpp b/metrics.cpp index 1f3f8a1..fd5e54c 100644 --- a/metrics.cpp +++ b/metrics.cpp @@ -4,13 +4,20 @@ #include #include +#include #include #include using namespace std; +using namespace std::chrono; Metrics global_metrics; +double get_timestamp_for_metrics() +{ + return duration(system_clock::now().time_since_epoch()).count(); +} + namespace { string serialize_name(const string &name, const vector> &labels) diff --git a/metrics.h b/metrics.h index a815658..bd506a9 100644 --- a/metrics.h +++ b/metrics.h @@ -15,6 +15,12 @@ class Histogram; +// Prometheus recommends the use of timestamps instead of “time since event”, +// so you can use this to get the number of seconds since the epoch. +// Note that this will be wrong if your clock changes, so for non-metric use, +// you should use std::chrono::steady_clock instead. +double get_timestamp_for_metrics(); + class Metrics { public: enum Type { diff --git a/mixer.cpp b/mixer.cpp index 24f2a80..400fdd1 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -402,9 +402,11 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) set_output_card_internal(global_flags.output_card); } + metric_start_time_seconds = get_timestamp_for_metrics(); + global_metrics.add("frames_output_total", &metric_frames_output_total); global_metrics.add("frames_output_dropped", &metric_frames_output_dropped); - global_metrics.add("uptime_seconds", &metric_uptime_seconds); + global_metrics.add("start_time_seconds", &metric_start_time_seconds); global_metrics.add("memory_used_bytes", &metrics_memory_used_bytes); global_metrics.add("metrics_memory_locked_limit_bytes", &metrics_memory_locked_limit_bytes); } @@ -937,7 +939,6 @@ void Mixer::thread_func() metric_frames_output_total = frame_num; metric_frames_output_dropped = stats_dropped_frames; - metric_uptime_seconds = elapsed; if (frame_num % 100 == 0) { printf("%d frames (%d dropped) in %.3f seconds = %.1f fps (%.1f ms/frame)", diff --git a/mixer.h b/mixer.h index e2490ed..8504d5e 100644 --- a/mixer.h +++ b/mixer.h @@ -543,7 +543,7 @@ private: // Metrics. std::atomic metric_frames_output_total{0}; std::atomic metric_frames_output_dropped{0}; - std::atomic metric_uptime_seconds{0.0}; + std::atomic metric_start_time_seconds{0.0 / 0.0}; std::atomic metrics_memory_used_bytes{0}; std::atomic metrics_memory_locked_limit_bytes{0.0 / 0.0}; }; -- 2.39.2