use parity_scale_codec::{Decode, Encode};
use sp_std::prelude::*;
#[derive(Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum RuntimeMetricOp {
IncrementCounterVec(u64, RuntimeMetricLabelValues),
IncrementCounter(u64),
}
#[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 sp_std::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;
pub trait AsStr {
fn as_str(&self) -> Option<&str>;
}
impl AsStr for RuntimeMetricLabel {
fn as_str(&self) -> Option<&str> {
sp_std::str::from_utf8(&self.0).ok()
}
}
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 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 `enter_inner`.",
};
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 `enter_inner`.",
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 `enter_inner`.",
labels: &["category"],
};
pub const PARACHAIN_INHERENT_DATA_DISPUTE_SETS_INCLUDED: CounterDefinition =
CounterDefinition {
name: "polkadot_parachain_inherent_data_dispute_sets_included",
description:
"Counts the number of dispute statements sets included in a block in `enter_inner`.",
};
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 `enter_inner`.",
labels: &["validity"],
};
}