Skip to content

Commit

Permalink
feat(rollup): init telemetry (#50)
Browse files Browse the repository at this point in the history
Added the ability to start tracing to `stdout` with the following
options:

- tracing target: provided by default with `RUST_LOG` directives. Falls
back to `hera=info` if not provided.
- whether to show the target in stdout: `RUST_LOG_TARGET` (don't show it
only when set to `0`)
- whether to use ANSI formatting and colors: `RUST_LOG_STYLE` (don't use
it only if set to `never`)

Also initialized a Prometheus subscriber on the provided port.
Tested on the Hera binary.

closes #33
  • Loading branch information
merklefruit authored Aug 27, 2024
1 parent 13278aa commit b0e797d
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
94 changes: 94 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions bin/hera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
# Local Dependencies
rollup = { path = "../../crates/rollup" }

# Workspace
eyre.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
Expand Down
6 changes: 5 additions & 1 deletion bin/hera/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
use eyre::Result;

fn main() -> Result<()> {
unimplemented!()
rollup::init_telemetry_stack(8090)?;

tracing::info!("Hera OP Stack Rollup node");

Ok(())
}
4 changes: 4 additions & 0 deletions crates/rollup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ kona-derive.workspace = true
kona-primitives.workspace = true
superchain-registry = { workspace = true, default-features = false }

# Telemetry
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt"] }
metrics-exporter-prometheus = { version = "0.15.3", features = ["http-listener"] }

# Misc
url = "2.5.2"
serde_json = "1"
Expand Down
3 changes: 3 additions & 0 deletions crates/rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ pub use validator::AttributesValidator;
mod pipeline;
pub use pipeline::{new_rollup_pipeline, RollupPipeline};

mod telemetry;
pub use telemetry::init_telemetry_stack;

/// The identifier of the Hera Execution Extension.
pub const HERA_EXEX_ID: &str = "hera";
50 changes: 50 additions & 0 deletions crates/rollup/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::{io::IsTerminal, net::SocketAddr};

use eyre::{bail, Result};
use metrics_exporter_prometheus::PrometheusBuilder;
use tracing::{info, Level};
use tracing_subscriber::{
fmt::Layer as FmtLayer, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
};

/// Initialize the tracing stack and Prometheus metrics recorder.
///
/// This function should be called at the beginning of the program.
pub fn init_telemetry_stack(metrics_port: u16) -> Result<()> {
let filter = EnvFilter::builder().with_default_directive("hera=info".parse()?).from_env_lossy();

// Whether to use ANSI formatting and colors in the console output.
// If unset, always use colors if stdout is a tty.
// If set to "never", just disable all colors.
let should_use_colors = match std::env::var("RUST_LOG_STYLE") {
Ok(val) => val != "never",
Err(_) => std::io::stdout().is_terminal(),
};

// Whether to show the tracing target in the console output.
// If set, always show the target unless explicitly set to "0".
// If unset, show target only if the filter is more verbose than INFO.
let should_show_target = match std::env::var("RUST_LOG_TARGET") {
Ok(val) => val != "0",
Err(_) => filter.max_level_hint().map_or(true, |max_level| max_level > Level::INFO),
};

let std_layer = FmtLayer::new()
.with_ansi(should_use_colors)
.with_target(should_show_target)
.with_writer(std::io::stdout)
.with_filter(filter);

tracing_subscriber::registry().with(std_layer).try_init()?;

let prometheus_addr = SocketAddr::from(([0, 0, 0, 0], metrics_port));
let builder = PrometheusBuilder::new().with_http_listener(prometheus_addr);

if let Err(e) = builder.install() {
bail!("failed to install Prometheus recorder: {:?}", e);
} else {
info!("Telemetry initialized. Serving Prometheus metrics at: http://{}", prometheus_addr);
}

Ok(())
}

0 comments on commit b0e797d

Please sign in to comment.