Skip to content

Commit

Permalink
feat(examples): Send Logs to Loki (#321)
Browse files Browse the repository at this point in the history
* feat(examples): loki tracing

* feat(examples): loki tracing

* feat(examples): loki tracing

* fixes
  • Loading branch information
refcell authored Jun 26, 2024
1 parent f44f87f commit 3a9c70e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 9 deletions.
93 changes: 92 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/trusted-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ kona-derive = { path = "../../crates/derive", version = "0.0.2", features = ["se

# Custom dependencies
lazy_static = "1.5.0"
tracing-loki = "0.2.5"
reqwest = "0.12"
actix-web = "4.8.0"
prometheus = { version = "0.13.4", features = ["process"] }
tokio = { version = "1.37.0", features = ["full"] }
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
clap = { version = "4.5.4", features = ["derive", "env"] }
serde = { version = "1.0.198", features = ["derive"] }
alloy-provider = { version = "0.1", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions examples/trusted-sync/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
--start-l2-block $START_L2_BLOCK \
--metrics-server-addr $METRICS_SERVER_ADDR \
--metrics-server-port $METRICS_SERVER_PORT \
--loki-server-addr $LOKI_SERVER_ADDR \
--loki-server-port $LOKI_SERVER_PORT \
-vvv
18 changes: 18 additions & 0 deletions examples/trusted-sync/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const L2_RPC_URL: &str = "L2_RPC_URL";
const BEACON_URL: &str = "BEACON_URL";
const DEFAULT_METRICS_SERVER_ADDR: &str = "127.0.0.1";
const DEFAULT_METRICS_SERVER_PORT: u16 = 9000;
const DEFAULT_LOKI_SERVER_ADDR: &str = "127.0.0.1";
const DEFAULT_LOKI_SERVER_PORT: u16 = 3133;

/// The host binary CLI application arguments.
#[derive(Parser, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -34,6 +36,12 @@ pub struct Cli {
/// The metrics server port.
#[clap(long, help = "Port of the metrics server")]
pub metrics_server_port: Option<u16>,
/// The address of the loki server.
#[clap(long, help = "Address of the loki server")]
pub loki_server_addr: Option<String>,
/// The loki server port.
#[clap(long, help = "Port of the loki server")]
pub loki_server_port: Option<u16>,
}

impl Cli {
Expand All @@ -48,6 +56,16 @@ impl Cli {
)
}

/// Returns the full loki server address.
pub fn loki_addr(&self) -> Url {
let str = format!(
"http://{}:{}",
self.loki_server_addr.clone().unwrap_or_else(|| DEFAULT_LOKI_SERVER_ADDR.to_string()),
self.loki_server_port.unwrap_or(DEFAULT_LOKI_SERVER_PORT)
);
Url::parse(&str).expect("Failed to parse loki server address")
}

/// Returns the l1 rpc url from CLI or environment variable.
pub fn l1_rpc_url(&self) -> Result<Url> {
let url = if let Some(s) = self.l1_rpc_url.clone() {
Expand Down
3 changes: 2 additions & 1 deletion examples/trusted-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const LOG_TARGET: &str = "trusted-sync";
#[actix_web::main]
async fn main() -> Result<()> {
let cfg = cli::Cli::parse();
telemetry::init(cfg.v)?;
let loki_addr = cfg.loki_addr();
telemetry::init(cfg.v, loki_addr).await?;
let addr = cfg.metrics_server_addr();
let handle = tokio::spawn(async { sync(cfg).await });
tokio::select! {
Expand Down
25 changes: 19 additions & 6 deletions examples/trusted-sync/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
use anyhow::{anyhow, Result};
use reqwest::Url;
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, prelude::*};

pub fn init(v: u8) -> Result<()> {
let subscriber = tracing_subscriber::fmt()
.with_max_level(match v {
pub async fn init(v: u8, addr: Url) -> Result<()> {
let (loki_layer, task) = tracing_loki::builder()
.label("environment", "production")
.map_err(|e| anyhow!(e))?
.extra_field("pid", format!("{}", std::process::id()))
.map_err(|e| anyhow!(e))?
.build_url(addr)
.map_err(|e| anyhow!(e))?;

let std_layer = tracing_subscriber::fmt::Layer::default().with_writer(
std::io::stdout.with_max_level(match v {
0 => Level::ERROR,
1 => Level::WARN,
2 => Level::INFO,
3 => Level::DEBUG,
_ => Level::TRACE,
})
.finish();
tracing::subscriber::set_global_default(subscriber).map_err(|e| anyhow!(e))
}),
);
let subscriber = tracing_subscriber::registry().with(loki_layer).with(std_layer);
tracing::subscriber::set_global_default(subscriber).map_err(|e| anyhow!(e))?;
tokio::spawn(task);
Ok(())
}
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ docker-run-ts:
-e START_L2_BLOCK=$START_L2_BLOCK \
-e METRICS_SERVER_ADDR=$METRICS_SERVER_ADDR \
-e METRICS_SERVER_PORT=$METRICS_SERVER_PORT \
-e LOKI_SERVER_ADDR=$LOKI_SERVER_ADDR \
-e LOKI_SERVER_PORT=$LOKI_SERVER_PORT \
trusted-sync

0 comments on commit 3a9c70e

Please sign in to comment.