Add lots of networking metrics for Prometheus (#5126)

* Add some metrics

* Address concerns
This commit is contained in:
Gavin Wood
2020-03-05 21:38:41 +01:00
committed by GitHub
parent dbe0211137
commit d957220435
7 changed files with 220 additions and 23 deletions
+18 -2
View File
@@ -176,6 +176,11 @@ impl DiscoveryBehaviour {
pub fn put_value(&mut self, key: record::Key, value: Vec<u8>) {
self.kademlia.put_record(Record::new(key, value), Quorum::All);
}
/// Returns the number of nodes that are in the Kademlia k-buckets.
pub fn num_kbuckets_entries(&mut self) -> usize {
self.kademlia.kbuckets_entries().count()
}
}
/// Event generated by the `DiscoveryBehaviour`.
@@ -203,6 +208,9 @@ pub enum DiscoveryOut {
/// Inserting a value into the DHT failed.
ValuePutFailed(record::Key),
/// Started a random Kademlia query.
RandomKademliaStarted,
}
impl NetworkBehaviour for DiscoveryBehaviour {
@@ -330,25 +338,33 @@ impl NetworkBehaviour for DiscoveryBehaviour {
// Poll the stream that fires when we need to start a random Kademlia query.
while let Poll::Ready(_) = self.next_kad_random_query.poll_unpin(cx) {
if self.num_connections < self.discovery_only_if_under_num {
let actually_started = if self.num_connections < self.discovery_only_if_under_num {
let random_peer_id = PeerId::random();
debug!(target: "sub-libp2p", "Libp2p <= Starting random Kademlia request for \
{:?}", random_peer_id);
self.kademlia.get_closest_peers(random_peer_id);
true
} else {
debug!(
target: "sub-libp2p",
"Kademlia paused due to high number of connections ({})",
self.num_connections
);
}
false
};
// Schedule the next random query with exponentially increasing delay,
// capped at 60 seconds.
self.next_kad_random_query = Delay::new(self.duration_to_next_kad);
self.duration_to_next_kad = cmp::min(self.duration_to_next_kad * 2,
Duration::from_secs(60));
if actually_started {
let ev = DiscoveryOut::RandomKademliaStarted;
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
}
// Poll Kademlia.