Skip to content

Commit

Permalink
[bridge] add uptime metrics for bridge node (#19411)
Browse files Browse the repository at this point in the history
## Description 

As title.

## Test plan 

tested by running a bridge network locally.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
longbowlu authored Sep 19, 2024
1 parent 2d7c37c commit 695ff9a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
41 changes: 41 additions & 0 deletions crates/mysten-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,47 @@ pub fn uptime_metric(
Box::new(metric)
}

/// Similar to `uptime_metric`, but for the bridge node with different labels.
/// Create a metric that measures the uptime from when this metric was constructed.
/// The metric is labeled with:
/// - 'process': the process type. We keep this label to be able to distinguish between different binaries.
/// - 'version': binary version, generally be of the format: 'semver-gitrevision'
/// - 'sui_chain_identifier': the identifier of sui network which this process is part of
/// - 'eth_chain_identifier': the identifier of eth network which this process is part of
/// - 'client_enabled': whether the bridge node is running as a client
pub fn bridge_uptime_metric(
process: &str,
version: &'static str,
sui_chain_identifier: &str,
eth_chain_identifier: &str,
client_enabled: bool,
) -> Box<dyn prometheus::core::Collector> {
let opts = prometheus::opts!("uptime", "uptime of the node service in seconds")
.variable_label("process")
.variable_label("version")
.variable_label("sui_chain_identifier")
.variable_label("eth_chain_identifier")
.variable_label("client_enabled");

let start_time = std::time::Instant::now();
let uptime = move || start_time.elapsed().as_secs();
let metric = prometheus_closure_metric::ClosureMetric::new(
opts,
prometheus_closure_metric::ValueType::Counter,
uptime,
&[
process,
version,
sui_chain_identifier,
eth_chain_identifier,
if client_enabled { "true" } else { "false" },
],
)
.unwrap();

Box::new(metric)
}

pub const METRICS_ROUTE: &str = "/metrics";

// Creates a new http server that has as a sole purpose to expose
Expand Down
7 changes: 6 additions & 1 deletion crates/sui-bridge/src/eth_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ impl<P> EthClient<P>
where
P: JsonRpcClient,
{
pub async fn get_chain_id(&self) -> Result<u64, anyhow::Error> {
let chain_id = self.provider.get_chainid().await?;
Ok(chain_id.as_u64())
}

// TODO assert chain identifier
async fn describe(&self) -> anyhow::Result<()> {
let chain_id = self.provider.get_chainid().await?;
let chain_id = self.get_chain_id().await?;
let block_number = self.provider.get_block_number().await?;
tracing::info!(
"EthClient is connected to chain {chain_id}, current block number: {block_number}"
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-bridge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ async fn main() -> anyhow::Result<()> {
.with_prom_registry(&prometheus_registry)
.init();

let metadata =
BridgeNodePublicMetadata::new(VERSION.into(), config.metrics_key_pair.public().clone());
let metadata = BridgeNodePublicMetadata::new(VERSION, config.metrics_key_pair.public().clone());

start_metrics_push_task(
&config.metrics,
Expand Down
19 changes: 19 additions & 0 deletions crates/sui-bridge/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ pub async fn run_bridge_node(
init_all_struct_tags();
let metrics = Arc::new(BridgeMetrics::new(&prometheus_registry));
let (server_config, client_config) = config.validate(metrics.clone()).await?;
let sui_chain_identifier = server_config
.sui_client
.get_chain_identifier()
.await
.map_err(|e| anyhow::anyhow!("Failed to get sui chain identifier: {:?}", e))?;
let eth_chain_identifier = server_config
.eth_client
.get_chain_id()
.await
.map_err(|e| anyhow::anyhow!("Failed to get eth chain identifier: {:?}", e))?;
prometheus_registry
.register(mysten_metrics::bridge_uptime_metric(
"bridge",
metadata.version,
&sui_chain_identifier,
&eth_chain_identifier.to_string(),
client_config.is_some(),
))
.unwrap();

// Start Client
let _handles = if let Some(client_config) = client_config {
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-bridge/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ pub const ADD_TOKENS_ON_EVM_PATH: &str =
// Be careful with what to put here, as it is public.
#[derive(serde::Serialize)]
pub struct BridgeNodePublicMetadata {
pub version: Option<String>,
pub version: &'static str,
pub metrics_pubkey: Option<Arc<Ed25519PublicKey>>,
}

impl BridgeNodePublicMetadata {
pub fn new(version: String, metrics_pubkey: Ed25519PublicKey) -> Self {
pub fn new(version: &'static str, metrics_pubkey: Ed25519PublicKey) -> Self {
Self {
version: Some(version),
version,
metrics_pubkey: Some(metrics_pubkey.into()),
}
}

pub fn empty_for_testing() -> Self {
Self {
version: None,
version: "testing",
metrics_pubkey: None,
}
}
Expand Down

0 comments on commit 695ff9a

Please sign in to comment.