Skip to content

Commit

Permalink
core: Add Reset APIs to histogram and counters
Browse files Browse the repository at this point in the history
  • Loading branch information
hkadayam authored and gjasny committed Feb 9, 2023
1 parent bb21a73 commit 1833c18
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/include/prometheus/counter.h
Expand Up @@ -38,6 +38,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Counter {
/// The counter will not change if the given amount is negative.
void Increment(double);

/// \brief Reset the counter to 0
void Reset();

/// \brief Get the current value of the counter.
double Value() const;

Expand Down
6 changes: 6 additions & 0 deletions core/include/prometheus/histogram.h
Expand Up @@ -65,6 +65,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
void ObserveMultiple(const std::vector<double>& bucket_increments,
double sum_of_values);

/// \brief Reset all data points collected so far.
///
/// All buckets and sum are reset to its oringal value. This is especially
/// useful if histogram is tracked elsewhere but report in prometheus system.
void Reset();

/// \brief Get the current value of the histogram.
///
/// Collect is called by the Registry when collecting metrics.
Expand Down
2 changes: 2 additions & 0 deletions core/src/counter.cc
Expand Up @@ -13,6 +13,8 @@ void Counter::Increment(const double val) {

double Counter::Value() const { return gauge_.Value(); }

void Counter::Reset() { gauge_.Set(0); }

ClientMetric Counter::Collect() const {
ClientMetric metric;
metric.counter.value = Value();
Expand Down
8 changes: 8 additions & 0 deletions core/src/histogram.cc
Expand Up @@ -64,6 +64,14 @@ void Histogram::ObserveMultiple(const std::vector<double>& bucket_increments,
}
}

void Histogram::Reset() {
std::lock_guard<std::mutex> lock(mutex_);
for (std::size_t i = 0; i < bucket_counts_.size(); ++i) {
bucket_counts_[i].Reset();
}
sum_.Set(0);
}

ClientMetric Histogram::Collect() const {
std::lock_guard<std::mutex> lock(mutex_);

Expand Down
10 changes: 10 additions & 0 deletions core/tests/counter_test.cc
Expand Up @@ -37,5 +37,15 @@ TEST(CounterTest, inc_negative_value) {
EXPECT_EQ(counter.Value(), 5.0);
}

TEST(CounterTest, reset) {
Counter counter;
counter.Increment();
counter.Reset();
EXPECT_EQ(counter.Value(), 0.0);
counter.Increment(5);
counter.Increment();
EXPECT_EQ(counter.Value(), 6.0);
}

} // namespace
} // namespace prometheus
20 changes: 20 additions & 0 deletions core/tests/histogram_test.cc
Expand Up @@ -118,6 +118,26 @@ TEST(HistogramTest, observe_multiple_test_length_error) {
ASSERT_THROW(histogram.ObserveMultiple({5, 9}, 20), std::length_error);
}

TEST(HistogramTest, test_reset) {
Histogram histogram{{1, 2}};
histogram.ObserveMultiple({5, 9, 3}, 20);
histogram.ObserveMultiple({0, 20, 6}, 34);
histogram.Reset();
auto metric = histogram.Collect();
auto h = metric.histogram;
EXPECT_EQ(h.sample_count, 0U);
EXPECT_EQ(h.sample_sum, 0U);
EXPECT_EQ(h.bucket.at(0).cumulative_count, 0U);
EXPECT_EQ(h.bucket.at(1).cumulative_count, 0U);
EXPECT_EQ(h.bucket.at(2).cumulative_count, 0U);
histogram.ObserveMultiple({5, 9, 3}, 20);
histogram.ObserveMultiple({0, 20, 6}, 34);
metric = histogram.Collect();
h = metric.histogram;
EXPECT_EQ(h.sample_count, 43U);
EXPECT_EQ(h.sample_sum, 54);
}

TEST(HistogramTest, sum_can_go_down) {
Histogram histogram{{1}};
auto metric1 = histogram.Collect();
Expand Down

0 comments on commit 1833c18

Please sign in to comment.