From 0a4e2fe4b301b7743df21befa3bd627441de2d0a Mon Sep 17 00:00:00 2001 From: Danil Nemirovsky <4614623+ameten@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:22:06 +0000 Subject: [PATCH] Made liveness metric to be timestamp based --- .../hyperlane-base/src/contract_sync/metrics.rs | 16 ++++++++-------- .../main/hyperlane-base/src/contract_sync/mod.rs | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/rust/main/hyperlane-base/src/contract_sync/metrics.rs b/rust/main/hyperlane-base/src/contract_sync/metrics.rs index df98346bad..7c906b28f1 100644 --- a/rust/main/hyperlane-base/src/contract_sync/metrics.rs +++ b/rust/main/hyperlane-base/src/contract_sync/metrics.rs @@ -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, @@ -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)); @@ -67,7 +67,7 @@ impl ContractSyncMetrics { indexed_height, stored_events, message_nonce, - iterations, + liveness_metrics, cursor_metrics, } } diff --git a/rust/main/hyperlane-base/src/contract_sync/mod.rs b/rust/main/hyperlane-base/src/contract_sync/mod.rs index a18f619efb..8810e0bdaf 100644 --- a/rust/main/hyperlane-base/src/contract_sync/mod.rs +++ b/rust/main/hyperlane-base/src/contract_sync/mod.rs @@ -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; @@ -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; } @@ -125,6 +126,15 @@ where info!(chain = chain_name, label, "contract sync loop exit"); } + fn update_liveness_metric(liveness_metric: &GenericGauge) { + 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,