Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: Expose litep2p metrics in an agnostic manner #294

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2245703
cargo: Add metrics feature flag with prometheus
lexnv Nov 29, 2024
c6154da
litep2p/config: Expose prometheus metrics in config
lexnv Nov 29, 2024
f3948c1
cargo: Make litep2p agnostic of metrics
lexnv Nov 29, 2024
31144bf
metrics: Add abstraction layers over metrics
lexnv Nov 29, 2024
77332aa
Expose metrics to litep2p transport layers
lexnv Nov 29, 2024
3ec010c
tcp: Extend collected metrics at intervals for code simplicity
lexnv Nov 29, 2024
5936083
tcp: Selectively poll interval if the metrics are enabled
lexnv Nov 29, 2024
c68ef20
metric: Extend Gauge methods with inc / dec
lexnv Nov 29, 2024
907312f
metrics: Add scope RAII metric counter
lexnv Nov 29, 2024
4362919
metrics: Make register methods less restrictive
lexnv Nov 29, 2024
5d91741
tcp: Extend with connection metrics
lexnv Nov 29, 2024
05f5741
websocket: Add specific metrics
lexnv Nov 29, 2024
f6d478d
req-resp: Propagate metrics
lexnv Nov 29, 2024
b5e80e2
notifications: Expose metrics
lexnv Nov 29, 2024
fc70eea
ping: Add metrics
lexnv Nov 29, 2024
b91b8a6
identify: Propagate metrics
lexnv Nov 29, 2024
48cb7ca
kad: Add metrics
lexnv Nov 29, 2024
27042cb
transport/manager: Add metrics
lexnv Nov 29, 2024
2de8a68
Simplify metrics collection
lexnv Nov 29, 2024
142e08f
tests: Adjust testing
lexnv Nov 29, 2024
7079ade
protocol: To metric name string
lexnv Nov 29, 2024
e543853
transport: Simplify collection
lexnv Nov 29, 2024
17786b6
tcp: Decrement metrics on connection closed
lexnv Dec 3, 2024
e43ee71
websocket: Decrement metrics on connection closed
lexnv Dec 3, 2024
fd084c8
websocket: Use ref mut self
lexnv Dec 3, 2024
09bd786
websocket: Fix clone move
lexnv Dec 3, 2024
cbc273d
metrics: Add MeteredFuturesStream
lexnv Dec 3, 2024
deb8b1a
metrics: Use MeteredFuturesStream for tcp and websocket
lexnv Dec 3, 2024
cf3faf1
Merge remote-tracking branch 'origin/master' into lexnv/metrics
lexnv Dec 3, 2024
125ee19
Fix docs
lexnv Dec 3, 2024
8ccf710
Merge branch 'master' into lexnv/metrics
lexnv Dec 3, 2024
dfd4b0d
Merge remote-tracking branch 'origin/master' into lexnv/metrics
lexnv Dec 11, 2024
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 src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use crate::{
crypto::ed25519::Keypair,
executor::{DefaultExecutor, Executor},
metrics::MetricsRegistry,
protocol::{
libp2p::{bitswap, identify, kademlia, ping},
mdns::Config as MdnsConfig,
Expand Down Expand Up @@ -83,6 +84,9 @@ pub struct ConfigBuilder {
#[cfg(feature = "websocket")]
websocket: Option<WebSocketConfig>,

/// Metrics registry
metrics_registry: Option<MetricsRegistry>,

/// Keypair.
keypair: Option<Keypair>,

Expand Down Expand Up @@ -143,6 +147,7 @@ impl ConfigBuilder {
webrtc: None,
#[cfg(feature = "websocket")]
websocket: None,
metrics_registry: None,
keypair: None,
ping: None,
identify: None,
Expand Down Expand Up @@ -187,6 +192,12 @@ impl ConfigBuilder {
self
}

/// Add metrics registry.
pub fn with_metrics_registry(mut self, registry: MetricsRegistry) -> Self {
self.metrics_registry = Some(registry);
self
}

/// Add keypair.
///
/// If no keypair is specified, litep2p creates a new keypair.
Expand Down Expand Up @@ -295,6 +306,7 @@ impl ConfigBuilder {
webrtc: self.webrtc.take(),
#[cfg(feature = "websocket")]
websocket: self.websocket.take(),
metrics_registry: self.metrics_registry.take(),
ping: self.ping.take(),
identify: self.identify.take(),
kademlia: self.kademlia.take(),
Expand Down Expand Up @@ -328,6 +340,9 @@ pub struct Litep2pConfig {
#[cfg(feature = "websocket")]
pub(crate) websocket: Option<WebSocketConfig>,

/// Prometheus metrics registry.
pub(crate) metrics_registry: Option<MetricsRegistry>,

/// Keypair.
pub(crate) keypair: Keypair,

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub enum Error {
ConnectionLimit(ConnectionLimitsError),
#[error("Failed to dial peer immediately")]
ImmediateDialError(#[from] ImmediateDialError),
#[error("Invalid metric: `{0}`")]
MetricError(String),
}

/// Error type for address parsing.
Expand Down
77 changes: 59 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub mod config;
pub mod crypto;
pub mod error;
pub mod executor;
pub mod metrics;
pub mod protocol;
pub mod substream;
pub mod transport;
Expand Down Expand Up @@ -164,7 +165,8 @@ impl Litep2p {
bandwidth_sink.clone(),
litep2p_config.max_parallel_dials,
litep2p_config.connection_limits,
);
litep2p_config.metrics_registry.clone(),
)?;

// add known addresses to `TransportManager`, if any exist
if !litep2p_config.known_addresses.is_empty() {
Expand All @@ -188,9 +190,14 @@ impl Litep2p {
litep2p_config.keep_alive_timeout,
);
let executor = Arc::clone(&litep2p_config.executor);
litep2p_config.executor.run(Box::pin(async move {
NotificationProtocol::new(service, config, executor).run().await
}));
let notification = NotificationProtocol::new(
service,
config,
executor,
litep2p_config.metrics_registry.clone(),
)?;

litep2p_config.executor.run(Box::pin(async move { notification.run().await }));
}

// start request-response protocol event loops
Expand All @@ -207,9 +214,15 @@ impl Litep2p {
config.codec,
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
RequestResponseProtocol::new(service, config).run().await
}));
let request_response = RequestResponseProtocol::new(
service,
config,
litep2p_config.metrics_registry.clone(),
)?;

litep2p_config
.executor
.run(Box::pin(async move { request_response.run().await }));
}

// start user protocol event loops
Expand Down Expand Up @@ -241,9 +254,13 @@ impl Litep2p {
ping_config.codec,
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
Ping::new(service, ping_config).run().await
}));
let ping = Ping::new(
service,
ping_config,
litep2p_config.metrics_registry.clone(),
)?;

litep2p_config.executor.run(Box::pin(async move { ping.run().await }));
}

// start kademlia protocol event loop if enabled
Expand All @@ -264,8 +281,14 @@ impl Litep2p {
kademlia_config.codec,
litep2p_config.keep_alive_timeout,
);
let kad = Kademlia::new(
service,
kademlia_config,
litep2p_config.metrics_registry.clone(),
)?;

litep2p_config.executor.run(Box::pin(async move {
let _ = Kademlia::new(service, kademlia_config).run().await;
let _ = kad.run().await;
}));
}

Expand Down Expand Up @@ -313,8 +336,11 @@ impl Litep2p {
// enable tcp transport if the config exists
if let Some(config) = litep2p_config.tcp.take() {
let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor));
let (transport, transport_listen_addresses) =
<TcpTransport as TransportBuilder>::new(handle, config)?;
let (transport, transport_listen_addresses) = <TcpTransport as TransportBuilder>::new(
handle,
config,
litep2p_config.metrics_registry.clone(),
)?;

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
Expand All @@ -330,8 +356,11 @@ impl Litep2p {
#[cfg(feature = "quic")]
if let Some(config) = litep2p_config.quic.take() {
let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor));
let (transport, transport_listen_addresses) =
<QuicTransport as TransportBuilder>::new(handle, config)?;
let (transport, transport_listen_addresses) = <QuicTransport as TransportBuilder>::new(
handle,
config,
litep2p_config.metrics_registry.clone(),
)?;

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
Expand All @@ -348,7 +377,11 @@ impl Litep2p {
if let Some(config) = litep2p_config.webrtc.take() {
let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor));
let (transport, transport_listen_addresses) =
<WebRtcTransport as TransportBuilder>::new(handle, config)?;
<WebRtcTransport as TransportBuilder>::new(
handle,
config,
litep2p_config.metrics_registry.clone(),
)?;

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
Expand All @@ -365,7 +398,11 @@ impl Litep2p {
if let Some(config) = litep2p_config.websocket.take() {
let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor));
let (transport, transport_listen_addresses) =
<WebSocketTransport as TransportBuilder>::new(handle, config)?;
<WebSocketTransport as TransportBuilder>::new(
handle,
config,
litep2p_config.metrics_registry.clone(),
)?;

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
Expand All @@ -390,7 +427,11 @@ impl Litep2p {
// if identify was enabled, give it the enabled protocols and listen addresses and start it
if let Some((service, mut identify_config)) = identify_info.take() {
identify_config.protocols = transport_manager.protocols().cloned().collect();
let identify = Identify::new(service, identify_config);
let identify = Identify::new(
service,
identify_config,
litep2p_config.metrics_registry.clone(),
)?;

litep2p_config.executor.run(Box::pin(async move {
let _ = identify.run().await;
Expand Down
150 changes: 150 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// Copyright 2024 litep2p developers
//
// 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.

//! A generic module for handling the metrics exposed by litep2p.
//!
//! Contains the traits and types that are used to define and interact with metrics.

use crate::{utils::futures_stream::FuturesStream, Error};
use futures::{Stream, StreamExt};
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};

pub type MetricCounter = Arc<dyn MetricCounterT>;

pub type MetricGauge = Arc<dyn MetricGaugeT>;

pub type MetricsRegistry = Arc<dyn MetricsRegistryT>;

/// Represents a metric that can only go up.
pub trait MetricCounterT: Send + Sync {
/// Increment the counter by `value`.
fn inc(&self, value: u64);
}

/// Represents a metric that can arbitrarily go up and down.
pub trait MetricGaugeT: Send + Sync {
/// Set the gauge to `value`.
fn set(&self, value: u64);

/// Increment the gauge.
fn inc(&self);

/// Decrement the gauge.
fn dec(&self);

/// Add `value` to the gauge.
fn add(&self, value: u64);

/// Subtract `value` from the gauge.
fn sub(&self, value: u64);
}

/// A registry for metrics.
pub trait MetricsRegistryT: Send + Sync {
/// Register a new counter.
fn register_counter(&self, name: String, help: String) -> Result<MetricCounter, Error>;

/// Register a new gauge.
fn register_gauge(&self, name: String, help: String) -> Result<MetricGauge, Error>;
}

/// A scope for metrics that modifies a provided gauge in an RAII fashion.
///
/// The gauge is incremented when constructed and decremented when the object is dropped.
#[derive(Clone)]
pub struct ScopeGaugeMetric {
inner: MetricGauge,
}

impl ScopeGaugeMetric {
/// Create a new [`ScopeGaugeMetric`].
pub fn new(inner: MetricGauge) -> Self {
inner.inc();
ScopeGaugeMetric { inner }
}
}

impl Drop for ScopeGaugeMetric {
fn drop(&mut self) {
self.inner.dec();
}
}

/// Wrapper around `FuturesStream` that provides information to the given metric.
#[derive(Default)]
pub struct MeteredFuturesStream<F> {
stream: FuturesStream<F>,
metric: Option<MetricGauge>,
}

impl<F> MeteredFuturesStream<F> {
pub fn new(metric: Option<MetricGauge>) -> Self {
MeteredFuturesStream {
stream: FuturesStream::new(),
metric,
}
}

pub fn push(&mut self, future: F) {
if let Some(ref metric) = self.metric {
metric.inc();
}

self.stream.push(future);
}

/// Number of futures in the stream.
pub fn len(&self) -> usize {
self.stream.len()
}

/// Returns `true` if the stream is empty.
pub fn is_empty(&self) -> bool {
self.stream.len() == 0
}
}

impl<F: Future> Stream for MeteredFuturesStream<F> {
type Item = <F as Future>::Output;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let result = self.stream.poll_next_unpin(cx);
if result.is_ready() {
if let Some(ref metric) = self.metric {
metric.dec();
}
}
result
}
}

impl<F> Drop for MeteredFuturesStream<F> {
fn drop(&mut self) {
if let Some(ref metric) = self.metric {
metric.sub(self.len() as u64);
}
}
}
Loading
Loading