2021-08-13 22:51:54 +02:00
|
|
|
// Copyright 2021 Protocol Labs.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2023-01-03 20:42:32 +01:00
|
|
|
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
|
2022-02-03 11:31:41 +01:00
|
|
|
use prometheus_client::metrics::counter::Counter;
|
|
|
|
use prometheus_client::metrics::family::Family;
|
|
|
|
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
|
|
|
|
use prometheus_client::registry::{Registry, Unit};
|
2021-08-13 22:51:54 +02:00
|
|
|
|
2023-01-03 20:42:32 +01:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
|
2021-08-13 22:51:54 +02:00
|
|
|
struct FailureLabels {
|
|
|
|
reason: Failure,
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:19:34 +10:00
|
|
|
impl From<&libp2p_ping::Failure> for FailureLabels {
|
|
|
|
fn from(failure: &libp2p_ping::Failure) -> Self {
|
2021-08-13 22:51:54 +02:00
|
|
|
match failure {
|
2022-10-01 00:19:34 +10:00
|
|
|
libp2p_ping::Failure::Timeout => FailureLabels {
|
2021-08-13 22:51:54 +02:00
|
|
|
reason: Failure::Timeout,
|
|
|
|
},
|
2022-10-01 00:19:34 +10:00
|
|
|
libp2p_ping::Failure::Unsupported => FailureLabels {
|
2021-08-13 22:51:54 +02:00
|
|
|
reason: Failure::Unsupported,
|
|
|
|
},
|
2022-10-01 00:19:34 +10:00
|
|
|
libp2p_ping::Failure::Other { .. } => FailureLabels {
|
2021-08-13 22:51:54 +02:00
|
|
|
reason: Failure::Other,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 20:42:32 +01:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelValue, Debug)]
|
2021-08-13 22:51:54 +02:00
|
|
|
enum Failure {
|
|
|
|
Timeout,
|
|
|
|
Unsupported,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2023-04-26 09:31:56 +02:00
|
|
|
pub(crate) struct Metrics {
|
2021-08-13 22:51:54 +02:00
|
|
|
rtt: Histogram,
|
|
|
|
failure: Family<FailureLabels, Counter>,
|
|
|
|
pong_received: Counter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metrics {
|
2023-04-26 09:31:56 +02:00
|
|
|
pub(crate) fn new(registry: &mut Registry) -> Self {
|
2021-08-13 22:51:54 +02:00
|
|
|
let sub_registry = registry.sub_registry_with_prefix("ping");
|
|
|
|
|
|
|
|
let rtt = Histogram::new(exponential_buckets(0.001, 2.0, 12));
|
|
|
|
sub_registry.register_with_unit(
|
|
|
|
"rtt",
|
|
|
|
"Round-trip time sending a 'ping' and receiving a 'pong'",
|
|
|
|
Unit::Seconds,
|
2023-01-03 20:42:32 +01:00
|
|
|
rtt.clone(),
|
2021-08-13 22:51:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let failure = Family::default();
|
|
|
|
sub_registry.register(
|
|
|
|
"failure",
|
|
|
|
"Failure while sending a 'ping' or receiving a 'pong'",
|
2023-01-03 20:42:32 +01:00
|
|
|
failure.clone(),
|
2021-08-13 22:51:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let pong_received = Counter::default();
|
|
|
|
sub_registry.register(
|
|
|
|
"pong_received",
|
|
|
|
"Number of 'pong's received",
|
2023-01-03 20:42:32 +01:00
|
|
|
pong_received.clone(),
|
2021-08-13 22:51:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
rtt,
|
|
|
|
failure,
|
|
|
|
pong_received,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:19:34 +10:00
|
|
|
impl super::Recorder<libp2p_ping::Event> for Metrics {
|
|
|
|
fn record(&self, event: &libp2p_ping::Event) {
|
2021-08-13 22:51:54 +02:00
|
|
|
match &event.result {
|
2022-10-01 00:19:34 +10:00
|
|
|
Ok(libp2p_ping::Success::Pong) => {
|
2022-07-15 09:16:03 +02:00
|
|
|
self.pong_received.inc();
|
2021-08-13 22:51:54 +02:00
|
|
|
}
|
2022-10-01 00:19:34 +10:00
|
|
|
Ok(libp2p_ping::Success::Ping { rtt }) => {
|
2022-07-15 09:16:03 +02:00
|
|
|
self.rtt.observe(rtt.as_secs_f64());
|
2021-08-13 22:51:54 +02:00
|
|
|
}
|
|
|
|
Err(failure) => {
|
2022-07-15 09:16:03 +02:00
|
|
|
self.failure.get_or_create(&failure.into()).inc();
|
2021-08-13 22:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|