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

misc/metrics: Add protocols label to address-specific metrics #2982

Merged
merged 27 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d7c0f89
Getting started
John-LittleBearLabs Oct 4, 2022
3345407
Merge remote-tracking branch 'jt/master' into addrstak_metric
John-LittleBearLabs Oct 4, 2022
32d5eb0
Merge remote-tracking branch 'origin/master' into addrstak_metric
John-LittleBearLabs Oct 7, 2022
d815970
Post merge fixup and re-check.
John-LittleBearLabs Oct 7, 2022
14e906a
Pulling protocol_stack functionality into this repo, by request.
John-LittleBearLabs Oct 11, 2022
808c439
A couple of suggestions from PR
John-LittleBearLabs Oct 13, 2022
4842a64
Merge remote-tracking branch 'origin/master' into addrstak_metric
John-LittleBearLabs Oct 13, 2022
9c6b300
Label wrapper
John-LittleBearLabs Oct 14, 2022
f165e01
MultiaddrExt
John-LittleBearLabs Oct 14, 2022
b2c74ed
not From
John-LittleBearLabs Oct 14, 2022
50c8bb5
cargo fmt
John-LittleBearLabs Oct 14, 2022
d74e7e5
clippy
John-LittleBearLabs Oct 14, 2022
e5974ca
changelog
John-LittleBearLabs Oct 14, 2022
1d13413
Merge remote-tracking branch 'origin/master' into addrstak_metric
John-LittleBearLabs Oct 17, 2022
3900f69
Version bump.
John-LittleBearLabs Oct 17, 2022
9a227ca
PR comment
John-LittleBearLabs Oct 19, 2022
b618cf8
Merge remote-tracking branch 'origin/master' into addrstak_metric
John-LittleBearLabs Nov 2, 2022
16c802b
A couple PR comments.
John-LittleBearLabs Nov 2, 2022
6a5a593
Use protocol stack for a few more metrics.
John-LittleBearLabs Nov 3, 2022
6983a59
metric label naming consistency
John-LittleBearLabs Nov 4, 2022
c8bcc82
remove impl From
John-LittleBearLabs Nov 4, 2022
ece8c9b
PR comments & clippy
John-LittleBearLabs Nov 8, 2022
db1bcdc
Merge from master, update protocol_stack to use upstream. Should have…
John-LittleBearLabs Nov 14, 2022
0f6bdd1
Fix a clippy and a no-default-feature use stmt
John-LittleBearLabs Nov 14, 2022
f787368
Move `Labels` to usage site
thomaseizinger Nov 15, 2022
0d3ec5b
Merge branch 'master' into addrstak_metric
mergify[bot] Nov 15, 2022
7176f14
Merge branch 'master' into addrstak_metric
thomaseizinger Nov 15, 2022
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ libp2p-swarm-derive = { version = "0.30.1", path = "swarm-derive" }
libp2p-uds = { version = "0.36.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.37.0", path = "transports/wasm-ext", optional = true }
libp2p-yamux = { version = "0.41.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.14.0" }
multiaddr = { path = "../rust-multiaddr" }
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
parking_lot = "0.12.0"
pin-project = "1.0.0"
smallvec = "1.6.1"


[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.37.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.37.0", path = "transports/dns", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ instant = "0.1.11"
lazy_static = "1.2"
libsecp256k1 = { version = "0.7.0", optional = true }
log = "0.4"
multiaddr = { version = "0.14.0" }
multiaddr = { path = "../../rust-multiaddr" }
multihash = { version = "0.16", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] }
multistream-select = { version = "0.12", path = "../misc/multistream-select" }
p256 = { version = "0.11.1", default-features = false, features = ["ecdsa"], optional = true }
Expand Down
8 changes: 4 additions & 4 deletions misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ dcutr = ["libp2p-dcutr"]
[dependencies]
libp2p-core = { version = "0.37.0", path = "../../core" }
libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true }
libp2p-identify = { version = "0.40.0", path = "../../protocols/identify", optional = true }
libp2p-identify = { path = "../../protocols/identify", optional = true }
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true }
libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true }
libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true }
libp2p-swarm = { version = "0.40.0", path = "../../swarm" }
libp2p-ping = { path = "../../protocols/ping", optional = true }
libp2p-relay = { path = "../../protocols/relay", optional = true }
libp2p-swarm = { path = "../../swarm" }
prometheus-client = "0.18.0"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
Expand Down
27 changes: 23 additions & 4 deletions misc/metrics/examples/metrics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use futures::stream::StreamExt;
use libp2p::core::Multiaddr;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::swarm::SwarmEvent;
use libp2p::{identity, ping, NetworkBehaviour, PeerId, Swarm};
use libp2p::{identify, identity, ping, NetworkBehaviour, PeerId, Swarm};
use libp2p_swarm::keep_alive;
use log::info;
use prometheus_client::registry::Registry;
Expand All @@ -71,8 +71,8 @@ fn main() -> Result<(), Box<dyn Error>> {
info!("Local peer id: {:?}", local_peer_id);

let mut swarm = Swarm::new(
block_on(libp2p::development_transport(local_key))?,
Behaviour::default(),
block_on(libp2p::development_transport(local_key.clone()))?,
Behaviour::new(local_key),
local_peer_id,
);

Expand All @@ -95,6 +95,10 @@ fn main() -> Result<(), Box<dyn Error>> {
info!("{:?}", ping_event);
metrics.record(&ping_event);
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify_event)) => {
info!("{:?}", identify_event);
metrics.record(&identify_event);
}
swarm_event => {
info!("{:?}", swarm_event);
metrics.record(&swarm_event);
Expand All @@ -109,8 +113,23 @@ fn main() -> Result<(), Box<dyn Error>> {
///
/// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen
/// and can be observed via the metrics.
#[derive(NetworkBehaviour, Default)]
#[derive(NetworkBehaviour)]
// #[behaviour(out_event = "Event")]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
struct Behaviour {
identify: identify::Behaviour,
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}

impl Behaviour{
fn new(local_key: libp2p::identity::Keypair) -> Self {
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
Self{
ping: ping::Behaviour::default(),
identify: identify::Behaviour::new( identify::Config::new(
"/ipfs/0.1.0".into(),
local_key.public(),
)),
keep_alive: keep_alive::Behaviour::default(),
}
}
}
14 changes: 14 additions & 0 deletions misc/metrics/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use prometheus_client::registry::Registry;
use std::collections::HashMap;
use std::iter;
use std::sync::{Arc, Mutex};
use prometheus_client::metrics::family::Family;

pub struct Metrics {
protocols: Protocols,
Expand All @@ -36,6 +37,7 @@ pub struct Metrics {
received_info_listen_addrs: Histogram,
received_info_protocols: Histogram,
sent: Counter,
listen_addresses: Family<Vec<&'static str>, Counter>,
}

impl Metrics {
Expand Down Expand Up @@ -100,6 +102,13 @@ impl Metrics {
Box::new(sent.clone()),
);

let listen_addresses = Family::default();
sub_registry.register(
"listen_addresses",
"Number of listen addresses for remote peer per protocol stack",
Box::new(listen_addresses.clone())
);

Self {
protocols,
error,
Expand All @@ -108,6 +117,7 @@ impl Metrics {
received_info_listen_addrs,
received_info_protocols,
sent,
listen_addresses
}
}
}
Expand Down Expand Up @@ -167,6 +177,10 @@ impl super::Recorder<libp2p_identify::Event> for Metrics {
.observe(info.protocols.len() as f64);
self.received_info_listen_addrs
.observe(info.listen_addrs.len() as f64);
for listen_addr in &info.listen_addrs {
let key : Vec<&'static str> = listen_addr.protocol_stack().collect();
self.listen_addresses.get_or_create(&key).inc();
}
}
libp2p_identify::Event::Sent { .. } => {
self.sent.inc();
Expand Down
12 changes: 7 additions & 5 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;

pub struct Metrics {
connections_incoming: Counter,
connections_incoming: Family<Vec<&'static str>, Counter>,
connections_incoming_error: Family<IncomingConnectionErrorLabels, Counter>,

connections_established: Family<ConnectionEstablishedLabels, Counter>,
Expand All @@ -45,10 +45,10 @@ impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("swarm");

let connections_incoming = Counter::default();
let connections_incoming = Family::default();
sub_registry.register(
"connections_incoming",
"Number of incoming connections",
"Number of incoming connections per address stack",
Box::new(connections_incoming.clone()),
);

Expand Down Expand Up @@ -156,8 +156,9 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
})
.inc();
}
libp2p_swarm::SwarmEvent::IncomingConnection { .. } => {
self.connections_incoming.inc();
libp2p_swarm::SwarmEvent::IncomingConnection { send_back_addr, .. } => {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
let labels : Vec<&'static str> = send_back_addr.protocol_stack().collect();
self.connections_incoming.get_or_create(&labels).inc();
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
}
libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => {
self.connections_incoming_error
Expand Down Expand Up @@ -246,6 +247,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
#[derive(Encode, Hash, Clone, Eq, PartialEq)]
struct ConnectionEstablishedLabels {
role: Role,

John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
Expand Down