Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose relay version metric #1750

Merged
merged 8 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
semver = "1.0"
structopt = "0.3"
signal-hook = "0.3.14"
signal-hook-async-std = "0.2.2"
Expand Down
56 changes: 48 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 semver::Version;
use signal_hook::consts::*;
use signal_hook_async_std::Signals;
use structopt::{clap::arg_enum, StructOpt};
Expand Down Expand Up @@ -254,17 +255,56 @@ 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 {
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's not halt the relay if we were unable to detect package version or git commit - it
// maybe some local build with broken version

let env_relay_version = option_env!("CARGO_PKG_VERSION").unwrap_or_else(|| {
log::debug!(
target: "bridge",
"Missing CARGO_PKG_VERSION variable while compiling. Using default value",
);

"0.0.0"
});
let relay_version = Version::parse(env_relay_version).unwrap_or_else(|e| {
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
log::debug!(
target: "bridge",
"Failed to parse CARGO_PKG_VERSION variable {}: {}",
env_relay_version,
e,
);

Version::new(0, 0, 0)
});

// we'll assume that we're running on GitLab, for local builds we don't care
let relay_commit = option_env!("CI_COMMIT_SHA").unwrap_or_else(|| {
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
log::debug!(
target: "bridge",
"Missing CARGO_PKG_VERSION variable while compiling. Using no-git value",
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
);

"no-git"
});

relay_utils::metrics::MetricsParams::new(
metrics_address,
relay_version,
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
1 change: 1 addition & 0 deletions relays/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ futures = "0.3.5"
jsonpath_lib = "0.2"
log = "0.4.17"
num-traits = "0.2"
semver = "1.0"
serde_json = "1.0"
sysinfo = "0.15"
time = { version = "0.3", features = ["formatting", "local-offset", "std"] }
Expand Down
41 changes: 35 additions & 6 deletions relays/utils/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use substrate_prometheus_endpoint::{

use async_std::sync::{Arc, RwLock};
use async_trait::async_trait;
use semver::Version;
use std::{fmt::Debug, time::Duration};

mod float_json_value;
Expand Down Expand Up @@ -99,6 +100,40 @@ impl Default for MetricsAddress {
}

impl MetricsParams {
/// Creates metrics params from metrics address.
pub fn new(
address: Option<MetricsAddress>,
relay_version: Version,
relay_commit: String,
) -> Result<Self, PrometheusError> {
const BUILD_INFO_METRIC: &str = "substrate_relay_build_info";

let registry = Registry::new();
let relay_version = format!("{relay_version}");
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
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 {} metrtic: version={} commit={}",
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
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 +147,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