move Metrics to utils (#1765)

This commit is contained in:
Andronik Ordian
2020-09-29 13:42:20 +02:00
committed by GitHub
parent 5d4eb6bdce
commit de05bec4d6
29 changed files with 99 additions and 120 deletions
-44
View File
@@ -19,8 +19,6 @@
//! Node-side logic for Polkadot is mostly comprised of Subsystems, which are discrete components
//! that communicate via message-passing. They are coordinated by an overseer, provided by a
//! separate crate.
//!
//! This crate also reexports Prometheus metric types which are expected to be implemented by subsystems.
#![warn(missing_docs)]
@@ -204,9 +202,6 @@ pub trait SubsystemContext: Send + 'static {
/// [`Overseer`]: struct.Overseer.html
/// [`Subsystem`]: trait.Subsystem.html
pub trait Subsystem<C: SubsystemContext> {
/// Subsystem-specific Prometheus metrics.
type Metrics: metrics::Metrics;
/// Start this `Subsystem` and return `SpawnedSubsystem`.
fn start(self, ctx: C) -> SpawnedSubsystem;
}
@@ -216,8 +211,6 @@ pub trait Subsystem<C: SubsystemContext> {
pub struct DummySubsystem;
impl<C: SubsystemContext> Subsystem<C> for DummySubsystem {
type Metrics = ();
fn start(self, mut ctx: C) -> SpawnedSubsystem {
let future = Box::pin(async move {
loop {
@@ -235,40 +228,3 @@ impl<C: SubsystemContext> Subsystem<C> for DummySubsystem {
}
}
}
/// This module reexports Prometheus types and defines the [`Metrics`] trait.
pub mod metrics {
/// Reexport Prometheus types.
pub use substrate_prometheus_endpoint as prometheus;
/// Subsystem- or job-specific Prometheus metrics.
///
/// Usually implemented as a wrapper for `Option<ActualMetrics>`
/// to ensure `Default` bounds or as a dummy type ().
/// Prometheus metrics internally hold an `Arc` reference, so cloning them is fine.
pub trait Metrics: Default + Clone {
/// Try to register metrics in the Prometheus registry.
fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError>;
/// Convience method to register metrics in the optional Prometheus registry.
/// If the registration fails, prints a warning and returns `Default::default()`.
fn register(registry: Option<&prometheus::Registry>) -> Self {
registry.map(|r| {
match Self::try_register(r) {
Err(e) => {
log::warn!("Failed to register metrics: {:?}", e);
Default::default()
},
Ok(metrics) => metrics,
}
}).unwrap_or_default()
}
}
// dummy impl
impl Metrics for () {
fn try_register(_registry: &prometheus::Registry) -> Result<(), prometheus::PrometheusError> {
Ok(())
}
}
}