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(rollup): init telemetry #50

Merged
merged 1 commit into from
Aug 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
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(())
}