Skip to content

Commit

Permalink
fix: use vise metrics exporter (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki authored Oct 20, 2023
1 parent 6f74b4b commit a31a246
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 143 deletions.
136 changes: 22 additions & 114 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ serde_json = "1.0"
color-eyre = "0.6.2"
eyre = "0.6.8"
dotenvy = "0.15.7"
metrics = "0.21"
envconfig = "0.10.0"
metrics-exporter-prometheus = "0.12"
proc-macro2 = "1.0.69"
bincode = "1.3.3"
futures = "0.3.28"
Expand All @@ -49,6 +47,7 @@ pretty_assertions = "1"
sqlx = "0.7"
chrono = { version = "0.4.31", default-features = false }
vise = { git = "https://github.com/matter-labs/vise.git" }
vise-exporter = { git = "https://github.com/matter-labs/vise.git" }
client = { path = "./client" }
chain-events = { path = "./chain-events" }
storage = { path = "./storage" }
Expand Down
4 changes: 2 additions & 2 deletions bin/withdrawal-finalizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ tokio-util = { workspace = true }
sqlx = { workspace = true, features = ["postgres", "runtime-tokio-rustls"] }
dotenvy = { workspace = true }
tracing = { workspace = true }
vise-exporter = { workspace = true }
vise = { workspace = true }

client = { workspace = true }
storage = { workspace = true }
chain-events = { workspace = true }
vlog = { workspace = true }
finalizer = { workspace = true }
watcher = { workspace = true }
metrics-exporter-prometheus = { workspace = true }
metrics = { workspace = true }
47 changes: 22 additions & 25 deletions bin/withdrawal-finalizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,26 @@ use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgConnection, PgPool};
use chain_events::{BlockEvents, L2EventsListener};
use client::{l1bridge::codegen::IL1Bridge, zksync_contract::codegen::IZkSync, ZksyncMiddleware};
use config::Config;
use metrics_exporter_prometheus::PrometheusBuilder;
use tokio::task::JoinHandle;
use tokio::sync::watch;
use vise_exporter::MetricsExporter;
use watcher::Watcher;

use crate::metrics::MAIN_FINALIZER_METRICS;

mod config;
mod metrics;

const CHANNEL_CAPACITY: usize = 1024 * 16;

fn run_prometheus_exporter() -> Result<JoinHandle<()>> {
let builder = {
let addr = ([0, 0, 0, 0], 3312);
PrometheusBuilder::new().with_http_listener(addr)
};

let (recorder, exporter) = builder.build()?;

metrics::set_boxed_recorder(Box::new(recorder)).expect("failed to set the metrics recorder");
fn run_vise_exporter() -> Result<watch::Sender<()>> {
let (shutdown_sender, mut shutdown_receiver) = watch::channel(());
let exporter = MetricsExporter::default().with_graceful_shutdown(async move {
shutdown_receiver.changed().await.ok();
});
let bind_address = "0.0.0.0:3312".parse().unwrap();
tokio::spawn(exporter.start(bind_address));

Ok(tokio::spawn(async move {
tokio::pin!(exporter);
loop {
tokio::select! {
_ = &mut exporter => {}
}
}
}))
Ok(shutdown_sender)
}

async fn start_from_l1_block<M1, M2>(
Expand Down Expand Up @@ -159,7 +153,7 @@ async fn main() -> Result<()> {
tracing::info!("No sentry url configured");
}

let prometheus_exporter_handle = run_prometheus_exporter()?;
let stop_vise_exporter = run_vise_exporter()?;

// Successful reconnections do not reset the reconnection count trackers in the
// `ethers-rs`. In the logic of reconnections have to happen as long
Expand Down Expand Up @@ -240,8 +234,12 @@ async fn main() -> Result<()> {
let mut interval = tokio::time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
metrics::gauge!("watcher.l1_channel.capacity", blocks_tx.capacity() as f64);
metrics::gauge!("watcher.l2_channel.capacity", we_tx.capacity() as f64)
MAIN_FINALIZER_METRICS
.watcher_l1_channel_capacity
.set(blocks_tx.capacity() as i64);
MAIN_FINALIZER_METRICS
.watcher_l2_channel_capacity
.set(we_tx.capacity() as i64);
}
});

Expand Down Expand Up @@ -290,13 +288,12 @@ async fn main() -> Result<()> {
r = watcher_handle => {
tracing::error!("Finalizer main loop ended with {r:?}");
}
r = prometheus_exporter_handle => {
tracing::error!("Prometheus exporter ended with {r:?}");
}
r = finalizer_handle => {
tracing::error!("Finalizer ended with {r:?}");
}
}

stop_vise_exporter.send_replace(());

Ok(())
}
17 changes: 17 additions & 0 deletions bin/withdrawal-finalizer/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Metrics for main binary

use vise::{Gauge, Metrics};

/// Main finalizer binary metrics
#[derive(Debug, Metrics)]
#[metrics(prefix = "withdrawal_finalizer")]
pub(super) struct FinalizerMainMetrics {
/// Capacity of the channel sending L1 events.
pub watcher_l1_channel_capacity: Gauge,

/// Capacity of the channel sending L2 events.
pub watcher_l2_channel_capacity: Gauge,
}

#[vise::register]
pub(super) static MAIN_FINALIZER_METRICS: vise::Global<FinalizerMainMetrics> = vise::Global::new();

0 comments on commit a31a246

Please sign in to comment.