-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
expected_network_height
metric
- Loading branch information
1 parent
ae1f1f4
commit 014309d
Showing
4 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use std::sync::Arc; | ||
|
||
use once_cell::sync::Lazy; | ||
use prometheus_client::metrics::gauge::Gauge; | ||
|
||
use super::ChainConfig; | ||
|
||
pub static EXPECTED_NETWORK_HEIGHT: Lazy<Gauge> = Lazy::new(|| { | ||
let metric = Gauge::default(); | ||
crate::metrics::default_registry().register( | ||
"expected_network_height", | ||
"The expected network height based on the current time and the genesis block time.", | ||
metric.clone(), | ||
); | ||
metric | ||
}); | ||
|
||
/// Task to periodically update network-related metrics. | ||
pub async fn network_metrics_loop(config: Arc<ChainConfig>, genesis_timestamp: u64) { | ||
loop { | ||
// For this particular metric, we theoretically could have initiated the value once and | ||
// increased it by one every `block_delay_secs`, but this would have made the metric less | ||
// responsive and more fragile, e.g., on system clock changes. Given that the operation is cheap, we just | ||
// recalculate it every second. | ||
let now_epoch = chrono::Utc::now() | ||
.timestamp() | ||
.saturating_add(config.block_delay_secs as i64) | ||
.saturating_sub(genesis_timestamp as i64) | ||
/ config.block_delay_secs as i64; | ||
|
||
EXPECTED_NETWORK_HEIGHT.set(now_epoch); | ||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters