mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 01:41:09 +00:00
Complex headers+messages Millau<->Rialto relay (#878)
* complex headers+messages relay * post-merge fix * fix + test issue with on-demand not starting
This commit is contained in:
committed by
Bastian Köcher
parent
0d60f42b5e
commit
e2131724fb
@@ -18,7 +18,7 @@ pub use float_json_value::FloatJsonValueMetric;
|
||||
pub use global::GlobalMetrics;
|
||||
pub use substrate_prometheus_endpoint::{
|
||||
prometheus::core::{Atomic, Collector},
|
||||
register, Counter, CounterVec, Gauge, GaugeVec, Opts, Registry, F64, U64,
|
||||
register, Counter, CounterVec, Gauge, GaugeVec, Opts, PrometheusError, Registry, F64, U64,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
@@ -43,13 +43,14 @@ pub struct MetricsParams {
|
||||
pub address: Option<MetricsAddress>,
|
||||
/// Metrics registry. May be `Some(_)` if several components share the same endpoint.
|
||||
pub registry: Option<Registry>,
|
||||
/// Prefix that must be used in metric names.
|
||||
pub metrics_prefix: Option<String>,
|
||||
}
|
||||
|
||||
/// Metrics API.
|
||||
pub trait Metrics: Clone + Send + Sync + 'static {
|
||||
/// Register metrics in the registry.
|
||||
fn register(&self, registry: &Registry) -> Result<(), String>;
|
||||
}
|
||||
pub trait Metrics: Clone + Send + Sync + 'static {}
|
||||
|
||||
impl<T: Clone + Send + Sync + 'static> Metrics for T {}
|
||||
|
||||
/// Standalone metrics API.
|
||||
///
|
||||
@@ -90,8 +91,21 @@ impl MetricsParams {
|
||||
MetricsParams {
|
||||
address: None,
|
||||
registry: None,
|
||||
metrics_prefix: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Do not expose metrics.
|
||||
pub fn disable(mut self) -> Self {
|
||||
self.address = None;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set prefix to use in metric names.
|
||||
pub fn metrics_prefix(mut self, prefix: String) -> Self {
|
||||
self.metrics_prefix = Some(prefix);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<MetricsAddress>> for MetricsParams {
|
||||
@@ -99,10 +113,20 @@ impl From<Option<MetricsAddress>> for MetricsParams {
|
||||
MetricsParams {
|
||||
address,
|
||||
registry: None,
|
||||
metrics_prefix: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns metric name optionally prefixed with given prefix.
|
||||
pub fn metric_name(prefix: Option<&str>, name: &str) -> String {
|
||||
if let Some(prefix) = prefix {
|
||||
format!("{}_{}", prefix, name)
|
||||
} else {
|
||||
name.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Set value of gauge metric.
|
||||
///
|
||||
/// If value is `Ok(None)` or `Err(_)`, metric would have default value.
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::metrics::{register, Gauge, Metrics, Registry, StandaloneMetrics, F64};
|
||||
use crate::metrics::{metric_name, register, Gauge, PrometheusError, Registry, StandaloneMetrics, F64};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use std::time::Duration;
|
||||
@@ -32,16 +32,19 @@ pub struct FloatJsonValueMetric {
|
||||
|
||||
impl FloatJsonValueMetric {
|
||||
/// Create new metric instance with given name and help.
|
||||
pub fn new(url: String, json_path: String, name: String, help: String) -> Self {
|
||||
FloatJsonValueMetric {
|
||||
pub fn new(
|
||||
registry: &Registry,
|
||||
prefix: Option<&str>,
|
||||
url: String,
|
||||
json_path: String,
|
||||
name: String,
|
||||
help: String,
|
||||
) -> Result<Self, PrometheusError> {
|
||||
Ok(FloatJsonValueMetric {
|
||||
url,
|
||||
json_path,
|
||||
metric: Gauge::new(name, help).expect(
|
||||
"only fails if gauge options are customized;\
|
||||
we use default options;\
|
||||
qed",
|
||||
),
|
||||
}
|
||||
metric: register(Gauge::new(metric_name(prefix, &name), help)?, registry)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Read value from HTTP service.
|
||||
@@ -69,13 +72,6 @@ impl FloatJsonValueMetric {
|
||||
}
|
||||
}
|
||||
|
||||
impl Metrics for FloatJsonValueMetric {
|
||||
fn register(&self, registry: &Registry) -> Result<(), String> {
|
||||
register(self.metric.clone(), registry).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl StandaloneMetrics for FloatJsonValueMetric {
|
||||
fn update_interval(&self) -> Duration {
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
//! Global system-wide Prometheus metrics exposed by relays.
|
||||
|
||||
use crate::metrics::{register, Gauge, GaugeVec, Metrics, Opts, Registry, StandaloneMetrics, F64, U64};
|
||||
use crate::metrics::{
|
||||
metric_name, register, Gauge, GaugeVec, Opts, PrometheusError, Registry, StandaloneMetrics, F64, U64,
|
||||
};
|
||||
|
||||
use async_std::sync::{Arc, Mutex};
|
||||
use async_trait::async_trait;
|
||||
@@ -35,12 +37,30 @@ pub struct GlobalMetrics {
|
||||
process_memory_usage_bytes: Gauge<U64>,
|
||||
}
|
||||
|
||||
impl Metrics for GlobalMetrics {
|
||||
fn register(&self, registry: &Registry) -> Result<(), String> {
|
||||
register(self.system_average_load.clone(), registry).map_err(|e| e.to_string())?;
|
||||
register(self.process_cpu_usage_percentage.clone(), registry).map_err(|e| e.to_string())?;
|
||||
register(self.process_memory_usage_bytes.clone(), registry).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
impl GlobalMetrics {
|
||||
/// Create and register global metrics.
|
||||
pub fn new(registry: &Registry, prefix: Option<&str>) -> Result<Self, PrometheusError> {
|
||||
Ok(GlobalMetrics {
|
||||
system: Arc::new(Mutex::new(System::new_with_specifics(RefreshKind::everything()))),
|
||||
system_average_load: register(
|
||||
GaugeVec::new(
|
||||
Opts::new(metric_name(prefix, "system_average_load"), "System load average"),
|
||||
&["over"],
|
||||
)?,
|
||||
registry,
|
||||
)?,
|
||||
process_cpu_usage_percentage: register(
|
||||
Gauge::new(metric_name(prefix, "process_cpu_usage_percentage"), "Process CPU usage")?,
|
||||
registry,
|
||||
)?,
|
||||
process_memory_usage_bytes: register(
|
||||
Gauge::new(
|
||||
metric_name(prefix, "process_memory_usage_bytes"),
|
||||
"Process memory (resident set size) usage",
|
||||
)?,
|
||||
registry,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,20 +109,3 @@ impl StandaloneMetrics for GlobalMetrics {
|
||||
UPDATE_INTERVAL
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GlobalMetrics {
|
||||
fn default() -> Self {
|
||||
GlobalMetrics {
|
||||
system: Arc::new(Mutex::new(System::new_with_specifics(RefreshKind::everything()))),
|
||||
system_average_load: GaugeVec::new(Opts::new("system_average_load", "System load average"), &["over"])
|
||||
.expect("metric is static and thus valid; qed"),
|
||||
process_cpu_usage_percentage: Gauge::new("process_cpu_usage_percentage", "Process CPU usage")
|
||||
.expect("metric is static and thus valid; qed"),
|
||||
process_memory_usage_bytes: Gauge::new(
|
||||
"process_memory_usage_bytes",
|
||||
"Process memory (resident set size) usage",
|
||||
)
|
||||
.expect("metric is static and thus valid; qed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::metrics::{Metrics, MetricsAddress, MetricsParams, StandaloneMetrics};
|
||||
use crate::metrics::{Metrics, MetricsAddress, MetricsParams, PrometheusError, StandaloneMetrics};
|
||||
use crate::{FailedClient, MaybeConnectionError};
|
||||
|
||||
use async_trait::async_trait;
|
||||
@@ -45,9 +45,7 @@ pub fn relay_loop<SC, TC>(source_client: SC, target_client: TC) -> Loop<SC, TC,
|
||||
}
|
||||
|
||||
/// Returns generic relay loop metrics that may be customized and used in one or several relay loops.
|
||||
pub fn relay_metrics(prefix: String, address: Option<MetricsAddress>) -> LoopMetrics<(), (), ()> {
|
||||
assert!(!prefix.is_empty(), "Metrics prefix can not be empty");
|
||||
|
||||
pub fn relay_metrics(prefix: Option<String>, params: MetricsParams) -> LoopMetrics<(), (), ()> {
|
||||
LoopMetrics {
|
||||
relay_loop: Loop {
|
||||
reconnect_delay: RECONNECT_DELAY,
|
||||
@@ -55,9 +53,9 @@ pub fn relay_metrics(prefix: String, address: Option<MetricsAddress>) -> LoopMet
|
||||
target_client: (),
|
||||
loop_metric: None,
|
||||
},
|
||||
address,
|
||||
registry: Registry::new_custom(Some(prefix), None)
|
||||
.expect("only fails if prefix is empty; prefix is not empty; qed"),
|
||||
address: params.address,
|
||||
registry: params.registry.unwrap_or_else(|| create_metrics_registry(prefix)),
|
||||
metrics_prefix: params.metrics_prefix,
|
||||
loop_metric: None,
|
||||
}
|
||||
}
|
||||
@@ -75,6 +73,7 @@ pub struct LoopMetrics<SC, TC, LM> {
|
||||
relay_loop: Loop<SC, TC, ()>,
|
||||
address: Option<MetricsAddress>,
|
||||
registry: Registry,
|
||||
metrics_prefix: Option<String>,
|
||||
loop_metric: Option<LM>,
|
||||
}
|
||||
|
||||
@@ -86,11 +85,7 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {
|
||||
}
|
||||
|
||||
/// Start building loop metrics using given prefix.
|
||||
///
|
||||
/// Panics if `prefix` is empty.
|
||||
pub fn with_metrics(self, prefix: String, params: MetricsParams) -> LoopMetrics<SC, TC, ()> {
|
||||
assert!(!prefix.is_empty(), "Metrics prefix can not be empty");
|
||||
|
||||
pub fn with_metrics(self, prefix: Option<String>, params: MetricsParams) -> LoopMetrics<SC, TC, ()> {
|
||||
LoopMetrics {
|
||||
relay_loop: Loop {
|
||||
reconnect_delay: self.reconnect_delay,
|
||||
@@ -99,11 +94,8 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {
|
||||
loop_metric: None,
|
||||
},
|
||||
address: params.address,
|
||||
registry: match params.registry {
|
||||
Some(registry) => registry,
|
||||
None => Registry::new_custom(Some(prefix), None)
|
||||
.expect("only fails if prefix is empty; prefix is not empty; qed"),
|
||||
},
|
||||
registry: params.registry.unwrap_or_else(|| create_metrics_registry(prefix)),
|
||||
metrics_prefix: params.metrics_prefix,
|
||||
loop_metric: None,
|
||||
}
|
||||
}
|
||||
@@ -177,21 +169,34 @@ impl<SC, TC, LM> LoopMetrics<SC, TC, LM> {
|
||||
/// Add relay loop metrics.
|
||||
///
|
||||
/// Loop metrics will be passed to the loop callback.
|
||||
pub fn loop_metric<NewLM: Metrics>(self, loop_metric: NewLM) -> Result<LoopMetrics<SC, TC, NewLM>, String> {
|
||||
loop_metric.register(&self.registry)?;
|
||||
pub fn loop_metric<NewLM: Metrics>(
|
||||
self,
|
||||
create_metric: impl FnOnce(&Registry, Option<&str>) -> Result<NewLM, PrometheusError>,
|
||||
) -> Result<LoopMetrics<SC, TC, NewLM>, String> {
|
||||
let loop_metric = create_metric(&self.registry, self.metrics_prefix.as_deref()).map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(LoopMetrics {
|
||||
relay_loop: self.relay_loop,
|
||||
address: self.address,
|
||||
registry: self.registry,
|
||||
metrics_prefix: self.metrics_prefix,
|
||||
loop_metric: Some(loop_metric),
|
||||
})
|
||||
}
|
||||
|
||||
/// Add standalone metrics.
|
||||
pub fn standalone_metric<M: StandaloneMetrics>(self, standalone_metrics: M) -> Result<Self, String> {
|
||||
standalone_metrics.register(&self.registry)?;
|
||||
standalone_metrics.spawn();
|
||||
pub fn standalone_metric<M: StandaloneMetrics>(
|
||||
self,
|
||||
create_metric: impl FnOnce(&Registry, Option<&str>) -> Result<M, PrometheusError>,
|
||||
) -> Result<Self, String> {
|
||||
// since standalone metrics are updating themselves, we may just ignore the fact that the same
|
||||
// standalone metric is exposed by several loops && only spawn single metric
|
||||
match create_metric(&self.registry, self.metrics_prefix.as_deref()) {
|
||||
Ok(standalone_metrics) => standalone_metrics.spawn(),
|
||||
Err(PrometheusError::AlreadyReg) => (),
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
@@ -200,6 +205,7 @@ impl<SC, TC, LM> LoopMetrics<SC, TC, LM> {
|
||||
MetricsParams {
|
||||
address: self.address,
|
||||
registry: Some(self.registry),
|
||||
metrics_prefix: self.metrics_prefix,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,3 +243,14 @@ impl<SC, TC, LM> LoopMetrics<SC, TC, LM> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Create new registry with global metrics.
|
||||
fn create_metrics_registry(prefix: Option<String>) -> Registry {
|
||||
match prefix {
|
||||
Some(prefix) => {
|
||||
assert!(!prefix.is_empty(), "Metrics prefix can not be empty");
|
||||
Registry::new_custom(Some(prefix), None).expect("only fails if prefix is empty; prefix is not empty; qed")
|
||||
}
|
||||
None => Registry::new(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user