Skip to content

Commit

Permalink
bugfix: only consider pre-existing peers for wg bytes metric
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Jan 16, 2025
1 parent c202e2d commit 41d856f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
27 changes: 18 additions & 9 deletions common/wireguard/src/peer_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
const ACTIVITY_THRESHOLD: Duration = Duration::from_secs(60);

let old_host = self.host_information.read().await;

let total_peers = new_host.peers.len();
let mut active_peers = 0;
let mut total_rx = 0;
let mut total_tx = 0;
let mut new_rx = 0;
let mut new_tx = 0;

for (peer_key, peer) in new_host.peers.iter() {
// only consider pre-existing peers,
// 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);

for peer in new_host.peers.values() {
total_rx += peer.rx_bytes;
total_tx += peer.tx_bytes;
new_rx += delta_rx;
new_tx += delta_tx;
}

// if a peer hasn't performed a handshake in last minute,
// I think it's reasonable to assume it's no longer active
Expand All @@ -296,10 +305,10 @@ impl PeerController {
self.metrics.wireguard.update(
// if the conversion fails it means we're running not running on a 64bit system
// 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?",
),
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?",
),
total_peers,
Expand All @@ -316,7 +325,7 @@ impl PeerController {
log::error!("Can't read wireguard kernel data");
continue;
};
self.update_metrics(&host);
self.update_metrics(&host).await;

*self.host_information.write().await = host;
}
Expand Down
8 changes: 4 additions & 4 deletions nym-node/nym-node-metrics/src/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ impl WireguardStats {

pub fn update(
&self,
bytes_rx: usize,
bytes_tx: usize,
new_bytes_rx: usize,
new_bytes_tx: usize,
total_peers: usize,
active_peers: usize,
) {
self.bytes_rx.store(bytes_rx, Ordering::Relaxed);
self.bytes_tx.store(bytes_tx, Ordering::Relaxed);
self.bytes_rx.fetch_add(new_bytes_rx, Ordering::Relaxed);
self.bytes_tx.fetch_add(new_bytes_tx, Ordering::Relaxed);
self.total_peers.store(total_peers, Ordering::Relaxed);
self.active_peers.store(active_peers, Ordering::Relaxed);
}
Expand Down

0 comments on commit 41d856f

Please sign in to comment.