Skip to content

Commit

Permalink
Expose relay version metric (#1750)
Browse files Browse the repository at this point in the history
* expose relay version metric

* spelling

* and clippy

* clippy

* typo

* use version directly and determine git commit

* typos and leftovers
  • Loading branch information
svyatonik authored Jan 11, 2023
1 parent 13f4a01 commit a3a2a06
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 207 deletions.
434 changes: 245 additions & 189 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions relays/bin-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ hex = "0.4"
log = "0.4.17"
num-format = "0.4"
num-traits = "0.2"
rbtag = "0.3"
structopt = "0.3"
signal-hook = "0.3.14"
signal-hook-async-std = "0.2.2"
Expand Down
30 changes: 22 additions & 8 deletions relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::convert::TryInto;
use async_std::prelude::*;
use codec::{Decode, Encode};
use futures::{select, FutureExt};
use rbtag::BuildInfo;
use signal_hook::consts::*;
use signal_hook_async_std::Signals;
use structopt::{clap::arg_enum, StructOpt};
Expand Down Expand Up @@ -254,17 +255,30 @@ pub struct PrometheusParams {
pub prometheus_port: u16,
}

impl From<PrometheusParams> for relay_utils::metrics::MetricsParams {
fn from(cli_params: PrometheusParams) -> relay_utils::metrics::MetricsParams {
if !cli_params.no_prometheus {
/// Struct to get git commit info and build time.
#[derive(BuildInfo)]
struct SubstrateRelayBuildInfo;

impl PrometheusParams {
/// Tries to convert CLI metrics params into metrics params, used by the relay.
pub fn into_metrics_params(self) -> anyhow::Result<relay_utils::metrics::MetricsParams> {
let metrics_address = if !self.no_prometheus {
Some(relay_utils::metrics::MetricsAddress {
host: cli_params.prometheus_host,
port: cli_params.prometheus_port,
host: self.prometheus_host,
port: self.prometheus_port,
})
.into()
} else {
None.into()
}
None
};

let relay_version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
let relay_commit = SubstrateRelayBuildInfo.get_build_commit();
relay_utils::metrics::MetricsParams::new(
metrics_address,
relay_version.into(),
relay_commit.into(),
)
.map_err(|e| anyhow::format_err!("{:?}", e))
}
}

Expand Down
3 changes: 2 additions & 1 deletion relays/bin-substrate/src/cli/relay_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ where
let target_transactions_mortality = data.target_sign.target_transactions_mortality;
let target_sign = data.target_sign.to_keypair::<Self::Target>()?;

let metrics_params: relay_utils::metrics::MetricsParams = data.prometheus_params.into();
let metrics_params: relay_utils::metrics::MetricsParams =
data.prometheus_params.into_metrics_params()?;
GlobalMetrics::new()?.register_and_spawn(&metrics_params.registry)?;

let target_transactions_params = substrate_relay_helper::TransactionParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<Left: ChainWithTransactions + CliChain, Right: ChainWithTransactions + CliC
right: BridgeEndCommonParams<Right>,
) -> anyhow::Result<Self> {
// Create metrics registry.
let metrics_params = shared.prometheus_params.clone().into();
let metrics_params = shared.prometheus_params.clone().into_metrics_params()?;
let metrics_params = relay_utils::relay_metrics(metrics_params).into_params();

Ok(Self { shared, left, right, metrics_params })
Expand Down
2 changes: 1 addition & 1 deletion relays/bin-substrate/src/cli/relay_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
source_to_target_headers_relay: None,
target_to_source_headers_relay: None,
lane_id: data.lane.into(),
metrics_params: data.prometheus_params.into(),
metrics_params: data.prometheus_params.into_metrics_params()?,
})
.await
.map_err(|e| anyhow::format_err!("{}", e))
Expand Down
3 changes: 2 additions & 1 deletion relays/bin-substrate/src/cli/relay_parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ where
target_transaction_params,
);

let metrics_params: relay_utils::metrics::MetricsParams = data.prometheus_params.into();
let metrics_params: relay_utils::metrics::MetricsParams =
data.prometheus_params.into_metrics_params()?;
GlobalMetrics::new()?.register_and_spawn(&metrics_params.registry)?;

parachains_relay::parachains_loop::run(
Expand Down
39 changes: 33 additions & 6 deletions relays/utils/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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 {
Expand Down

0 comments on commit a3a2a06

Please sign in to comment.