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
25#include <boost/json.hpp>
26#include <boost/json/object.hpp>
27
28#include <chrono>
29#include <functional>
30#include <mutex>
31#include <string>
32#include <unordered_map>
33
34namespace rpc {
35
39class Counters {
40 using CounterType = std::reference_wrapper<util::prometheus::CounterInt>;
44 struct MethodInfo {
45 MethodInfo(std::string const& method);
46
47 CounterType started;
48 CounterType finished;
49 CounterType failed;
50 CounterType errored;
51 CounterType forwarded;
52 CounterType failedForward;
53 CounterType duration;
54 };
55
56 MethodInfo&
57 getMethodInfo(std::string const& method);
58
59 mutable std::mutex mutex_;
60 std::unordered_map<std::string, MethodInfo> methodInfo_;
61
62 // counters that don't carry RPC method information
63 CounterType tooBusyCounter_;
64 CounterType notReadyCounter_;
65 CounterType badSyntaxCounter_;
66 CounterType unknownCommandCounter_;
67 CounterType internalErrorCounter_;
68
69 std::reference_wrapper<WorkQueue const> workQueue_;
70 std::chrono::time_point<std::chrono::system_clock> startupTime_;
71
72public:
78 Counters(WorkQueue const& wq);
79
86 static Counters
88 {
89 return Counters{wq};
90 }
91
97 void
98 rpcFailed(std::string const& method);
99
105 void
106 rpcErrored(std::string const& method);
107
114 void
115 rpcComplete(std::string const& method, std::chrono::microseconds const& rpcDuration);
116
122 void
123 rpcForwarded(std::string const& method);
124
130 void
131 rpcFailedToForward(std::string const& method);
132
134 void
135 onTooBusy();
136
138 void
139 onNotReady();
140
142 void
143 onBadSyntax();
144
146 void
148
150 void
152
154 std::chrono::seconds
155 uptime() const;
156
158 boost::json::object
159 report() const;
160};
161
162} // namespace rpc
Holds information about successful, failed, forwarded, etc. RPC handler calls.
Definition Counters.hpp:39
void rpcComplete(std::string const &method, std::chrono::microseconds const &rpcDuration)
Increments the completed count for a particular RPC method.
Definition Counters.cpp:141
void onNotReady()
Increments the global not ready counter.
Definition Counters.cpp:173
void rpcForwarded(std::string const &method)
Increments the forwarded count for a particular RPC method.
Definition Counters.cpp:151
void rpcFailedToForward(std::string const &method)
Increments the failed to forward count for a particular RPC method.
Definition Counters.cpp:159
Counters(WorkQueue const &wq)
Creates a new counters instance that operates on the given WorkQueue.
Definition Counters.cpp:91
static Counters makeCounters(WorkQueue const &wq)
A factory function that creates a new counters instance.
Definition Counters.hpp:87
void onInternalError()
Increments the global internal error counter.
Definition Counters.cpp:191
std::chrono::seconds uptime() const
Definition Counters.cpp:197
void rpcFailed(std::string const &method)
Increments the failed count for a particular RPC method.
Definition Counters.cpp:123
boost::json::object report() const
Definition Counters.cpp:203
void onTooBusy()
Increments the global too busy counter.
Definition Counters.cpp:167
void onBadSyntax()
Increments the global bad syntax counter.
Definition Counters.cpp:179
void onUnknownCommand()
Increments the global unknown command/method counter.
Definition Counters.cpp:185
void rpcErrored(std::string const &method)
Increments the errored count for a particular RPC method.
Definition Counters.cpp:132
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:46
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36