From ac7b56ea0dcac85b04b6e7c7b448dc92b4e1a452 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Tue, 23 Nov 2021 13:05:52 +0400 Subject: [PATCH] feat: add ban peers metric --- comms/src/connectivity/manager.rs | 3 +++ comms/src/connectivity/metrics.rs | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/comms/src/connectivity/manager.rs b/comms/src/connectivity/manager.rs index ad75db44b0..e5c0e46bee 100644 --- a/comms/src/connectivity/manager.rs +++ b/comms/src/connectivity/manager.rs @@ -733,6 +733,9 @@ impl ConnectivityManagerActor { self.peer_manager.ban_peer_by_node_id(node_id, duration, reason).await?; + #[cfg(feature = "metrics")] + super::metrics::banned_peers_counter(node_id).inc(); + self.publish_event(ConnectivityEvent::PeerBanned(node_id.clone())); if let Some(conn) = self.pool.get_connection_mut(node_id) { diff --git a/comms/src/connectivity/metrics.rs b/comms/src/connectivity/metrics.rs index 1470b4c9c7..0cc90578d0 100644 --- a/comms/src/connectivity/metrics.rs +++ b/comms/src/connectivity/metrics.rs @@ -20,9 +20,9 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use crate::connection_manager::ConnectionDirection; +use crate::{connection_manager::ConnectionDirection, peer_manager::NodeId}; use once_cell::sync::Lazy; -use tari_metrics::{IntGauge, IntGaugeVec}; +use tari_metrics::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec}; pub fn connections(direction: ConnectionDirection) -> IntGauge { static METER: Lazy = Lazy::new(|| { @@ -43,3 +43,16 @@ pub fn uptime() -> IntGauge { METER.clone() } + +pub fn banned_peers_counter(peer: &NodeId) -> IntCounter { + static METER: Lazy = Lazy::new(|| { + tari_metrics::register_int_counter_vec( + "comms::connectivity::banned_peers", + "The number of peer bans by peer", + &["peer_id"], + ) + .unwrap() + }); + + METER.with_label_values(&[peer.to_string().as_str()]) +}