mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
103 lines
2.9 KiB
C++
103 lines
2.9 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of clio: https://github.com/XRPLF/clio
|
|
Copyright (c) 2022, the clio developers.
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include <rpc/Counters.h>
|
|
#include <rpc/RPC.h>
|
|
#include <rpc/RPCHelpers.h>
|
|
|
|
namespace RPC {
|
|
|
|
void
|
|
Counters::initializeCounter(std::string const& method)
|
|
{
|
|
std::shared_lock lk(mutex_);
|
|
if (methodInfo_.count(method) == 0)
|
|
{
|
|
lk.unlock();
|
|
std::scoped_lock ulk(mutex_);
|
|
|
|
// This calls the default constructor for methodInfo of the method.
|
|
methodInfo_[method];
|
|
}
|
|
}
|
|
|
|
void
|
|
Counters::rpcErrored(std::string const& method)
|
|
{
|
|
initializeCounter(method);
|
|
|
|
std::shared_lock lk(mutex_);
|
|
MethodInfo& counters = methodInfo_[method];
|
|
|
|
counters.started++;
|
|
counters.errored++;
|
|
}
|
|
|
|
void
|
|
Counters::rpcComplete(std::string const& method, std::chrono::microseconds const& rpcDuration)
|
|
{
|
|
initializeCounter(method);
|
|
|
|
std::shared_lock lk(mutex_);
|
|
MethodInfo& counters = methodInfo_[method];
|
|
|
|
counters.started++;
|
|
counters.finished++;
|
|
counters.duration += rpcDuration.count();
|
|
}
|
|
|
|
void
|
|
Counters::rpcForwarded(std::string const& method)
|
|
{
|
|
initializeCounter(method);
|
|
|
|
std::shared_lock lk(mutex_);
|
|
MethodInfo& counters = methodInfo_[method];
|
|
|
|
counters.forwarded++;
|
|
}
|
|
|
|
boost::json::object
|
|
Counters::report() const
|
|
{
|
|
std::shared_lock lk(mutex_);
|
|
auto obj = boost::json::object{};
|
|
|
|
obj[JS(rpc)] = boost::json::object{};
|
|
auto& rpc = obj[JS(rpc)].as_object();
|
|
|
|
for (auto const& [method, info] : methodInfo_)
|
|
{
|
|
auto counters = boost::json::object{};
|
|
counters[JS(started)] = std::to_string(info.started);
|
|
counters[JS(finished)] = std::to_string(info.finished);
|
|
counters[JS(errored)] = std::to_string(info.errored);
|
|
counters["forwarded"] = std::to_string(info.forwarded);
|
|
counters[JS(duration_us)] = std::to_string(info.duration);
|
|
|
|
rpc[method] = std::move(counters);
|
|
}
|
|
|
|
obj["work_queue"] = workQueue_.get().report();
|
|
|
|
return obj;
|
|
}
|
|
|
|
} // namespace RPC
|