Shared reference to conversion rate metric value (#1034)

* shared conversion rate metric value

* clippy
This commit is contained in:
Svyatoslav Nikolsky
2021-06-29 14:24:54 +03:00
committed by Bastian Köcher
parent db0216dabb
commit ecd20d9d24
23 changed files with 260 additions and 161 deletions
@@ -17,7 +17,8 @@
//! Millau-to-Rialto messages sync entrypoint.
use crate::messages_lane::{
select_delivery_transaction_limits, MessagesRelayParams, SubstrateMessageLane, SubstrateMessageLaneToSubstrate,
select_delivery_transaction_limits, MessagesRelayParams, StandaloneMessagesMetrics, SubstrateMessageLane,
SubstrateMessageLaneToSubstrate,
};
use crate::messages_source::SubstrateMessagesSource;
use crate::messages_target::SubstrateMessagesTarget;
@@ -30,10 +31,8 @@ use frame_support::dispatch::GetDispatchInfo;
use messages_relay::message_lane::MessageLane;
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{
metrics::{FloatStorageValueMetric, StorageProofOverheadMetric},
Chain, TransactionSignScheme,
};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use sp_core::{Bytes, Pair};
use std::{ops::RangeInclusive, time::Duration};
@@ -136,7 +135,7 @@ type RialtoTargetClient =
/// Run Millau-to-Rialto messages sync.
pub async fn run(
params: MessagesRelayParams<Millau, MillauSigningParams, Rialto, RialtoSigningParams>,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let relayer_id_at_millau = (*params.source_sign.public().as_array_ref()).into();
@@ -172,6 +171,7 @@ pub async fn run(
max_messages_weight_in_single_batch,
);
let (metrics_params, _) = add_standalone_metrics(params.metrics_params, source_client.clone())?;
messages_relay::message_lane_loop::run(
messages_relay::message_lane_loop::Params {
lane: lane_id,
@@ -202,36 +202,25 @@ pub async fn run(
MILLAU_CHAIN_ID,
params.source_to_target_headers_relay,
),
relay_utils::relay_metrics(
Some(messages_relay::message_lane_loop::metrics_prefix::<
MillauMessagesToRialto,
>(&lane_id)),
params.metrics_params,
)
.standalone_metric(|registry, prefix| {
StorageProofOverheadMetric::new(
registry,
prefix,
source_client.clone(),
"millau_storage_proof_overhead".into(),
"Millau storage proof overhead".into(),
)
})?
.standalone_metric(|registry, prefix| {
FloatStorageValueMetric::<_, sp_runtime::FixedU128>::new(
registry,
prefix,
source_client,
sp_core::storage::StorageKey(
millau_runtime::rialto_messages::RialtoToMillauConversionRate::key().to_vec(),
),
Some(millau_runtime::rialto_messages::INITIAL_RIALTO_TO_MILLAU_CONVERSION_RATE),
"millau_rialto_to_millau_conversion_rate".into(),
"Rialto to Millau tokens conversion rate (used by Rialto)".into(),
)
})?
.into_params(),
metrics_params,
futures::future::pending(),
)
.await
}
/// Add standalone metrics for the Millau -> Rialto messages loop.
pub(crate) fn add_standalone_metrics(
metrics_params: MetricsParams,
source_client: Client<Millau>,
) -> anyhow::Result<(MetricsParams, StandaloneMessagesMetrics)> {
crate::messages_lane::add_standalone_metrics::<MillauMessagesToRialto>(
metrics_params,
source_client,
None,
None,
Some((
sp_core::storage::StorageKey(millau_runtime::rialto_messages::RialtoToMillauConversionRate::key().to_vec()),
millau_runtime::rialto_messages::INITIAL_RIALTO_TO_MILLAU_CONVERSION_RATE,
)),
)
}
+28 -28
View File
@@ -32,38 +32,38 @@ mod rococo;
mod westend;
mod wococo;
use relay_utils::metrics::{FloatJsonValueMetric, MetricsParams};
use relay_utils::metrics::{FloatJsonValueMetric, MetricsParams, PrometheusError, Registry};
pub(crate) fn add_polkadot_kusama_price_metrics<T: finality_relay::FinalitySyncPipeline>(
params: MetricsParams,
) -> anyhow::Result<MetricsParams> {
Ok(
relay_utils::relay_metrics(Some(finality_relay::metrics_prefix::<T>()), params)
// Polkadot/Kusama prices are added as metrics here, because atm we don't have Polkadot <-> Kusama
// relays, but we want to test metrics/dashboards in advance
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Polkadot&vs_currencies=btc".into(),
"$.polkadot.btc".into(),
"polkadot_to_base_conversion_rate".into(),
"Rate used to convert from DOT to some BASE tokens".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Kusama&vs_currencies=btc".into(),
"$.kusama.btc".into(),
"kusama_to_base_conversion_rate".into(),
"Rate used to convert from KSM to some BASE tokens".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.into_params(),
// Polkadot/Kusama prices are added as metrics here, because atm we don't have Polkadot <-> Kusama
// relays, but we want to test metrics/dashboards in advance
Ok(relay_utils::relay_metrics(None, params)
.standalone_metric(|registry, prefix| token_price_metric(registry, prefix, "polkadot"))?
.standalone_metric(|registry, prefix| token_price_metric(registry, prefix, "kusama"))?
.into_params())
}
/// Creates standalone token price metric.
pub(crate) fn token_price_metric(
registry: &Registry,
prefix: Option<&str>,
token_id: &str,
) -> Result<FloatJsonValueMetric, PrometheusError> {
FloatJsonValueMetric::new(
registry,
prefix,
format!(
"https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=btc",
token_id
),
format!("$.{}.btc", token_id),
format!("{}_to_base_conversion_rate", token_id.replace("-", "_")),
format!(
"Rate used to convert from {} to some BASE tokens",
token_id.to_uppercase()
),
)
}
@@ -17,7 +17,8 @@
//! Rialto-to-Millau messages sync entrypoint.
use crate::messages_lane::{
select_delivery_transaction_limits, MessagesRelayParams, SubstrateMessageLane, SubstrateMessageLaneToSubstrate,
select_delivery_transaction_limits, MessagesRelayParams, StandaloneMessagesMetrics, SubstrateMessageLane,
SubstrateMessageLaneToSubstrate,
};
use crate::messages_source::SubstrateMessagesSource;
use crate::messages_target::SubstrateMessagesTarget;
@@ -30,10 +31,8 @@ use frame_support::dispatch::GetDispatchInfo;
use messages_relay::message_lane::MessageLane;
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{
metrics::{FloatStorageValueMetric, StorageProofOverheadMetric},
Chain, TransactionSignScheme,
};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use sp_core::{Bytes, Pair};
use std::{ops::RangeInclusive, time::Duration};
@@ -136,7 +135,7 @@ type MillauTargetClient =
/// Run Rialto-to-Millau messages sync.
pub async fn run(
params: MessagesRelayParams<Rialto, RialtoSigningParams, Millau, MillauSigningParams>,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let relayer_id_at_rialto = (*params.source_sign.public().as_array_ref()).into();
@@ -171,6 +170,7 @@ pub async fn run(
max_messages_weight_in_single_batch,
);
let (metrics_params, _) = add_standalone_metrics(params.metrics_params, source_client.clone())?;
messages_relay::message_lane_loop::run(
messages_relay::message_lane_loop::Params {
lane: lane_id,
@@ -201,36 +201,25 @@ pub async fn run(
RIALTO_CHAIN_ID,
params.source_to_target_headers_relay,
),
relay_utils::relay_metrics(
Some(messages_relay::message_lane_loop::metrics_prefix::<
RialtoMessagesToMillau,
>(&lane_id)),
params.metrics_params,
)
.standalone_metric(|registry, prefix| {
StorageProofOverheadMetric::new(
registry,
prefix,
source_client.clone(),
"rialto_storage_proof_overhead".into(),
"Rialto storage proof overhead".into(),
)
})?
.standalone_metric(|registry, prefix| {
FloatStorageValueMetric::<_, sp_runtime::FixedU128>::new(
registry,
prefix,
source_client,
sp_core::storage::StorageKey(
rialto_runtime::millau_messages::MillauToRialtoConversionRate::key().to_vec(),
),
Some(rialto_runtime::millau_messages::INITIAL_MILLAU_TO_RIALTO_CONVERSION_RATE),
"rialto_millau_to_rialto_conversion_rate".into(),
"Millau to Rialto tokens conversion rate (used by Millau)".into(),
)
})?
.into_params(),
metrics_params,
futures::future::pending(),
)
.await
}
/// Add standalone metrics for the Rialto -> Millau messages loop.
pub(crate) fn add_standalone_metrics(
metrics_params: MetricsParams,
source_client: Client<Rialto>,
) -> anyhow::Result<(MetricsParams, StandaloneMessagesMetrics)> {
crate::messages_lane::add_standalone_metrics::<RialtoMessagesToMillau>(
metrics_params,
source_client,
None,
None,
Some((
sp_core::storage::StorageKey(rialto_runtime::millau_messages::MillauToRialtoConversionRate::key().to_vec()),
rialto_runtime::millau_messages::INITIAL_MILLAU_TO_RIALTO_CONVERSION_RATE,
)),
)
}
@@ -17,7 +17,8 @@
//! Rococo-to-Wococo messages sync entrypoint.
use crate::messages_lane::{
select_delivery_transaction_limits, MessagesRelayParams, SubstrateMessageLane, SubstrateMessageLaneToSubstrate,
select_delivery_transaction_limits, MessagesRelayParams, StandaloneMessagesMetrics, SubstrateMessageLane,
SubstrateMessageLaneToSubstrate,
};
use crate::messages_source::SubstrateMessagesSource;
use crate::messages_target::SubstrateMessagesTarget;
@@ -28,7 +29,8 @@ use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use codec::Encode;
use messages_relay::message_lane::MessageLane;
use relay_rococo_client::{HeaderId as RococoHeaderId, Rococo, SigningParams as RococoSigningParams};
use relay_substrate_client::{metrics::StorageProofOverheadMetric, Chain, TransactionSignScheme};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use relay_wococo_client::{HeaderId as WococoHeaderId, SigningParams as WococoSigningParams, Wococo};
use sp_core::{Bytes, Pair};
use std::{ops::RangeInclusive, time::Duration};
@@ -142,7 +144,7 @@ type WococoTargetClient = SubstrateMessagesTarget<
/// Run Rococo-to-Wococo messages sync.
pub async fn run(
params: MessagesRelayParams<Rococo, RococoSigningParams, Wococo, WococoSigningParams>,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let relayer_id_at_rococo = (*params.source_sign.public().as_array_ref()).into();
@@ -183,6 +185,7 @@ pub async fn run(
max_messages_weight_in_single_batch,
);
let (metrics_params, _) = add_standalone_metrics(params.metrics_params, source_client.clone())?;
messages_relay::message_lane_loop::run(
messages_relay::message_lane_loop::Params {
lane: lane_id,
@@ -213,23 +216,22 @@ pub async fn run(
ROCOCO_CHAIN_ID,
params.source_to_target_headers_relay,
),
relay_utils::relay_metrics(
Some(messages_relay::message_lane_loop::metrics_prefix::<
RococoMessagesToWococo,
>(&lane_id)),
params.metrics_params,
)
.standalone_metric(|registry, prefix| {
StorageProofOverheadMetric::new(
registry,
prefix,
source_client.clone(),
"rococo_storage_proof_overhead".into(),
"Rococo storage proof overhead".into(),
)
})?
.into_params(),
metrics_params,
futures::future::pending(),
)
.await
}
/// Add standalone metrics for the Rococo -> Wococo messages loop.
pub(crate) fn add_standalone_metrics(
metrics_params: MetricsParams,
source_client: Client<Rococo>,
) -> anyhow::Result<(MetricsParams, StandaloneMessagesMetrics)> {
crate::messages_lane::add_standalone_metrics::<RococoMessagesToWococo>(
metrics_params,
source_client,
None,
None,
None,
)
}
@@ -17,7 +17,8 @@
//! Wococo-to-Rococo messages sync entrypoint.
use crate::messages_lane::{
select_delivery_transaction_limits, MessagesRelayParams, SubstrateMessageLane, SubstrateMessageLaneToSubstrate,
select_delivery_transaction_limits, MessagesRelayParams, StandaloneMessagesMetrics, SubstrateMessageLane,
SubstrateMessageLaneToSubstrate,
};
use crate::messages_source::SubstrateMessagesSource;
use crate::messages_target::SubstrateMessagesTarget;
@@ -28,7 +29,8 @@ use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use codec::Encode;
use messages_relay::message_lane::MessageLane;
use relay_rococo_client::{HeaderId as RococoHeaderId, Rococo, SigningParams as RococoSigningParams};
use relay_substrate_client::{metrics::StorageProofOverheadMetric, Chain, TransactionSignScheme};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use relay_wococo_client::{HeaderId as WococoHeaderId, SigningParams as WococoSigningParams, Wococo};
use sp_core::{Bytes, Pair};
use std::{ops::RangeInclusive, time::Duration};
@@ -142,7 +144,7 @@ type RococoTargetClient = SubstrateMessagesTarget<
/// Run Wococo-to-Rococo messages sync.
pub async fn run(
params: MessagesRelayParams<Wococo, WococoSigningParams, Rococo, RococoSigningParams>,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let relayer_id_at_wococo = (*params.source_sign.public().as_array_ref()).into();
@@ -183,6 +185,7 @@ pub async fn run(
max_messages_weight_in_single_batch,
);
let (metrics_params, _) = add_standalone_metrics(params.metrics_params, source_client.clone())?;
messages_relay::message_lane_loop::run(
messages_relay::message_lane_loop::Params {
lane: lane_id,
@@ -213,23 +216,22 @@ pub async fn run(
WOCOCO_CHAIN_ID,
params.source_to_target_headers_relay,
),
relay_utils::relay_metrics(
Some(messages_relay::message_lane_loop::metrics_prefix::<
WococoMessagesToRococo,
>(&lane_id)),
params.metrics_params,
)
.standalone_metric(|registry, prefix| {
StorageProofOverheadMetric::new(
registry,
prefix,
source_client.clone(),
"wococo_storage_proof_overhead".into(),
"Wococo storage proof overhead".into(),
)
})?
.into_params(),
metrics_params,
futures::future::pending(),
)
.await
}
/// Add standalone metrics for the Wococo -> Rococo messages loop.
pub(crate) fn add_standalone_metrics(
metrics_params: MetricsParams,
source_client: Client<Wococo>,
) -> anyhow::Result<(MetricsParams, StandaloneMessagesMetrics)> {
crate::messages_lane::add_standalone_metrics::<WococoMessagesToRococo>(
metrics_params,
source_client,
None,
None,
None,
)
}
@@ -100,8 +100,12 @@ macro_rules! select_bridge {
const MAX_MISSING_LEFT_HEADERS_AT_RIGHT: bp_millau::BlockNumber = bp_millau::SESSION_LENGTH;
const MAX_MISSING_RIGHT_HEADERS_AT_LEFT: bp_rialto::BlockNumber = bp_rialto::SESSION_LENGTH;
use crate::chains::millau_messages_to_rialto::run as left_to_right_messages;
use crate::chains::rialto_messages_to_millau::run as right_to_left_messages;
use crate::chains::millau_messages_to_rialto::{
add_standalone_metrics as add_left_to_right_standalone_metrics, run as left_to_right_messages,
};
use crate::chains::rialto_messages_to_millau::{
add_standalone_metrics as add_right_to_left_standalone_metrics, run as right_to_left_messages,
};
$generic
}
@@ -120,8 +124,12 @@ macro_rules! select_bridge {
const MAX_MISSING_LEFT_HEADERS_AT_RIGHT: bp_rococo::BlockNumber = bp_rococo::SESSION_LENGTH;
const MAX_MISSING_RIGHT_HEADERS_AT_LEFT: bp_wococo::BlockNumber = bp_wococo::SESSION_LENGTH;
use crate::chains::rococo_messages_to_wococo::run as left_to_right_messages;
use crate::chains::wococo_messages_to_rococo::run as right_to_left_messages;
use crate::chains::rococo_messages_to_wococo::{
add_standalone_metrics as add_left_to_right_standalone_metrics, run as left_to_right_messages,
};
use crate::chains::wococo_messages_to_rococo::{
add_standalone_metrics as add_right_to_left_standalone_metrics, run as right_to_left_messages,
};
$generic
}
@@ -153,6 +161,8 @@ impl RelayHeadersAndMessages {
let metrics_params: MetricsParams = params.shared.prometheus_params.into();
let metrics_params = relay_utils::relay_metrics(None, metrics_params).into_params();
let (metrics_params, _) = add_left_to_right_standalone_metrics(metrics_params, left_client.clone())?;
let (metrics_params, _) = add_right_to_left_standalone_metrics(metrics_params, right_client.clone())?;
let left_to_right_on_demand_headers = OnDemandHeadersRelay::new(
left_client.clone(),
@@ -21,9 +21,16 @@ use crate::on_demand_headers::OnDemandHeadersRelay;
use bp_messages::{LaneId, MessageNonce};
use frame_support::weights::Weight;
use messages_relay::message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf};
use relay_substrate_client::{BlockNumberOf, Chain, Client, HashOf};
use relay_utils::{metrics::MetricsParams, BlockNumberBase};
use sp_core::Bytes;
use relay_substrate_client::{
metrics::{FloatStorageValueMetric, StorageProofOverheadMetric},
BlockNumberOf, Chain, Client, HashOf,
};
use relay_utils::{
metrics::{F64SharedRef, MetricsParams},
BlockNumberBase,
};
use sp_core::{storage::StorageKey, Bytes};
use sp_runtime::FixedU128;
use std::ops::RangeInclusive;
/// Substrate <-> Substrate messages relay parameters.
@@ -185,6 +192,84 @@ pub fn select_delivery_transaction_limits<W: pallet_bridge_messages::WeightInfoE
(max_number_of_messages, weight_for_messages_dispatch)
}
/// Shared references to the values of standalone metrics of the message lane relay loop.
#[derive(Debug, Clone)]
pub struct StandaloneMessagesMetrics {
/// Shared reference to the actual target -> <base> chain token conversion rate.
pub target_to_base_conversion_rate: Option<F64SharedRef>,
/// Shared reference to the actual source -> <base> chain token conversion rate.
pub source_to_base_conversion_rate: Option<F64SharedRef>,
}
/// Add general standalone metrics for the message lane relay loop.
pub fn add_standalone_metrics<P: SubstrateMessageLane>(
metrics_params: MetricsParams,
source_client: Client<P::SourceChain>,
source_chain_token_id: Option<&str>,
target_chain_token_id: Option<&str>,
target_to_source_conversion_rate_params: Option<(StorageKey, FixedU128)>,
) -> anyhow::Result<(MetricsParams, StandaloneMessagesMetrics)> {
let mut source_to_base_conversion_rate = None;
let mut target_to_base_conversion_rate = None;
let mut metrics_params =
relay_utils::relay_metrics(None, metrics_params).standalone_metric(|registry, prefix| {
StorageProofOverheadMetric::new(
registry,
prefix,
source_client.clone(),
format!("{}_storage_proof_overhead", P::SourceChain::NAME.to_lowercase()),
format!("{} storage proof overhead", P::SourceChain::NAME),
)
})?;
if let Some((target_to_source_conversion_rate_storage_key, initial_target_to_source_conversion_rate)) =
target_to_source_conversion_rate_params
{
metrics_params = metrics_params.standalone_metric(|registry, prefix| {
let metric = FloatStorageValueMetric::<_, sp_runtime::FixedU128>::new(
registry,
prefix,
source_client,
target_to_source_conversion_rate_storage_key,
Some(initial_target_to_source_conversion_rate),
format!(
"{}_{}_to_{}_conversion_rate",
P::SourceChain::NAME,
P::TargetChain::NAME,
P::SourceChain::NAME
),
format!(
"{} to {} tokens conversion rate (used by {})",
P::TargetChain::NAME,
P::SourceChain::NAME,
P::SourceChain::NAME
),
)?;
Ok(metric)
})?;
}
if let Some(source_chain_token_id) = source_chain_token_id {
metrics_params = metrics_params.standalone_metric(|registry, prefix| {
let metric = crate::chains::token_price_metric(registry, prefix, source_chain_token_id)?;
source_to_base_conversion_rate = Some(metric.shared_value_ref());
Ok(metric)
})?;
}
if let Some(target_chain_token_id) = target_chain_token_id {
metrics_params = metrics_params.standalone_metric(|registry, prefix| {
let metric = crate::chains::token_price_metric(registry, prefix, target_chain_token_id)?;
target_to_base_conversion_rate = Some(metric.shared_value_ref());
Ok(metric)
})?;
}
Ok((
metrics_params.into_params(),
StandaloneMessagesMetrics {
source_to_base_conversion_rate,
target_to_base_conversion_rate,
},
))
}
#[cfg(test)]
mod tests {
use super::*;