mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 21:41:12 +00:00
initial prometheus metrics (#1536)
* service-new: cosmetic changes * overseer: draft of prometheus metrics * metrics: update active_leaves metrics * metrics: extract into functions * metrics: resolve XXX * metrics: it's ugly, but it works * Bump Substrate * metrics: move a bunch of code around * Bumb substrate again * metrics: fix a warning * fix a warning in runtime * metrics: statements signed * metrics: statements impl RegisterMetrics * metrics: refactor Metrics trait * metrics: add Metrics assoc type to JobTrait * metrics: move Metrics trait to util * metrics: fix overseer * metrics: fix backing * metrics: fix candidate validation * metrics: derive Default * metrics: docs * metrics: add stubs for other subsystems * metrics: add more stubs and fix compilation * metrics: fix doctest * metrics: move to subsystem * metrics: fix candidate validation * metrics: bitfield signing * metrics: av store * metrics: chain API * metrics: runtime API * metrics: stub for avad * metrics: candidates seconded * metrics: ok I gave up * metrics: provisioner * metrics: remove a clone by requiring Metrics: Sync * metrics: YAGNI * metrics: remove another TODO * metrics: for later * metrics: add parachain_ prefix * metrics: s/signed_statement/signed_statements * utils: add a comment for job metrics * metrics: address review comments * metrics: oops * metrics: make sure to save files before commit 😅 * use _total suffix for requests metrics Co-authored-by: Max Inden <mail@max-inden.de> * metrics: add tests for overseer * update Cargo.lock * overseer: add a test for CollationGeneration * collation-generation: impl metrics * collation-generation: use kebab-case for name * collation-generation: add a constructor Co-authored-by: Gav Wood <gavin@parity.io> Co-authored-by: Ashley Ruglys <ashley.ruglys@gmail.com> Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@@ -31,6 +31,7 @@ use polkadot_node_subsystem::{
|
||||
errors::RuntimeApiError,
|
||||
messages::{AllMessages, CollationGenerationMessage, CollatorProtocolMessage},
|
||||
FromOverseer, SpawnedSubsystem, Subsystem, SubsystemContext, SubsystemError, SubsystemResult,
|
||||
metrics::{self, prometheus},
|
||||
};
|
||||
use polkadot_node_subsystem_util::{
|
||||
self as util, request_availability_cores_ctx, request_global_validation_data_ctx,
|
||||
@@ -47,9 +48,18 @@ use std::sync::Arc;
|
||||
/// Collation Generation Subsystem
|
||||
pub struct CollationGenerationSubsystem {
|
||||
config: Option<Arc<CollationGenerationConfig>>,
|
||||
metrics: Metrics,
|
||||
}
|
||||
|
||||
impl CollationGenerationSubsystem {
|
||||
/// Create a new instance of the `CollationGenerationSubsystem`.
|
||||
pub fn new(metrics: Metrics) -> Self {
|
||||
Self {
|
||||
config: None,
|
||||
metrics,
|
||||
}
|
||||
}
|
||||
|
||||
/// Run this subsystem
|
||||
///
|
||||
/// Conceptually, this is very simple: it just loops forever.
|
||||
@@ -112,8 +122,9 @@ impl CollationGenerationSubsystem {
|
||||
Ok(Signal(ActiveLeaves(ActiveLeavesUpdate { activated, .. }))) => {
|
||||
// follow the procedure from the guide
|
||||
if let Some(config) = &self.config {
|
||||
let metrics = self.metrics.clone();
|
||||
if let Err(err) =
|
||||
handle_new_activations(config.clone(), &activated, ctx, sender).await
|
||||
handle_new_activations(config.clone(), &activated, ctx, metrics, sender).await
|
||||
{
|
||||
log::warn!(target: "collation_generation", "failed to handle new activations: {:?}", err);
|
||||
return true;
|
||||
@@ -146,13 +157,13 @@ impl<Context> Subsystem<Context> for CollationGenerationSubsystem
|
||||
where
|
||||
Context: SubsystemContext<Message = CollationGenerationMessage>,
|
||||
{
|
||||
fn start(self, ctx: Context) -> SpawnedSubsystem {
|
||||
let subsystem = CollationGenerationSubsystem { config: None };
|
||||
type Metrics = Metrics;
|
||||
|
||||
let future = Box::pin(subsystem.run(ctx));
|
||||
fn start(self, ctx: Context) -> SpawnedSubsystem {
|
||||
let future = Box::pin(self.run(ctx));
|
||||
|
||||
SpawnedSubsystem {
|
||||
name: "CollationGenerationSubsystem",
|
||||
name: "collation-generation-subsystem",
|
||||
future,
|
||||
}
|
||||
}
|
||||
@@ -178,6 +189,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
|
||||
config: Arc<CollationGenerationConfig>,
|
||||
activated: &[Hash],
|
||||
ctx: &mut Context,
|
||||
metrics: Metrics,
|
||||
sender: &mpsc::Sender<AllMessages>,
|
||||
) -> Result<()> {
|
||||
// follow the procedure from the guide:
|
||||
@@ -230,6 +242,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
|
||||
let task_global_validation_data = global_validation_data.clone();
|
||||
let task_config = config.clone();
|
||||
let mut task_sender = sender.clone();
|
||||
let metrics = metrics.clone();
|
||||
ctx.spawn("collation generation collation builder", Box::pin(async move {
|
||||
let validation_data_hash =
|
||||
validation_data_hash(&task_global_validation_data, &local_validation_data);
|
||||
@@ -273,6 +286,8 @@ async fn handle_new_activations<Context: SubsystemContext>(
|
||||
},
|
||||
};
|
||||
|
||||
metrics.on_collation_generated();
|
||||
|
||||
if let Err(err) = task_sender.send(AllMessages::CollatorProtocol(
|
||||
CollatorProtocolMessage::DistributeCollation(ccr, collation.proof_of_validity)
|
||||
)).await {
|
||||
@@ -305,6 +320,38 @@ fn erasure_root(
|
||||
Ok(polkadot_erasure_coding::branches(&chunks).root())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MetricsInner {
|
||||
collations_generated_total: prometheus::Counter<prometheus::U64>,
|
||||
}
|
||||
|
||||
/// CollationGenerationSubsystem metrics.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Metrics(Option<MetricsInner>);
|
||||
|
||||
impl Metrics {
|
||||
fn on_collation_generated(&self) {
|
||||
if let Some(metrics) = &self.0 {
|
||||
metrics.collations_generated_total.inc();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl metrics::Metrics for Metrics {
|
||||
fn try_register(registry: &prometheus::Registry) -> std::result::Result<Self, prometheus::PrometheusError> {
|
||||
let metrics = MetricsInner {
|
||||
collations_generated_total: prometheus::register(
|
||||
prometheus::Counter::new(
|
||||
"parachain_collations_generated_total",
|
||||
"Number of collations generated."
|
||||
)?,
|
||||
registry,
|
||||
)?,
|
||||
};
|
||||
Ok(Metrics(Some(metrics)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
mod handle_new_activations {
|
||||
@@ -411,6 +458,7 @@ mod tests {
|
||||
test_config(123),
|
||||
&subsystem_activated_hashes,
|
||||
&mut ctx,
|
||||
Metrics(None),
|
||||
&tx,
|
||||
)
|
||||
.await
|
||||
@@ -498,7 +546,7 @@ mod tests {
|
||||
let (tx, _rx) = mpsc::channel(0);
|
||||
|
||||
subsystem_test_harness(overseer, |mut ctx| async move {
|
||||
handle_new_activations(test_config(16), &activated_hashes, &mut ctx, &tx)
|
||||
handle_new_activations(test_config(16), &activated_hashes, &mut ctx, Metrics(None), &tx)
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
@@ -581,7 +629,7 @@ mod tests {
|
||||
let sent_messages = Arc::new(Mutex::new(Vec::new()));
|
||||
let subsystem_sent_messages = sent_messages.clone();
|
||||
subsystem_test_harness(overseer, |mut ctx| async move {
|
||||
handle_new_activations(subsystem_config, &activated_hashes, &mut ctx, &tx)
|
||||
handle_new_activations(subsystem_config, &activated_hashes, &mut ctx, Metrics(None), &tx)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user