From 5e642085cdce1157e21ed40900782bdd6dbb4af6 Mon Sep 17 00:00:00 2001 From: Jupp Mueller Date: Thu, 19 Dec 2019 14:04:37 -0800 Subject: [PATCH] Fix interior mutablility problems --- core/include/prometheus/detail/time_window_quantiles.h | 10 +++++----- core/include/prometheus/family.h | 4 ++-- core/include/prometheus/registry.h | 2 +- core/include/prometheus/summary.h | 2 +- core/src/detail/time_window_quantiles.cc | 4 ++-- core/src/family.cc | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/include/prometheus/detail/time_window_quantiles.h b/core/include/prometheus/detail/time_window_quantiles.h index d2a90c31..3a3ac65c 100644 --- a/core/include/prometheus/detail/time_window_quantiles.h +++ b/core/include/prometheus/detail/time_window_quantiles.h @@ -17,17 +17,17 @@ class PROMETHEUS_CPP_CORE_EXPORT TimeWindowQuantiles { TimeWindowQuantiles(const std::vector& quantiles, Clock::duration max_age_seconds, int age_buckets); - double get(double q); + double get(double q) const; void insert(double value); private: - CKMSQuantiles& rotate(); + CKMSQuantiles& rotate() const; const std::vector& quantiles_; - std::vector ckms_quantiles_; - std::size_t current_bucket_; + mutable std::vector ckms_quantiles_; + mutable std::size_t current_bucket_; - Clock::time_point last_rotation_; + mutable Clock::time_point last_rotation_; const Clock::duration rotation_interval_; }; diff --git a/core/include/prometheus/family.h b/core/include/prometheus/family.h index 1c05c69f..aa946832 100644 --- a/core/include/prometheus/family.h +++ b/core/include/prometheus/family.h @@ -143,9 +143,9 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable { const std::string name_; const std::string help_; const std::map constant_labels_; - std::mutex mutex_; + mutable std::mutex mutex_; - ClientMetric CollectMetric(std::size_t hash, T* metric); + ClientMetric CollectMetric(std::size_t hash, T* metric) const; T& Add(const std::map& labels, std::unique_ptr object); }; diff --git a/core/include/prometheus/registry.h b/core/include/prometheus/registry.h index 433a1e34..c8fdeb20 100644 --- a/core/include/prometheus/registry.h +++ b/core/include/prometheus/registry.h @@ -92,7 +92,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable { std::vector>> gauges_; std::vector>> histograms_; std::vector>> summaries_; - std::mutex mutex_; + mutable std::mutex mutex_; }; } // namespace prometheus diff --git a/core/include/prometheus/summary.h b/core/include/prometheus/summary.h index 80ce7d6e..7f6a4556 100644 --- a/core/include/prometheus/summary.h +++ b/core/include/prometheus/summary.h @@ -85,7 +85,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Summary { private: const Quantiles quantiles_; - std::mutex mutex_; + mutable std::mutex mutex_; std::uint64_t count_; double sum_; detail::TimeWindowQuantiles quantile_values_; diff --git a/core/src/detail/time_window_quantiles.cc b/core/src/detail/time_window_quantiles.cc index eec6f2d3..e767122d 100644 --- a/core/src/detail/time_window_quantiles.cc +++ b/core/src/detail/time_window_quantiles.cc @@ -12,7 +12,7 @@ TimeWindowQuantiles::TimeWindowQuantiles( last_rotation_(Clock::now()), rotation_interval_(max_age / age_buckets) {} -double TimeWindowQuantiles::get(double q) { +double TimeWindowQuantiles::get(double q) const { CKMSQuantiles& current_bucket = rotate(); return current_bucket.get(q); } @@ -24,7 +24,7 @@ void TimeWindowQuantiles::insert(double value) { } } -CKMSQuantiles& TimeWindowQuantiles::rotate() { +CKMSQuantiles& TimeWindowQuantiles::rotate() const { auto delta = Clock::now() - last_rotation_; while (delta > rotation_interval_) { ckms_quantiles_[current_bucket_].reset(); diff --git a/core/src/family.cc b/core/src/family.cc index eebe18ec..83631abc 100644 --- a/core/src/family.cc +++ b/core/src/family.cc @@ -82,7 +82,7 @@ std::vector Family::Collect() const { } template -ClientMetric Family::CollectMetric(std::size_t hash, T* metric) { +ClientMetric Family::CollectMetric(std::size_t hash, T* metric) const { auto collected = metric->Collect(); auto add_label = [&collected](const std::pair& label_pair) {