Skip to content

Commit

Permalink
Merge pull request #49 from Commit-Boost/feat/persist-prometheus
Browse files Browse the repository at this point in the history
Prometheus persist: Programatically adds a docker volume on to the docker-compose to be used for storing prometheus data
  • Loading branch information
ltitanb authored Aug 2, 2024
2 parents 2fa4b82 + 22c94b9 commit a5f4dca
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions crates/cli/src/docker_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use cb_common::{
utils::random_jwt,
};
use docker_compose_types::{
Compose, DependsOnOptions, Environment, LoggingParameters, MapOrEmpty, NetworkSettings,
Networks, Ports, Service, Services, SingleValue, Volumes,
Compose, ComposeVolume, DependsOnOptions, Environment, Labels, LoggingParameters, MapOrEmpty,
NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes, Volumes,
};
use eyre::Result;
use indexmap::IndexMap;
Expand All @@ -22,6 +22,7 @@ pub(super) const CB_CONFIG_FILE: &str = "cb-config.toml";
pub(super) const CB_COMPOSE_FILE: &str = "cb.docker-compose.yml";
pub(super) const CB_ENV_FILE: &str = ".cb.env";
pub(super) const CB_TARGETS_FILE: &str = "targets.json"; // needs to match prometheus.yml
pub(super) const PROMETHEUS_DATA_VOLUME: &str = "prometheus-data";

const METRICS_NETWORK: &str = "monitoring_network";
const SIGNER_NETWORK: &str = "signer_network";
Expand All @@ -36,6 +37,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
let cb_config = CommitBoostConfig::from_file(&config_path)?;

let mut services = IndexMap::new();
let mut volumes = IndexMap::new();

// config volume to pass to all services
let config_volume = Volumes::Simple(format!("./{}:{}:ro", config_path, CB_CONFIG_NAME));
Expand Down Expand Up @@ -253,10 +255,23 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
let targets_volume =
Volumes::Simple(format!("./{}:/etc/prometheus/targets.json", CB_TARGETS_FILE));

let data_volume = Volumes::Simple(format!("{}:/prometheus", PROMETHEUS_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,
}),
);

let prometheus_service = Service {
container_name: Some("cb_prometheus".to_owned()),
image: Some("prom/prometheus:latest".to_owned()),
volumes: vec![prom_volume, targets_volume],
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()]),
Expand All @@ -282,6 +297,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
}

compose.services = Services(services);
compose.volumes = TopLevelVolumes(volumes);

// write compose to file
let compose_str = serde_yaml::to_string(&compose)?;
Expand Down

0 comments on commit a5f4dca

Please sign in to comment.