feat(swarm): expose ConnectionId and add conn duration metric

- Exposes the `ConnectionId` in the various `SwarmEvent` variants.
- Tracks connection duration in `libp2p-metrics::swarm`.

Pull-Request: #3927.
This commit is contained in:
Max Inden
2023-05-17 07:19:53 +02:00
committed by GitHub
parent a580906a88
commit cc5b346a7c
13 changed files with 159 additions and 51 deletions

View File

@ -187,6 +187,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
ConnectionEstablished {
/// Identity of the peer that we have connected to.
peer_id: PeerId,
/// Identifier of the connection.
connection_id: ConnectionId,
/// Endpoint of the connection that has been opened.
endpoint: ConnectedPoint,
/// Number of established connections to this peer, including the one that has just been
@ -204,6 +206,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
ConnectionClosed {
/// Identity of the peer that we have connected to.
peer_id: PeerId,
/// Identifier of the connection.
connection_id: ConnectionId,
/// Endpoint of the connection that has been closed.
endpoint: ConnectedPoint,
/// Number of other remaining connections to this same peer.
@ -218,6 +222,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
/// [`IncomingConnectionError`](SwarmEvent::IncomingConnectionError) event will later be
/// generated for this connection.
IncomingConnection {
/// Identifier of the connection.
connection_id: ConnectionId,
/// Local connection address.
/// This address has been earlier reported with a [`NewListenAddr`](SwarmEvent::NewListenAddr)
/// event.
@ -230,6 +236,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
/// This can include, for example, an error during the handshake of the encryption layer, or
/// the connection unexpectedly closed.
IncomingConnectionError {
/// Identifier of the connection.
connection_id: ConnectionId,
/// Local connection address.
/// This address has been earlier reported with a [`NewListenAddr`](SwarmEvent::NewListenAddr)
/// event.
@ -241,6 +249,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
},
/// An error happened on an outbound connection.
OutgoingConnectionError {
/// Identifier of the connection.
connection_id: ConnectionId,
/// If known, [`PeerId`] of the peer we tried to reach.
peer_id: Option<PeerId>,
/// Error that has been encountered.
@ -286,7 +296,13 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
/// reported if the dialing attempt succeeds, otherwise a
/// [`OutgoingConnectionError`](SwarmEvent::OutgoingConnectionError) event
/// is reported.
Dialing(PeerId),
Dialing {
/// Identity of the peer that we are connecting to.
peer_id: Option<PeerId>,
/// Identifier of the connection.
connection_id: ConnectionId,
},
}
impl<TBehaviourOutEvent, THandlerErr> SwarmEvent<TBehaviourOutEvent, THandlerErr> {
@ -773,6 +789,7 @@ where
return Some(SwarmEvent::OutgoingConnectionError {
peer_id: Some(peer_id),
connection_id: id,
error: dial_error,
});
}
@ -801,6 +818,7 @@ where
));
return Some(SwarmEvent::IncomingConnectionError {
connection_id: id,
send_back_addr,
local_addr,
error: listen_error,
@ -856,6 +874,7 @@ where
self.supported_protocols = supported_protocols;
return Some(SwarmEvent::ConnectionEstablished {
peer_id,
connection_id: id,
num_established,
endpoint,
concurrent_dial_errors,
@ -884,6 +903,7 @@ where
return Some(SwarmEvent::OutgoingConnectionError {
peer_id: peer,
connection_id,
error,
});
}
@ -904,6 +924,7 @@ where
connection_id: id,
}));
return Some(SwarmEvent::IncomingConnectionError {
connection_id: id,
local_addr,
send_back_addr,
error,
@ -946,6 +967,7 @@ where
}));
return Some(SwarmEvent::ConnectionClosed {
peer_id,
connection_id: id,
endpoint,
cause: error,
num_established,
@ -1008,6 +1030,7 @@ where
}));
return Some(SwarmEvent::IncomingConnectionError {
connection_id,
local_addr,
send_back_addr,
error: listen_error,
@ -1025,6 +1048,7 @@ where
);
Some(SwarmEvent::IncomingConnection {
connection_id,
local_addr,
send_back_addr,
})
@ -1111,10 +1135,12 @@ where
ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)),
ToSwarm::Dial { opts } => {
let peer_id = opts.get_or_parse_peer_id();
let connection_id = opts.connection_id();
if let Ok(()) = self.dial(opts) {
if let Ok(Some(peer_id)) = peer_id {
return Some(SwarmEvent::Dialing(peer_id));
}
return Some(SwarmEvent::Dialing {
peer_id: peer_id.ok().flatten(),
connection_id,
});
}
}
ToSwarm::NotifyHandler {
@ -2432,6 +2458,7 @@ mod tests {
peer_id,
// multiaddr,
error: DialError::Transport(errors),
..
} => {
assert_eq!(target, peer_id.unwrap());