Skip to content

Commit

Permalink
Add new constructor for Histogram and Summary taking rvalue ref of pa…
Browse files Browse the repository at this point in the history
…rameters
  • Loading branch information
sjanel committed Apr 3, 2022
1 parent 4d66807 commit 56b37e8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions core/include/prometheus/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
/// exponential etc..
///
/// The bucket boundaries cannot be changed once the histogram is created.
Histogram(const BucketBoundaries& buckets);
explicit Histogram(const BucketBoundaries& buckets);

explicit Histogram(BucketBoundaries&& buckets);

/// \brief Observe the given amount.
///
Expand All @@ -68,7 +70,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
ClientMetric Collect() const;

private:
const BucketBoundaries bucket_boundaries_;
BucketBoundaries bucket_boundaries_;
mutable std::mutex mutex_;
std::vector<Counter> bucket_counts_;
Gauge sum_;
Expand Down
16 changes: 10 additions & 6 deletions core/include/prometheus/summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ class PROMETHEUS_CPP_CORE_EXPORT Summary {
/// and how smooth the time window is moved. With only one age bucket it
/// effectively results in a complete reset of the summary each time max_age
/// has passed. The default value is 5.
Summary(const Quantiles& quantiles,
std::chrono::milliseconds max_age = std::chrono::seconds{60},
int age_buckets = 5);
explicit Summary(const Quantiles& quantiles,
std::chrono::milliseconds max_age = std::chrono::seconds{60},
int age_buckets = 5);

explicit Summary(Quantiles&& quantiles,
std::chrono::milliseconds max_age = std::chrono::seconds{60},
int age_buckets = 5);

/// \brief Observe the given amount.
void Observe(double value);
Expand All @@ -84,10 +88,10 @@ class PROMETHEUS_CPP_CORE_EXPORT Summary {
ClientMetric Collect() const;

private:
const Quantiles quantiles_;
Quantiles quantiles_;
mutable std::mutex mutex_;
std::uint64_t count_;
double sum_;
std::uint64_t count_{};
double sum_{};
detail::TimeWindowQuantiles quantile_values_;
};

Expand Down
10 changes: 9 additions & 1 deletion core/src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ bool is_strict_sorted(ForwardIterator first, ForwardIterator last) {
} // namespace

Histogram::Histogram(const BucketBoundaries& buckets)
: bucket_boundaries_{buckets}, bucket_counts_{buckets.size() + 1}, sum_{} {
: bucket_boundaries_{buckets}, bucket_counts_{buckets.size() + 1} {
if (!is_strict_sorted(begin(bucket_boundaries_), end(bucket_boundaries_))) {
throw std::invalid_argument("Bucket Boundaries must be strictly sorted");
}
}

Histogram::Histogram(BucketBoundaries&& buckets)
: bucket_boundaries_{std::move(buckets)},
bucket_counts_{bucket_boundaries_.size() + 1} {
if (!is_strict_sorted(begin(bucket_boundaries_), end(bucket_boundaries_))) {
throw std::invalid_argument("Bucket Boundaries must be strictly sorted");
}
Expand Down
11 changes: 7 additions & 4 deletions core/src/summary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ namespace prometheus {
Summary::Summary(const Quantiles& quantiles,
const std::chrono::milliseconds max_age, const int age_buckets)
: quantiles_{quantiles},
count_{0},
sum_{0},
quantile_values_{quantiles_, max_age, age_buckets} {}

Summary::Summary(Quantiles&& quantiles, const std::chrono::milliseconds max_age,
const int age_buckets)
: quantiles_{std::move(quantiles)},
quantile_values_{quantiles_, max_age, age_buckets} {}

void Summary::Observe(const double value) {
Expand All @@ -20,13 +23,13 @@ void Summary::Observe(const double value) {
}

ClientMetric Summary::Collect() const {
auto metric = ClientMetric{};
ClientMetric metric{};

std::lock_guard<std::mutex> lock(mutex_);

metric.summary.quantile.reserve(quantiles_.size());
for (const auto& quantile : quantiles_) {
auto metricQuantile = ClientMetric::Quantile{};
ClientMetric::Quantile metricQuantile{};
metricQuantile.quantile = quantile.quantile;
metricQuantile.value = quantile_values_.get(quantile.quantile);
metric.summary.quantile.push_back(std::move(metricQuantile));
Expand Down

0 comments on commit 56b37e8

Please sign in to comment.