From c733c525c19c6de8c74d2e8cf869a83660467fa0 Mon Sep 17 00:00:00 2001
From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Date: Sun, 6 Oct 2024 14:54:07 +0300
Subject: [PATCH] beefy/metrics: Add number of live peers available for
 requests (#5859)

This PR adds a new beefy metric to monitor the number of live beefy
peers.

Part of investigation of litep2p request failures:
https://github.com/paritytech/polkadot-sdk/issues/4985

cc @paritytech/networking

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
---
 prdoc/pr_5859.prdoc                                   | 11 +++++++++++
 .../request_response/outgoing_requests_engine.rs      |  4 +++-
 substrate/client/consensus/beefy/src/metrics.rs       |  9 +++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 prdoc/pr_5859.prdoc

diff --git a/prdoc/pr_5859.prdoc b/prdoc/pr_5859.prdoc
new file mode 100644
index 00000000000..edb3008238b
--- /dev/null
+++ b/prdoc/pr_5859.prdoc
@@ -0,0 +1,11 @@
+title: Add number of live peers available for requests
+
+doc:
+  - audience: [Node Operator, Node Dev]
+    description: |
+      This PR adds a new metric for the number of live peers available for beefy requests.
+      The metric is exposed under the name `substrate_beefy_on_demand_live_peers`.
+
+crates:
+  - name: sc-consensus-beefy
+    bump: minor
diff --git a/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs b/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
index 95ecf35557a..5408d95acf2 100644
--- a/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
+++ b/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
@@ -38,7 +38,7 @@ use crate::{
 		request_response::{Error, JustificationRequest, BEEFY_SYNC_LOG_TARGET},
 	},
 	justification::{decode_and_verify_finality_proof, BeefyVersionedFinalityProof},
-	metric_inc,
+	metric_inc, metric_set,
 	metrics::{register_metrics, OnDemandOutgoingRequestsMetrics},
 	KnownPeers,
 };
@@ -242,6 +242,8 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
 		// meaning we're done with current state. Move the engine to `State::Idle`.
 		self.state = State::Idle;
 
+		metric_set!(self.metrics, beefy_on_demand_live_peers, self.live_peers.lock().len() as u64);
+
 		let block = req_info.block;
 		match self.process_response(&peer, &req_info, resp) {
 			Err(err) => {
diff --git a/substrate/client/consensus/beefy/src/metrics.rs b/substrate/client/consensus/beefy/src/metrics.rs
index 30180fe43ec..15f2f9f9033 100644
--- a/substrate/client/consensus/beefy/src/metrics.rs
+++ b/substrate/client/consensus/beefy/src/metrics.rs
@@ -236,6 +236,8 @@ pub struct OnDemandOutgoingRequestsMetrics {
 	pub beefy_on_demand_justification_invalid_proof: Counter<U64>,
 	/// Number of on-demand justification good proof
 	pub beefy_on_demand_justification_good_proof: Counter<U64>,
+	/// Number of live beefy peers available for requests.
+	pub beefy_on_demand_live_peers: Gauge<U64>,
 }
 
 impl PrometheusRegister for OnDemandOutgoingRequestsMetrics {
@@ -277,6 +279,13 @@ impl PrometheusRegister for OnDemandOutgoingRequestsMetrics {
 				)?,
 				registry,
 			)?,
+			beefy_on_demand_live_peers: register(
+				Gauge::new(
+					"substrate_beefy_on_demand_live_peers",
+					"Number of live beefy peers available for requests.",
+				)?,
+				registry,
+			)?,
 		})
 	}
 }
-- 
GitLab