misc/metrics: Track # connected nodes supporting specific protocol (#2734)

* misc/metrics: Explicitly delegate event recording to each recorder

This allows delegating a single event to multiple `Recorder`s. That enables e.g. the
`identify::Metrics` `Recorder` to act both on `IdentifyEvent` and `SwarmEvent`. The latter enables
it to garbage collect per peer data on disconnects.

* protocols/dcutr: Expose PROTOCOL_NAME

* protocols/identify: Expose PROTOCOL_NAME and PUSH_PROTOCOL_NAME

* protocols/ping: Expose PROTOCOL_NAME

* protocols/relay: Expose HOP_PROTOCOL_NAME and STOP_PROTOCOL_NAME

* misc/metrics: Track # connected nodes supporting specific protocol

An example metric exposed with this patch:

```
libp2p_identify_protocols{protocol="/ipfs/ping/1.0.0"} 10
```

This implies that 10 of the currently connected nodes support the ping protocol.
This commit is contained in:
Max Inden
2022-07-15 09:16:03 +02:00
committed by GitHub
parent 7c8a97739f
commit d4f8ec2d48
21 changed files with 259 additions and 78 deletions

View File

@ -138,34 +138,29 @@ impl Metrics {
}
}
impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>>
for super::Metrics
{
impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
match event {
libp2p_swarm::SwarmEvent::Behaviour(_) => {}
libp2p_swarm::SwarmEvent::ConnectionEstablished { endpoint, .. } => {
self.swarm
.connections_established
self.connections_established
.get_or_create(&ConnectionEstablishedLabels {
role: endpoint.into(),
})
.inc();
}
libp2p_swarm::SwarmEvent::ConnectionClosed { endpoint, .. } => {
self.swarm
.connections_closed
self.connections_closed
.get_or_create(&ConnectionClosedLabels {
role: endpoint.into(),
})
.inc();
}
libp2p_swarm::SwarmEvent::IncomingConnection { .. } => {
self.swarm.connections_incoming.inc();
self.connections_incoming.inc();
}
libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => {
self.swarm
.connections_incoming_error
self.connections_incoming_error
.get_or_create(&IncomingConnectionErrorLabels {
error: error.into(),
})
@ -178,8 +173,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
};
let record = |error| {
self.swarm
.outgoing_connection_error
self.outgoing_connection_error
.get_or_create(&OutgoingConnectionErrorLabels { peer, error })
.inc();
};
@ -228,22 +222,22 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
};
}
libp2p_swarm::SwarmEvent::BannedPeer { .. } => {
self.swarm.connected_to_banned_peer.inc();
self.connected_to_banned_peer.inc();
}
libp2p_swarm::SwarmEvent::NewListenAddr { .. } => {
self.swarm.new_listen_addr.inc();
self.new_listen_addr.inc();
}
libp2p_swarm::SwarmEvent::ExpiredListenAddr { .. } => {
self.swarm.expired_listen_addr.inc();
self.expired_listen_addr.inc();
}
libp2p_swarm::SwarmEvent::ListenerClosed { .. } => {
self.swarm.listener_closed.inc();
self.listener_closed.inc();
}
libp2p_swarm::SwarmEvent::ListenerError { .. } => {
self.swarm.listener_error.inc();
self.listener_error.inc();
}
libp2p_swarm::SwarmEvent::Dialing(_) => {
self.swarm.dial_attempt.inc();
self.dial_attempt.inc();
}
}
}