diff --git a/config.example.toml b/config.example.toml index d067c466..b8f70d4a 100644 --- a/config.example.toml +++ b/config.example.toml @@ -117,7 +117,7 @@ docker_image = "test_da_commit" # See also `examples/da_commit/src/main.rs` for more information sleep_secs = 5 -# Configuration for how metrics should be collected and scraped +# Optional configuration for collecting and scraping metrics. [metrics] # Path to a `prometheus.yml` file to use in Prometheus. If using a custom config file, be sure to add a # file discovery section as follows: diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 6ea0f82c..30e672bf 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -29,7 +29,7 @@ pub(super) const CB_TARGETS_FILE: &str = "targets.json"; // needs to match prome pub(super) const PROMETHEUS_DATA_VOLUME: &str = "prometheus-data"; pub(super) const GRAFANA_DATA_VOLUME: &str = "grafana-data"; -const METRICS_NETWORK: &str = "monitoring_network"; +const DEFAULT_NETWORK: &str = "default_network"; const SIGNER_NETWORK: &str = "signer_network"; /// Builds the docker compose file for the Commit-Boost services @@ -126,7 +126,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> image: Some(module.docker_image), // TODO: allow service to open ports here networks: Networks::Simple(vec![ - METRICS_NETWORK.to_owned(), + DEFAULT_NETWORK.to_owned(), SIGNER_NETWORK.to_owned(), ]), volumes: vec![config_volume.clone(), log_volume], @@ -157,7 +157,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> Service { container_name: Some(module_cid.clone()), image: Some(module.docker_image), - networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), + networks: Networks::Simple(vec![DEFAULT_NETWORK.to_owned()]), volumes: vec![config_volume.clone(), log_volume], environment: Environment::KvPair(module_envs), depends_on: DependsOnOptions::Simple(vec!["cb_pbs".to_owned()]), @@ -186,7 +186,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> "{}:{}", cb_config.pbs.pbs_config.port, cb_config.pbs.pbs_config.port )]), - networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), + networks: Networks::Simple(vec![DEFAULT_NETWORK.to_owned()]), volumes: vec![config_volume.clone(), log_volume], environment: Environment::KvPair(pbs_envs), ..Service::default() @@ -251,7 +251,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> container_name: Some("cb_signer".to_owned()), image: Some(signer_config.docker_image), networks: Networks::Simple(vec![ - METRICS_NETWORK.to_owned(), + DEFAULT_NETWORK.to_owned(), SIGNER_NETWORK.to_owned(), ]), volumes, @@ -271,7 +271,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> let mut compose = Compose::default(); compose.networks.0.insert( - METRICS_NETWORK.to_owned(), + DEFAULT_NETWORK.to_owned(), MapOrEmpty::Map(NetworkSettings { driver: Some("bridge".to_owned()), ..NetworkSettings::default() @@ -284,99 +284,103 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> ..NetworkSettings::default() }), ); - - let prom_volume = Volumes::Simple(format!( - "{}:/etc/prometheus/prometheus.yml", - cb_config.metrics.prometheus_config - )); - // TODO: fix path to targets file - let targets_volume = - Volumes::Simple(format!("./{}:/etc/prometheus/targets.json", CB_TARGETS_FILE)); - - let data_volume = Volumes::Simple(format!("{}:/prometheus", PROMETHEUS_DATA_VOLUME)); - - let grafana_data_volume = Volumes::Simple(format!("{}:/var/lib/grafana", GRAFANA_DATA_VOLUME)); - - volumes.insert( - PROMETHEUS_DATA_VOLUME.to_owned(), - MapOrEmpty::Map(ComposeVolume { - driver: Some("local".to_owned()), - driver_opts: IndexMap::default(), - external: None, - labels: Labels::default(), - name: None, - }), - ); - - volumes.insert( - GRAFANA_DATA_VOLUME.to_owned(), - MapOrEmpty::Map(ComposeVolume { - driver: Some("local".to_owned()), - driver_opts: IndexMap::default(), - external: None, - labels: Labels::default(), - name: None, - }), - ); - exposed_ports_warn.push("prometheus has an exported port on 9090".to_string()); - let prometheus_service = Service { - container_name: Some("cb_prometheus".to_owned()), - image: Some("prom/prometheus:latest".to_owned()), - volumes: vec![prom_volume, targets_volume, data_volume], - // to inspect prometheus from localhost - ports: Ports::Short(vec!["9090:9090".to_owned()]), - networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), - ..Service::default() - }; - - services.insert("cb_prometheus".to_owned(), Some(prometheus_service)); - - if cb_config.metrics.use_grafana { - exposed_ports_warn.push("grafana has an exported port on 3000".to_string()); - let grafana_service = Service { - container_name: Some("cb_grafana".to_owned()), - image: Some("grafana/grafana:latest".to_owned()), - ports: Ports::Short(vec!["3000:3000".to_owned()]), - networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), - depends_on: DependsOnOptions::Simple(vec!["cb_prometheus".to_owned()]), - environment: Environment::List(vec!["GF_SECURITY_ADMIN_PASSWORD=admin".to_owned()]), - volumes: vec![ - Volumes::Simple( - "./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned(), - ), - Volumes::Simple( - "./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned(), - ), - grafana_data_volume, - ], - // TODO: re-enable logging here once we move away from docker logs - logging: Some(LoggingParameters { driver: Some("none".to_owned()), options: None }), + if let Some(metrics) = cb_config.metrics { + let prom_volume = Volumes::Simple(format!( + "{}:/etc/prometheus/prometheus.yml", + metrics.prometheus_config + )); + // TODO: fix path to targets file + let targets_volume = + Volumes::Simple(format!("./{}:/etc/prometheus/targets.json", CB_TARGETS_FILE)); + + let data_volume = Volumes::Simple(format!("{}:/prometheus", PROMETHEUS_DATA_VOLUME)); + + let grafana_data_volume = + Volumes::Simple(format!("{}:/var/lib/grafana", GRAFANA_DATA_VOLUME)); + + volumes.insert( + PROMETHEUS_DATA_VOLUME.to_owned(), + MapOrEmpty::Map(ComposeVolume { + driver: Some("local".to_owned()), + driver_opts: IndexMap::default(), + external: None, + labels: Labels::default(), + name: None, + }), + ); + + volumes.insert( + GRAFANA_DATA_VOLUME.to_owned(), + MapOrEmpty::Map(ComposeVolume { + driver: Some("local".to_owned()), + driver_opts: IndexMap::default(), + external: None, + labels: Labels::default(), + name: None, + }), + ); + exposed_ports_warn.push("prometheus has an exported port on 9090".to_string()); + let prometheus_service = Service { + container_name: Some("cb_prometheus".to_owned()), + image: Some("prom/prometheus:latest".to_owned()), + volumes: vec![prom_volume, targets_volume, data_volume], + // to inspect prometheus from localhost + ports: Ports::Short(vec!["9090:9090".to_owned()]), + networks: Networks::Simple(vec![DEFAULT_NETWORK.to_owned()]), ..Service::default() }; - services.insert("cb_grafana".to_owned(), Some(grafana_service)); - } - exposed_ports_warn.push("cadvisor has an exported port on 8080".to_string()); - services.insert( - "cb_cadvisor".to_owned(), - Some(Service { - container_name: Some("cb_cadvisor".to_owned()), - image: Some("gcr.io/cadvisor/cadvisor".to_owned()), - ports: Ports::Short(vec![format!("{cadvisor_port}:8080")]), - networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), - volumes: vec![ - Volumes::Simple("/var/run/docker.sock:/var/run/docker.sock:ro".to_owned()), - Volumes::Simple("/sys:/sys:ro".to_owned()), - Volumes::Simple("/var/lib/docker/:/var/lib/docker:ro".to_owned()), - ], - ..Service::default() - }), - ); + services.insert("cb_prometheus".to_owned(), Some(prometheus_service)); + + if metrics.use_grafana { + exposed_ports_warn.push("grafana has an exported port on 3000".to_string()); + let grafana_service = Service { + container_name: Some("cb_grafana".to_owned()), + image: Some("grafana/grafana:latest".to_owned()), + ports: Ports::Short(vec!["3000:3000".to_owned()]), + networks: Networks::Simple(vec![DEFAULT_NETWORK.to_owned()]), + depends_on: DependsOnOptions::Simple(vec!["cb_prometheus".to_owned()]), + environment: Environment::List(vec!["GF_SECURITY_ADMIN_PASSWORD=admin".to_owned()]), + volumes: vec![ + Volumes::Simple( + "./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned(), + ), + Volumes::Simple( + "./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned(), + ), + grafana_data_volume, + ], + // TODO: re-enable logging here once we move away from docker logs + logging: Some(LoggingParameters { driver: Some("none".to_owned()), options: None }), + ..Service::default() + }; - targets.push(PrometheusTargetConfig { - targets: vec![format!("cb_cadvisor:{cadvisor_port}")], - labels: PrometheusLabelsConfig { job: "cadvisor".to_owned() }, - }); + services.insert("cb_grafana".to_owned(), Some(grafana_service)); + } + exposed_ports_warn.push("cadvisor has an exported port on 8080".to_string()); + services.insert( + "cb_cadvisor".to_owned(), + Some(Service { + container_name: Some("cb_cadvisor".to_owned()), + image: Some("gcr.io/cadvisor/cadvisor".to_owned()), + ports: Ports::Short(vec![format!("{cadvisor_port}:8080")]), + networks: Networks::Simple(vec![DEFAULT_NETWORK.to_owned()]), + volumes: vec![ + Volumes::Simple("/var/run/docker.sock:/var/run/docker.sock:ro".to_owned()), + Volumes::Simple("/sys:/sys:ro".to_owned()), + Volumes::Simple("/var/lib/docker/:/var/lib/docker:ro".to_owned()), + ], + ..Service::default() + }), + ); + + targets.push(PrometheusTargetConfig { + targets: vec![format!("cb_cadvisor:{cadvisor_port}")], + labels: PrometheusLabelsConfig { job: "cadvisor".to_owned() }, + }); + } else { + println!("[WARN]: metrics disabled") + } compose.services = Services(services); compose.volumes = TopLevelVolumes(volumes); diff --git a/crates/common/src/config/mod.rs b/crates/common/src/config/mod.rs index 9bd82a14..55302ce1 100644 --- a/crates/common/src/config/mod.rs +++ b/crates/common/src/config/mod.rs @@ -28,7 +28,8 @@ pub struct CommitBoostConfig { pub pbs: StaticPbsConfig, pub modules: Option>, pub signer: Option, - pub metrics: MetricsConfig, + #[serde(default)] + pub metrics: Option, #[serde(default)] pub logs: LogsSettings, }