Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
BackendCounters.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/prometheus/Counter.hpp"
23#include "util/prometheus/Gauge.hpp"
24#include "util/prometheus/Histogram.hpp"
25
26#include <boost/json/object.hpp>
27
28#include <chrono>
29#include <cstdint>
30#include <functional>
31#include <memory>
32#include <string>
33
34namespace data {
35
39template <typename T>
40concept SomeBackendCounters = requires(T a) {
41 typename T::PtrType;
42 { a.registerTooBusy() } -> std::same_as<void>;
43 { a.registerWriteSync(std::chrono::steady_clock::time_point{}) } -> std::same_as<void>;
44 { a.registerWriteSyncRetry() } -> std::same_as<void>;
45 { a.registerWriteStarted() } -> std::same_as<void>;
46 { a.registerWriteFinished(std::chrono::steady_clock::time_point{}) } -> std::same_as<void>;
47 { a.registerWriteRetry() } -> std::same_as<void>;
48 { a.registerReadStarted(std::uint64_t{}) } -> std::same_as<void>;
49 {
50 a.registerReadFinished(std::chrono::steady_clock::time_point{}, std::uint64_t{})
51 } -> std::same_as<void>;
52 { a.registerReadRetry(std::uint64_t{}) } -> std::same_as<void>;
53 { a.registerReadError(std::uint64_t{}) } -> std::same_as<void>;
54 { a.report() } -> std::same_as<boost::json::object>;
55};
56
62class BackendCounters {
63public:
64 using PtrType = std::shared_ptr<BackendCounters>;
65
71 static PtrType
72 make();
73
77 void
79
85 void
86 registerWriteSync(std::chrono::steady_clock::time_point startTime);
87
91 void
93
97 void
99
105 void
106 registerWriteFinished(std::chrono::steady_clock::time_point startTime);
107
111 void
113
119 void
120 registerReadStarted(std::uint64_t count = 1u);
121
128 void
129 registerReadFinished(std::chrono::steady_clock::time_point startTime, std::uint64_t count = 1u);
130
136 void
137 registerReadRetry(std::uint64_t count = 1u);
138
144 void
145 registerReadError(std::uint64_t count = 1u);
146
152 boost::json::object
153 report() const;
154
155private:
156 BackendCounters();
157
158 class AsyncOperationCounters {
159 public:
160 AsyncOperationCounters(std::string name);
161
162 void
163 registerStarted(std::uint64_t count);
164
165 void
166 registerFinished(std::uint64_t count);
167
168 void
169 registerRetry(std::uint64_t count);
170
171 void
172 registerError(std::uint64_t count);
173
174 boost::json::object
175 report() const;
176
177 private:
178 std::string name_;
179 std::reference_wrapper<util::prometheus::GaugeInt> pendingCounter_;
180 std::reference_wrapper<util::prometheus::CounterInt> completedCounter_;
181 std::reference_wrapper<util::prometheus::CounterInt> retryCounter_;
182 std::reference_wrapper<util::prometheus::CounterInt> errorCounter_;
183 };
184
185 std::reference_wrapper<util::prometheus::CounterInt> tooBusyCounter_;
186
187 std::reference_wrapper<util::prometheus::CounterInt> writeSyncCounter_;
188 std::reference_wrapper<util::prometheus::CounterInt> writeSyncRetryCounter_;
189
190 AsyncOperationCounters asyncWriteCounters_{"write_async"};
191 AsyncOperationCounters asyncReadCounters_{"read_async"};
192
193 std::reference_wrapper<util::prometheus::HistogramInt> readDurationHistogram_;
194 std::reference_wrapper<util::prometheus::HistogramInt> writeDurationHistogram_;
195};
196
197} // namespace data
void registerTooBusy()
Register that the backend was too busy to process a request.
Definition BackendCounters.cpp:105
void registerWriteRetry()
Register that a write operation was retried.
Definition BackendCounters.cpp:138
void registerReadStarted(std::uint64_t count=1u)
Register that one or more read operations were started.
Definition BackendCounters.cpp:144
boost::json::object report() const
Get a report of the backend counters.
Definition BackendCounters.cpp:174
void registerWriteFinished(std::chrono::steady_clock::time_point startTime)
Register that a write operation was finished.
Definition BackendCounters.cpp:130
void registerWriteStarted()
Register that a write operation was started.
Definition BackendCounters.cpp:124
void registerWriteSync(std::chrono::steady_clock::time_point startTime)
Register that a write operation was started.
Definition BackendCounters.cpp:111
void registerReadFinished(std::chrono::steady_clock::time_point startTime, std::uint64_t count=1u)
Register that one or more read operations were finished.
Definition BackendCounters.cpp:150
void registerReadRetry(std::uint64_t count=1u)
Register that one or more read operations were retried.
Definition BackendCounters.cpp:162
void registerWriteSyncRetry()
Register that a write operation was retried.
Definition BackendCounters.cpp:118
static PtrType make()
Create a new BackendCounters object.
Definition BackendCounters.cpp:98
void registerReadError(std::uint64_t count=1u)
Register that one or more read operations had an error.
Definition BackendCounters.cpp:168
A concept for a class that can be used to count backend operations.
Definition BackendCounters.hpp:40
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:75