From c1e14d64b8c65fff1d68d7ae8d5da33976c132fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?= Date: Thu, 3 Oct 2024 13:46:20 -0400 Subject: [PATCH] dht_proxy_server: add push notification statistics --- include/opendht/dht_proxy_server.h | 20 ++++++++++++++++++++ src/dht_proxy_server.cpp | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index d6297edcd..7de0d9363 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -107,6 +107,16 @@ class OPENDHT_PUBLIC DhtProxyServer size_t totalPermanentPuts {0}; /** Current number of push tokens with at least one listen operation */ size_t pushListenersCount {0}; + /** Time at which the server was started */ + time_point serverStartTime; + /** Last time at which the stats were updated */ + time_point lastUpdated; + /** Total number of high priority push notification requests + * that the server attempted to send since being started */ + uint64_t highPriorityPushRequestsCount {0}; + /** Total number of normal priority push notification requests + * that the server attempted to send since being started */ + uint64_t normalPriorityPushRequestsCount {0}; /** Average requests per second */ double requestRate {0}; /** Node Info **/ @@ -115,6 +125,9 @@ class OPENDHT_PUBLIC DhtProxyServer std::string toString() const { std::ostringstream ss; ss << "Listens: " << listenCount << " Puts: " << putCount << " PushListeners: " << pushListenersCount << std::endl; + ss << "Push requests in the last " << print_duration(lastUpdated - serverStartTime) << ": " + << highPriorityPushRequestsCount << " high priority, " + << normalPriorityPushRequestsCount << " normal priority" << std::endl; ss << "Requests: " << requestRate << " per second." << std::endl; if (nodeInfo) { auto& ipv4 = nodeInfo->ipv4; @@ -136,6 +149,10 @@ class OPENDHT_PUBLIC DhtProxyServer result["putCount"] = static_cast(putCount); result["totalPermanentPuts"] = static_cast(totalPermanentPuts); result["pushListenersCount"] = static_cast(pushListenersCount); + result["serverStartTime"] = static_cast(to_time_t(serverStartTime)); + result["lastUpdated"] = static_cast(to_time_t(lastUpdated)); + result["highPriorityPushRequestsCount"] = static_cast(highPriorityPushRequestsCount); + result["normalPriorityPushRequestsCount"] = static_cast(normalPriorityPushRequestsCount); result["requestRate"] = requestRate; if (nodeInfo) result["nodeInfo"] = nodeInfo->toJson(); @@ -393,6 +410,9 @@ class OPENDHT_PUBLIC DhtProxyServer std::shared_ptr stats_; std::shared_ptr nodeInfo_ {}; std::unique_ptr printStatsTimer_; + const time_point serverStartTime_; + uint64_t highPriorityPushRequestsCount_ {0}; + uint64_t normalPriorityPushRequestsCount_ {0}; // Thread-safe access to listeners map. std::mutex lockListener_; diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 0fe40af69..6fa4f503c 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -223,6 +223,7 @@ DhtProxyServer::DhtProxyServer(const std::shared_ptr& dht, : ioContext_(std::make_shared()), dht_(dht), persistPath_(config.persistStatePath), logger_(logger), printStatsTimer_(std::make_unique(*ioContext_, 3s)), + serverStartTime_(clock::now()), connListener_(std::make_shared(std::bind(&DhtProxyServer::onConnectionClosed, this, std::placeholders::_1))), pushServer_(config.pushServer), bundleId_(config.bundleId) @@ -542,7 +543,11 @@ DhtProxyServer::updateStats(std::shared_ptr info) const stats.requestRate = count / dt.count(); #ifdef OPENDHT_PUSH_NOTIFICATIONS stats.pushListenersCount = pushListeners_.size(); + stats.highPriorityPushRequestsCount = highPriorityPushRequestsCount_; + stats.normalPriorityPushRequestsCount = normalPriorityPushRequestsCount_; #endif + stats.serverStartTime = serverStartTime_; + stats.lastUpdated = now; stats.totalPermanentPuts = 0; std::for_each(puts_.begin(), puts_.end(), [&stats](const auto& put) { stats.totalPermanentPuts += put.second.puts.size(); @@ -1155,6 +1160,11 @@ DhtProxyServer::sendPushNotification(const std::string& token, Json::Value&& jso requests_[reqid] = request; } request->send(); + // For monitoring purposes + if (highPriority) + highPriorityPushRequestsCount_++; + else + normalPriorityPushRequestsCount_++; } catch (const std::exception &e){ if (logger_)