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
2 changes: 1 addition & 1 deletion crates/astria-bridge-withdrawer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod api;
mod build_info;
pub(crate) mod config;
pub mod metrics_init;
pub(crate) mod metrics;
pub mod withdrawer;

pub use build_info::BUILD_INFO;
Expand Down
4 changes: 1 addition & 3 deletions crates/astria-bridge-withdrawer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::process::ExitCode;

use astria_bridge_withdrawer::{
metrics_init,
Config,
Service,
BUILD_INFO,
Expand Down Expand Up @@ -35,8 +34,7 @@ async fn main() -> ExitCode {
if !cfg.no_metrics {
telemetry_conf = telemetry_conf
.metrics_addr(&cfg.metrics_http_listener_addr)
.service_name(env!("CARGO_PKG_NAME"))
.register_metrics(metrics_init::register);
.service_name(env!("CARGO_PKG_NAME"));
}

if let Err(e) = telemetry_conf
Expand Down
142 changes: 142 additions & 0 deletions crates/astria-bridge-withdrawer/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use std::time::Duration;

use metrics::{
counter,
describe_counter,
describe_gauge,
describe_histogram,
gauge,
histogram,
Counter,
Gauge,
Histogram,
Unit,
};
use telemetry::metric_names;

pub(crate) struct Metrics {
nonce_fetch_count: Counter,
nonce_fetch_failure_count: Counter,
nonce_fetch_latency: Histogram,
current_nonce: Gauge,
sequencer_submission_failure_count: Counter,
sequencer_submission_latency: Histogram,
}

impl Metrics {
#[must_use]
pub(crate) fn new() -> Self {
describe_counter!(
NONCE_FETCH_COUNT,
Unit::Count,
"The number of times we have attempted to fetch the nonce"
);
let nonce_fetch_count = counter!(NONCE_FETCH_COUNT);

describe_counter!(
NONCE_FETCH_FAILURE_COUNT,
Unit::Count,
"The number of times we have failed to fetch the nonce"
);
let nonce_fetch_failure_count = counter!(NONCE_FETCH_FAILURE_COUNT);

describe_histogram!(
NONCE_FETCH_LATENCY,
Unit::Seconds,
"The latency of nonce fetch"
);
let nonce_fetch_latency = histogram!(NONCE_FETCH_LATENCY);

describe_gauge!(CURRENT_NONCE, Unit::Count, "The current nonce");
let current_nonce = gauge!(CURRENT_NONCE);

describe_counter!(
SEQUENCER_SUBMISSION_FAILURE_COUNT,
Unit::Count,
"The number of failed transaction submissions to the sequencer"
);
let sequencer_submission_failure_count = counter!(SEQUENCER_SUBMISSION_FAILURE_COUNT);

describe_histogram!(
SEQUENCER_SUBMISSION_LATENCY,
Unit::Seconds,
"The latency of submitting a transaction to the sequencer"
);
let sequencer_submission_latency = histogram!(SEQUENCER_SUBMISSION_LATENCY);

Self {
nonce_fetch_count,
nonce_fetch_failure_count,
nonce_fetch_latency,
current_nonce,
sequencer_submission_failure_count,
sequencer_submission_latency,
}
}

pub(crate) fn increment_nonce_fetch_count(&self) {
self.nonce_fetch_count.increment(1);
}

pub(crate) fn increment_nonce_fetch_failure_count(&self) {
self.nonce_fetch_failure_count.increment(1);
}

pub(crate) fn record_nonce_fetch_latency(&self, latency: Duration) {
self.nonce_fetch_latency.record(latency);
}

pub(crate) fn set_current_nonce(&self, nonce: u32) {
self.current_nonce.set(nonce);
}

pub(crate) fn record_sequencer_submission_latency(&self, latency: Duration) {
self.sequencer_submission_latency.record(latency);
}

pub(crate) fn increment_sequencer_submission_failure_count(&self) {
self.sequencer_submission_failure_count.increment(1);
}
}

metric_names!(pub const METRICS_NAMES:
CURRENT_NONCE,
NONCE_FETCH_COUNT,
NONCE_FETCH_FAILURE_COUNT,
NONCE_FETCH_LATENCY,
SEQUENCER_SUBMISSION_FAILURE_COUNT,
SEQUENCER_SUBMISSION_LATENCY
);

#[cfg(test)]
mod tests {
use super::{
CURRENT_NONCE,
NONCE_FETCH_COUNT,
NONCE_FETCH_FAILURE_COUNT,
NONCE_FETCH_LATENCY,
SEQUENCER_SUBMISSION_FAILURE_COUNT,
SEQUENCER_SUBMISSION_LATENCY,
};

#[track_caller]
fn assert_const(actual: &'static str, suffix: &str) {
// XXX: hard-code this so the crate name isn't accidentally changed.
const CRATE_NAME: &str = "astria_bridge_withdrawer";
let expected = format!("{CRATE_NAME}_{suffix}");
assert_eq!(expected, actual);
}

#[test]
fn metrics_are_as_expected() {
assert_const(CURRENT_NONCE, "current_nonce");
assert_const(NONCE_FETCH_COUNT, "nonce_fetch_count");
assert_const(NONCE_FETCH_FAILURE_COUNT, "nonce_fetch_failure_count");
assert_const(NONCE_FETCH_LATENCY, "nonce_fetch_latency");
assert_const(
SEQUENCER_SUBMISSION_FAILURE_COUNT,
"sequencer_submission_failure_count",
);
assert_const(SEQUENCER_SUBMISSION_LATENCY, "sequencer_submission_latency");
}
}
83 changes: 0 additions & 83 deletions crates/astria-bridge-withdrawer/src/metrics_init.rs

This file was deleted.

10 changes: 9 additions & 1 deletion crates/astria-bridge-withdrawer/src/withdrawer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{
net::SocketAddr,
sync::Arc,
sync::{
Arc,
OnceLock,
},
time::Duration,
};

Expand Down Expand Up @@ -39,6 +42,7 @@ use self::{
use crate::{
api,
config::Config,
metrics::Metrics,
};

mod batch;
Expand All @@ -62,6 +66,9 @@ impl Service {
///
/// - If the provided `api_addr` string cannot be parsed as a socket address.
pub fn new(cfg: Config) -> eyre::Result<(Self, ShutdownHandle)> {
static METRICS: OnceLock<Metrics> = OnceLock::new();
let metrics = METRICS.get_or_init(Metrics::new);

let shutdown_handle = ShutdownHandle::new();
let Config {
api_addr,
Expand All @@ -87,6 +94,7 @@ impl Service {
state: state.clone(),
expected_fee_asset_id: asset::Id::from_denom(&fee_asset_denomination),
min_expected_fee_asset_balance: u128::from(min_expected_fee_asset_balance),
metrics,
}
.build()
.wrap_err("failed to initialize submitter")?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ use tokio_util::sync::CancellationToken;
use tracing::info;

use super::state::State;
use crate::withdrawer::{
submitter::Batch,
SequencerStartupInfo,
use crate::{
metrics::Metrics,
withdrawer::{
submitter::Batch,
SequencerStartupInfo,
},
};

const BATCH_QUEUE_SIZE: usize = 256;
Expand Down Expand Up @@ -60,6 +63,7 @@ pub(crate) struct Builder {
pub(crate) state: Arc<State>,
pub(crate) expected_fee_asset_id: asset::Id,
pub(crate) min_expected_fee_asset_balance: u128,
pub(crate) metrics: &'static Metrics,
}

impl Builder {
Expand All @@ -73,6 +77,7 @@ impl Builder {
state,
expected_fee_asset_id,
min_expected_fee_asset_balance,
metrics,
} = self;

let signer = super::signer::SequencerKey::try_from_path(sequencer_key_path)
Expand All @@ -98,6 +103,7 @@ impl Builder {
startup_tx,
expected_fee_asset_id,
min_expected_fee_asset_balance,
metrics,
},
handle,
))
Expand Down
Loading