-
Notifications
You must be signed in to change notification settings - Fork 196
feat: add prometheus metrics network_version and actor_version
#6079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed05df0
feat: add prometheus metrics `network_height` and `network_version`
hanabi1224 d1c8129
changelog
hanabi1224 7eda899
fix GC
hanabi1224 3537f0d
update docs
hanabi1224 c49a095
Merge remote-tracking branch 'origin/main' into hm/metric-network-ver…
hanabi1224 4a5eb4f
network_version_revision
hanabi1224 3b5bd97
update docs
hanabi1224 1bea7fe
reset prometheus registry after GC
hanabi1224 2be478f
add actor_version metrics
hanabi1224 41a80d7
Merge remote-tracking branch 'origin/main' into hm/metric-network-ver…
hanabi1224 9965521
revert metrics registry reset changes
hanabi1224 821be7d
collect "head_epoch" metrics with collector
hanabi1224 5e3c574
Merge remote-tracking branch 'origin/main' into hm/metric-network-ver…
hanabi1224 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,47 +1,117 @@ | ||
| // Copyright 2019-2025 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use prometheus_client::{collector::Collector, encoding::EncodeMetric, metrics::gauge::Gauge}; | ||
| use educe::Educe; | ||
| use prometheus_client::{ | ||
| collector::Collector, | ||
| encoding::{DescriptorEncoder, EncodeMetric}, | ||
| metrics::gauge::Gauge, | ||
| }; | ||
|
|
||
| use super::calculate_expected_epoch; | ||
| use crate::shim::{clock::ChainEpoch, version::NetworkVersion}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct NetworkHeightCollector { | ||
| #[derive(Educe)] | ||
| #[educe(Debug)] | ||
| pub struct NetworkHeightCollector<F> | ||
| where | ||
| F: Fn() -> ChainEpoch, | ||
| { | ||
| block_delay_secs: u32, | ||
| genesis_timestamp: u64, | ||
| network_height: Gauge, | ||
| #[educe(Debug(ignore))] | ||
| get_chain_head_height: F, | ||
| } | ||
|
|
||
| impl NetworkHeightCollector { | ||
| pub fn new(block_delay_secs: u32, genesis_timestamp: u64) -> Self { | ||
| impl<F> NetworkHeightCollector<F> | ||
| where | ||
| F: Fn() -> ChainEpoch, | ||
| { | ||
| pub fn new(block_delay_secs: u32, genesis_timestamp: u64, get_chain_head_height: F) -> Self { | ||
| Self { | ||
| block_delay_secs, | ||
| genesis_timestamp, | ||
| network_height: Gauge::default(), | ||
| get_chain_head_height, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Collector for NetworkHeightCollector { | ||
| impl<F> Collector for NetworkHeightCollector<F> | ||
| where | ||
| F: Fn() -> ChainEpoch + Send + Sync + 'static, | ||
| { | ||
| fn encode( | ||
| &self, | ||
| mut encoder: prometheus_client::encoding::DescriptorEncoder, | ||
| ) -> Result<(), std::fmt::Error> { | ||
| { | ||
| let network_height: Gauge = Default::default(); | ||
| let epoch = (self.get_chain_head_height)(); | ||
| network_height.set(epoch); | ||
| let metric_encoder = encoder.encode_descriptor( | ||
| "network_height", | ||
| "The current network height", | ||
| None, | ||
| network_height.metric_type(), | ||
| )?; | ||
| network_height.encode(metric_encoder)?; | ||
| } | ||
| { | ||
| let expected_network_height: Gauge = Default::default(); | ||
| let expected_epoch = calculate_expected_epoch( | ||
| chrono::Utc::now().timestamp() as u64, | ||
| self.genesis_timestamp, | ||
| self.block_delay_secs, | ||
| ); | ||
| expected_network_height.set(expected_epoch); | ||
| let metric_encoder = encoder.encode_descriptor( | ||
| "expected_network_height", | ||
| "The expected network height based on the current time and the genesis block time", | ||
| None, | ||
| expected_network_height.metric_type(), | ||
| )?; | ||
| expected_network_height.encode(metric_encoder)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Educe)] | ||
| #[educe(Debug)] | ||
| pub struct NetworkVersionCollector<F> | ||
| where | ||
| F: Fn() -> NetworkVersion, | ||
| { | ||
| #[educe(Debug(ignore))] | ||
| get_chain_head_network_version: F, | ||
| } | ||
|
|
||
| impl<F> NetworkVersionCollector<F> | ||
| where | ||
| F: Fn() -> NetworkVersion, | ||
| { | ||
| pub fn new(get_chain_head_network_version: F) -> Self { | ||
| Self { | ||
| get_chain_head_network_version, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<F> Collector for NetworkVersionCollector<F> | ||
| where | ||
| F: Fn() -> NetworkVersion + Send + Sync + 'static, | ||
| { | ||
| fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { | ||
| let network_version = (self.get_chain_head_network_version)(); | ||
| let nv_gauge: Gauge = Default::default(); | ||
| nv_gauge.set(u32::from(network_version) as _); | ||
| let metric_encoder = encoder.encode_descriptor( | ||
| "expected_network_height", | ||
| "The expected network height based on the current time and the genesis block time", | ||
| "network_version", | ||
| "Network version of the current chain head", | ||
| None, | ||
| self.network_height.metric_type(), | ||
| nv_gauge.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); | ||
| self.network_height.encode(metric_encoder)?; | ||
|
|
||
| nv_gauge.encode(metric_encoder)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Operational note: avoid heavy work in actor_version closure
If get_chain_head_actor_version traverses state on each scrape, consider caching per-head and updating on head changes to avoid scrape latency spikes.
🏁 Script executed:
Length of output: 4398
Avoid rebuilding the state on each Prometheus scrape — cache actor version and update on head changes.
get_chain_head_actor_version in src/daemon/mod.rs (lines 219–229) constructs a StateTree and reads actor bundle metadata on every invocation; replace with a cached value (e.g., AtomicU64/Arc) updated on chain-head changes so the metrics closure returns immediately and avoids scrape latency spikes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loading a state tree is not expensive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hanabi1224, thank you for the clarification! I appreciate you sharing that domain knowledge about StateTree loading performance in the Forest codebase.
✏️ Learnings added
🧠 Learnings used