misc/metrics/src/swarm: Expose role on connections_closed (#2220)

Expose whether closed connection was a Dialer or Listener.
This commit is contained in:
Max Inden
2021-09-09 15:35:45 +02:00
committed by GitHub
parent 9b1f405012
commit fcb2f62752

View File

@ -27,8 +27,8 @@ pub struct Metrics {
connections_incoming: Counter, connections_incoming: Counter,
connections_incoming_error: Family<IncomingConnectionErrorLabels, Counter>, connections_incoming_error: Family<IncomingConnectionErrorLabels, Counter>,
connections_established: Family<ConnectionEstablishedLabeles, Counter>, connections_established: Family<ConnectionEstablishedLabels, Counter>,
connections_closed: Counter, connections_closed: Family<ConnectionClosedLabels, Counter>,
new_listen_addr: Counter, new_listen_addr: Counter,
expired_listen_addr: Counter, expired_listen_addr: Counter,
@ -115,7 +115,7 @@ impl Metrics {
Box::new(connections_established.clone()), Box::new(connections_established.clone()),
); );
let connections_closed = Counter::default(); let connections_closed = Family::default();
sub_registry.register( sub_registry.register(
"connections_closed", "connections_closed",
"Number of connections closed", "Number of connections closed",
@ -147,13 +147,18 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
libp2p_swarm::SwarmEvent::ConnectionEstablished { endpoint, .. } => { libp2p_swarm::SwarmEvent::ConnectionEstablished { endpoint, .. } => {
self.swarm self.swarm
.connections_established .connections_established
.get_or_create(&ConnectionEstablishedLabeles { .get_or_create(&ConnectionEstablishedLabels {
role: endpoint.into(), role: endpoint.into(),
}) })
.inc(); .inc();
} }
libp2p_swarm::SwarmEvent::ConnectionClosed { .. } => { libp2p_swarm::SwarmEvent::ConnectionClosed { endpoint, .. } => {
self.swarm.connections_closed.inc(); self.swarm
.connections_closed
.get_or_create(&ConnectionClosedLabels {
role: endpoint.into(),
})
.inc();
} }
libp2p_swarm::SwarmEvent::IncomingConnection { .. } => { libp2p_swarm::SwarmEvent::IncomingConnection { .. } => {
self.swarm.connections_incoming.inc(); self.swarm.connections_incoming.inc();
@ -201,7 +206,12 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
} }
#[derive(Encode, Hash, Clone, Eq, PartialEq)] #[derive(Encode, Hash, Clone, Eq, PartialEq)]
struct ConnectionEstablishedLabeles { struct ConnectionEstablishedLabels {
role: Role,
}
#[derive(Encode, Hash, Clone, Eq, PartialEq)]
struct ConnectionClosedLabels {
role: Role, role: Role,
} }