Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bindings/napi/metrics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin");
const js = @import("zapi:zapi").js;
const state_transition = @import("state_transition");
const napi_io = @import("./io.zig");

var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = if (builtin.mode == .Debug)
Expand All @@ -11,6 +12,13 @@ else

var initialized: bool = false;

/// JS: metrics.init() → void
pub fn init() !void {
if (initialized) return;
try state_transition.metrics.init(allocator, napi_io.get(), .{});
initialized = true;
}

/// JS: metrics.scrapeMetrics() → string
pub fn scrapeMetrics() !js.String {
var aw: std.Io.Writer.Allocating = .init(allocator);
Expand Down
5 changes: 5 additions & 0 deletions bindings/src/metrics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Initialize native state-transition metrics. */
export declare function init(): void;

/** Scrape native state-transition metrics in Prometheus text format. */
export declare function scrapeMetrics(): string;
6 changes: 6 additions & 0 deletions bindings/src/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import bindings from "./bindings.js";

const native = bindings.metrics;

export const init = native.init;
export const scrapeMetrics = native.scrapeMetrics;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"./state-transition": {
"import": "./bindings/src/state-transition.js",
"types": "./bindings/src/state-transition.d.ts"
},
"./metrics": {
"import": "./bindings/src/metrics.js",
"types": "./bindings/src/metrics.d.ts"
}
},
"scripts": {
Expand Down
93 changes: 49 additions & 44 deletions src/state_transition/metrics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,144 +106,149 @@ const Metrics = struct {
///
/// Meant to be called once on application startup.
pub fn init(allocator: Allocator, io: std.Io, comptime opts: m.RegistryOpts) !void {
const metric_opts = comptime m.RegistryOpts{
.prefix = if (opts.prefix.len == 0) "lodestar_" else opts.prefix,
.exclude = opts.exclude,
};

var epoch_transition_step = try Metrics.EpochTransitionStep.init(
allocator,
io,
"lodestar_stfn_epoch_transition_step_seconds",
"stfn_epoch_transition_step_seconds",
.{ .help = "Time to call each step of epoch transition in seconds" },
opts,
metric_opts,
);
errdefer epoch_transition_step.deinit();
var state_hash_tree_root = try Metrics.StateHashTreeRoot.init(
allocator,
io,
"lodestar_stfn_hash_tree_root_seconds",
"stfn_hash_tree_root_seconds",
.{ .help = "Time to compute the hash tree root of a post state in seconds" },
opts,
metric_opts,
);
errdefer state_hash_tree_root.deinit();
var pre_state_balances_nodes_populated_miss = try Metrics.GaugeVecSource.init(
allocator,
io,
"lodestar_stfn_balances_nodes_populated_miss_total",
"stfn_balances_nodes_populated_miss_total",
.{ .help = "Total count state.balances nodesPopulated is false on stfn" },
opts,
metric_opts,
);
errdefer pre_state_balances_nodes_populated_miss.deinit();
var pre_state_balances_nodes_populated_hit = try Metrics.GaugeVecSource.init(
allocator,
io,
"lodestar_stfn_balances_nodes_populated_hit_total",
"stfn_balances_nodes_populated_hit_total",
.{ .help = "Total count state.balances nodesPopulated is true on stfn" },
opts,
metric_opts,
);
errdefer pre_state_balances_nodes_populated_hit.deinit();
var pre_state_validators_nodes_populated_miss = try Metrics.GaugeVecSource.init(
allocator,
io,
"lodestar_stfn_validators_nodes_populated_miss_total",
"stfn_validators_nodes_populated_miss_total",
.{ .help = "Total count state.validators nodesPopulated is false on stfn" },
opts,
metric_opts,
);
errdefer pre_state_validators_nodes_populated_miss.deinit();
var pre_state_validators_nodes_populated_hit = try Metrics.GaugeVecSource.init(
allocator,
io,
"lodestar_stfn_validators_nodes_populated_hit_total",
"stfn_validators_nodes_populated_hit_total",
.{ .help = "Total count state.validators nodesPopulated is true on stfn" },
opts,
metric_opts,
);
errdefer pre_state_validators_nodes_populated_hit.deinit();
var proposer_rewards = try Metrics.ProposerRewardsGauge.init(
allocator,
io,
"lodestar_stfn_proposer_rewards_total",
"stfn_proposer_rewards_total",
.{ .help = "Proposer reward by type per block" },
opts,
metric_opts,
);
errdefer proposer_rewards.deinit();

state_transition = .{
.epoch_transition = Metrics.EpochTransition.init(
"lodestar_stfn_epoch_transition_seconds",
"stfn_epoch_transition_seconds",
.{ .help = "Time to process a single epoch transition in seconds" },
opts,
metric_opts,
),
.epoch_transition_commit = Metrics.EpochTransitionCommit.init(
"lodestar_stfn_epoch_transition_commit_seconds",
"stfn_epoch_transition_commit_seconds",
.{ .help = "Time to call commit after process a single epoch transition in seconds" },
opts,
metric_opts,
),
.epoch_transition_step = epoch_transition_step,
.process_block = Metrics.ProcessBlock.init(
"lodestar_stfn_process_block_seconds",
"stfn_process_block_seconds",
.{ .help = "Time to process a single block in seconds" },
opts,
metric_opts,
),
.process_block_commit = Metrics.ProcessBlockCommit.init(
"lodestar_stfn_process_block_commit_seconds",
"stfn_process_block_commit_seconds",
.{ .help = "Time to call commit after process a single block in seconds" },
opts,
metric_opts,
),
.state_hash_tree_root = state_hash_tree_root,
.num_effective_balance_updates = Metrics.CountGauge.init(
"lodestar_stfn_effective_balance_updates_count",
"stfn_effective_balance_updates_count",
.{ .help = "Total count of effective balance updates" },
opts,
metric_opts,
),
.validators_in_activation_queue = Metrics.CountGauge.init(
"lodestar_stfn_validators_in_activation_queue",
"stfn_validators_in_activation_queue",
.{ .help = "Current number of validators in the activation queue" },
opts,
metric_opts,
),
.validators_in_exit_queue = Metrics.CountGauge.init(
"lodestar_stfn_validators_in_exit_queue",
"stfn_validators_in_exit_queue",
.{ .help = "Current number of validators in the exit queue" },
opts,
metric_opts,
),
.pre_state_balances_nodes_populated_miss = pre_state_balances_nodes_populated_miss,
.pre_state_balances_nodes_populated_hit = pre_state_balances_nodes_populated_hit,
.pre_state_validators_nodes_populated_miss = pre_state_validators_nodes_populated_miss,
.pre_state_validators_nodes_populated_hit = pre_state_validators_nodes_populated_hit,
.pre_state_cloned_count = Metrics.PreStateClonedCount.init(
"lodestar_stfn_state_cloned_count",
"stfn_state_cloned_count",
.{ .help = "Histogram of cloned count per state every time state.clone() is called" },
opts,
metric_opts,
),
.post_state_balances_nodes_populated_hit = Metrics.CountGauge.init(
"lodestar_stfn_post_state_balances_nodes_populated_hit_total",
"stfn_post_state_balances_nodes_populated_hit_total",
.{ .help = "Total count state.validators nodesPopulated is true on stfn for post state" },
opts,
metric_opts,
),
.post_state_balances_nodes_populated_miss = Metrics.CountGauge.init(
"lodestar_stfn_post_state_balances_nodes_populated_miss_total",
"stfn_post_state_balances_nodes_populated_miss_total",
.{ .help = "Total count state.validators nodesPopulated is false on stfn for post state" },
opts,
metric_opts,
),
.post_state_validators_nodes_populated_hit = Metrics.CountGauge.init(
"lodestar_stfn_post_state_validators_nodes_populated_hit_total",
"stfn_post_state_validators_nodes_populated_hit_total",
.{ .help = "Total count state.validators nodesPopulated is true on stfn for post state" },
opts,
metric_opts,
),
.post_state_validators_nodes_populated_miss = Metrics.CountGauge.init(
"lodestar_stfn_post_state_validators_nodes_populated_miss_total",
"stfn_post_state_validators_nodes_populated_miss_total",
.{ .help = "Total count state.validators nodesPopulated is false on stfn for post state" },
opts,
metric_opts,
),
.new_seen_attesters_per_block = Metrics.CountGauge.init(
"lodestar_stfn_new_seen_attesters_per_block_total",
"stfn_new_seen_attesters_per_block_total",
.{ .help = "Total count of new seen attesters per block" },
opts,
metric_opts,
),
.new_seen_attesters_effective_balance_per_block = Metrics.CountGauge.init(
"lodestar_stfn_new_seen_attesters_effective_balance_per_block_total",
"stfn_new_seen_attesters_effective_balance_per_block_total",
.{ .help = "Total effective balance increment of new seen attesters per block" },
opts,
metric_opts,
),
.attestations_per_block = Metrics.CountGauge.init(
"lodestar_stfn_attestations_per_block_total",
"stfn_attestations_per_block_total",
.{ .help = "Total count of attestations per block" },
opts,
metric_opts,
),
.proposer_rewards = proposer_rewards,
};
Expand Down
4 changes: 3 additions & 1 deletion src/state_transition/state_transition.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,17 @@ pub fn stateTransition(
}
metrics.state_transition.process_block.observe(time.durationSeconds(time.since(io, timer)));

timer = time.start(io);
try post_state.commit();
metrics.state_transition.process_block_commit.observe(time.durationSeconds(time.since(io, timer)));

try metrics.state_transition.onPostState(post_cached_state);

// Verify state root
if (opts.verify_state_root) {
timer = time.start(io);
const post_state_root = try post_state.hashTreeRoot();
try metrics.state_transition.state_hash_tree_root.observe(.{ .source = .block_transition }, time.durationSeconds(time.since(io, timer)));
try metrics.state_transition.state_hash_tree_root.observe(.{ .source = .state_transition }, time.durationSeconds(time.since(io, timer)));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a previous PR review @twoeths mentioned that it might be more accurate to rename this block_transition, but I suppose we should leave it like so so we can be the same as upstream consumer at this point


const block_state_root = block.stateRoot();
if (!std.mem.eql(u8, post_state_root, block_state_root)) {
Expand Down
Loading