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

Commit

Permalink
Setup Prometheus metrics (#125)
Browse files Browse the repository at this point in the history
* setup Prometheus metrics

* expose validator set id

* cargo fmt

* Update beefy-gadget/src/lib.rs

Co-authored-by: Tomasz Drwięga <[email protected]>

* add vote messages gossiped metric

* track authorities change, before checking for MMR root digest

Co-authored-by: Tomasz Drwięga <[email protected]>
  • Loading branch information
adoerr and tomusdrw authored Mar 24, 2021
1 parent 9e28469 commit c88af04
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
1 change: 1 addition & 0 deletions client/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parking_lot = "0.11"
thiserror = "1.0"

codec = { version = "2.0.0", package = "parity-scale-codec", features = ["derive"] }
prometheus = { package = "substrate-prometheus-endpoint", git = "https://github.com/paritytech/substrate", branch = "master"}

sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
19 changes: 19 additions & 0 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::{convert::TryFrom, fmt::Debug, sync::Arc};

use beefy_primitives::BeefyApi;
use codec::Codec;
use log::debug;
use prometheus::Registry;

use sc_client_api::{Backend, BlockchainEvents, Finalizer};
use sc_network_gossip::{
Expand All @@ -33,6 +35,7 @@ use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::traits::Block;

mod error;
mod metrics;
mod round;
mod worker;

Expand Down Expand Up @@ -113,6 +116,7 @@ pub async fn start_beefy_gadget<B, P, BE, C, N, SO>(
network: N,
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
_sync_oracle: SO,
prometheus_registry: Option<Registry>,
) where
B: Block,
P: sp_core::Pair,
Expand All @@ -133,11 +137,26 @@ pub async fn start_beefy_gadget<B, P, BE, C, N, SO>(
None,
);

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

let worker = worker::BeefyWorker::<_, P::Signature, _, BE, P>::new(
client.clone(),
key_store,
signed_commitment_sender,
gossip_engine,
metrics,
);

worker.run().await
Expand Down
41 changes: 41 additions & 0 deletions client/beefy/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! BEEFY Prometheus metrics definition

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

/// BEEFY metrics exposed through Prometheus
pub(crate) struct Metrics {
/// Current active validator set id
pub beefy_validator_set_id: Gauge<U64>,
pub beefy_gadget_votes: Counter<U64>,
}

impl Metrics {
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_validator_set_id: register(
Gauge::new("beefy_validator_set_id", "Current BEEFY active validator set id.")?,
registry,
)?,
beefy_gadget_votes: register(
Counter::new("beefy_gadget_votes_total", "Total number of vote messages gossiped.")?,
registry,
)?,
})
}
}
23 changes: 18 additions & 5 deletions client/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use sp_runtime::{

use crate::{
error::{self},
metrics::Metrics,
notification, round, Client,
};

Expand Down Expand Up @@ -93,6 +94,7 @@ where
best_block_voted_on: NumberFor<B>,
validator_set_id: ValidatorSetId,
client: Arc<C>,
metrics: Option<Metrics>,
_backend: PhantomData<BE>,
_pair: PhantomData<P>,
}
Expand Down Expand Up @@ -121,6 +123,7 @@ where
key_store: SyncCryptoStorePtr,
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, S>,
gossip_engine: GossipEngine<B>,
metrics: Option<Metrics>,
) -> Self {
BeefyWorker {
state: State::New,
Expand All @@ -135,6 +138,7 @@ where
best_block_voted_on: Zero::zero(),
validator_set_id: 0,
client,
metrics,
_backend: PhantomData,
_pair: PhantomData,
}
Expand Down Expand Up @@ -240,18 +244,23 @@ where
return;
};

if let Some(new) = find_authorities_change::<B, P::Public>(&notification.header) {
debug!(target: "beefy", "🥩 New validator set: {:?}", new);

if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_validator_set_id.set(new.id);
}

self.validator_set_id = new.id;
};

let mmr_root = if let Some(hash) = find_mmr_root_digest::<B, P::Public>(&notification.header) {
hash
} else {
warn!(target: "beefy", "🥩 No MMR root digest found for: {:?}", notification.header.hash());
return;
};

if let Some(new) = find_authorities_change::<B, P::Public>(&notification.header) {
debug!(target: "beefy", "🥩 New validator set: {:?}", new);
self.validator_set_id = new.id;
};

let commitment = Commitment {
payload: mmr_root,
block_number: notification.header.number(),
Expand Down Expand Up @@ -280,6 +289,10 @@ where

debug!(target: "beefy", "🥩 Sent vote message: {:?}", message);

if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_gadget_votes.inc();
}

self.handle_vote(
(message.commitment.payload, *message.commitment.block_number),
(message.id, message.signature),
Expand Down

0 comments on commit c88af04

Please sign in to comment.