| 1234567891011121314151617181920212223242526272829303132333435363738394041 | #include <algorithm>#include <numeric>#include "histogram.h"namespace prometheus {Histogram::Histogram(const BucketBoundaries& buckets)    : bucket_boundaries_{buckets}, bucket_counts_(buckets.size() + 1) {}void Histogram::Observe(double value) {  // TODO: determine bucket list size at which binary search would be faster  auto bucket_index = std::max(      0L, std::find_if(bucket_boundaries_.begin(), bucket_boundaries_.end(),                       [value](double boundary) { return boundary > value; }) -              bucket_boundaries_.begin());  sum_.Increment(value);  bucket_counts_[bucket_index].Increment();}io::prometheus::client::Metric Histogram::Collect() {  auto metric = io::prometheus::client::Metric{};  auto histogram = metric.mutable_histogram();  auto sample_count = std::accumulate(      bucket_counts_.begin(), bucket_counts_.end(), double{0},      [](double sum, const Counter& counter) { return sum + counter.Value(); });  histogram->set_sample_count(sample_count);  histogram->set_sample_sum(sum_.Value());  for (int i = 0; i < bucket_counts_.size(); i++) {    auto& count = bucket_counts_[i];    auto bucket = histogram->add_bucket();    bucket->set_cumulative_count(count.Value());    bucket->set_upper_bound(i == bucket_boundaries_.size()                                ? std::numeric_limits<double>::infinity()                                : bucket_boundaries_[i]);  }  return metric;}}
 |