Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Counters.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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 "rpc/WorkQueue.hpp"
23#include "util/prometheus/Counter.hpp"
24#include "util/prometheus/Histogram.hpp"
25
26#include <boost/json.hpp>
27#include <boost/json/object.hpp>
28
29#include <chrono>
30#include <cstdint>
31#include <functional>
32#include <mutex>
33#include <string>
34#include <unordered_map>
35
36namespace rpc {
37
41class Counters {
42 using CounterType = std::reference_wrapper<util::prometheus::CounterInt>;
46 struct MethodInfo {
47 MethodInfo(std::string const& method);
48
49 CounterType started;
50 CounterType finished;
51 CounterType failed;
52 CounterType errored;
53 CounterType forwarded;
54 CounterType failedForward;
55 CounterType duration;
56 };
57
58 MethodInfo&
59 getMethodInfo(std::string const& method);
60
61 mutable std::mutex mutex_;
62 std::unordered_map<std::string, MethodInfo> methodInfo_;
63
64 // counters that don't carry RPC method information
65 CounterType tooBusyCounter_;
66 CounterType notReadyCounter_;
67 CounterType badSyntaxCounter_;
68 CounterType unknownCommandCounter_;
69 CounterType internalErrorCounter_;
70
71 std::reference_wrapper<util::prometheus::HistogramInt> ledgerAgeLedgersHistogram_;
72 CounterType ledgerHashRequestsCounter_;
73
74 std::reference_wrapper<Reportable const> workQueue_;
75 std::chrono::time_point<std::chrono::system_clock> startupTime_;
76
77public:
83 Counters(Reportable const& wq);
84
91 static Counters
93 {
94 return Counters{wq};
95 }
96
102 void
103 rpcFailed(std::string const& method);
104
110 void
111 rpcErrored(std::string const& method);
112
119 void
120 rpcComplete(std::string const& method, std::chrono::microseconds const& rpcDuration);
121
127 void
128 rpcForwarded(std::string const& method);
129
135 void
136 rpcFailedToForward(std::string const& method);
137
139 void
140 onTooBusy();
141
143 void
144 onNotReady();
145
147 void
148 onBadSyntax();
149
151 void
153
155 void
157
164 void
165 recordLedgerRequest(boost::json::object const& params, std::uint32_t currentLedgerSequence);
166
168 std::chrono::seconds
169 uptime() const;
170
172 boost::json::object
173 report() const;
174};
175
176} // namespace rpc
void rpcComplete(std::string const &method, std::chrono::microseconds const &rpcDuration)
Increments the completed count for a particular RPC method.
Definition Counters.cpp:186
void onNotReady()
Increments the global not ready counter.
Definition Counters.cpp:218
void rpcForwarded(std::string const &method)
Increments the forwarded count for a particular RPC method.
Definition Counters.cpp:196
void rpcFailedToForward(std::string const &method)
Increments the failed to forward count for a particular RPC method.
Definition Counters.cpp:204
void recordLedgerRequest(boost::json::object const &params, std::uint32_t currentLedgerSequence)
Records ledger request metrics based on the ledger parameter in the request.
Definition Counters.cpp:242
static Counters makeCounters(WorkQueue const &wq)
A factory function that creates a new counters instance.
Definition Counters.hpp:92
void onInternalError()
Increments the global internal error counter.
Definition Counters.cpp:236
std::chrono::seconds uptime() const
Definition Counters.cpp:271
void rpcFailed(std::string const &method)
Increments the failed count for a particular RPC method.
Definition Counters.cpp:168
boost::json::object report() const
Definition Counters.cpp:279
void onTooBusy()
Increments the global too busy counter.
Definition Counters.cpp:212
void onBadSyntax()
Increments the global bad syntax counter.
Definition Counters.cpp:224
void onUnknownCommand()
Increments the global unknown command/method counter.
Definition Counters.cpp:230
void rpcErrored(std::string const &method)
Increments the errored count for a particular RPC method.
Definition Counters.cpp:177
Counters(Reportable const &wq)
Creates a new counters instance that operates on the given WorkQueue.
Definition Counters.cpp:111
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:62
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
An interface for any class providing a report as json object.
Definition WorkQueue.hpp:47