Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
client/beefy: deduplicate metrics registration code
  • Loading branch information
acatangiu committed Feb 14, 2023
commit fe3ef4cd4a56be2b4739594be65a0459ed4e60b6
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
BEEFY_SYNC_LOG_TARGET,
},
metric_inc,
metrics::OnDemandIncomingRequestsMetrics,
metrics::{register_metrics, OnDemandIncomingRequestsMetrics},
};

/// A request coming in, including a sender for sending responses.
Expand Down Expand Up @@ -143,27 +143,7 @@ where
let (request_receiver, config) =
on_demand_justifications_protocol_config(genesis_hash, fork_id);
let justif_protocol_name = config.name.clone();
let metrics = prometheus_registry
.as_ref()
.map(OnDemandIncomingRequestsMetrics::register)
.and_then(|result| match result {
Ok(metrics) => {
debug!(
target: "beefy",
"🥩 Registered on-demand incoming justification requests metrics"
);
Some(metrics)
},
Err(err) => {
debug!(
target: "beefy",
"🥩 Failed to register incoming justification requests metrics: {:?}",
err
);
None
},
});

let metrics = register_metrics(prometheus_registry);
(
Self { request_receiver, justif_protocol_name, client, metrics, _block: PhantomData },
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
communication::request_response::{Error, JustificationRequest, BEEFY_SYNC_LOG_TARGET},
justification::{decode_and_verify_finality_proof, BeefyVersionedFinalityProof},
metric_inc,
metrics::OnDemandOutgoingRequestsMetrics,
metrics::{register_metrics, OnDemandOutgoingRequestsMetrics},
KnownPeers,
};

Expand Down Expand Up @@ -73,26 +73,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
live_peers: Arc<Mutex<KnownPeers<B>>>,
prometheus_registry: Option<prometheus::Registry>,
) -> Self {
let metrics = prometheus_registry
.as_ref()
.map(OnDemandOutgoingRequestsMetrics::register)
.and_then(|result| match result {
Ok(metrics) => {
debug!(
target: "beefy",
"🥩 Registered on-demand outgoing justification requests metrics"
);
Some(metrics)
},
Err(err) => {
debug!(
target: "beefy",
"🥩 Failed to register outgoing justification requests metrics: {:?}",
err
);
None
},
});
let metrics = register_metrics(prometheus_registry);
Self {
network,
protocol_name,
Expand Down
34 changes: 4 additions & 30 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
},
},
import::BeefyBlockImport,
metrics::register_metrics,
round::Rounds,
worker::PersistedState,
};
Expand All @@ -36,7 +37,7 @@ use beefy_primitives::{
GENESIS_AUTHORITY_SET_ID,
};
use futures::{stream::Fuse, StreamExt};
use log::{debug, error, info};
use log::{error, info};
use parking_lot::Mutex;
use prometheus::Registry;
use sc_client_api::{Backend, BlockBackend, BlockchainEvents, FinalityNotifications, Finalizer};
Expand Down Expand Up @@ -153,20 +154,7 @@ where
// BlockImport -> Voter links
let (to_voter_justif_sender, from_block_import_justif_stream) =
BeefyVersionedFinalityProofStream::<B>::channel();

let metrics = prometheus_registry
.as_ref()
.map(metrics::BlockImportMetrics::register)
.and_then(|result| match result {
Ok(metrics) => {
debug!(target: "beefy", "🥩 Registered block-import metrics");
Some(metrics)
},
Err(err) => {
debug!(target: "beefy", "🥩 Failed to register block-import metrics: {:?}", err);
None
},
});
let metrics = register_metrics(prometheus_registry);

// BlockImport
let import = BeefyBlockImport::new(
Expand Down Expand Up @@ -262,21 +250,7 @@ where
gossip_validator.clone(),
None,
);

let metrics =
prometheus_registry
.as_ref()
.map(metrics::VoterMetrics::register)
.and_then(|result| match result {
Ok(metrics) => {
debug!(target: LOG_TARGET, "🥩 Registered voter metrics");
Some(metrics)
},
Err(err) => {
debug!(target: LOG_TARGET, "🥩 Failed to register voter metrics: {:?}", err);
None
},
});
let metrics = register_metrics(prometheus_registry.clone());

// The `GossipValidator` adds and removes known peers based on valid votes and network events.
let on_demand_justifications = OnDemandJustificationsEngine::new(
Expand Down
42 changes: 34 additions & 8 deletions client/beefy/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

//! BEEFY Prometheus metrics definition

use log::debug;
use prometheus::{register, Counter, Gauge, PrometheusError, Registry, U64};

/// Helper trait for registering BEEFY metrics to Prometheus registry.
pub(crate) trait PrometheusRegister<T: Sized = Self>: Sized {
const DESCRIPTION: &'static str;
fn register(registry: &Registry) -> Result<Self, PrometheusError>;
}

/// BEEFY voting-related metrics exposed through Prometheus
#[derive(Clone, Debug)]
pub struct VoterMetrics {
Expand Down Expand Up @@ -59,8 +66,9 @@ pub struct VoterMetrics {
pub beefy_successful_handled_votes: Counter<U64>,
}

impl VoterMetrics {
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
impl PrometheusRegister for VoterMetrics {
const DESCRIPTION: &'static str = "voter";
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_validator_set_id: register(
Gauge::new(
Expand Down Expand Up @@ -179,8 +187,9 @@ pub struct BlockImportMetrics {
pub beefy_bad_justification_imports: Counter<U64>,
}

impl BlockImportMetrics {
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
impl PrometheusRegister for BlockImportMetrics {
const DESCRIPTION: &'static str = "block-import";
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_good_justification_imports: register(
Counter::new(
Expand Down Expand Up @@ -209,8 +218,9 @@ pub struct OnDemandIncomingRequestsMetrics {
pub beefy_failed_justification_responses: Counter<U64>,
}

impl OnDemandIncomingRequestsMetrics {
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
impl PrometheusRegister for OnDemandIncomingRequestsMetrics {
const DESCRIPTION: &'static str = "on-demand incoming justification requests";
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_successful_justification_responses: register(
Counter::new(
Expand Down Expand Up @@ -245,8 +255,9 @@ pub struct OnDemandOutgoingRequestsMetrics {
pub beefy_on_demand_justification_good_proof: Counter<U64>,
}

impl OnDemandOutgoingRequestsMetrics {
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
impl PrometheusRegister for OnDemandOutgoingRequestsMetrics {
const DESCRIPTION: &'static str = "on-demand outgoing justification requests";
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_on_demand_justification_no_peer_to_request_from: register(
Counter::new(
Expand Down Expand Up @@ -287,6 +298,21 @@ impl OnDemandOutgoingRequestsMetrics {
}
}

pub(crate) fn register_metrics<T: PrometheusRegister>(
prometheus_registry: Option<prometheus::Registry>,
) -> Option<T> {
prometheus_registry.as_ref().map(T::register).and_then(|result| match result {
Ok(metrics) => {
debug!(target: "beefy", "🥩 Registered {} metrics", T::DESCRIPTION);
Some(metrics)
},
Err(err) => {
debug!(target: "beefy", "🥩 Failed to register {} metrics: {:?}", T::DESCRIPTION, err);
None
},
})
}

// Note: we use the `format` macro to convert an expr into a `u64`. This will fail,
// if expr does not derive `Display`.
#[macro_export]
Expand Down