Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
HistogramImpl.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Assert.hpp"
23#include "util/Concepts.hpp"
24#include "util/Mutex.hpp"
25#include "util/prometheus/OStream.hpp"
26
27#include <cstdint>
28#include <limits>
29#include <memory>
30#include <mutex>
31#include <string>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace util::prometheus::impl {
37
38template <typename T>
39concept SomeHistogramImpl = requires(T t) {
40 typename std::remove_cvref_t<T>::ValueType;
42 { t.observe(typename std::remove_cvref_t<T>::ValueType{1}) } -> std::same_as<void>;
43 { t.setBuckets(std::vector<typename std::remove_cvref_t<T>::ValueType>{}) } -> std::same_as<void>;
44 { t.serializeValue(std::string{}, std::string{}, std::declval<OStream&>()) } -> std::same_as<void>;
45};
46
47template <SomeNumberType NumberType>
49public:
50 using ValueType = NumberType;
51
52 HistogramImpl() = default;
53
54 HistogramImpl(HistogramImpl const&) = delete;
55 HistogramImpl(HistogramImpl&&) = default;
56
58 operator=(HistogramImpl const&) = delete;
60 operator=(HistogramImpl&&) = default;
61
62 void
63 setBuckets(std::vector<ValueType> const& bounds)
64 {
65 auto data = data_->template lock<std::scoped_lock>();
66 ASSERT(data->buckets.empty(), "Buckets can be set only once.");
67 data->buckets.reserve(bounds.size());
68 for (auto const& bound : bounds) {
69 data->buckets.emplace_back(bound);
70 }
71 }
72
73 void
74 observe(ValueType const value)
75 {
76 auto data = data_->template lock<std::scoped_lock>();
77 auto const bucket = std::lower_bound(
78 data->buckets.begin(), data->buckets.end(), value, [](Bucket const& bucket, ValueType const& value) {
79 return bucket.upperBound < value;
80 }
81 );
82 if (bucket != data->buckets.end()) {
83 ++bucket->count;
84 } else {
85 ++data->lastBucket.count;
86 }
87 data->sum += value;
88 }
89
90 void
91 serializeValue(std::string const& name, std::string labelsString, OStream& stream) const
92 {
93 if (labelsString.empty()) {
94 labelsString = "{";
95 } else {
96 ASSERT(
97 labelsString.front() == '{' && labelsString.back() == '}',
98 "Labels must be in Prometheus serialized format."
99 );
100 labelsString.back() = ',';
101 }
102
103 auto data = data_->template lock<std::scoped_lock>();
104 std::uint64_t cumulativeCount = 0;
105
106 for (auto const& bucket : data->buckets) {
107 cumulativeCount += bucket.count;
108 stream << name << "_bucket" << labelsString << "le=\"" << bucket.upperBound << "\"} " << cumulativeCount
109 << '\n';
110 }
111 cumulativeCount += data->lastBucket.count;
112 stream << name << "_bucket" << labelsString << "le=\"+Inf\"} " << cumulativeCount << '\n';
113
114 if (labelsString.size() == 1) {
115 labelsString = "";
116 } else {
117 labelsString.back() = '}';
118 }
119 stream << name << "_sum" << labelsString << " " << data->sum << '\n';
120 stream << name << "_count" << labelsString << " " << cumulativeCount << '\n';
121 }
122
123private:
124 struct Bucket {
125 Bucket(ValueType upperBound) : upperBound(upperBound)
126 {
127 }
128
129 ValueType upperBound;
130 std::uint64_t count = 0;
131 };
132
133 struct Data {
134 std::vector<Bucket> buckets;
135 Bucket lastBucket{std::numeric_limits<ValueType>::max()};
136 ValueType sum = 0;
137 };
138 std::unique_ptr<util::Mutex<Data>> data_ = std::make_unique<util::Mutex<Data>>();
139};
140
141} // namespace util::prometheus::impl
A stream that can optionally compress its data.
Definition OStream.hpp:31
Definition HistogramImpl.hpp:48
Specifies a number type.
Definition Concepts.hpp:34
Definition HistogramImpl.hpp:39
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70