Skip to content

Commit

Permalink
Statistics: Fix histogram.
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Spörri <[email protected]>
  • Loading branch information
pspoerri committed Mar 25, 2023
1 parent 93eadfc commit 4df04c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
16 changes: 2 additions & 14 deletions src/statistics/Statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,8 @@ std::shared_ptr<StatisticsHistogram>
Statistics::createNanoSecondHistogram(const std::string &label) {
constexpr size_t ONE_MIN = 60000000000; // Minute
const static auto NANOSECOND_HISTOGRAM = std::vector<size_t>{
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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/statistics/StatisticsHistogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace geds {

class StatisticsHistogram : public StatisticsItem {
std::atomic<size_t> _sum;
std::atomic<size_t> _total;
std::atomic<size_t> _totalCount;
std::vector<std::atomic<size_t>> _count;
const std::vector<size_t> &_buckets;
StatisticsHistogram(std::string labelArg, const std::vector<size_t> &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;
}
Expand All @@ -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 <basename>_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;
}

Expand Down

0 comments on commit 4df04c0

Please sign in to comment.