From bfae2a16c942ea2cc542a0caf53bd091c9517acf Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Fri, 10 Jan 2025 18:08:44 +0530 Subject: [PATCH] feat(metrics): implement custom relay client metrics --- ant-networking/src/event/swarm.rs | 4 ++ ant-networking/src/metrics/mod.rs | 10 +++++ ant-networking/src/metrics/relay_client.rs | 47 ++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 ant-networking/src/metrics/relay_client.rs diff --git a/ant-networking/src/event/swarm.rs b/ant-networking/src/event/swarm.rs index b37165bd50..fbd0c4bcb1 100644 --- a/ant-networking/src/event/swarm.rs +++ b/ant-networking/src/event/swarm.rs @@ -55,6 +55,10 @@ impl SwarmDriver { self.handle_kad_event(kad_event)?; } SwarmEvent::Behaviour(NodeEvent::RelayClient(event)) => { + #[cfg(feature = "open-metrics")] + if let Some(metrics_recorder) = &self.metrics_recorder { + metrics_recorder.record(&(*event)); + } event_string = "relay_client_event"; info!(?event, "relay client event"); diff --git a/ant-networking/src/metrics/mod.rs b/ant-networking/src/metrics/mod.rs index 2cc80fe424..71d7f8fdbb 100644 --- a/ant-networking/src/metrics/mod.rs +++ b/ant-networking/src/metrics/mod.rs @@ -8,6 +8,7 @@ // Implementation to record `libp2p::upnp::Event` metrics mod bad_node; +mod relay_client; pub mod service; #[cfg(feature = "upnp")] mod upnp; @@ -39,6 +40,7 @@ pub(crate) struct NetworkMetricsRecorder { libp2p_metrics: Libp2pMetrics, #[cfg(feature = "upnp")] upnp_events: Family, + relay_client_events: Family, // metrics from ant-networking pub(crate) connected_peers: Gauge, @@ -136,6 +138,13 @@ impl NetworkMetricsRecorder { upnp_events.clone(), ); + let relay_client_events = Family::default(); + sub_registry.register( + "relay_client_events", + "Events emitted by the relay client", + relay_client_events.clone(), + ); + let process_memory_used_mb = Gauge::::default(); sub_registry.register( "process_memory_used_mb", @@ -211,6 +220,7 @@ impl NetworkMetricsRecorder { libp2p_metrics, #[cfg(feature = "upnp")] upnp_events, + relay_client_events, records_stored, estimated_network_size, diff --git a/ant-networking/src/metrics/relay_client.rs b/ant-networking/src/metrics/relay_client.rs new file mode 100644 index 0000000000..cd03822619 --- /dev/null +++ b/ant-networking/src/metrics/relay_client.rs @@ -0,0 +1,47 @@ +// Copyright 2024 MaidSafe.net limited. +// +// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. +// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed +// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. Please review the Licences for the specific language governing +// permissions and limitations relating to use of the SAFE Network Software. + +use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue}; + +#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)] +pub(crate) struct RelayClientEventLabels { + event: EventType, +} + +#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)] +enum EventType { + ReservationReqAccepted, + OutboundCircuitEstablished, + InboundCircuitEstablished, +} + +impl From<&libp2p::relay::client::Event> for EventType { + fn from(event: &libp2p::relay::client::Event) -> Self { + match event { + libp2p::relay::client::Event::ReservationReqAccepted { .. } => { + EventType::ReservationReqAccepted + } + libp2p::relay::client::Event::OutboundCircuitEstablished { .. } => { + EventType::OutboundCircuitEstablished + } + libp2p::relay::client::Event::InboundCircuitEstablished { .. } => { + EventType::InboundCircuitEstablished + } + } + } +} + +impl super::Recorder for super::NetworkMetricsRecorder { + fn record(&self, event: &libp2p::relay::client::Event) { + self.relay_client_events + .get_or_create(&RelayClientEventLabels { + event: event.into(), + }) + .inc(); + } +}