From 6b9d14a1742341b439025d3878036cec632e42ac Mon Sep 17 00:00:00 2001
From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Date: Wed, 3 Jul 2024 11:56:20 +0300
Subject: [PATCH] litep2p: Increment random walk metrics (#4929)

This PR exposes the `RandomKademliaStarted` event from the litep2p
network backend, and then increments the appropriate metrics.

This is part of: https://github.com/paritytech/polkadot-sdk/issues/4681.
However, it is more of an effort to debug low peer count

### Testing Done
- Started a node and fetched queries:
`substrate_sub_libp2p_kademlia_random_queries_total` produces results
for litep2p backend

cc @paritytech/networking

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
---
 substrate/client/network/src/litep2p/discovery.rs | 4 ++++
 substrate/client/network/src/litep2p/mod.rs       | 6 ++++++
 2 files changed, 10 insertions(+)

diff --git a/substrate/client/network/src/litep2p/discovery.rs b/substrate/client/network/src/litep2p/discovery.rs
index ef0de981929..6ff05e6af32 100644
--- a/substrate/client/network/src/litep2p/discovery.rs
+++ b/substrate/client/network/src/litep2p/discovery.rs
@@ -145,6 +145,9 @@ pub enum DiscoveryEvent {
 		/// Record.
 		record: Record,
 	},
+
+	/// Started a random Kademlia query.
+	RandomKademliaStarted,
 }
 
 /// Discovery.
@@ -456,6 +459,7 @@ impl Stream for Discovery {
 					match this.kademlia_handle.try_find_node(peer) {
 						Ok(query_id) => {
 							this.find_node_query_id = Some(query_id);
+							return Poll::Ready(Some(DiscoveryEvent::RandomKademliaStarted))
 						},
 						Err(()) => {
 							this.duration_to_next_find_query = cmp::min(
diff --git a/substrate/client/network/src/litep2p/mod.rs b/substrate/client/network/src/litep2p/mod.rs
index 329eb68997b..824f62082ca 100644
--- a/substrate/client/network/src/litep2p/mod.rs
+++ b/substrate/client/network/src/litep2p/mod.rs
@@ -929,6 +929,12 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
 								expires,
 							)
 						));
+					},
+
+					Some(DiscoveryEvent::RandomKademliaStarted) => {
+						if let Some(metrics) = self.metrics.as_ref() {
+							metrics.kademlia_random_queries_total.inc();
+						}
 					}
 				},
 				event = self.litep2p.next_event() => match event {
-- 
GitLab