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
239 changes: 175 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ insta = "1.36.1"
itertools = "0.12.1"
itoa = "1.0.10"
jsonrpsee = { version = "0.20" }
metrics = "0.22.1"
once_cell = "1.17.1"
pbjson-types = "0.6"
# Note that when updating the penumbra versions, vendored types in `proto/sequencerapis/astria_vendored` may need to be updated as well.
Expand Down
1 change: 0 additions & 1 deletion crates/astria-bridge-withdrawer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ethers = { workspace = true, features = ["ws"] }
hyper = { workspace = true }
humantime = { workspace = true }
ibc-types = { workspace = true }
metrics = { workspace = true }
once_cell = { workspace = true }
pin-project-lite = { workspace = true }
prost = { workspace = true }
Expand Down
10 changes: 2 additions & 8 deletions crates/astria-bridge-withdrawer/src/bridge_withdrawer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
net::SocketAddr,
sync::{
Arc,
OnceLock,
},
sync::Arc,
time::Duration,
};

Expand Down Expand Up @@ -72,10 +69,7 @@ impl BridgeWithdrawer {
/// # Errors
///
/// - 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);

pub fn new(cfg: Config, metrics: &'static Metrics) -> eyre::Result<(Self, ShutdownHandle)> {
let shutdown_handle = ShutdownHandle::new();
let Config {
api_addr,
Expand Down
1 change: 1 addition & 0 deletions crates/astria-bridge-withdrawer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub(crate) mod metrics;
pub use bridge_withdrawer::BridgeWithdrawer;
pub use build_info::BUILD_INFO;
pub use config::Config;
pub use metrics::Metrics;
22 changes: 12 additions & 10 deletions crates/astria-bridge-withdrawer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ async fn main() -> ExitCode {
.set_no_otel(cfg.no_otel)
.set_force_stdout(cfg.force_stdout)
.set_pretty_print(cfg.pretty_print)
.filter_directives(&cfg.log);
.set_filter_directives(&cfg.log);

if !cfg.no_metrics {
telemetry_conf = telemetry_conf
.metrics_addr(&cfg.metrics_http_listener_addr)
.service_name(env!("CARGO_PKG_NAME"));
telemetry_conf =
telemetry_conf.set_metrics(&cfg.metrics_http_listener_addr, env!("CARGO_PKG_NAME"));
}

if let Err(e) = telemetry_conf
.try_init()
let (metrics, _telemetry_guard) = match telemetry_conf
.try_init(&())
.wrap_err("failed to setup telemetry")
{
eprintln!("initializing conductor failed:\n{e:?}");
return ExitCode::FAILURE;
}
Err(e) => {
eprintln!("initializing conductor failed:\n{e:?}");
return ExitCode::FAILURE;
}
Ok(metrics_and_guard) => metrics_and_guard,
};

info!(
config = serde_json::to_string(&cfg).expect("serializing to a string cannot fail"),
Expand All @@ -52,7 +54,7 @@ async fn main() -> ExitCode {

let mut sigterm = signal(SignalKind::terminate())
.expect("setting a SIGTERM listener should always work on Unix");
let (withdrawer, shutdown_handle) = match BridgeWithdrawer::new(cfg) {
let (withdrawer, shutdown_handle) = match BridgeWithdrawer::new(cfg, metrics) {
Err(error) => {
error!(%error, "failed initializing bridge withdrawer");
return ExitCode::FAILURE;
Expand Down
134 changes: 69 additions & 65 deletions crates/astria-bridge-withdrawer/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::time::Duration;

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

pub(crate) struct Metrics {
pub struct Metrics {
current_nonce: Gauge,
nonce_fetch_count: Counter,
nonce_fetch_failure_count: Counter,
Expand All @@ -24,56 +21,6 @@ pub(crate) struct Metrics {
}

impl Metrics {
#[must_use]
pub(crate) fn new() -> Self {
describe_gauge!(CURRENT_NONCE, Unit::Count, "The current nonce");
let current_nonce = gauge!(CURRENT_NONCE);

describe_counter!(
NONCE_FETCH_COUNT,
Unit::Count,
"The number of times a nonce was fetched from the sequencer"
);
let nonce_fetch_count = counter!(NONCE_FETCH_COUNT);

describe_counter!(
NONCE_FETCH_FAILURE_COUNT,
Unit::Count,
"The number of failed attempts to fetch nonce from sequencer"
);
let nonce_fetch_failure_count = counter!(NONCE_FETCH_FAILURE_COUNT);

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

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 {
current_nonce,
nonce_fetch_count,
nonce_fetch_failure_count,
nonce_fetch_latency,
sequencer_submission_failure_count,
sequencer_submission_latency,
}
}

pub(crate) fn set_current_nonce(&self, nonce: u32) {
self.current_nonce.set(nonce);
}
Expand All @@ -99,11 +46,68 @@ impl Metrics {
}
}

metric_names!(pub const METRICS_NAMES:
CURRENT_NONCE,
impl metrics::Metrics for Metrics {
type Config = ();

fn register(
builder: &mut RegisteringBuilder,
_config: &Self::Config,
) -> Result<Self, metrics::Error> {
let current_nonce = builder
.new_gauge_factory(CURRENT_NONCE, "The current nonce")?
.register()?;

let nonce_fetch_count = builder
.new_counter_factory(
NONCE_FETCH_COUNT,
"The number of times a nonce was fetched from the sequencer",
)?
.register()?;

let nonce_fetch_failure_count = builder
.new_counter_factory(
NONCE_FETCH_FAILURE_COUNT,
"The number of failed attempts to fetch nonce from sequencer",
)?
.register()?;

let nonce_fetch_latency = builder
.new_histogram_factory(
NONCE_FETCH_LATENCY,
"The latency of fetching nonce from sequencer",
)?
.register()?;

let sequencer_submission_failure_count = builder
.new_counter_factory(
SEQUENCER_SUBMISSION_FAILURE_COUNT,
"The number of failed transaction submissions to the sequencer",
)?
.register()?;

let sequencer_submission_latency = builder
.new_histogram_factory(
SEQUENCER_SUBMISSION_LATENCY,
"The latency of submitting a transaction to the sequencer",
)?
.register()?;

Ok(Self {
current_nonce,
nonce_fetch_count,
nonce_fetch_failure_count,
nonce_fetch_latency,
sequencer_submission_failure_count,
sequencer_submission_latency,
})
}
}

metric_names!(const METRICS_NAMES:
NONCE_FETCH_COUNT,
NONCE_FETCH_FAILURE_COUNT,
NONCE_FETCH_LATENCY,
CURRENT_NONCE,
SEQUENCER_SUBMISSION_FAILURE_COUNT,
SEQUENCER_SUBMISSION_LATENCY
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use astria_bridge_withdrawer::{
bridge_withdrawer::ShutdownHandle,
BridgeWithdrawer,
Config,
Metrics,
};
use astria_core::{
primitive::v1::asset::{
Expand Down Expand Up @@ -38,6 +39,7 @@ use sequencer_client::{
Address,
NonceResponse,
};
use telemetry::metrics;
use tempfile::NamedTempFile;
use tokio::task::JoinHandle;
use tracing::{
Expand Down Expand Up @@ -73,17 +75,17 @@ static TELEMETRY: Lazy<()> = Lazy::new(|| {
if std::env::var_os("TEST_LOG").is_some() {
let filter_directives = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
telemetry::configure()
.no_otel()
.stdout_writer(std::io::stdout)
.set_no_otel(true)
.set_stdout_writer(std::io::stdout)
.set_pretty_print(true)
.filter_directives(&filter_directives)
.try_init()
.set_filter_directives(&filter_directives)
.try_init::<Metrics>(&())
.unwrap();
} else {
telemetry::configure()
.no_otel()
.stdout_writer(std::io::sink)
.try_init()
.set_no_otel(true)
.set_stdout_writer(std::io::sink)
.try_init::<Metrics>(&())
.unwrap();
}
});
Expand All @@ -109,6 +111,9 @@ pub struct TestBridgeWithdrawer {

/// The config used to initialize the bridge withdrawer.
pub config: Config,

/// A handle to the metrics.
pub metrics_handle: metrics::Handle,
}

impl Drop for TestBridgeWithdrawer {
Expand Down Expand Up @@ -282,8 +287,15 @@ impl TestBridgeWithdrawerConfig {
};

info!(config = serde_json::to_string(&config).unwrap());

let (metrics, metrics_handle) = metrics::ConfigBuilder::new()
.set_global_recorder(false)
.build(&())
.unwrap();
let metrics = Box::leak(Box::new(metrics));

let (bridge_withdrawer, bridge_withdrawer_shutdown_handle) =
BridgeWithdrawer::new(config.clone()).unwrap();
BridgeWithdrawer::new(config.clone(), metrics).unwrap();
let api_address = bridge_withdrawer.local_addr();
let bridge_withdrawer = tokio::task::spawn(bridge_withdrawer.run());

Expand All @@ -295,6 +307,7 @@ impl TestBridgeWithdrawerConfig {
bridge_withdrawer_shutdown_handle: Some(bridge_withdrawer_shutdown_handle),
bridge_withdrawer,
config,
metrics_handle,
};

test_bridge_withdrawer.mount_startup_responses().await;
Expand Down
1 change: 0 additions & 1 deletion crates/astria-composer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ tracing = { workspace = true, features = ["attributes"] }
tryhard = { workspace = true }
tonic = { workspace = true }
tokio-stream = { workspace = true, features = ["net"] }
metrics = { workspace = true }

[dependencies.sequencer-client]
package = "astria-sequencer-client"
Expand Down
2 changes: 1 addition & 1 deletion crates/astria-composer/src/collectors/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use ethers::{
},
types::Transaction,
};
use metrics::Counter;
use telemetry::metrics::Counter;
use tokio::{
select,
sync::{
Expand Down
9 changes: 2 additions & 7 deletions crates/astria-composer/src/composer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::HashMap,
net::SocketAddr,
sync::OnceLock,
time::Duration,
};

Expand Down Expand Up @@ -120,12 +119,7 @@ impl Composer {
/// An error is returned if the composer fails to be initialized.
/// See `[from_config]` for its error scenarios.
#[instrument(skip_all, err)]
pub async fn from_config(cfg: &Config) -> eyre::Result<Self> {
static METRICS: OnceLock<Metrics> = OnceLock::new();

let rollups = cfg.parse_rollups()?;
let metrics = METRICS.get_or_init(|| Metrics::new(rollups.keys()));

pub async fn from_config(cfg: &Config, metrics: &'static Metrics) -> eyre::Result<Self> {
let (composer_status_sender, _) = watch::channel(Status::default());
let shutdown_token = CancellationToken::new();

Expand Down Expand Up @@ -166,6 +160,7 @@ impl Composer {
"API server listening"
);

let rollups = cfg.parse_rollups()?;
let geth_collectors = rollups
.iter()
.map(|(rollup_name, url)| {
Expand Down
Loading