Skip to content
Open
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
15 changes: 15 additions & 0 deletions anchor/network/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@ pub static HANDSHAKE_SUBNET_MATCHES: LazyLock<Result<IntGaugeVec>> = LazyLock::n
&["match_count"],
)
});

pub static PEERS_BLOCKED: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
try_create_int_gauge(
"libp2p_peers_blocked",
"Current count of blocked libp2p peers",
)
});

pub static PEER_BLOCKED_INBOUND_CONNECTIONS: LazyLock<Result<IntGaugeVec>> = LazyLock::new(|| {
try_create_int_gauge_vec(
"libp2p_blocked_peer_connection_attempts",
"Count of blocked peers trying to reconnect",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High Cardinality Concern

Using peer_id as a label creates high cardinality, which can cause performance issues in Prometheus:

  • Each unique blocked peer creates a new time series
  • In a large network, this could result in thousands of time series
  • High cardinality can slow down queries and increase memory usage

Considerations:

  1. Is per-peer granularity necessary? Consider if aggregate metrics (total blocked connection attempts) would suffice
  2. If per-peer tracking is essential, consider:
    • Implementing a limit on tracked peers
    • Using a shorter peer ID representation (first 8 chars)
    • Adding documentation about the cardinality implications
  3. Consider whether this data might be better suited for structured logs rather than metrics

The aggregate PEERS_BLOCKED gauge already tracks the count of blocked peers, which may be sufficient for most monitoring needs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if its an issue.

&["blocked_peer_id"],
)
});
15 changes: 14 additions & 1 deletion anchor/network/src/peer_manager/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use libp2p::{
};
use tracing::debug;

use crate::scoring::peer_score_config::RETAIN_SCORE_EPOCH_MULTIPLIER;
use crate::{
metrics::{PEER_BLOCKED_INBOUND_CONNECTIONS, PEERS_BLOCKED},
scoring::peer_score_config::RETAIN_SCORE_EPOCH_MULTIPLIER,
};

/// Manages peer blocking functionality
pub struct BlockingManager {
Expand All @@ -38,6 +41,7 @@ impl BlockingManager {
self.blocked_peers_timestamps
.insert(peer_id, tokio::time::Instant::now());
debug!(?peer_id, "Blocked peer");
metrics::inc_gauge(&PEERS_BLOCKED);
true
} else {
false
Expand All @@ -50,6 +54,12 @@ impl BlockingManager {
if was_removed {
self.blocked_peers_timestamps.remove(&peer_id);
debug!(?peer_id, "Unblocked peer after retain_score duration");
metrics::dec_gauge(&PEERS_BLOCKED);
metrics::set_gauge_vec(
&PEER_BLOCKED_INBOUND_CONNECTIONS,
&[&peer_id.to_base58()],
0,
);
}
was_removed
}
Expand Down Expand Up @@ -103,6 +113,9 @@ impl BlockingManager {
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<(), ConnectionDenied> {
if self.blocked_peers().contains(&peer) {
metrics::inc_gauge_vec(&PEER_BLOCKED_INBOUND_CONNECTIONS, &[&peer.to_base58()]);
}
self.block_list
.handle_established_inbound_connection(connection_id, peer, local_addr, remote_addr)
.map(|_| ()) // Discard the handler, we just want to know if connection is allowed
Expand Down