Skip to content

Commit

Permalink
Prometheus (#699)
Browse files Browse the repository at this point in the history
* prometheus

* config test

* example.env

* cargo vet

* update service name
  • Loading branch information
0xForerunner authored Feb 15, 2024
1 parent 3d6012d commit 86ce0f8
Showing 7 changed files with 90 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
commitments.json
.*

!.gitignore
!.github
!.editorconfig
!.dockerignore
9 changes: 8 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
16 changes: 16 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 25 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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<DatadogConfig>,
pub statsd: Option<StatsdConfig>,
#[serde(default = "default::metrics")]
pub metrics: Option<MetricsConfig>,
}

#[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<MetricsConfig> {
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());
}
}
44 changes: 34 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Config> {
}

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::<Config>()?)
}

fn init_telemetry(service: &ServiceConfig) -> anyhow::Result<TracingShutdownHandle> {
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<TracingShutdownHand
Ok(StdoutBattery::init())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_example_env() {
dotenv::from_path("example.env").ok();
let args = Args { config: None };
let config = load_config(&args).unwrap();
println!("{:#?}", config);
}
}
4 changes: 4 additions & 0 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
@@ -540,6 +540,10 @@ criteria = "safe-to-deploy"
version = "0.3.0"
criteria = "safe-to-deploy"

[[exemptions.dotenv]]
version = "0.15.0"
criteria = "safe-to-run"

[[exemptions.dotenvy]]
version = "0.15.7"
criteria = "safe-to-deploy"

0 comments on commit 86ce0f8

Please sign in to comment.