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 {
44 t.setBuckets(std::vector<typename std::remove_cvref_t<T>::ValueType>{})
45 } -> std::same_as<void>;
46 {
47 t.serializeValue(std::string{}, std::string{}, std::declval<OStream&>())
48 } -> std::same_as<void>;
49};
50
51template <SomeNumberType NumberType>
52class HistogramImpl {
53public:
54 using ValueType = NumberType;
55
56 HistogramImpl() = default;
57
58 HistogramImpl(HistogramImpl const&) = delete;
59 HistogramImpl(HistogramImpl&&) = default;
60
61 HistogramImpl&
62 operator=(HistogramImpl const&) = delete;
63 HistogramImpl&
64 operator=(HistogramImpl&&) = default;
65
66 void
67 setBuckets(std::vector<ValueType> const& bounds)
68 {
69 auto data = data_->template lock<std::scoped_lock>();
70 ASSERT(data->buckets.empty(), "Buckets can be set only once.");
71 data->buckets.reserve(bounds.size());
72 for (auto const& bound : bounds) {
73 data->buckets.emplace_back(bound);
74 }
75 }
76
77 void
78 observe(ValueType const value)
79 {
80 auto data = data_->template lock<std::scoped_lock>();
81 auto const bucket = std::lower_bound(
82 data->buckets.begin(),
83 data->buckets.end(),
84 value,
85 [](Bucket const& bucket, ValueType const& value) { return bucket.upperBound < value; }
86 );
87 if (bucket != data->buckets.end()) {
88 ++bucket->count;
89 } else {
90 ++data->lastBucket.count;
91 }
92 data->sum += value;
93 }
94
95 void
96 serializeValue(std::string const& name, std::string labelsString, OStream& stream) const
97 {
98 if (labelsString.empty()) {
99 labelsString = "{";
100 } else {
101 ASSERT(
102 labelsString.front() == '{' && labelsString.back() == '}',
103 "Labels must be in Prometheus serialized format."
104 );
105 labelsString.back() = ',';
106 }
107
108 auto data = data_->template lock<std::scoped_lock>();
109 std::uint64_t cumulativeCount = 0;
110
111 for (auto const& bucket : data->buckets) {
112 cumulativeCount += bucket.count;
113 stream << name << "_bucket" << labelsString << "le=\"" << bucket.upperBound << "\"} "
114 << cumulativeCount << '\n';
115 }
116 cumulativeCount += data->lastBucket.count;
117 stream << name << "_bucket" << labelsString << "le=\"+Inf\"} " << cumulativeCount << '\n';
118
119 if (labelsString.size() == 1) {
120 labelsString = "";
121 } else {
122 labelsString.back() = '}';
123 }
124 stream << name << "_sum" << labelsString << " " << data->sum << '\n';
125 stream << name << "_count" << labelsString << " " << cumulativeCount << '\n';
126 }
127
128private:
129 struct Bucket {
130 Bucket(ValueType upperBound) : upperBound(upperBound)
131 {
132 }
133
134 ValueType upperBound;
135 std::uint64_t count = 0;
136 };
137
138 struct Data {
139 std::vector<Bucket> buckets;
140 Bucket lastBucket{std::numeric_limits<ValueType>::max()};
141 ValueType sum = 0;
142 };
143 std::unique_ptr<util::Mutex<Data>> data_ = std::make_unique<util::Mutex<Data>>();
144};
145
146} // namespace util::prometheus::impl
A stream that can optionally compress its data.
Definition OStream.hpp:31
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:75