Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 46c1bd5

Browse files
dharjeezyacatangiu
andauthored
client/beefy: add more metrics for production visibility (#12910)
* few beefy metrics * more beefy metrics * some beefy metrics * some beefy metrics * more metrics * other metrics * fix tests * merge changes * Apply suggestions from code review * client/beefy: fix metrics * client/beefy: separate metrics per component, avoid double registering * client/beefy: deduplicate metrics registration code * remove unused metric * impl review suggestions --------- Co-authored-by: Adrian Catangiu <[email protected]>
1 parent 6a36704 commit 46c1bd5

File tree

7 files changed

+316
-49
lines changed

7 files changed

+316
-49
lines changed

client/beefy/src/communication/request_response/incoming_requests_handler.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ use sc_network_common::protocol::ProtocolName;
2929
use sp_runtime::traits::Block;
3030
use std::{marker::PhantomData, sync::Arc};
3131

32-
use crate::communication::request_response::{
33-
on_demand_justifications_protocol_config, Error, JustificationRequest, BEEFY_SYNC_LOG_TARGET,
32+
use crate::{
33+
communication::request_response::{
34+
on_demand_justifications_protocol_config, Error, JustificationRequest,
35+
BEEFY_SYNC_LOG_TARGET,
36+
},
37+
metric_inc,
38+
metrics::{register_metrics, OnDemandIncomingRequestsMetrics},
3439
};
3540

3641
/// A request coming in, including a sender for sending responses.
@@ -119,6 +124,7 @@ pub struct BeefyJustifsRequestHandler<B, Client> {
119124
pub(crate) request_receiver: IncomingRequestReceiver,
120125
pub(crate) justif_protocol_name: ProtocolName,
121126
pub(crate) client: Arc<Client>,
127+
pub(crate) metrics: Option<OnDemandIncomingRequestsMetrics>,
122128
pub(crate) _block: PhantomData<B>,
123129
}
124130

@@ -132,12 +138,16 @@ where
132138
genesis_hash: Hash,
133139
fork_id: Option<&str>,
134140
client: Arc<Client>,
141+
prometheus_registry: Option<prometheus::Registry>,
135142
) -> (Self, RequestResponseConfig) {
136143
let (request_receiver, config) =
137144
on_demand_justifications_protocol_config(genesis_hash, fork_id);
138145
let justif_protocol_name = config.name.clone();
139-
140-
(Self { request_receiver, justif_protocol_name, client, _block: PhantomData }, config)
146+
let metrics = register_metrics(prometheus_registry);
147+
(
148+
Self { request_receiver, justif_protocol_name, client, metrics, _block: PhantomData },
149+
config,
150+
)
141151
}
142152

143153
/// Network request-response protocol name used by this handler.
@@ -180,12 +190,14 @@ where
180190
let peer = request.peer;
181191
match self.handle_request(request) {
182192
Ok(()) => {
193+
metric_inc!(self, beefy_successful_justification_responses);
183194
debug!(
184195
target: BEEFY_SYNC_LOG_TARGET,
185196
"🥩 Handled BEEFY justification request from {:?}.", peer
186197
)
187198
},
188199
Err(e) => {
200+
metric_inc!(self, beefy_failed_justification_responses);
189201
// TODO (issue #12293): apply reputation changes here based on error type.
190202
debug!(
191203
target: BEEFY_SYNC_LOG_TARGET,

client/beefy/src/communication/request_response/outgoing_requests_engine.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use std::{collections::VecDeque, result::Result, sync::Arc};
3434
use crate::{
3535
communication::request_response::{Error, JustificationRequest, BEEFY_SYNC_LOG_TARGET},
3636
justification::{decode_and_verify_finality_proof, BeefyVersionedFinalityProof},
37+
metric_inc,
38+
metrics::{register_metrics, OnDemandOutgoingRequestsMetrics},
3739
KnownPeers,
3840
};
3941

@@ -61,20 +63,24 @@ pub struct OnDemandJustificationsEngine<B: Block> {
6163
peers_cache: VecDeque<PeerId>,
6264

6365
state: State<B>,
66+
metrics: Option<OnDemandOutgoingRequestsMetrics>,
6467
}
6568

6669
impl<B: Block> OnDemandJustificationsEngine<B> {
6770
pub fn new(
6871
network: Arc<dyn NetworkRequest + Send + Sync>,
6972
protocol_name: ProtocolName,
7073
live_peers: Arc<Mutex<KnownPeers<B>>>,
74+
prometheus_registry: Option<prometheus::Registry>,
7175
) -> Self {
76+
let metrics = register_metrics(prometheus_registry);
7277
Self {
7378
network,
7479
protocol_name,
7580
live_peers,
7681
peers_cache: VecDeque::new(),
7782
state: State::Idle,
83+
metrics,
7884
}
7985
}
8086

@@ -130,6 +136,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
130136
if let Some(peer) = self.try_next_peer() {
131137
self.request_from_peer(peer, RequestInfo { block, active_set });
132138
} else {
139+
metric_inc!(self, beefy_on_demand_justification_no_peer_to_request_from);
133140
debug!(
134141
target: BEEFY_SYNC_LOG_TARGET,
135142
"🥩 no good peers to request justif #{:?} from", block
@@ -159,6 +166,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
159166
) -> Result<BeefyVersionedFinalityProof<B>, Error> {
160167
response
161168
.map_err(|e| {
169+
metric_inc!(self, beefy_on_demand_justification_peer_hang_up);
162170
debug!(
163171
target: BEEFY_SYNC_LOG_TARGET,
164172
"🥩 for on demand justification #{:?}, peer {:?} hung up: {:?}",
@@ -169,6 +177,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
169177
Error::InvalidResponse
170178
})?
171179
.map_err(|e| {
180+
metric_inc!(self, beefy_on_demand_justification_peer_error);
172181
debug!(
173182
target: BEEFY_SYNC_LOG_TARGET,
174183
"🥩 for on demand justification #{:?}, peer {:?} error: {:?}",
@@ -185,6 +194,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
185194
&req_info.active_set,
186195
)
187196
.map_err(|e| {
197+
metric_inc!(self, beefy_on_demand_justification_invalid_proof);
188198
debug!(
189199
target: BEEFY_SYNC_LOG_TARGET,
190200
"🥩 for on demand justification #{:?}, peer {:?} responded with invalid proof: {:?}",
@@ -224,6 +234,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
224234
}
225235
})
226236
.map(|proof| {
237+
metric_inc!(self, beefy_on_demand_justification_good_proof);
227238
debug!(
228239
target: BEEFY_SYNC_LOG_TARGET,
229240
"🥩 received valid on-demand justif #{:?} from {:?}", block, peer

client/beefy/src/import.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ use sc_consensus::{BlockCheckParams, BlockImport, BlockImportParams, ImportResul
3535
use crate::{
3636
communication::notification::BeefyVersionedFinalityProofSender,
3737
justification::{decode_and_verify_finality_proof, BeefyVersionedFinalityProof},
38+
metric_inc,
39+
metrics::BlockImportMetrics,
3840
LOG_TARGET,
3941
};
4042

@@ -49,6 +51,7 @@ pub struct BeefyBlockImport<Block: BlockT, Backend, RuntimeApi, I> {
4951
runtime: Arc<RuntimeApi>,
5052
inner: I,
5153
justification_sender: BeefyVersionedFinalityProofSender<Block>,
54+
metrics: Option<BlockImportMetrics>,
5255
}
5356

5457
impl<Block: BlockT, BE, Runtime, I: Clone> Clone for BeefyBlockImport<Block, BE, Runtime, I> {
@@ -58,6 +61,7 @@ impl<Block: BlockT, BE, Runtime, I: Clone> Clone for BeefyBlockImport<Block, BE,
5861
runtime: self.runtime.clone(),
5962
inner: self.inner.clone(),
6063
justification_sender: self.justification_sender.clone(),
64+
metrics: self.metrics.clone(),
6165
}
6266
}
6367
}
@@ -69,8 +73,9 @@ impl<Block: BlockT, BE, Runtime, I> BeefyBlockImport<Block, BE, Runtime, I> {
6973
runtime: Arc<Runtime>,
7074
inner: I,
7175
justification_sender: BeefyVersionedFinalityProofSender<Block>,
76+
metrics: Option<BlockImportMetrics>,
7277
) -> BeefyBlockImport<Block, BE, Runtime, I> {
73-
BeefyBlockImport { backend, runtime, inner, justification_sender }
78+
BeefyBlockImport { backend, runtime, inner, justification_sender, metrics }
7479
}
7580
}
7681

@@ -147,13 +152,16 @@ where
147152
self.justification_sender
148153
.notify(|| Ok::<_, ()>(proof))
149154
.expect("forwards closure result; the closure always returns Ok; qed.");
155+
156+
metric_inc!(self, beefy_good_justification_imports);
150157
} else {
151158
debug!(
152159
target: LOG_TARGET,
153160
"🥩 error decoding justification: {:?} for imported block {:?}",
154161
encoded,
155162
number,
156163
);
164+
metric_inc!(self, beefy_bad_justification_imports);
157165
}
158166
},
159167
_ => (),

client/beefy/src/lib.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::{
2828
},
2929
},
3030
import::BeefyBlockImport,
31+
metrics::register_metrics,
3132
round::Rounds,
3233
worker::PersistedState,
3334
};
@@ -36,7 +37,7 @@ use beefy_primitives::{
3637
GENESIS_AUTHORITY_SET_ID,
3738
};
3839
use futures::{stream::Fuse, StreamExt};
39-
use log::{debug, error, info};
40+
use log::{error, info};
4041
use parking_lot::Mutex;
4142
use prometheus::Registry;
4243
use sc_client_api::{Backend, BlockBackend, BlockchainEvents, FinalityNotifications, Finalizer};
@@ -133,6 +134,7 @@ pub fn beefy_block_import_and_links<B, BE, RuntimeApi, I>(
133134
wrapped_block_import: I,
134135
backend: Arc<BE>,
135136
runtime: Arc<RuntimeApi>,
137+
prometheus_registry: Option<Registry>,
136138
) -> (BeefyBlockImport<B, BE, RuntimeApi, I>, BeefyVoterLinks<B>, BeefyRPCLinks<B>)
137139
where
138140
B: Block,
@@ -152,10 +154,16 @@ where
152154
// BlockImport -> Voter links
153155
let (to_voter_justif_sender, from_block_import_justif_stream) =
154156
BeefyVersionedFinalityProofStream::<B>::channel();
157+
let metrics = register_metrics(prometheus_registry);
155158

156159
// BlockImport
157-
let import =
158-
BeefyBlockImport::new(backend, runtime, wrapped_block_import, to_voter_justif_sender);
160+
let import = BeefyBlockImport::new(
161+
backend,
162+
runtime,
163+
wrapped_block_import,
164+
to_voter_justif_sender,
165+
metrics,
166+
);
159167
let voter_links = BeefyVoterLinks {
160168
from_block_import_justif_stream,
161169
to_rpc_justif_sender,
@@ -242,28 +250,16 @@ where
242250
gossip_validator.clone(),
243251
None,
244252
);
253+
let metrics = register_metrics(prometheus_registry.clone());
245254

246255
// The `GossipValidator` adds and removes known peers based on valid votes and network events.
247256
let on_demand_justifications = OnDemandJustificationsEngine::new(
248257
network.clone(),
249258
justifications_protocol_name,
250259
known_peers,
260+
prometheus_registry.clone(),
251261
);
252262

253-
let metrics =
254-
prometheus_registry.as_ref().map(metrics::Metrics::register).and_then(
255-
|result| match result {
256-
Ok(metrics) => {
257-
debug!(target: LOG_TARGET, "🥩 Registered metrics");
258-
Some(metrics)
259-
},
260-
Err(err) => {
261-
debug!(target: LOG_TARGET, "🥩 Failed to register metrics: {:?}", err);
262-
None
263-
},
264-
},
265-
);
266-
267263
// Subscribe to finality notifications and justifications before waiting for runtime pallet and
268264
// reuse the streams, so we don't miss notifications while waiting for pallet to be available.
269265
let mut finality_notifications = client.finality_notification_stream().fuse();

0 commit comments

Comments
 (0)