Skip to content

Commit

Permalink
feat(derive): Stage Level Metrics (#309)
Browse files Browse the repository at this point in the history
* feat(derive): stage metrics

* feat: pull out feature flags into macros

* fix: merge

* fix: use macros
  • Loading branch information
refcell authored Jun 25, 2024
1 parent 8a83559 commit 28c8c7c
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 125 deletions.
9 changes: 5 additions & 4 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ alloc-no-stdlib = "2.0.4"
serde = { version = "1.0.203", default-features = false, features = ["derive"], optional = true }

# `online` feature dependencies
lazy_static = { version = "1.5.0", optional = true }
prometheus = { version = "0.13.4", features = ["process"], optional = true }
c-kzg = { version = "1.0.2", default-features = false, optional = true }
sha2 = { version = "0.10.8", default-features = false, optional = true }
alloy-transport = { version = "0.1", default-features = false, optional = true }
Expand All @@ -50,6 +48,10 @@ alloy-rpc-types = { version = "0.1", default-features = false, optional = true }
serde_json = { version = "1.0.94", default-features = false, optional = true }
reqwest = { version = "0.12.4", default-features = false, optional = true }

# `metrics` feature dependencies
lazy_static = { version = "1.5.0", optional = true }
prometheus = { version = "0.13.4", features = ["process"], optional = true }

# `test-utils` feature dependencies
alloy-node-bindings = { version = "0.1", default-features = false, optional = true }
tracing-subscriber = { version = "0.3.18", optional = true }
Expand All @@ -73,6 +75,7 @@ serde = [
"op-alloy-consensus/serde"
]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256", "op-alloy-consensus/k256"]
metrics = ["dep:prometheus", "dep:lazy_static"]
online = [
"dep:serde_json",
"dep:revm",
Expand All @@ -83,8 +86,6 @@ online = [
"dep:alloy-transport",
"dep:alloy-transport-http",
"dep:reqwest",
"dep:prometheus",
"dep:lazy_static",
"alloy-provider/reqwest",
"alloy-rpc-client/reqwest",
"alloy-transport-http/reqwest",
Expand Down
7 changes: 6 additions & 1 deletion crates/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]
#![cfg_attr(not(any(test, feature = "metrics")), no_std)]

extern crate alloc;

mod macros;

mod params;
pub use params::{
ChannelID, CHANNEL_ID_LENGTH, CONFIG_UPDATE_EVENT_VERSION_0, CONFIG_UPDATE_TOPIC,
Expand All @@ -22,3 +24,6 @@ pub mod types;

#[cfg(feature = "online")]
pub mod online;

#[cfg(feature = "metrics")]
pub mod metrics;
55 changes: 55 additions & 0 deletions crates/derive/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Macros for use across derive.
/// Starts the timer with a label value.
#[macro_export]
macro_rules! timer {
(START, $metric:ident, $labels:expr, $timer:ident) => {
#[cfg(feature = "metrics")]
let $timer = $crate::metrics::$metric.with_label_values($labels).start_timer();
#[cfg(not(feature = "metrics"))]
let $timer = ();
};
(DISCARD, $timer:ident) => {
#[cfg(feature = "metrics")]
$timer.stop_and_discard();
};
(STOP, $timer:ident) => {
#[cfg(feature = "metrics")]
$timer.stop_and_record();
};
}

/// Increments a metric with a label value.
#[macro_export]
macro_rules! inc_gauge {
($metric:ident, $label:expr) => {
#[cfg(feature = "metrics")]
$crate::metrics::$metric.with_label_values(&[$label]).inc();
};
($metric:ident, $value:expr, $label:expr) => {
#[cfg(feature = "metrics")]
$crate::metrics::$metric.with_label_values(&[$label]).add($value);
};
}

/// Observes a metric with a label value.
#[macro_export]
macro_rules! observe_histogram {
($metric:ident, $value:expr) => {
#[cfg(feature = "metrics")]
$crate::metrics::$metric.observe($value);
};
($metric:ident, $value:expr, $label:expr) => {
#[cfg(feature = "metrics")]
$crate::metrics::$metric.with_label_values(&[$label]).observe($value);
};
}

/// Sets a metric value.
#[macro_export]
macro_rules! metrics_set {
($metric:ident, $value:expr) => {
#[cfg(feature = "metrics")]
$crate::metrics::$metric.set($value);
};
}
54 changes: 54 additions & 0 deletions crates/derive/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Metrics for derivation pipeline stages.
use alloc::{boxed::Box, string::String};
use lazy_static::lazy_static;
use prometheus::{
self, opts, register_gauge_vec, register_histogram, register_histogram_vec, register_int_gauge,
GaugeVec, Histogram, HistogramVec, IntGauge,
};

const RESPONSE_TIME_CUSTOM_BUCKETS: &[f64; 18] = &[
0.00001, 0.00005, 0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.008, 0.01, 0.02, 0.05, 0.08,
0.1, 0.2, 0.5, 0.8, 1.0,
];

lazy_static! {
/// Tracks the L1 origin for the L1 Traversal Stage.
pub static ref ORIGIN_GAUGE: IntGauge = register_int_gauge!(
"origin_gauge",
"Tracks the L1 origin for the L1 Traversal Stage"
).expect("Origin Gauge failed to register");

/// Tracks the time taken for provider methods.
pub static ref PROVIDER_RESPONSE_TIME: HistogramVec = register_histogram_vec!(
"provider_response_time_seconds",
"Provider response times",
&["provider", "method"],
RESPONSE_TIME_CUSTOM_BUCKETS.to_vec()
)
.expect("Failed to register histogram vec");

/// Tracks the time taken for stage advance methods.
pub static ref STAGE_ADVANCE_RESPONSE_TIME: HistogramVec = register_histogram_vec!(
"stage_advance_response_time_seconds",
"Stage advance response times",
&["stage"],
RESPONSE_TIME_CUSTOM_BUCKETS.to_vec()
).expect("Failed to register histogram vec");

/// Tracks the number of derived frames.
pub static ref DERIVED_FRAMES_COUNT: GaugeVec = {
let opts = opts!("derived_frames_count", "Number of derived frames");
register_gauge_vec!(opts, &["status"]).expect("Derived Frames Count failed to register")
};

/// Tracks the number of channel timeouts.
pub static ref CHANNEL_TIMEOUTS: Histogram = {
let channel_timeout_buckets: [f64; 100] = core::array::from_fn(|i| (i * 10) as f64);
register_histogram!(
"channel_timeouts",
"Channel timeouts",
channel_timeout_buckets.to_vec()
).expect("Failed to register histogram vec")
};
}
Loading

0 comments on commit 28c8c7c

Please sign in to comment.