From 86ce0f831575e601979e2ba61b02a24f3e5cdc00 Mon Sep 17 00:00:00 2001 From: Eric Woolsey Date: Thu, 15 Feb 2024 14:55:36 -0800 Subject: [PATCH] Prometheus (#699) * prometheus * config test * example.env * cargo vet * update service name --- .gitignore | 1 + Cargo.lock | 9 +++++++- Cargo.toml | 3 ++- example.env | 16 +++++++++++++++ src/config.rs | 34 +++++++++++++++++++++++-------- src/main.rs | 44 +++++++++++++++++++++++++++++++--------- supply-chain/config.toml | 4 ++++ 7 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 example.env diff --git a/.gitignore b/.gitignore index 80325d9c..d4bdd4e7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ commitments.json .* +!.gitignore !.github !.editorconfig !.dockerignore diff --git a/Cargo.lock b/Cargo.lock index e6a009c6..1d93f48d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1908,6 +1908,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -5474,6 +5480,7 @@ dependencies = [ "chrono", "clap 4.4.18", "config", + "dotenv", "ethers", "ethers-solc", "eyre", @@ -6016,7 +6023,7 @@ checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" [[package]] name = "telemetry-batteries" version = "0.1.0" -source = "git+https://github.com/worldcoin/telemetry-batteries.git?rev=545e96d#545e96ddb84e4325578531d1858a88edba3e91e2" +source = "git+https://github.com/worldcoin/telemetry-batteries.git?rev=c3c6921#c3c692148d5e5619413e7f588300d692bbef10b6" dependencies = [ "chrono", "dirs", diff --git a/Cargo.toml b/Cargo.toml index 8f06c4cc..ab9f0336 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ axum-server = "0.4.4" bytes = "1.4.0" chrono = { version = "0.4.19", features = ["serde"] } clap = { version = "4.0", features = ["derive"] } -telemetry-batteries = { git = "https://github.com/worldcoin/telemetry-batteries.git", rev = "545e96d" } +telemetry-batteries = { git = "https://github.com/worldcoin/telemetry-batteries.git", rev = "c3c6921" } config = "0.13.4" ethers = { version = "2.0.10", features = ["ws", "ipc", "openssl", "abigen"] } ethers-solc = "2.0.10" @@ -81,6 +81,7 @@ url = { version = "2.2", features = ["serde"] } zeroize = "1.6.0" [dev-dependencies] +dotenv = "0.15.0" hex = "0.4.3" maplit = "1.0.2" micro-oz = { path = "crates/micro-oz" } diff --git a/example.env b/example.env new file mode 100644 index 00000000..a7c231bc --- /dev/null +++ b/example.env @@ -0,0 +1,16 @@ +RUST_LOG="trace,ethers_providers::provider=debug" +SEQ__APP__MIN_BATCH_DELETION_SIZE="10" +SEQ__TREE__TREE_DEPTH="30" +SEQ__TREE__DENSE_TREE_PREFIX_DEPTH="22" +SEQ__TREE__TREE_GC_THRESHOLD="10000" +SEQ__NETWORK__IDENTITY_MANAGER_ADDRESS="0x2a968958E6136FA0FC2Ed068aF8Bef464c7b66EA" +SEQ__RELAYER__KIND="tx_sitter" +SEQ__RELAYER__TX_SITTER_URL="https://tx-sitter.domain.com/1/api/" +SEQ__RELAYER__TX_SITTER_ADDRESS="0x9497c82f012c197a82a325d3c687798ba8850bbc" +SEQ__RELAYER__TX_SITTER_GAS_LIMIT="2000000" +SEQ__APP__PROVERS_URLS='[{"url": "https://prover.domain.com", "prover_type": "insertion", "batch_size": 100,"timeout_s": 30}]' +SEQ__DATABASE__DATABASE="https://1.2.3.4:5432/" +SEQ__SERVICE__DATADOG__TRACES_ENDPOINT="http://1.2.3.4:8126" +SEQ__SERVER__ADDRESS="0.0.0.0:8080" +SEQ__TREE__CACHE_FILE="/data/dense_tree_mmap" +SEQ__PROVIDERS__PRIMARY_NETWORK_PROVIDER="http://localhost:8545" diff --git a/src/config.rs b/src/config.rs index 4b2d73b8..c6fb910e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,7 @@ use std::time::Duration; use ethers::types::{Address, H160}; use semaphore::Field; use serde::{Deserialize, Serialize}; +use telemetry_batteries::metrics::prometheus::PrometheusExporterConfig; use crate::prover::ProverConfig; use crate::utils::secret::SecretUrl; @@ -212,7 +213,15 @@ pub struct ServiceConfig { #[serde(default = "default::service_name")] pub service_name: String, pub datadog: Option, - pub statsd: Option, + #[serde(default = "default::metrics")] + pub metrics: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum MetricsConfig { + Prometheus(PrometheusExporterConfig), + Statsd(StatsdConfig), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -232,8 +241,20 @@ pub struct StatsdConfig { pub mod default { use std::time::Duration; + use telemetry_batteries::metrics::prometheus::PrometheusExporterConfig; + + use super::MetricsConfig; + pub fn service_name() -> String { - "signup-sequencer".to_string() + "signup_sequencer".to_string() + } + + pub fn metrics() -> Option { + Some(MetricsConfig::Prometheus( + PrometheusExporterConfig::HttpListener { + listen_address: "0.0.0.0:9998".parse().unwrap(), + }, + )) } pub fn oz_api_url() -> String { @@ -405,19 +426,14 @@ mod tests { [service.datadog] traces_endpoint = "http://localhost:8126" - [service.statsd] - metrics_host = "localhost" - metrics_port = 8125 - metrics_queue_size = 100 - metrics_buffer_size = 100 - metrics_prefix = "signup_sequencer" + [service.metrics.prometheus.http_listener] + listen_address = "0.0.0.0:9998" "#}; #[test] fn full_toml_round_trip() { let config: Config = toml::from_str(FULL_TOML).unwrap(); let serialized = toml::to_string_pretty(&config).unwrap(); - similar_asserts::assert_eq!(serialized.trim(), FULL_TOML.trim()); } } diff --git a/src/main.rs b/src/main.rs index 6414a09c..4adfa8b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,11 @@ use std::path::PathBuf; use clap::Parser; use signup_sequencer::app::App; -use signup_sequencer::config::{Config, ServiceConfig}; +use signup_sequencer::config::{Config, MetricsConfig, ServiceConfig}; use signup_sequencer::server; use signup_sequencer::shutdown::watch_shutdown_signals; use signup_sequencer::task_monitor::TaskMonitor; +use telemetry_batteries::metrics::prometheus::PrometheusBattery; use telemetry_batteries::metrics::statsd::StatsdBattery; use telemetry_batteries::tracing::datadog::DatadogBattery; use telemetry_batteries::tracing::stdout::StdoutBattery; @@ -67,21 +68,31 @@ fn load_config(args: &Args) -> anyhow::Result { } let settings = settings - .add_source(config::Environment::with_prefix("SEQ").separator("__")) + .add_source( + config::Environment::with_prefix("SEQ") + .separator("__") + .try_parsing(true), + ) .build()?; Ok(settings.try_deserialize::()?) } fn init_telemetry(service: &ServiceConfig) -> anyhow::Result { - if let Some(ref statsd) = service.statsd { - StatsdBattery::init( - &statsd.metrics_host, - statsd.metrics_port, - statsd.metrics_queue_size, - statsd.metrics_buffer_size, - Some(&statsd.metrics_prefix), - )?; + match service.metrics.clone() { + Some(MetricsConfig::Prometheus(prometheus)) => { + PrometheusBattery::init(Some(prometheus))?; + } + Some(MetricsConfig::Statsd(statsd)) => { + StatsdBattery::init( + &statsd.metrics_host, + statsd.metrics_port, + statsd.metrics_queue_size, + statsd.metrics_buffer_size, + Some(&statsd.metrics_prefix), + )?; + } + _ => {} } if let Some(ref datadog) = service.datadog { @@ -95,3 +106,16 @@ fn init_telemetry(service: &ServiceConfig) -> anyhow::Result