Skip to content

Commit f20475d

Browse files
committed
add comment explaining why the metric example exists
1 parent 6645881 commit f20475d

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

examples/metrics_era.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
//! Run a Prometheus instance to see the data visually.
66
//!
77
//! To run this example, run `zig build run:metrics_era`.
8+
//!
9+
//! Note: this example is mainly meant to test that our metrics is working; realistically we do not need
10+
//! such an example. The bulk of this code should be moved to our beacon node implementation once it's ready.
811

912
const download_era_options = @import("download_era_options");
1013
const era = @import("era");

src/state_transition/epoch/process_epoch.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const CachedBeaconState = @import("../cache/state_cache.zig").CachedBeaconState;
33
const Timer = std.time.Timer;
44
const metrics = @import("../metrics.zig");
5+
56
const ForkSeq = @import("config").ForkSeq;
67
const EpochTransitionCache = @import("../cache/epoch_transition_cache.zig").EpochTransitionCache;
78
const processJustificationAndFinalization = @import("./process_justification_and_finalization.zig").processJustificationAndFinalization;
@@ -90,7 +91,6 @@ pub fn processEpoch(allocator: std.mem.Allocator, cached_state: *CachedBeaconSta
9091

9192
if (state.forkSeq().gte(.fulu)) {
9293
timer = try Timer.start();
93-
timer = try metrics.state_transition.epoch_transition_step.time();
9494
try processProposerLookahead(allocator, cached_state, cache);
9595
try observeEpochTransitionStep(.{ .step = .process_proposer_lookahead }, timer.read());
9696
}

src/state_transition/metrics.zig

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const std = @import("std");
22
const Allocator = std.mem.Allocator;
33
const m = @import("metrics");
44

5-
const CachedBeaconStateAllForks = @import("cache/state_cache.zig").CachedBeaconStateAllForks;
5+
const CachedBeaconState = @import("cache/state_cache.zig").CachedBeaconState;
66

77
/// Defaults to noop metrics, making this safe to use whether or not `metrics.init` is called.
88
pub var state_transition = m.initializeNoop(Metrics);
@@ -84,25 +84,29 @@ const Metrics = struct {
8484
const PreStateClonedCount = m.Histogram(u32, &.{ 1, 2, 5, 10, 50, 250 });
8585
const ProposerRewardsGauge = m.GaugeVec(u64, ProposerRewardLabel);
8686

87-
pub fn onStateClone(self: *Metrics, state: *CachedBeaconStateAllForks, source: StateCloneSource) !void {
88-
try if (state.state.balances().items.len > 0)
87+
pub fn onStateClone(self: *Metrics, state: *CachedBeaconState, source: StateCloneSource) !void {
88+
var balances = try state.state.balances();
89+
try if (try balances.length() > 0)
8990
self.pre_state_balances_nodes_populated_hit.incr(.{ .source = source })
9091
else
9192
self.pre_state_balances_nodes_populated_miss.incr(.{ .source = source });
9293

93-
try if (state.state.validators().items.len > 0)
94+
var validators = try state.state.validators();
95+
try if (try validators.length() > 0)
9496
self.pre_state_validators_nodes_populated_hit.incr(.{ .source = source })
9597
else
9698
self.pre_state_validators_nodes_populated_miss.incr(.{ .source = source });
9799
}
98100

99-
pub fn onPostState(self: *Metrics, state: *CachedBeaconStateAllForks) void {
100-
if (state.state.balances().items.len > 0)
101+
pub fn onPostState(self: *Metrics, state: *CachedBeaconState) !void {
102+
var balances = try state.state.balances();
103+
if (try balances.length() > 0)
101104
self.post_state_balances_nodes_populated_hit.incr()
102105
else
103106
self.post_state_balances_nodes_populated_miss.incr();
104107

105-
if (state.state.validators().items.len > 0)
108+
var validators = try state.state.validators();
109+
if (try validators.length() > 0)
106110
self.post_state_validators_nodes_populated_hit.incr()
107111
else
108112
self.post_state_validators_nodes_populated_miss.incr();

src/state_transition/state_transition.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,12 @@ pub fn stateTransition(
172172
);
173173
metrics.state_transition.process_block.observe(readSeconds(&timer));
174174

175-
metrics.state_transition.onPostState(post_state);
175+
try metrics.state_transition.onPostState(post_state);
176176

177177
// Verify state root
178178
if (opts.verify_state_root) {
179179
timer = try Timer.start();
180180
const post_state_root = try post_state.state.hashTreeRoot();
181-
182181
try metrics.state_transition.state_hash_tree_root.observe(.{ .source = .compute_new_state_root }, readSeconds(&timer));
183182

184183
const block_state_root = switch (block) {

0 commit comments

Comments
 (0)