Compare commits

...

1 Commits

Author SHA1 Message Date
Jędrzej Stuczyński d1a8dd45f4 bugfix: only consider pre-existing peers for wg bytes metric 2025-01-16 10:21:05 +00:00
2 changed files with 21 additions and 12 deletions
+17 -8
View File
@@ -267,18 +267,27 @@ impl PeerController {
})) }))
} }
fn update_metrics(&self, new_host: &Host) { async fn update_metrics(&self, new_host: &Host) {
let now = SystemTime::now(); let now = SystemTime::now();
const ACTIVITY_THRESHOLD: Duration = Duration::from_secs(60); const ACTIVITY_THRESHOLD: Duration = Duration::from_secs(60);
let old_host = self.host_information.read().await;
let total_peers = new_host.peers.len(); let total_peers = new_host.peers.len();
let mut active_peers = 0; let mut active_peers = 0;
let mut total_rx = 0; let mut new_rx = 0;
let mut total_tx = 0; let mut new_tx = 0;
for peer in new_host.peers.values() { for (peer_key, peer) in new_host.peers.iter() {
total_rx += peer.rx_bytes; // only consider pre-existing peers,
total_tx += peer.tx_bytes; // so that the value would always be increasing
if let Some(prior) = old_host.peers.get(peer_key) {
let delta_rx = peer.rx_bytes.saturating_sub(prior.rx_bytes);
let delta_tx = prior.tx_bytes.saturating_sub(prior.tx_bytes);
new_rx += delta_rx;
new_tx += delta_tx;
}
// if a peer hasn't performed a handshake in last minute, // if a peer hasn't performed a handshake in last minute,
// I think it's reasonable to assume it's no longer active // I think it's reasonable to assume it's no longer active
@@ -296,10 +305,10 @@ impl PeerController {
self.metrics.wireguard.update( self.metrics.wireguard.update(
// if the conversion fails it means we're running not running on a 64bit system // if the conversion fails it means we're running not running on a 64bit system
// and that's a reason enough for this failure. // and that's a reason enough for this failure.
total_rx.try_into().expect( new_rx.try_into().expect(
"failed to convert bytes from u64 to usize - are you running on non 64bit system?", "failed to convert bytes from u64 to usize - are you running on non 64bit system?",
), ),
total_tx.try_into().expect( new_tx.try_into().expect(
"failed to convert bytes from u64 to usize - are you running on non 64bit system?", "failed to convert bytes from u64 to usize - are you running on non 64bit system?",
), ),
total_peers, total_peers,
+4 -4
View File
@@ -31,13 +31,13 @@ impl WireguardStats {
pub fn update( pub fn update(
&self, &self,
bytes_rx: usize, new_bytes_rx: usize,
bytes_tx: usize, new_bytes_tx: usize,
total_peers: usize, total_peers: usize,
active_peers: usize, active_peers: usize,
) { ) {
self.bytes_rx.store(bytes_rx, Ordering::Relaxed); self.bytes_rx.fetch_add(new_bytes_rx, Ordering::Relaxed);
self.bytes_tx.store(bytes_tx, Ordering::Relaxed); self.bytes_tx.fetch_add(new_bytes_tx, Ordering::Relaxed);
self.total_peers.store(total_peers, Ordering::Relaxed); self.total_peers.store(total_peers, Ordering::Relaxed);
self.active_peers.store(active_peers, Ordering::Relaxed); self.active_peers.store(active_peers, Ordering::Relaxed);
} }