use alloc::vec::Vec;
use codec::{Decode, Encode};
#[derive(Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum RuntimeMetricOp {
IncrementCounterVec(u64, RuntimeMetricLabelValues),
IncrementCounter(u64),
ObserveHistogram(u128),
}
#[derive(Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct RuntimeMetricUpdate {
pub metric_name: Vec<u8>,
pub op: RuntimeMetricOp,
}
fn vec_to_str<'a>(v: &'a Vec<u8>, default: &'static str) -> &'a str {
return alloc::str::from_utf8(v).unwrap_or(default)
}
impl RuntimeMetricLabels {
pub fn as_str_vec(&self) -> Vec<&str> {
self.0
.iter()
.map(|label_vec| vec_to_str(&label_vec.0, "invalid_label"))
.collect()
}
pub fn clear(&mut self) {
self.0.clear();
}
}
impl From<&[&'static str]> for RuntimeMetricLabels {
fn from(v: &[&'static str]) -> RuntimeMetricLabels {
RuntimeMetricLabels(
v.iter().map(|label| RuntimeMetricLabel(label.as_bytes().to_vec())).collect(),
)
}
}
impl RuntimeMetricUpdate {
pub fn metric_name(&self) -> &str {
vec_to_str(&self.metric_name, "invalid_metric_name")
}
}
#[derive(Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct RuntimeMetricLabels(Vec<RuntimeMetricLabel>);
#[derive(Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct RuntimeMetricLabel(Vec<u8>);
pub type RuntimeMetricLabelValue = RuntimeMetricLabel;
pub type RuntimeMetricLabelValues = RuntimeMetricLabels;
impl From<&'static str> for RuntimeMetricLabel {
fn from(s: &'static str) -> Self {
Self(s.as_bytes().to_vec())
}
}
pub mod metric_definitions {
pub struct CounterDefinition {
pub name: &'static str,
pub description: &'static str,
}
pub struct CounterVecDefinition<'a> {
pub name: &'static str,
pub description: &'static str,
pub labels: &'a [&'static str],
}
pub struct HistogramDefinition<'a> {
pub name: &'static str,
pub description: &'static str,
pub buckets: &'a [f64],
}
pub const PARACHAIN_INHERENT_DATA_WEIGHT: CounterVecDefinition = CounterVecDefinition {
name: "polkadot_parachain_inherent_data_weight",
description: "Inherent data weight before and after filtering",
labels: &["when"],
};
pub const PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED: CounterDefinition = CounterDefinition {
name: "polkadot_parachain_inherent_data_bitfields_processed",
description: "Counts the number of bitfields processed in `process_inherent_data`.",
};
pub const PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED: CounterVecDefinition =
CounterVecDefinition {
name: "polkadot_parachain_inherent_data_candidates_processed",
description:
"Counts the number of parachain block candidates processed in `process_inherent_data`.",
labels: &["category"],
};
pub const PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED: CounterVecDefinition =
CounterVecDefinition {
name: "polkadot_parachain_inherent_data_dispute_sets_processed",
description:
"Counts the number of dispute statements sets processed in `process_inherent_data`.",
labels: &["category"],
};
pub const PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS: CounterVecDefinition =
CounterVecDefinition {
name: "polkadot_parachain_create_inherent_bitfields_signature_checks",
description:
"Counts the number of bitfields signature checked in `process_inherent_data`.",
labels: &["validity"],
};
pub const PARACHAIN_VERIFY_DISPUTE_SIGNATURE: HistogramDefinition =
HistogramDefinition {
name: "polkadot_parachain_verify_dispute_signature",
description: "How much time does it take to verify a single validator signature of a dispute statement, in seconds",
buckets: &[0.0, 0.00005, 0.00006, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.3, 0.5, 1.0],
};
}