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 { a.registerReadFinished(std::chrono::steady_clock::time_point{}, std::uint64_t{}) } -> std::same_as<void>;
50 { a.registerReadRetry(std::uint64_t{}) } -> std::same_as<void>;
51 { a.registerReadError(std::uint64_t{}) } -> std::same_as<void>;
52 { a.report() } -> std::same_as<boost::json::object>;
53};
54
61public:
62 using PtrType = std::shared_ptr<BackendCounters>;
63
69 static PtrType
70 make();
71
75 void
77
83 void
84 registerWriteSync(std::chrono::steady_clock::time_point startTime);
85
89 void
91
95 void
97
103 void
104 registerWriteFinished(std::chrono::steady_clock::time_point startTime);
105
109 void
111
117 void
118 registerReadStarted(std::uint64_t count = 1u);
119
126 void
127 registerReadFinished(std::chrono::steady_clock::time_point startTime, std::uint64_t count = 1u);
128
134 void
135 registerReadRetry(std::uint64_t count = 1u);
136
142 void
143 registerReadError(std::uint64_t count = 1u);
144
150 boost::json::object
151 report() const;
152
153private:
155
156 class AsyncOperationCounters {
157 public:
158 AsyncOperationCounters(std::string name);
159
160 void
161 registerStarted(std::uint64_t count);
162
163 void
164 registerFinished(std::uint64_t count);
165
166 void
167 registerRetry(std::uint64_t count);
168
169 void
170 registerError(std::uint64_t count);
171
172 boost::json::object
173 report() const;
174
175 private:
176 std::string name_;
177 std::reference_wrapper<util::prometheus::GaugeInt> pendingCounter_;
178 std::reference_wrapper<util::prometheus::CounterInt> completedCounter_;
179 std::reference_wrapper<util::prometheus::CounterInt> retryCounter_;
180 std::reference_wrapper<util::prometheus::CounterInt> errorCounter_;
181 };
182
183 std::reference_wrapper<util::prometheus::CounterInt> tooBusyCounter_;
184
185 std::reference_wrapper<util::prometheus::CounterInt> writeSyncCounter_;
186 std::reference_wrapper<util::prometheus::CounterInt> writeSyncRetryCounter_;
187
188 AsyncOperationCounters asyncWriteCounters_{"write_async"};
189 AsyncOperationCounters asyncReadCounters_{"read_async"};
190
191 std::reference_wrapper<util::prometheus::HistogramInt> readDurationHistogram_;
192 std::reference_wrapper<util::prometheus::HistogramInt> writeDurationHistogram_;
193};
194
195} // namespace data
Holds statistics about the backend.
Definition BackendCounters.hpp:60
void registerTooBusy()
Register that the backend was too busy to process a request.
Definition BackendCounters.cpp:92
void registerWriteRetry()
Register that a write operation was retried.
Definition BackendCounters.cpp:125
void registerReadStarted(std::uint64_t count=1u)
Register that one or more read operations were started.
Definition BackendCounters.cpp:131
boost::json::object report() const
Get a report of the backend counters.
Definition BackendCounters.cpp:158
void registerWriteFinished(std::chrono::steady_clock::time_point startTime)
Register that a write operation was finished.
Definition BackendCounters.cpp:117
void registerWriteStarted()
Register that a write operation was started.
Definition BackendCounters.cpp:111
void registerWriteSync(std::chrono::steady_clock::time_point startTime)
Register that a write operation was started.
Definition BackendCounters.cpp:98
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:137
void registerReadRetry(std::uint64_t count=1u)
Register that one or more read operations were retried.
Definition BackendCounters.cpp:146
void registerWriteSyncRetry()
Register that a write operation was retried.
Definition BackendCounters.cpp:105
static PtrType make()
Create a new BackendCounters object.
Definition BackendCounters.cpp:85
void registerReadError(std::uint64_t count=1u)
Register that one or more read operations had an error.
Definition BackendCounters.cpp:152
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:70