From b784988caf757d919abe6ddc30fe62ee28a9d779 Mon Sep 17 00:00:00 2001 From: mbhandary Date: Thu, 5 Dec 2019 12:50:05 -0500 Subject: [PATCH] Added support for statsD Traffic Counts reporting --- src/ripple/app/main/Application.cpp | 2 +- src/ripple/overlay/impl/OverlayImpl.cpp | 25 ++++- src/ripple/overlay/impl/OverlayImpl.h | 58 +++++++++++- src/ripple/overlay/impl/TrafficCount.h | 91 ++++++++++--------- src/ripple/overlay/make_Overlay.h | 3 +- .../peerfinder/impl/PeerfinderManager.cpp | 36 +++++++- src/ripple/peerfinder/make_Manager.h | 2 +- 7 files changed, 161 insertions(+), 56 deletions(-) diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 377f37e78e..b74501c71e 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -1482,7 +1482,7 @@ bool ApplicationImp::setup() // if (!config_.standalone()) m_overlay = make_Overlay (*this, setup_Overlay(*config_), *m_jobQueue, *serverHandler_, *m_resourceManager, *m_resolver, get_io_service(), - *config_); + *config_, m_collectorManager->collector ()); add (*m_overlay); // add to PropertyStream if (!config_->standalone()) diff --git a/src/ripple/overlay/impl/OverlayImpl.cpp b/src/ripple/overlay/impl/OverlayImpl.cpp index afc7ff813d..602c692f6c 100644 --- a/src/ripple/overlay/impl/OverlayImpl.cpp +++ b/src/ripple/overlay/impl/OverlayImpl.cpp @@ -144,7 +144,8 @@ OverlayImpl::OverlayImpl ( Resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_service& io_service, - BasicConfig const& config) + BasicConfig const& config, + beast::insight::Collector::ptr const& collector) : Overlay (parent) , app_ (app) , io_service_ (io_service) @@ -155,10 +156,25 @@ OverlayImpl::OverlayImpl ( , serverHandler_(serverHandler) , m_resourceManager (resourceManager) , m_peerFinder (PeerFinder::make_Manager (*this, io_service, - stopwatch(), app_.journal("PeerFinder"), config)) + stopwatch(), app_.journal("PeerFinder"), config, collector)) , m_resolver (resolver) , next_id_(1) , timer_count_(0) + , m_stats ( + std::bind(&OverlayImpl::collect_metrics, this), + collector, + [counts = m_traffic.getCounts(), collector]() + { + std::vector ret; + ret.reserve(counts.size()); + + for (size_t i = 0; i < counts.size(); ++i) + { + ret.push_back(TrafficGauges(counts[i].name, collector)); + } + + return ret; + }()) { beast::PropertyStream::Source::add (m_peerFinder.get()); } @@ -1353,10 +1369,11 @@ make_Overlay ( Resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_service& io_service, - BasicConfig const& config) + BasicConfig const& config, + beast::insight::Collector::ptr const& collector) { return std::make_unique(app, setup, parent, serverHandler, - resourceManager, resolver, io_service, config); + resourceManager, resolver, io_service, config, collector); } } diff --git a/src/ripple/overlay/impl/OverlayImpl.h b/src/ripple/overlay/impl/OverlayImpl.h index bd04127b61..b4e5838478 100644 --- a/src/ripple/overlay/impl/OverlayImpl.h +++ b/src/ripple/overlay/impl/OverlayImpl.h @@ -137,7 +137,7 @@ public: OverlayImpl (Application& app, Setup const& setup, Stoppable& parent, ServerHandler& serverHandler, Resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_service& io_service, - BasicConfig const& config); + BasicConfig const& config, beast::insight::Collector::ptr const& collector); ~OverlayImpl(); @@ -458,6 +458,62 @@ private: void sendEndpoints(); + +private: + + struct TrafficGauges{ + TrafficGauges (char const* name, beast::insight::Collector::ptr const& collector) + : bytesIn(collector->make_gauge(name,"Bytes_In")) + , bytesOut(collector->make_gauge(name,"Bytes_Out")) + , messagesIn(collector->make_gauge(name,"Messages_In")) + , messagesOut(collector->make_gauge(name,"Messages_Out")) + { + } + beast::insight::Gauge bytesIn; + beast::insight::Gauge bytesOut; + beast::insight::Gauge messagesIn; + beast::insight::Gauge messagesOut; + }; + + + struct Stats + { + + template + Stats ( + Handler const& handler, + beast::insight::Collector::ptr const& collector, + std::vector&& trafficGauges_) + : peerDisconnects (collector->make_gauge("Overlay","Peer_Disconnects")) + , trafficGauges (std::move(trafficGauges_)) + , hook (collector->make_hook (handler)) + { + } + + beast::insight::Gauge peerDisconnects; + std::vector trafficGauges; + beast::insight::Hook hook; + }; + + Stats m_stats; + std::mutex m_statsMutex; + +private: + void collect_metrics() + { + auto counts = m_traffic.getCounts(); + std::lock_guard lock (m_statsMutex); + assert(counts.size() == m_stats.trafficGauges.size()); + + for (std::size_t i = 0; i < counts.size(); ++i) + { + m_stats.trafficGauges[i].bytesIn = counts[i].bytesIn; + m_stats.trafficGauges[i].bytesOut = counts[i].bytesOut; + m_stats.trafficGauges[i].messagesIn = counts[i].messagesIn; + m_stats.trafficGauges[i].messagesOut = counts[i].messagesOut; + } + m_stats.peerDisconnects = getPeerDisconnect(); + } }; } // ripple diff --git a/src/ripple/overlay/impl/TrafficCount.h b/src/ripple/overlay/impl/TrafficCount.h index a118532ae5..cf89b0327a 100644 --- a/src/ripple/overlay/impl/TrafficCount.h +++ b/src/ripple/overlay/impl/TrafficCount.h @@ -35,7 +35,7 @@ public: class TrafficStats { public: - std::string const name; + char const* name; std::atomic bytesIn {0}; std::atomic bytesOut {0}; @@ -63,8 +63,8 @@ public: }; // If you add entries to this enum, you need to update the initialization - // of the array at the bottom of this file which maps array numbers to - // human-readable names. + // of the arrays at the bottom of this file which map array numbers to + // human-readable, monitoring-tool friendly names. enum category : std::size_t { base, // basic peer overhead, must be first @@ -172,56 +172,57 @@ public: @return an object which satisfies the requirements of Container */ - auto + auto const& getCounts () const { return counts_; } + protected: std::array counts_ {{ - { "overhead" }, // category::base - { "overhead: cluster" }, // category::cluster - { "overhead: overlay" }, // category::overlay - { "overhead: manifest" }, // category::manifests - { "transactions" }, // category::transaction - { "proposals" }, // category::proposal - { "validations" }, // category::validation - { "shards" }, // category::shards - { "set (get)" }, // category::get_set - { "set (share)" }, // category::share_set - { "ledger data: Transaction Set candidate (get)" }, // category::ld_tsc_get - { "ledger data: Transaction Set candidate (share)" }, // category::ld_tsc_share - { "ledger data: Transaction Node (get)" }, // category::ld_txn_get - { "ledger data: Transaction Node (share)" }, // category::ld_txn_share - { "ledger data: Account State Node (get)" }, // category::ld_asn_get - { "ledger data: Account State Node (share)" }, // category::ld_asn_share - { "ledger data (get)" }, // category::ld_get - { "ledger data (share)" }, // category::ld_share - { "ledger: Transaction Set candidate (share)" }, // category::gl_tsc_share - { "ledger: Transaction Set candidate (get)" }, // category::gl_tsc_get - { "ledger: Transaction node (share)" }, // category::gl_txn_share - { "ledger: Transaction node (get)" }, // category::gl_txn_get - { "ledger: Account State node (share)" }, // category::gl_asn_share - { "ledger: Account State node (get)" }, // category::gl_asn_get - { "ledger (share)" }, // category::gl_share - { "ledger (get)" }, // category::gl_get - { "getobject: Ledger (share)" }, // category::share_hash_ledger - { "getobject: Ledger (get)" }, // category::get_hash_ledger - { "getobject: Transaction (share)" }, // category::share_hash_tx - { "getobject: Transaction (get)" }, // category::get_hash_tx - { "getobject: Transaction node (share)" }, // category::share_hash_txnode - { "getobject: Transaction node (get)" }, // category::get_hash_txnode - { "getobject: Account State node (share)" }, // category::share_hash_asnode - { "getobject: Account State node (get)" }, // category::get_hash_asnode - { "getobject: CAS (share)" }, // category::share_cas_object - { "getobject: CAS (get)" }, // category::get_cas_object - { "getobject: Fetch Pack (share)" }, // category::share_fetch_pack - { "getobject: Fetch Pack (get)" }, // category::get_fetch_pack - { "getobject (share)" }, // category::share_hash - { "getobject (get)" }, // category::get_hash - { "unknown" } // category::unknown + {"overhead"}, // category::base + {"overhead_cluster"}, // category::cluster + {"overhead_overlay"}, // category::overlay + {"overhead_manifest"}, // category::manifests + {"transactions"}, // category::transaction + {"proposals"}, // category::proposal + {"validations"}, // category::validation + {"shards"}, // category::shards + {"set_get"}, // category::get_set + {"set_share"}, // category::share_set + {"ledger_data_Transaction_Set_candidate_get"}, // category::ld_tsc_get + {"ledger_data_Transaction_Set_candidate_share"}, // category::ld_tsc_share + {"ledger_data_Transaction_Node_get"}, // category::ld_txn_get + {"ledger_data_Transaction_Node_share"}, // category::ld_txn_share + {"ledger_data_Account_State_Node_get"}, // category::ld_asn_get + {"ledger_data_Account_State_Node_share"}, // category::ld_asn_share + {"ledger_data_get"}, // category::ld_get + {"ledger_data_share"}, // category::ld_share + {"ledger_Transaction_Set_candidate_share"}, // category::gl_tsc_share + {"ledger_Transaction_Set_candidate_get"}, // category::gl_tsc_get + {"ledger_Transaction_node_share"}, // category::gl_txn_share + {"ledger_Transaction_node_get"}, // category::gl_txn_get + {"ledger_Account_State_node_share"}, // category::gl_asn_share + {"ledger_Account_State_node_get"}, // category::gl_asn_get + {"ledger_share"}, // category::gl_share + {"ledger_get"}, // category::gl_get + {"getobject_Ledger_share"}, // category::share_hash_ledger + {"getobject_Ledger_get"}, // category::get_hash_ledger + {"getobject_Transaction_share"}, // category::share_hash_tx + {"getobject_Transaction_get"}, // category::get_hash_tx + {"getobject_Transaction_node_share"}, // category::share_hash_txnode + {"getobject_Transaction_node_get"}, // category::get_hash_txnode + {"getobject_Account_State_node_share"}, // category::share_hash_asnode + {"getobject_Account_State_node_get"}, // category::get_hash_asnode + {"getobject_CAS_share"}, // category::share_cas_object + {"getobject_CAS_get"}, // category::get_cas_object + {"getobject_Fetch_Pack_share"}, // category::share_fetch_pack + {"getobject_Fetch Pack_get"}, // category::get_fetch_pack + {"getobject_share"}, // category::share_hash + {"getobject_get"}, // category::get_hash + {"unknown"} // category::unknown }}; }; diff --git a/src/ripple/overlay/make_Overlay.h b/src/ripple/overlay/make_Overlay.h index d34c35a665..0a0e942858 100644 --- a/src/ripple/overlay/make_Overlay.h +++ b/src/ripple/overlay/make_Overlay.h @@ -43,7 +43,8 @@ make_Overlay ( Resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_service& io_service, - BasicConfig const& config); + BasicConfig const& config, + beast::insight::Collector::ptr const& collector); } // ripple diff --git a/src/ripple/peerfinder/impl/PeerfinderManager.cpp b/src/ripple/peerfinder/impl/PeerfinderManager.cpp index 6f4a660f62..80a1ef3255 100644 --- a/src/ripple/peerfinder/impl/PeerfinderManager.cpp +++ b/src/ripple/peerfinder/impl/PeerfinderManager.cpp @@ -52,7 +52,8 @@ public: boost::asio::io_service& io_service, clock_type& clock, beast::Journal journal, - BasicConfig const& config) + BasicConfig const& config, + beast::insight::Collector::ptr const& collector) : Manager (stoppable) , io_service_(io_service) , work_(boost::in_place(std::ref(io_service_))) @@ -62,6 +63,7 @@ public: , checker_ (io_service_) , m_logic (clock, m_store, checker_, journal) , m_sociConfig (config, "peerfinder") + , m_stats(std::bind(&ManagerImp::collect_metrics, this),collector) { } @@ -239,6 +241,34 @@ public: { m_logic.onWrite (map); } + +private: + struct Stats + { + + template + Stats ( + Handler const& handler, + beast::insight::Collector::ptr const& collector) + : hook (collector->make_hook (handler)) + , activeInboundPeers(collector->make_gauge("Peer_Finder","Active_Inbound_Peers")) + , activeOutboundPeers(collector->make_gauge("Peer_Finder","Active_Outbound_Peers")) + { + } + + beast::insight::Hook hook; + beast::insight::Gauge activeInboundPeers; + beast::insight::Gauge activeOutboundPeers; + }; + + std::mutex m_statsMutex; + Stats m_stats; + + void collect_metrics(){ + std::lock_guard lock (m_statsMutex); + m_stats.activeInboundPeers = m_logic.counts_.inboundActive(); + m_stats.activeOutboundPeers = m_logic.counts_.out_active(); + } }; //------------------------------------------------------------------------------ @@ -251,10 +281,10 @@ Manager::Manager (Stoppable& parent) std::unique_ptr make_Manager (Stoppable& parent, boost::asio::io_service& io_service, - clock_type& clock, beast::Journal journal, BasicConfig const& config) + clock_type& clock, beast::Journal journal, BasicConfig const& config, beast::insight::Collector::ptr const& collector) { return std::make_unique ( - parent, io_service, clock, journal, config); + parent, io_service, clock, journal, config, collector); } } diff --git a/src/ripple/peerfinder/make_Manager.h b/src/ripple/peerfinder/make_Manager.h index fb9476ec04..49bd2a2337 100644 --- a/src/ripple/peerfinder/make_Manager.h +++ b/src/ripple/peerfinder/make_Manager.h @@ -30,7 +30,7 @@ namespace PeerFinder { /** Create a new Manager. */ std::unique_ptr make_Manager (Stoppable& parent, boost::asio::io_service& io_service, - clock_type& clock, beast::Journal journal, BasicConfig const& config); + clock_type& clock, beast::Journal journal, BasicConfig const& config, beast::insight::Collector::ptr const& collector); } }