Skip to content

Commit

Permalink
Made liveness metric to be timestamp based
Browse files Browse the repository at this point in the history
  • Loading branch information
ameten committed Jan 16, 2025
1 parent b9973d3 commit 0a4e2fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
16 changes: 8 additions & 8 deletions rust/main/hyperlane-base/src/contract_sync/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct ContractSyncMetrics {
/// See `last_known_message_nonce` in CoreMetrics.
pub message_nonce: IntGaugeVec,

/// Contract sync iteration metric
pub iterations: IntCounterVec,
/// Contract sync liveness metric
pub liveness_metrics: IntGaugeVec,

/// Metrics for SequenceAware and RateLimited cursors.
pub cursor_metrics: Arc<CursorMetrics>,
Expand All @@ -52,13 +52,13 @@ impl ContractSyncMetrics {
)
.expect("failed to register stored_events metric");

let iterations = metrics
.new_int_counter(
"contract_sync_iterations",
"Number of iterations made by contract sync",
let liveness_metrics = metrics
.new_int_gauge(
"contract_sync_liveness",
"Last timestamp observed by contract sync",
&["data_type", "chain"],
)
.expect("failed to register iterations metric");
.expect("failed to register liveness metric");

let message_nonce = metrics.last_known_message_nonce();
let cursor_metrics = Arc::new(CursorMetrics::new(metrics));
Expand All @@ -67,7 +67,7 @@ impl ContractSyncMetrics {
indexed_height,
stored_events,
message_nonce,
iterations,
liveness_metrics,
cursor_metrics,
}
}
Expand Down
16 changes: 13 additions & 3 deletions rust/main/hyperlane-base/src/contract_sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashSet, fmt::Debug, hash::Hash, marker::PhantomData, sync::Arc, time::Duration,
time::UNIX_EPOCH,
};

use axum::async_trait;
Expand Down Expand Up @@ -98,13 +99,13 @@ where
.metrics
.stored_events
.with_label_values(&[label, chain_name]);
let iteration_metric = self
let liveness_metric = self
.metrics
.iterations
.liveness_metrics
.with_label_values(&[label, chain_name]);

loop {
iteration_metric.inc();
Self::update_liveness_metric(&liveness_metric);
if let Some(rx) = opts.tx_id_receiver.as_mut() {
self.fetch_logs_from_receiver(rx, &stored_logs_metric).await;
}
Expand All @@ -125,6 +126,15 @@ where
info!(chain = chain_name, label, "contract sync loop exit");
}

fn update_liveness_metric(liveness_metric: &GenericGauge<AtomicI64>) {
liveness_metric.set(
UNIX_EPOCH
.elapsed()
.map(|d| d.as_millis() as i64)
.unwrap_or(0),
);
}

#[instrument(fields(domain=self.domain().name()), skip(self, recv, stored_logs_metric))]
async fn fetch_logs_from_receiver(
&self,
Expand Down

0 comments on commit 0a4e2fe

Please sign in to comment.