Skip to content
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

feat(derive): Granular Provider Metrics #325

Merged
merged 2 commits into from
Jun 27, 2024
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
16 changes: 8 additions & 8 deletions crates/derive/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ macro_rules! timer {

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

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

Expand Down
19 changes: 17 additions & 2 deletions crates/derive/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
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,
self, opts, register_counter_vec, register_gauge_vec, register_histogram,
register_histogram_vec, register_int_gauge, CounterVec, GaugeVec, Histogram, HistogramVec,
IntGauge,
};

const RESPONSE_TIME_CUSTOM_BUCKETS: &[f64; 18] = &[
Expand All @@ -19,6 +20,20 @@ lazy_static! {
"Tracks the L1 origin for the L1 Traversal Stage"
).expect("Origin Gauge failed to register");

/// Tracks the number of provider method calls.
pub static ref PROVIDER_CALLS: CounterVec = register_counter_vec!(
"provider_calls",
"Number of provider method calls",
&["provider", "method"]
).expect("Provider Calls failed to register");

/// Tracks the number of errors in provider methods.
pub static ref PROVIDER_ERRORS: CounterVec = register_counter_vec!(
"provider_errors",
"Number of provider errors",
&["provider", "method", "error"]
).expect("Provider Errors failed to register");

/// Tracks the time taken for provider methods.
pub static ref PROVIDER_RESPONSE_TIME: HistogramVec = register_histogram_vec!(
"provider_response_time_seconds",
Expand Down
51 changes: 51 additions & 0 deletions crates/derive/src/online/alloy_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl AlloyChainProvider {
#[async_trait]
impl ChainProvider for AlloyChainProvider {
async fn header_by_hash(&mut self, hash: B256) -> Result<Header> {
crate::inc!(PROVIDER_CALLS, &["chain_provider", "header_by_hash"]);
crate::timer!(START, PROVIDER_RESPONSE_TIME, &["chain_provider", "header_by_hash"], timer);
if let Some(header) = self.header_by_hash_cache.get(&hash) {
return Ok(header.clone());
Expand All @@ -76,6 +77,10 @@ impl ChainProvider for AlloyChainProvider {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["chain_provider", "header_by_hash", "debug_getRawHeader"]
);
return Err(e);
}
};
Expand All @@ -86,12 +91,14 @@ impl ChainProvider for AlloyChainProvider {
}
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["chain_provider", "header_by_hash", "decode"]);
Err(e)
}
}
}

async fn block_info_by_number(&mut self, number: u64) -> Result<BlockInfo> {
crate::inc!(PROVIDER_CALLS, &["chain_provider", "block_info_by_number"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -108,13 +115,18 @@ impl ChainProvider for AlloyChainProvider {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["chain_provider", "block_info_by_number", "debug_getRawHeader"]
);
return Err(e);
}
};
let header = match Header::decode(&mut raw_header.as_ref()).map_err(|e| anyhow!(e)) {
Ok(h) => h,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["chain_provider", "block_info_by_number", "decode"]);
return Err(e);
}
};
Expand All @@ -130,6 +142,7 @@ impl ChainProvider for AlloyChainProvider {
}

async fn receipts_by_hash(&mut self, hash: B256) -> Result<Vec<Receipt>> {
crate::inc!(PROVIDER_CALLS, &["chain_provider", "receipts_by_hash"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -146,6 +159,10 @@ impl ChainProvider for AlloyChainProvider {
Ok(r) => r,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["chain_provider", "receipts_by_hash", "debug_getRawReceipts"]
);
return Err(e);
}
};
Expand All @@ -167,6 +184,7 @@ impl ChainProvider for AlloyChainProvider {
Ok(r) => r,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["chain_provider", "receipts_by_hash", "decode"]);
return Err(e);
}
};
Expand All @@ -178,6 +196,7 @@ impl ChainProvider for AlloyChainProvider {
&mut self,
hash: B256,
) -> Result<(BlockInfo, Vec<TxEnvelope>)> {
crate::inc!(PROVIDER_CALLS, &["chain_provider", "block_info_and_transactions_by_hash"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -195,13 +214,21 @@ impl ChainProvider for AlloyChainProvider {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["chain_provider", "block_info_and_transactions_by_hash", "debug_getRawBlock"]
);
return Err(e);
}
};
let block = match Block::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e)) {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["chain_provider", "block_info_and_transactions_by_hash", "decode"]
);
return Err(e);
}
};
Expand Down Expand Up @@ -270,6 +297,7 @@ impl AlloyL2ChainProvider {
#[async_trait]
impl L2ChainProvider for AlloyL2ChainProvider {
async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo> {
crate::inc!(PROVIDER_CALLS, &["l2_chain_provider", "l2_block_info_by_number"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -284,13 +312,21 @@ impl L2ChainProvider for AlloyL2ChainProvider {
Ok(p) => p,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["l2_chain_provider", "l2_block_info_by_number", "payload_by_number"]
);
return Err(e);
}
};
let l2_block_info = match payload.to_l2_block_ref(self.rollup_config.as_ref()) {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["l2_chain_provider", "l2_block_info_by_number", "to_l2_block_ref"]
);
return Err(e);
}
};
Expand All @@ -299,6 +335,7 @@ impl L2ChainProvider for AlloyL2ChainProvider {
}

async fn payload_by_number(&mut self, number: u64) -> Result<L2ExecutionPayloadEnvelope> {
crate::inc!(PROVIDER_CALLS, &["l2_chain_provider", "payload_by_number"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -315,13 +352,18 @@ impl L2ChainProvider for AlloyL2ChainProvider {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["l2_chain_provider", "payload_by_number", "debug_getRawBlock"]
);
return Err(e);
}
};
let block = match OpBlock::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e)) {
Ok(b) => b,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["l2_chain_provider", "payload_by_number", "decode"]);
return Err(e);
}
};
Expand All @@ -336,6 +378,7 @@ impl L2ChainProvider for AlloyL2ChainProvider {
number: u64,
rollup_config: Arc<RollupConfig>,
) -> Result<SystemConfig> {
crate::inc!(PROVIDER_CALLS, &["l2_chain_provider", "system_config_by_number"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -350,13 +393,21 @@ impl L2ChainProvider for AlloyL2ChainProvider {
Ok(e) => e,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["l2_chain_provider", "system_config_by_number", "payload_by_number"]
);
return Err(e);
}
};
let sys_config = match envelope.to_system_config(&rollup_config) {
Ok(s) => s,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["l2_chain_provider", "system_config_by_number", "to_system_config"]
);
return Err(e);
}
};
Expand Down
47 changes: 37 additions & 10 deletions crates/derive/src/online/beacon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl OnlineBeaconClient {
#[async_trait]
impl BeaconClient for OnlineBeaconClient {
async fn config_spec(&self) -> Result<APIConfigResponse> {
crate::inc!(PROVIDER_CALLS, &["beacon_client", "config_spec"]);
crate::timer!(START, PROVIDER_RESPONSE_TIME, &["beacon_client", "config_spec"], timer);
let first = match self
.inner
Expand All @@ -67,13 +68,22 @@ impl BeaconClient for OnlineBeaconClient {
Ok(response) => response,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["beacon_client", "config_spec", "request"]);
return Err(e);
}
};
first.json::<APIConfigResponse>().await.map_err(|e| anyhow!(e))
match first.json::<APIConfigResponse>().await.map_err(|e| anyhow!(e)) {
Ok(response) => Ok(response),
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["beacon_client", "config_spec", "decode"]);
Err(e)
}
}
}

async fn beacon_genesis(&self) -> Result<APIGenesisResponse> {
crate::inc!(PROVIDER_CALLS, &["beacon_client", "beacon_genesis"]);
crate::timer!(START, PROVIDER_RESPONSE_TIME, &["beacon_client", "beacon_genesis"], timer);
let first = match self
.inner
Expand All @@ -85,17 +95,26 @@ impl BeaconClient for OnlineBeaconClient {
Ok(response) => response,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["beacon_client", "beacon_genesis", "request"]);
return Err(e);
}
};
first.json::<APIGenesisResponse>().await.map_err(|e| anyhow!(e))
match first.json::<APIGenesisResponse>().await.map_err(|e| anyhow!(e)) {
Ok(response) => Ok(response),
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["beacon_client", "beacon_genesis", "decode"]);
Err(e)
}
}
}

async fn beacon_blob_side_cars(
&self,
slot: u64,
hashes: &[IndexedBlobHash],
) -> Result<Vec<APIBlobSidecar>> {
crate::inc!(PROVIDER_CALLS, &["beacon_client", "beacon_blob_side_cars"]);
crate::timer!(
START,
PROVIDER_RESPONSE_TIME,
Expand All @@ -112,17 +131,25 @@ impl BeaconClient for OnlineBeaconClient {
Ok(response) => response,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["beacon_client", "beacon_blob_side_cars", "request"]
);
return Err(e);
}
};
let raw_response = match raw_response
.json::<APIGetBlobSidecarsResponse>()
.await
.map_err(|e| anyhow!(e))
{
Ok(response) => response,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["beacon_client", "beacon_blob_side_cars", "decode"]);
return Err(e);
}
};
let raw_response =
match raw_response.json::<APIGetBlobSidecarsResponse>().await.map_err(|e| anyhow!(e)) {
Ok(response) => response,
Err(e) => {
crate::timer!(DISCARD, timer);
return Err(e);
}
};

let mut sidecars = Vec::with_capacity(hashes.len());

Expand Down
7 changes: 7 additions & 0 deletions crates/derive/src/online/blob_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ where
block_ref: &BlockInfo,
blob_hashes: &[IndexedBlobHash],
) -> Result<Vec<Blob>, BlobProviderError> {
crate::inc!(PROVIDER_CALLS, &["blob_provider", "get_blobs"]);
crate::timer!(START, PROVIDER_RESPONSE_TIME, &["blob_provider", "get_blobs"], timer);
// Fetches the genesis timestamp and slot interval from the
// [BeaconGenesis] and [ConfigSpec] if not previously loaded.
if let Err(e) = self.load_configs().await {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["blob_provider", "get_blobs", "load_configs"]);
return Err(e);
}

Expand All @@ -141,6 +143,10 @@ where
Ok(sidecars) => sidecars,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(
PROVIDER_ERRORS,
&["blob_provider", "get_blobs", "fetch_filtered_sidecars"]
);
return Err(e);
}
};
Expand All @@ -161,6 +167,7 @@ where
Ok(blobs) => blobs,
Err(e) => {
crate::timer!(DISCARD, timer);
crate::inc!(PROVIDER_ERRORS, &["blob_provider", "get_blobs", "verify_blob"]);
return Err(BlobProviderError::Custom(e));
}
};
Expand Down
Loading
Loading