Skip to content

Commit 7e32408

Browse files
committed
Improve Summary and Histogram API. Add constructors from rvalue references, observe from templated input it
1 parent d81fd3b commit 7e32408

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

core/include/prometheus/histogram.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

3+
#include <cstddef>
34
#include <mutex>
5+
#include <stdexcept>
46
#include <vector>
57

68
#include "prometheus/client_metric.h"
@@ -62,7 +64,28 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
6264
/// this function must have already sorted the values into buckets).
6365
/// Also increments the total sum of all observations by the given value.
6466
void ObserveMultiple(const std::vector<double>& bucket_increments,
65-
const double sum_of_values);
67+
double sum_of_values);
68+
69+
/// Same as above with custom iterator type.
70+
template <class InputIt>
71+
void ObserveMultiple(InputIt from, InputIt end, double sum_of_values) {
72+
std::lock_guard<std::mutex> lock(mutex_);
73+
sum_.Increment(sum_of_values);
74+
75+
for (std::size_t i{0}; i < bucket_counts_.size(); ++i, ++from) {
76+
if (from == end) {
77+
throw std::length_error(
78+
"The size of bucket_increments should be equal to "
79+
"the number of buckets in the histogram.");
80+
}
81+
bucket_counts_[i].Increment(*from);
82+
}
83+
if (from != end) {
84+
throw std::length_error(
85+
"The size of bucket_increments should be equal to "
86+
"the number of buckets in the histogram.");
87+
}
88+
}
6689

6790
/// \brief Get the current value of the histogram.
6891
///

core/tests/histogram_test.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <gtest/gtest.h>
44

5+
#include <forward_list>
56
#include <limits>
67
#include <memory>
78
#include <stdexcept>
@@ -89,7 +90,7 @@ TEST(HistogramTest, cumulative_bucket_count) {
8990
EXPECT_EQ(h.bucket.at(2).cumulative_count, 7U);
9091
}
9192

92-
TEST(HistogramTest, observe_multiple_test_bucket_counts) {
93+
TEST(HistogramTest, observe_multiple_test_bucket_counts_1) {
9394
Histogram histogram{{1, 2}};
9495
histogram.ObserveMultiple({5, 9, 3}, 20);
9596
histogram.ObserveMultiple({0, 20, 6}, 34);
@@ -101,6 +102,20 @@ TEST(HistogramTest, observe_multiple_test_bucket_counts) {
101102
EXPECT_EQ(h.bucket.at(2).cumulative_count, 43U);
102103
}
103104

105+
TEST(HistogramTest, observe_multiple_test_bucket_counts_2) {
106+
Histogram histogram{{1, 2}};
107+
const std::forward_list<double> values1 = {5, 9, 3};
108+
const std::forward_list<double> values2 = {0, 20, 6};
109+
histogram.ObserveMultiple(values1.begin(), values1.end(), 20);
110+
histogram.ObserveMultiple(values2.begin(), values2.end(), 34);
111+
auto metric = histogram.Collect();
112+
auto h = metric.histogram;
113+
ASSERT_EQ(h.bucket.size(), 3U);
114+
EXPECT_EQ(h.bucket.at(0).cumulative_count, 5U);
115+
EXPECT_EQ(h.bucket.at(1).cumulative_count, 34U);
116+
EXPECT_EQ(h.bucket.at(2).cumulative_count, 43U);
117+
}
118+
104119
TEST(HistogramTest, observe_multiple_test_total_sum) {
105120
Histogram histogram{{1, 2}};
106121
histogram.ObserveMultiple({5, 9, 3}, 20);
@@ -111,13 +126,23 @@ TEST(HistogramTest, observe_multiple_test_total_sum) {
111126
EXPECT_EQ(h.sample_sum, 54);
112127
}
113128

114-
TEST(HistogramTest, observe_multiple_test_length_error) {
129+
TEST(HistogramTest, observe_multiple_test_length_error1) {
115130
Histogram histogram{{1, 2}};
116131
// 2 bucket boundaries means there are 3 buckets, so giving just 2 bucket
117132
// increments should result in a length_error.
118133
ASSERT_THROW(histogram.ObserveMultiple({5, 9}, 20), std::length_error);
119134
}
120135

136+
TEST(HistogramTest, observe_multiple_test_length_error2) {
137+
Histogram histogram{{1, 2}};
138+
const std::forward_list<double> values1 = {5, 9};
139+
ASSERT_THROW(histogram.ObserveMultiple(values1.begin(), values1.end(), 20),
140+
std::length_error);
141+
const std::forward_list<double> values2 = {5, 9, 5, 6};
142+
ASSERT_THROW(histogram.ObserveMultiple(values2.begin(), values2.end(), 20),
143+
std::length_error);
144+
}
145+
121146
TEST(HistogramTest, sum_can_go_down) {
122147
Histogram histogram{{1}};
123148
auto metric1 = histogram.Collect();

0 commit comments

Comments
 (0)