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(),
79 data->buckets.end(),
80 value,
81 [](Bucket const& bucket, ValueType const& value) { return bucket.upperBound < value; }
82 );
83 if (bucket != data->buckets.end()) {
84 ++bucket->count;
85 } else {
86 ++data->lastBucket.count;
87 }
88 data->sum += value;
89 }
90
91 void
92 serializeValue(std::string const& name, std::string labelsString, OStream& stream) const
93 {
94 if (labelsString.empty()) {
95 labelsString = "{";
96 } else {
97 ASSERT(
98 labelsString.front() == '{' && labelsString.back() == '}',
99 "Labels must be in Prometheus serialized format."
100 );
101 labelsString.back() = ',';
102 }
103
104 auto data = data_->template lock<std::scoped_lock>();
105 std::uint64_t cumulativeCount = 0;
106
107 for (auto const& bucket : data->buckets) {
108 cumulativeCount += bucket.count;
109 stream << name << "_bucket" << labelsString << "le=\"" << bucket.upperBound << "\"} " << cumulativeCount
110 << '\n';
111 }
112 cumulativeCount += data->lastBucket.count;
113 stream << name << "_bucket" << labelsString << "le=\"+Inf\"} " << cumulativeCount << '\n';
114
115 if (labelsString.size() == 1) {
116 labelsString = "";
117 } else {
118 labelsString.back() = '}';
119 }
120 stream << name << "_sum" << labelsString << " " << data->sum << '\n';
121 stream << name << "_count" << labelsString << " " << cumulativeCount << '\n';
122 }
123
124private:
125 struct Bucket {
126 Bucket(ValueType upperBound) : upperBound(upperBound)
127 {
128 }
129
130 ValueType upperBound;
131 std::uint64_t count = 0;
132 };
133
134 struct Data {
135 std::vector<Bucket> buckets;
136 Bucket lastBucket{std::numeric_limits<ValueType>::max()};
137 ValueType sum = 0;
138 };
139 std::unique_ptr<util::Mutex<Data>> data_ = std::make_unique<util::Mutex<Data>>();
140};
141
142} // 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