Skip to content

Commit ce7c8bc

Browse files
authored
feat: add blocked peer metrics (#708)
Addresses Issue #653 Adds a few more metrics about blocked peers. Tracks the number of blocked peers, and how many times a particular blocked peer tried to dial us. Co-Authored-By: petarjuki7 <[email protected]> Co-Authored-By: petarjuki7 <[email protected]>
1 parent 24d0801 commit ce7c8bc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

anchor/network/src/metrics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,18 @@ pub static HANDSHAKE_SUBNET_MATCHES: LazyLock<Result<IntGaugeVec>> = LazyLock::n
3636
&["match_count"],
3737
)
3838
});
39+
40+
pub static PEERS_BLOCKED: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
41+
try_create_int_gauge(
42+
"libp2p_peers_blocked",
43+
"Current count of blocked libp2p peers",
44+
)
45+
});
46+
47+
pub static PEER_BLOCKED_INBOUND_CONNECTIONS: LazyLock<Result<IntGaugeVec>> = LazyLock::new(|| {
48+
try_create_int_gauge_vec(
49+
"libp2p_blocked_peer_connection_attempts",
50+
"Count of blocked peers trying to reconnect",
51+
&["blocked_peer_id"],
52+
)
53+
});

anchor/network/src/peer_manager/blocking.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use libp2p::{
1111
};
1212
use tracing::debug;
1313

14-
use crate::scoring::peer_score_config::RETAIN_SCORE_EPOCH_MULTIPLIER;
14+
use crate::{
15+
metrics::{PEER_BLOCKED_INBOUND_CONNECTIONS, PEERS_BLOCKED},
16+
scoring::peer_score_config::RETAIN_SCORE_EPOCH_MULTIPLIER,
17+
};
1518

1619
/// Manages peer blocking functionality
1720
pub struct BlockingManager {
@@ -38,6 +41,7 @@ impl BlockingManager {
3841
self.blocked_peers_timestamps
3942
.insert(peer_id, tokio::time::Instant::now());
4043
debug!(?peer_id, "Blocked peer");
44+
metrics::inc_gauge(&PEERS_BLOCKED);
4145
true
4246
} else {
4347
false
@@ -50,6 +54,12 @@ impl BlockingManager {
5054
if was_removed {
5155
self.blocked_peers_timestamps.remove(&peer_id);
5256
debug!(?peer_id, "Unblocked peer after retain_score duration");
57+
metrics::dec_gauge(&PEERS_BLOCKED);
58+
metrics::set_gauge_vec(
59+
&PEER_BLOCKED_INBOUND_CONNECTIONS,
60+
&[&peer_id.to_base58()],
61+
0,
62+
);
5363
}
5464
was_removed
5565
}
@@ -103,6 +113,9 @@ impl BlockingManager {
103113
local_addr: &Multiaddr,
104114
remote_addr: &Multiaddr,
105115
) -> Result<(), ConnectionDenied> {
116+
if self.blocked_peers().contains(&peer) {
117+
metrics::inc_gauge_vec(&PEER_BLOCKED_INBOUND_CONNECTIONS, &[&peer.to_base58()]);
118+
}
106119
self.block_list
107120
.handle_established_inbound_connection(connection_id, peer, local_addr, remote_addr)
108121
.map(|_| ()) // Discard the handler, we just want to know if connection is allowed

0 commit comments

Comments
 (0)