Skip to content

Commit

Permalink
feat(metrics): implement custom relay client metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Jan 10, 2025
1 parent adb81ad commit bfae2a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ant-networking/src/event/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 10 additions & 0 deletions ant-networking/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,7 @@ pub(crate) struct NetworkMetricsRecorder {
libp2p_metrics: Libp2pMetrics,
#[cfg(feature = "upnp")]
upnp_events: Family<upnp::UpnpEventLabels, Counter>,
relay_client_events: Family<relay_client::RelayClientEventLabels, Counter>,

// metrics from ant-networking
pub(crate) connected_peers: Gauge,
Expand Down Expand Up @@ -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::<f64, AtomicU64>::default();
sub_registry.register(
"process_memory_used_mb",
Expand Down Expand Up @@ -211,6 +220,7 @@ impl NetworkMetricsRecorder {
libp2p_metrics,
#[cfg(feature = "upnp")]
upnp_events,
relay_client_events,

records_stored,
estimated_network_size,
Expand Down
47 changes: 47 additions & 0 deletions ant-networking/src/metrics/relay_client.rs
Original file line number Diff line number Diff line change
@@ -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<libp2p::relay::client::Event> for super::NetworkMetricsRecorder {
fn record(&self, event: &libp2p::relay::client::Event) {
self.relay_client_events
.get_or_create(&RelayClientEventLabels {
event: event.into(),
})
.inc();
}
}

0 comments on commit bfae2a1

Please sign in to comment.