Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe multiple from generic input iterators #537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/include/prometheus/histogram.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <cstddef>
#include <mutex>
#include <stdexcept>
#include <vector>

#include "prometheus/client_metric.h"
Expand Down Expand Up @@ -64,6 +66,27 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
void ObserveMultiple(const std::vector<double>& bucket_increments,
const double sum_of_values);

/// Same as above with custom iterator type.
template <class InputIt>
void ObserveMultiple(InputIt from, InputIt end, const double sum_of_values) {
std::lock_guard<std::mutex> lock(mutex_);
sum_.Increment(sum_of_values);

for (std::size_t i{0}; i < bucket_counts_.size(); ++i, ++from) {
if (from == end) {
throw std::length_error(
"The size of bucket_increments should be equal to "
"the number of buckets in the histogram.");
}
bucket_counts_[i].Increment(*from);
}
if (from != end) {
throw std::length_error(
"The size of bucket_increments should be equal to "
"the number of buckets in the histogram.");
}
}

/// \brief Get the current value of the histogram.
///
/// Collect is called by the Registry when collecting metrics.
Expand Down
29 changes: 27 additions & 2 deletions core/tests/histogram_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <gtest/gtest.h>

#include <forward_list>
#include <limits>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -89,7 +90,7 @@ TEST(HistogramTest, cumulative_bucket_count) {
EXPECT_EQ(h.bucket.at(2).cumulative_count, 7U);
}

TEST(HistogramTest, observe_multiple_test_bucket_counts) {
TEST(HistogramTest, observe_multiple_test_bucket_counts_1) {
Histogram histogram{{1, 2}};
histogram.ObserveMultiple({5, 9, 3}, 20);
histogram.ObserveMultiple({0, 20, 6}, 34);
Expand All @@ -101,6 +102,20 @@ TEST(HistogramTest, observe_multiple_test_bucket_counts) {
EXPECT_EQ(h.bucket.at(2).cumulative_count, 43U);
}

TEST(HistogramTest, observe_multiple_test_bucket_counts_2) {
Histogram histogram{{1, 2}};
const std::forward_list<double> values1 = {5, 9, 3};
const std::forward_list<double> values2 = {0, 20, 6};
histogram.ObserveMultiple(values1.begin(), values1.end(), 20);
histogram.ObserveMultiple(values2.begin(), values2.end(), 34);
auto metric = histogram.Collect();
auto h = metric.histogram;
ASSERT_EQ(h.bucket.size(), 3U);
EXPECT_EQ(h.bucket.at(0).cumulative_count, 5U);
EXPECT_EQ(h.bucket.at(1).cumulative_count, 34U);
EXPECT_EQ(h.bucket.at(2).cumulative_count, 43U);
}

TEST(HistogramTest, observe_multiple_test_total_sum) {
Histogram histogram{{1, 2}};
histogram.ObserveMultiple({5, 9, 3}, 20);
Expand All @@ -111,13 +126,23 @@ TEST(HistogramTest, observe_multiple_test_total_sum) {
EXPECT_EQ(h.sample_sum, 54);
}

TEST(HistogramTest, observe_multiple_test_length_error) {
TEST(HistogramTest, observe_multiple_test_length_error1) {
Histogram histogram{{1, 2}};
// 2 bucket boundaries means there are 3 buckets, so giving just 2 bucket
// increments should result in a length_error.
ASSERT_THROW(histogram.ObserveMultiple({5, 9}, 20), std::length_error);
}

TEST(HistogramTest, observe_multiple_test_length_error2) {
Histogram histogram{{1, 2}};
const std::forward_list<double> values1 = {5, 9};
ASSERT_THROW(histogram.ObserveMultiple(values1.begin(), values1.end(), 20),
std::length_error);
const std::forward_list<double> values2 = {5, 9, 5, 6};
ASSERT_THROW(histogram.ObserveMultiple(values2.begin(), values2.end(), 20),
std::length_error);
}

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