Skip to content

Commit

Permalink
feat: add expected_network_height metric
Browse files Browse the repository at this point in the history
  • Loading branch information
LesnyRumcajs committed May 15, 2024
1 parent ae1f1f4 commit 014309d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
- [#4315](https://github.com/ChainSafe/forest/pull/4315) Add support for the
`Filecoin.StateGetNetworkParams` RPC method.

- [#4326](https://github.com/ChainSafe/forest/pull/4326) Added
`expected_network_height` metric to the Prometheus metrics.

### Changed

- [#4170](https://github.com/ChainSafe/forest/pull/4170) Change the default
Expand Down
28 changes: 18 additions & 10 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ pub(super) async fn start(
});
}

// Read Genesis file
// * When snapshot command implemented, this genesis does not need to be
// initialized
let genesis_header = read_genesis_header(
config.client.genesis_file.as_ref(),
chain_config.genesis_bytes(&db).await?.as_deref(),
&db,
)
.await?;

if config.client.enable_metrics_endpoint {
// Start Prometheus server port
let prometheus_listener = TcpListener::bind(config.client.metrics_address)
Expand All @@ -212,17 +222,15 @@ pub(super) async fn start(
.await
.context("Failed to initiate prometheus server")
});
}

// Read Genesis file
// * When snapshot command implemented, this genesis does not need to be
// initialized
let genesis_header = read_genesis_header(
config.client.genesis_file.as_ref(),
chain_config.genesis_bytes(&db).await?.as_deref(),
&db,
)
.await?;
let chain_config_clone = chain_config.clone();
let genesis_timestamp = genesis_header.timestamp;
services.spawn(async move {
crate::networks::metrics::network_metrics_loop(chain_config_clone, genesis_timestamp)
.await;
Ok(())
});
}

// Initialize ChainStore
let chain_store = Arc::new(ChainStore::new(
Expand Down
37 changes: 37 additions & 0 deletions src/networks/metrics.rs
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;
}
}
2 changes: 2 additions & 0 deletions src/networks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod calibnet;
pub mod devnet;
pub mod mainnet;

pub mod metrics;

/// Newest network version for all networks
pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V17;

Expand Down

0 comments on commit 014309d

Please sign in to comment.