From 3936b60eefe64b6664732fc7c1686bb0612c9edd Mon Sep 17 00:00:00 2001 From: Frank Imeson Date: Tue, 29 Sep 2020 10:20:54 -0400 Subject: [PATCH] CP-1935: Added some reset hooks for each metric type --- core/include/prometheus/counter.h | 3 +++ core/include/prometheus/gauge.h | 3 +++ core/include/prometheus/histogram.h | 3 +++ core/include/prometheus/metric_base.h | 2 +- core/src/counter.cc | 5 +++++ core/src/gauge.cc | 6 ++++++ core/src/histogram.cc | 9 +++++++++ 7 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/include/prometheus/counter.h b/core/include/prometheus/counter.h index 84ee8a4e..2e339658 100644 --- a/core/include/prometheus/counter.h +++ b/core/include/prometheus/counter.h @@ -34,6 +34,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Counter: public MetricBase { /// \brief Create a counter that starts at 0. Counter(const bool alert_if_no_family = true); + /// \brief Resets the counter. + void Reset(); + /// \brief Increment the counter by a given amount. /// /// The counter will not change if the given amount is negative. diff --git a/core/include/prometheus/gauge.h b/core/include/prometheus/gauge.h index 0086f57a..f51c7af9 100644 --- a/core/include/prometheus/gauge.h +++ b/core/include/prometheus/gauge.h @@ -30,6 +30,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Gauge: public MetricBase { /// \brief Create a gauge that starts at the given amount. Gauge(const double value = 0, const bool alert_if_no_family = true); + /// \brief Reset the gauge. + void Reset(const double value = 0); + /// \brief Increment the gauge by the given amount. void Increment(const double value = 1); diff --git a/core/include/prometheus/histogram.h b/core/include/prometheus/histogram.h index 5fe1b68b..25c482bd 100644 --- a/core/include/prometheus/histogram.h +++ b/core/include/prometheus/histogram.h @@ -47,6 +47,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram: public MetricBase { /// The bucket boundaries cannot be changed once the histogram is created. Histogram(const BucketBoundaries& buckets, const bool alert_if_no_family = true); + /// \brief Reset the histogram. + void Reset(); + /// \brief Observe the given amount. /// /// The given amount selects the 'observed' bucket. The observed bucket is diff --git a/core/include/prometheus/metric_base.h b/core/include/prometheus/metric_base.h index 1846a741..09e58031 100644 --- a/core/include/prometheus/metric_base.h +++ b/core/include/prometheus/metric_base.h @@ -14,7 +14,7 @@ namespace prometheus { class PROMETHEUS_CPP_CORE_EXPORT MetricBase { public: MetricBase(const bool alert_if_no_family = true); - + /// \brief Prints a message to stderr if the metric doesn't have a family bool HasFamily(); diff --git a/core/src/counter.cc b/core/src/counter.cc index 2b6397c2..7050f2e5 100644 --- a/core/src/counter.cc +++ b/core/src/counter.cc @@ -5,6 +5,11 @@ namespace prometheus { Counter::Counter(const bool alert_if_no_family) : MetricBase(alert_if_no_family) {}; +void Counter::Reset() { + value_ = 0; + last_update_ = std::time(nullptr); + AlertIfNoFamily(); +} void Counter::Increment(const double value) { if (value < 0.0) return; diff --git a/core/src/gauge.cc b/core/src/gauge.cc index e0d9b2c5..c4e612b2 100644 --- a/core/src/gauge.cc +++ b/core/src/gauge.cc @@ -7,6 +7,12 @@ namespace prometheus { Gauge::Gauge(const double value, const bool alert_if_no_family) : MetricBase(alert_if_no_family), value_(value) {} +void Gauge::Reset(const double value) { + value_ = value; + last_update_ = std::time(nullptr); + AlertIfNoFamily(); +} + void Gauge::Increment(const double value) { if (value < 0.0) return; value_ = value_ + value; diff --git a/core/src/histogram.cc b/core/src/histogram.cc index 7433a34f..22a5c213 100644 --- a/core/src/histogram.cc +++ b/core/src/histogram.cc @@ -13,6 +13,15 @@ Histogram::Histogram(const BucketBoundaries& buckets, const bool alert_if_no_fam std::end(bucket_boundaries_))); } +void Histogram::Reset() { + for (unsigned int bucket_index = 0; bucket_index < bucket_counts_.size(); ++bucket_index) { + bucket_counts_[bucket_index] = 0; + } + sum_ = 0; + last_update_ = std::time(nullptr); + AlertIfNoFamily(); +} + void Histogram::Observe(const double value) { // TODO: determine bucket list size at which binary search would be faster const auto bucket_index = static_cast(std::distance(