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

time to establish connection #3134

Merged
merged 22 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bac7601
Buckets now exponential, by request.
John-LittleBearLabs Nov 17, 2022
221a9ba
Update misc/metrics/src/swarm.rs
John-LittleBearLabs Nov 17, 2022
582ca19
Update swarm/src/connection/pool.rs
John-LittleBearLabs Nov 17, 2022
4e330c5
Update swarm/src/connection/pool.rs
John-LittleBearLabs Nov 17, 2022
7d1f76c
Update swarm/src/connection/pool.rs
John-LittleBearLabs Nov 17, 2022
a00eb9e
Absorbing recommendations.
John-LittleBearLabs Nov 17, 2022
5825d3f
Merge branch 'jt/2745-time2establish' of github.com:John-LittleBearLa…
John-LittleBearLabs Nov 17, 2022
0deb409
Taking some suggestions.
John-LittleBearLabs Nov 17, 2022
44ef2fd
clippy
John-LittleBearLabs Nov 17, 2022
a9dad92
Update misc/metrics/src/swarm.rs
John-LittleBearLabs Nov 18, 2022
4e2cc29
Update swarm/src/connection/pool.rs
John-LittleBearLabs Nov 23, 2022
2be9db9
Remove clippy config, as it is not pertinent.
John-LittleBearLabs Nov 23, 2022
60a0429
Forgot to push this rename
John-LittleBearLabs Nov 28, 2022
a6d375d
Merge remote-tracking branch 'origin/master' into jt/2745-time2establish
John-LittleBearLabs Dec 2, 2022
a1a4fbf
Naming suggestions.
John-LittleBearLabs Dec 5, 2022
a9d26e5
naming
John-LittleBearLabs Dec 6, 2022
88ae127
elapsed()
John-LittleBearLabs Dec 8, 2022
bd0874c
Changelog
John-LittleBearLabs Dec 9, 2022
e4b582b
squash! Changelog
John-LittleBearLabs Dec 9, 2022
655dec4
Merge branch 'master' into jt/2745-time2establish
mxinden Dec 12, 2022
5a68ae1
swarm/CHANGELOG: Add entry
mxinden Dec 12, 2022
e60d9de
Merge remote-tracking branch 'origin/master' into jt/2745-time2establish
John-LittleBearLabs Dec 12, 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
42 changes: 33 additions & 9 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@

use crate::protocol_stack;
use prometheus_client::encoding::text::Encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::{
counter::Counter,
family::Family,
histogram::{exponential_buckets, Histogram},
};
use prometheus_client::registry::Registry;

pub struct Metrics {
connections_incoming: Family<AddressLabels, Counter>,
connections_incoming_error: Family<IncomingConnectionErrorLabels, Counter>,

connections_established: Family<ConnectionEstablishedLabels, Counter>,
connection_establishment_duration: Family<ConnectionEstablishedLabels, Histogram>,
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
connections_closed: Family<ConnectionClosedLabels, Counter>,

new_listen_addr: Family<AddressLabels, Counter>,
Expand Down Expand Up @@ -123,6 +127,15 @@ impl Metrics {
Box::new(connections_closed.clone()),
);

let connection_establishment_duration = Family::new_with_constructor(
create_connection_establishment_duration_histogram as fn() -> Histogram,
);
sub_registry.register(
"connection_establishment_duration",
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
"Time it took (locally) to establish connections",
Box::new(connection_establishment_duration.clone()),
);

Self {
connections_incoming,
connections_incoming_error,
Expand All @@ -135,6 +148,7 @@ impl Metrics {
dial_attempt,
outgoing_connection_error,
connected_to_banned_peer,
connection_establishment_duration,
}
}
}
Expand All @@ -143,13 +157,19 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
match event {
libp2p_swarm::SwarmEvent::Behaviour(_) => {}
libp2p_swarm::SwarmEvent::ConnectionEstablished { endpoint, .. } => {
self.connections_established
.get_or_create(&ConnectionEstablishedLabels {
role: endpoint.into(),
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
})
.inc();
libp2p_swarm::SwarmEvent::ConnectionEstablished {
endpoint,
time_taken,
..
} => {
let labels = ConnectionEstablishedLabels {
role: endpoint.into(),
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
};
self.connections_established.get_or_create(&labels).inc();
self.connection_establishment_duration
.get_or_create(&labels)
.observe(time_taken.as_secs_f64());
}
libp2p_swarm::SwarmEvent::ConnectionClosed { endpoint, .. } => {
self.connections_closed
Expand Down Expand Up @@ -372,3 +392,7 @@ impl<TTransErr> From<&libp2p_swarm::PendingInboundConnectionError<TTransErr>>
}
}
}

fn create_connection_establishment_duration_histogram() -> Histogram {
Histogram::new(exponential_buckets(1e-3, 2., 10))
}
1 change: 1 addition & 0 deletions protocols/autonat/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ async fn test_dial_back() {
},
num_established,
concurrent_dial_errors,
time_taken: _,
} => {
assert_eq!(peer_id, client_id);
assert_eq!(num_established, NonZeroU32::new(2).unwrap());
Expand Down
12 changes: 11 additions & 1 deletion swarm/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use futures::{
ready,
stream::FuturesUnordered,
};
use instant::Instant;
use libp2p_core::connection::{ConnectionId, Endpoint, PendingPoint};
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt};
use std::{
Expand Down Expand Up @@ -195,6 +196,8 @@ struct PendingConnection<THandler> {
endpoint: PendingPoint,
/// When dropped, notifies the task which then knows to terminate.
abort_notifier: Option<oneshot::Sender<Void>>,
/// The moment we became aware of this possible connection, useful for timing metrics.
accepted_at: Instant,
}

impl<THandler> PendingConnection<THandler> {
Expand Down Expand Up @@ -237,6 +240,8 @@ where
/// Addresses are dialed in parallel. Contains the addresses and errors
/// of dial attempts that failed before the one successful dial.
concurrent_dial_errors: Option<Vec<(Multiaddr, TransportError<TTrans::Error>)>>,
/// How long it took to establish this connection.
established_in: std::time::Duration,
},

/// An established connection was closed.
Expand Down Expand Up @@ -493,6 +498,7 @@ where
handler,
endpoint,
abort_notifier: Some(abort_notifier),
accepted_at: Instant::now(),
},
);
Ok(connection_id)
Expand Down Expand Up @@ -540,6 +546,7 @@ where
handler,
endpoint: endpoint.into(),
abort_notifier: Some(abort_notifier),
accepted_at: Instant::now(),
},
);
Ok(connection_id)
Expand Down Expand Up @@ -634,6 +641,7 @@ where
handler,
endpoint,
abort_notifier: _,
accepted_at,
} = self
.pending
.remove(&id)
Expand Down Expand Up @@ -783,13 +791,14 @@ where
)
.boxed(),
);

let time_taken = Instant::now() - accepted_at;
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
return Poll::Ready(PoolEvent::ConnectionEstablished {
peer_id: obtained_peer_id,
endpoint,
id,
other_established_connection_ids,
concurrent_dial_errors,
established_in: time_taken,
});
}
task::PendingConnectionEvent::PendingFailed { id, error } => {
Expand All @@ -798,6 +807,7 @@ where
handler,
endpoint,
abort_notifier: _,
accepted_at: _, // Ignoring the time it took for the connection to fail.
}) = self.pending.remove(&id)
{
self.counters.dec_pending(&endpoint);
Expand Down
4 changes: 4 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
/// Addresses are dialed concurrently. Contains the addresses and errors
/// of dial attempts that failed before the one successful dial.
concurrent_dial_errors: Option<Vec<(Multiaddr, TransportError<io::Error>)>>,
/// How long it took to establish this connection
time_taken: std::time::Duration,
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
},
/// A connection with the given peer has been closed,
/// possibly as a result of an error.
Expand Down Expand Up @@ -821,6 +823,7 @@ where
endpoint,
other_established_connection_ids,
concurrent_dial_errors,
established_in: time_taken,
John-LittleBearLabs marked this conversation as resolved.
Show resolved Hide resolved
} => {
if self.banned_peers.contains(&peer_id) {
// Mark the connection for the banned peer as banned, thus withholding any
Expand Down Expand Up @@ -861,6 +864,7 @@ where
num_established,
endpoint,
concurrent_dial_errors,
time_taken,
});
}
}
Expand Down