Expose relay version metric (#1750)

* expose relay version metric

* spelling

* and clippy

* clippy

* typo

* use version directly and determine git commit

* typos and leftovers
This commit is contained in:
Svyatoslav Nikolsky
2023-01-11 09:39:39 +03:00
committed by Bastian Köcher
parent 444dbe7173
commit 70eb076ab2
7 changed files with 62 additions and 18 deletions
+33 -6
View File
@@ -99,6 +99,39 @@ impl Default for MetricsAddress {
}
impl MetricsParams {
/// Creates metrics params from metrics address.
pub fn new(
address: Option<MetricsAddress>,
relay_version: String,
relay_commit: String,
) -> Result<Self, PrometheusError> {
const BUILD_INFO_METRIC: &str = "substrate_relay_build_info";
let registry = Registry::new();
register(
Gauge::<U64>::with_opts(
Opts::new(
BUILD_INFO_METRIC,
"A metric with a constant '1' value labeled by version",
)
.const_label("version", &relay_version)
.const_label("commit", &relay_commit),
)?,
&registry,
)?
.set(1);
log::info!(
target: "bridge",
"Exposed {} metric: version={} commit={}",
BUILD_INFO_METRIC,
relay_version,
relay_commit,
);
Ok(MetricsParams { address, registry })
}
/// Creates metrics params so that metrics are not exposed.
pub fn disabled() -> Self {
MetricsParams { address: None, registry: Registry::new() }
@@ -112,12 +145,6 @@ impl MetricsParams {
}
}
impl From<Option<MetricsAddress>> for MetricsParams {
fn from(address: Option<MetricsAddress>) -> Self {
MetricsParams { address, registry: Registry::new() }
}
}
/// Returns metric name optionally prefixed with given prefix.
pub fn metric_name(prefix: Option<&str>, name: &str) -> String {
if let Some(prefix) = prefix {