From 5657406fd49936ec7438eac6b76652d30c3ae815 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Fri, 21 Apr 2023 09:09:57 -0700 Subject: [PATCH] Add metrics for sent/received bytes (total) --- node/Metrics.cpp | 10 ++++++++++ node/Metrics.hpp | 6 ++++++ osdep/Phy.hpp | 10 ++++++++-- service/OneService.cpp | 14 +++++++++++--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/node/Metrics.cpp b/node/Metrics.cpp index fe2a6593c..0fc4d1765 100644 --- a/node/Metrics.cpp +++ b/node/Metrics.cpp @@ -22,6 +22,16 @@ namespace prometheus { namespace ZeroTier { namespace Metrics { + // Data Sent/Received Metrics + prometheus::simpleapi::counter_metric_t udp_send + { "zt_udp_data_sent", "number of bytes ZeroTier has sent via UDP" }; + prometheus::simpleapi::counter_metric_t udp_recv + { "zt_udp_data_recv", "number of bytes ZeroTier has received via UDP" }; + prometheus::simpleapi::counter_metric_t tcp_send + { "zt_tcp_data_sent", "number of bytes ZeroTier has sent via TCP" }; + prometheus::simpleapi::counter_metric_t tcp_recv + { "zt_tcp_data_recv", "number of bytes ZeroTier has received via TCP" }; + // General Controller Metrics prometheus::simpleapi::gauge_metric_t network_count {"controller_network_count", "number of networks the controller is serving"}; diff --git a/node/Metrics.hpp b/node/Metrics.hpp index 205ce1561..28cbd5990 100644 --- a/node/Metrics.hpp +++ b/node/Metrics.hpp @@ -22,6 +22,12 @@ namespace prometheus { namespace ZeroTier { namespace Metrics { + // Data Sent/Received Metrics + extern prometheus::simpleapi::counter_metric_t udp_send; + extern prometheus::simpleapi::counter_metric_t udp_recv; + extern prometheus::simpleapi::counter_metric_t tcp_send; + extern prometheus::simpleapi::counter_metric_t tcp_recv; + // General Controller Metrics extern prometheus::simpleapi::gauge_metric_t network_count; extern prometheus::simpleapi::gauge_metric_t member_count; diff --git a/osdep/Phy.hpp b/osdep/Phy.hpp index 7be6546fe..1e0c92503 100644 --- a/osdep/Phy.hpp +++ b/osdep/Phy.hpp @@ -50,6 +50,8 @@ #include #include +#include "../node/Metrics.hpp" + #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) #ifndef IPV6_DONTFRAG #define IPV6_DONTFRAG 62 @@ -452,9 +454,13 @@ public: { PhySocketImpl &sws = *(reinterpret_cast(sock)); #if defined(_WIN32) || defined(_WIN64) - return ((long)::sendto(sws.sock,reinterpret_cast(data),len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len); + ssize_t sent = ((long)::sendto(sws.sock,reinterpret_cast(data),len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len); + Metrics::udp_send += sent; + return sent; #else - return ((long)::sendto(sws.sock,data,len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len); + ssize_t sent = ((long)::sendto(sws.sock,data,len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len); + Metrics::udp_send += sent; + return sent; #endif } diff --git a/service/OneService.cpp b/service/OneService.cpp index 856103260..2993cbc09 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -2470,9 +2470,11 @@ public: if (_forceTcpRelay) { return; } + Metrics::udp_recv += len; const uint64_t now = OSUtils::now(); - if ((len >= 16)&&(reinterpret_cast(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) + if ((len >= 16)&&(reinterpret_cast(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) { _lastDirectReceiveFromGlobal = now; + } const ZT_ResultCode rc = _node->processWirePacket(nullptr,now,reinterpret_cast(sock),reinterpret_cast(from),data,len,&_nextBackgroundTaskDeadline); if (ZT_ResultCode_isFatal(rc)) { char tmp[256]; @@ -2559,6 +2561,7 @@ public: { try { if (!len) return; // sanity check, should never happen + Metrics::tcp_recv += len; TcpConnection *tc = reinterpret_cast(*uptr); tc->lastReceive = OSUtils::now(); switch(tc->type) { @@ -2697,6 +2700,7 @@ public: Mutex::Lock _l(tc->writeq_m); if (tc->writeq.length() > 0) { long sent = (long)_phy.streamSend(sock,tc->writeq.data(),(unsigned long)tc->writeq.length(),true); + Metrics::tcp_send += sent; if (sent > 0) { if ((unsigned long)sent >= (unsigned long)tc->writeq.length()) { tc->writeq.clear(); @@ -3235,9 +3239,13 @@ public: // working we can instantly "fail forward" to it and stop using TCP // proxy fallback, which is slow. if ((localSocket != -1)&&(localSocket != 0)&&(_binder.isUdpSocketValid((PhySocket *)((uintptr_t)localSocket)))) { - if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl); + if ((ttl)&&(addr->ss_family == AF_INET)) { + _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl); + } const bool r = _phy.udpSend((PhySocket *)((uintptr_t)localSocket),(const struct sockaddr *)addr,data,len); - if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255); + if ((ttl)&&(addr->ss_family == AF_INET)) { + _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255); + } return ((r) ? 0 : -1); } else { return ((_binder.udpSendAll(_phy,addr,data,len,ttl)) ? 0 : -1);