From 4df04c0a8ca1841cf87cfbf33f2ba39d72d295c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Spo=CC=88rri?= Date: Sat, 25 Mar 2023 12:47:33 +0100 Subject: [PATCH] Statistics: Fix histogram. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pascal SpoĢˆrri --- src/statistics/Statistics.cpp | 16 ++-------------- src/statistics/StatisticsHistogram.h | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/statistics/Statistics.cpp b/src/statistics/Statistics.cpp index 8a6d7cde..a1c893f7 100644 --- a/src/statistics/Statistics.cpp +++ b/src/statistics/Statistics.cpp @@ -72,20 +72,8 @@ std::shared_ptr Statistics::createNanoSecondHistogram(const std::string &label) { constexpr size_t ONE_MIN = 60000000000; // Minute const static auto NANOSECOND_HISTOGRAM = std::vector{ - 010, 020, 030, 040, 050, // 50ns - 100, 200, 300, 400, 500, // 0.5us - 1000, 2000, 3000, 4000, 5000, // 5us - 10000, 20000, 30000, 40000, 50000, // 50us - 100000, 200000, 300000, 400000, 500000, // 500us - 1000000, 2000000, 3000000, 4000000, 5000000, // 5ms - 10000000, 20000000, 30000000, 40000000, 50000000, // 50ms - 100000000, 200000000, 300000000, 400000000, 500000000, // 500ms - 1000000000, 2000000000, 3000000000, 4000000000, 5000000000, // 5s - 10000000000, 20000000000, 30000000000, 40000000000, 50000000000, // 50s - ONE_MIN * 1, ONE_MIN * 2, ONE_MIN * 3, ONE_MIN * 4, ONE_MIN * 5, // 5min, - ONE_MIN * 10, ONE_MIN * 20, ONE_MIN * 30, ONE_MIN * 40, ONE_MIN * 50, // 50min - ONE_MIN * 60 // 1h - }; + 1, 10, 100, 1000, 10000, 100000, 1000000, + 10000000, 100000000, 1000000000, 10000000000, ONE_MIN * 1, ONE_MIN * 10, ONE_MIN * 60}; return createHistogram(label, NANOSECOND_HISTOGRAM); } diff --git a/src/statistics/StatisticsHistogram.h b/src/statistics/StatisticsHistogram.h index 150663c3..988b7f56 100644 --- a/src/statistics/StatisticsHistogram.h +++ b/src/statistics/StatisticsHistogram.h @@ -18,11 +18,11 @@ namespace geds { class StatisticsHistogram : public StatisticsItem { std::atomic _sum; - std::atomic _total; + std::atomic _totalCount; std::vector> _count; const std::vector &_buckets; StatisticsHistogram(std::string labelArg, const std::vector &buckets) - : StatisticsItem(std::move(labelArg)), _count(buckets.size() + 1), _buckets(buckets) { + : StatisticsItem(std::move(labelArg)), _count(buckets.size()), _buckets(buckets) { for (auto &c : _count) { c = (size_t)0; } @@ -35,29 +35,29 @@ class StatisticsHistogram : public StatisticsItem { for (size_t i = 0; i < _buckets.size(); i++) { stream << prometheusLabel << "_bucket{le=\"" << _buckets[i] << "\"} " << _count[i] << "\n"; } - stream << prometheusLabel << "_bucket{le=\"+Inf\"}" << _count[_buckets.size()] << "\n" + stream << prometheusLabel << "_bucket{le=\"+Inf\"} " << _totalCount << "\n" << prometheusLabel << "_sum " << _sum << "\n" - << prometheusLabel << "_count " << _total << std::endl; + << prometheusLabel << "_count " << _totalCount << std::endl; } void printForConsole(std::stringstream &stream) const override { - stream << label << ", " << _total << ", sum " << _sum << ", "; + stream << label << ", " << _totalCount << ", sum " << _sum << ", "; for (size_t i = 0; i < _buckets.size(); i++) { stream << " {le=" << _buckets[i] << "} " << _count[i] << ", "; } - stream << " {le=+Inf }" << _count[_buckets.size()] << std::endl; + stream << " {le=+Inf} " << _totalCount << std::endl; } - StatisticsItem &operator+=(size_t value) override { + StatisticsHistogram &operator+=(size_t value) override { + // https://prometheus.io/docs/concepts/metric_types/#histogram _sum += value; - _total += 1; + _totalCount += 1; // Corresponds to _bucket{le="+Inf"} for (size_t i = 0; i < _buckets.size(); i++) { - if (value < _buckets[i]) { - return *this; + if (value > _buckets[i]) { + continue; } _count[i] += 1; } - _count[_buckets.size()] += 1; return *this; }