• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jupp0r / prometheus-cpp / 4192383612

pending completion
4192383612

Pull #634

github

GitHub
Merge 039bc354a into 1833c1848
Pull Request #634: Update deps

773 of 802 relevant lines covered (96.38%)

109256.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/core/src/histogram.cc
1
#include "prometheus/histogram.h"
2

3
#include <algorithm>
4
#include <cstddef>
5
#include <functional>
6
#include <iterator>
7
#include <limits>
8
#include <memory>
9
#include <stdexcept>
10
#include <utility>
11

12
namespace prometheus {
13

14
namespace {
15

16
template <class ForwardIterator>
17
bool is_strict_sorted(ForwardIterator first, ForwardIterator last) {
38✔
18
  return std::adjacent_find(first, last,
76✔
19
                            std::greater_equal<typename std::iterator_traits<
20
                                ForwardIterator>::value_type>()) == last;
76✔
21
}
22

23
}  // namespace
24

25
Histogram::Histogram(const BucketBoundaries& buckets)
8✔
26
    : bucket_boundaries_{buckets}, bucket_counts_{buckets.size() + 1} {
8✔
27
  if (!is_strict_sorted(begin(bucket_boundaries_), end(bucket_boundaries_))) {
8✔
28
    throw std::invalid_argument("Bucket Boundaries must be strictly sorted");
×
29
  }
30
}
8✔
31

32
Histogram::Histogram(BucketBoundaries&& buckets)
30✔
33
    : bucket_boundaries_{std::move(buckets)},
30✔
34
      bucket_counts_{bucket_boundaries_.size() + 1} {
38✔
35
  if (!is_strict_sorted(begin(bucket_boundaries_), end(bucket_boundaries_))) {
30✔
36
    throw std::invalid_argument("Bucket Boundaries must be strictly sorted");
4✔
37
  }
38
}
26✔
39

40
void Histogram::Observe(const double value) {
38✔
41
  const auto bucket_index = static_cast<std::size_t>(
42
      std::distance(bucket_boundaries_.begin(),
38✔
43
                    std::lower_bound(bucket_boundaries_.begin(),
44
                                     bucket_boundaries_.end(), value)));
38✔
45

46
  std::lock_guard<std::mutex> lock(mutex_);
76✔
47
  sum_.Increment(value);
38✔
48
  bucket_counts_[bucket_index].Increment();
38✔
49
}
38✔
50

51
void Histogram::ObserveMultiple(const std::vector<double>& bucket_increments,
18✔
52
                                const double sum_of_values) {
53
  if (bucket_increments.size() != bucket_counts_.size()) {
18✔
54
    throw std::length_error(
55
        "The size of bucket_increments was not equal to"
56
        "the number of buckets in the histogram.");
2✔
57
  }
58

59
  std::lock_guard<std::mutex> lock(mutex_);
32✔
60
  sum_.Increment(sum_of_values);
16✔
61

62
  for (std::size_t i{0}; i < bucket_counts_.size(); ++i) {
64✔
63
    bucket_counts_[i].Increment(bucket_increments[i]);
48✔
64
  }
65
}
16✔
66

67
void Histogram::Reset() {
2✔
68
  std::lock_guard<std::mutex> lock(mutex_);
4✔
69
  for (std::size_t i = 0; i < bucket_counts_.size(); ++i) {
8✔
70
    bucket_counts_[i].Reset();
6✔
71
  }
72
  sum_.Set(0);
2✔
73
}
2✔
74

75
ClientMetric Histogram::Collect() const {
36✔
76
  std::lock_guard<std::mutex> lock(mutex_);
72✔
77

78
  auto metric = ClientMetric{};
36✔
79

80
  auto cumulative_count = 0ULL;
36✔
81
  metric.histogram.bucket.reserve(bucket_counts_.size());
36✔
82
  for (std::size_t i{0}; i < bucket_counts_.size(); ++i) {
134✔
83
    cumulative_count += bucket_counts_[i].Value();
98✔
84
    auto bucket = ClientMetric::Bucket{};
98✔
85
    bucket.cumulative_count = cumulative_count;
98✔
86
    bucket.upper_bound = (i == bucket_boundaries_.size()
98✔
87
                              ? std::numeric_limits<double>::infinity()
160✔
88
                              : bucket_boundaries_[i]);
62✔
89
    metric.histogram.bucket.push_back(std::move(bucket));
98✔
90
  }
91
  metric.histogram.sample_count = cumulative_count;
36✔
92
  metric.histogram.sample_sum = sum_.Value();
36✔
93

94
  return metric;
72✔
95
}
96

97
}  // namespace prometheus
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc