Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: only consider pre-existing peers for wg bytes metric #5357

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading