-
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 c0e3950
Showing
6 changed files
with
137 additions
and
37 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
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,47 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use prometheus_client::{collector::Collector, encoding::EncodeMetric, metrics::gauge::Gauge}; | ||
|
||
use super::calculate_expected_epoch; | ||
|
||
#[derive(Debug)] | ||
pub struct NetworkHeightCollector { | ||
block_delay_secs: u64, | ||
genesis_timestamp: u64, | ||
network_height: Gauge, | ||
} | ||
|
||
impl NetworkHeightCollector { | ||
pub fn new(block_delay_secs: u64, genesis_timestamp: u64) -> Self { | ||
Self { | ||
block_delay_secs, | ||
genesis_timestamp, | ||
network_height: Gauge::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Collector for NetworkHeightCollector { | ||
fn encode( | ||
&self, | ||
mut encoder: prometheus_client::encoding::DescriptorEncoder, | ||
) -> Result<(), std::fmt::Error> { | ||
let metric_encoder = encoder.encode_descriptor( | ||
"expected_network_height", | ||
"The expected network height based on the current time and the genesis block time", | ||
None, | ||
self.network_height.metric_type(), | ||
)?; | ||
|
||
let expected_epoch = calculate_expected_epoch( | ||
chrono::Utc::now().timestamp() as u64, | ||
self.genesis_timestamp, | ||
self.block_delay_secs, | ||
); | ||
self.network_height.set(expected_epoch as i64); | ||
self.network_height.encode(metric_encoder)?; | ||
|
||
Ok(()) | ||
} | ||
} |
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