Skip to content

Commit 550bed5

Browse files
fbrvfbrv
andauthored
log rotation (Commit-Boost#48)
* hourly log rotation * prefix * implemente log rotation * add logs.prefixes * fmt * move to logs * comments * utils * handle guard * remove unused deps * default log location if not specified in configuration * as discussed * constants * config * names for modules * rolling duration * env var * lint, remove hard coded strings * debug * stuff * change duration * change CB_BASE_LOG_PATH --------- Co-authored-by: fbrv <[email protected]>
1 parent 104cb8b commit 550bed5

File tree

21 files changed

+153
-47
lines changed

21 files changed

+153
-47
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ serde_yaml = "0.9.33"
5454
# telemetry
5555
tracing = "0.1.40"
5656
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
57+
tracing-appender = "0.2.3"
5758
prometheus = "0.13.4"
5859

5960
# crypto

bin/default_pbs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use cb_common::{config::load_pbs_config, utils::initialize_tracing_log};
1+
use cb_common::{
2+
config::load_pbs_config, module_names::PBS_MODULE_NAME, utils::initialize_tracing_log,
3+
};
24
use cb_pbs::{DefaultBuilderApi, PbsService, PbsState};
35
use eyre::Result;
46

@@ -11,12 +13,10 @@ async fn main() -> Result<()> {
1113
std::env::set_var("RUST_BACKTRACE", "1");
1214
}
1315

14-
initialize_tracing_log();
15-
1616
// TODO: handle errors
1717
let pbs_config = load_pbs_config().expect("failed to load pbs config");
18+
let _guard = initialize_tracing_log(PBS_MODULE_NAME);
1819
let state = PbsState::<()>::new(pbs_config);
19-
2020
PbsService::init_metrics()?;
2121
PbsService::run::<(), DefaultBuilderApi>(state).await;
2222
Ok(())

bin/signer_module.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use cb_common::{config::StartSignerConfig, utils::initialize_tracing_log};
1+
use cb_common::{
2+
config::StartSignerConfig, module_names::SIGNER_MODULE_NAME, utils::initialize_tracing_log,
3+
};
24
use cb_signer::service::SigningService;
35
use eyre::Result;
46

@@ -11,8 +13,7 @@ async fn main() -> Result<()> {
1113
std::env::set_var("RUST_BACKTRACE", "1");
1214
}
1315

14-
initialize_tracing_log();
15-
1616
let config = StartSignerConfig::load_from_env()?;
17+
let _guard = initialize_tracing_log(SIGNER_MODULE_NAME);
1718
SigningService::run(config).await
1819
}

config.example.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ sleep_secs = 5
3939
id = "BUILDER_LOG"
4040
type = "events"
4141
docker_image = "test_builder_log"
42+
43+
[logs]
44+
duration = "daily"
45+
host-path="./logs"
46+
rust-log="info"

crates/cli/src/docker_init.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::{path::Path, vec};
22

33
use cb_common::{
44
config::{
5-
CommitBoostConfig, ModuleKind, BUILDER_SERVER_ENV, CB_CONFIG_ENV, CB_CONFIG_NAME, JWTS_ENV,
6-
METRICS_SERVER_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, SIGNER_DIR_KEYS, SIGNER_DIR_KEYS_ENV,
7-
SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV, SIGNER_KEYS, SIGNER_KEYS_ENV,
8-
SIGNER_SERVER_ENV,
5+
CommitBoostConfig, ModuleKind, BUILDER_SERVER_ENV, CB_BASE_LOG_PATH, CB_CONFIG_ENV,
6+
CB_CONFIG_NAME, JWTS_ENV, METRICS_SERVER_ENV, MODULE_ID_ENV, MODULE_JWT_ENV,
7+
SIGNER_DIR_KEYS, SIGNER_DIR_KEYS_ENV, SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV,
8+
SIGNER_KEYS, SIGNER_KEYS_ENV, SIGNER_SERVER_ENV,
99
},
1010
loader::SignerLoader,
11-
utils::random_jwt,
11+
utils::{random_jwt, ENV_ROLLING_DURATION},
1212
};
1313
use docker_compose_types::{
1414
Compose, ComposeVolume, DependsOnOptions, Environment, Labels, LoggingParameters, MapOrEmpty,
@@ -27,6 +27,8 @@ pub(super) const PROMETHEUS_DATA_VOLUME: &str = "prometheus-data";
2727
const METRICS_NETWORK: &str = "monitoring_network";
2828
const SIGNER_NETWORK: &str = "signer_network";
2929

30+
const ENV_RUST_LOG: &str = "RUST_LOG";
31+
3032
/// Builds the docker compose file for the Commit-Boost services
3133
3234
// TODO: do more validation for paths, images, etc
@@ -41,11 +43,17 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
4143

4244
// config volume to pass to all services
4345
let config_volume = Volumes::Simple(format!("./{}:{}:ro", config_path, CB_CONFIG_NAME));
46+
let log_volume = Volumes::Simple(format!(
47+
"{}:{}",
48+
cb_config.logs.host_path.to_str().unwrap(),
49+
CB_BASE_LOG_PATH
50+
));
4451

4552
let mut jwts = IndexMap::new();
4653
// envs to write in .env file
4754
let mut envs = IndexMap::from([(CB_CONFIG_ENV.into(), CB_CONFIG_NAME.into())]);
48-
55+
envs.insert(ENV_ROLLING_DURATION.into(), cb_config.logs.duration.to_string());
56+
envs.insert(ENV_RUST_LOG.into(), cb_config.logs.rust_log);
4957
// targets to pass to prometheus
5058
let mut targets = Vec::new();
5159
let metrics_port = 10000;
@@ -109,7 +117,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
109117
METRICS_NETWORK.to_owned(),
110118
SIGNER_NETWORK.to_owned(),
111119
]),
112-
volumes: vec![config_volume.clone()],
120+
volumes: vec![config_volume.clone(), log_volume.clone()],
113121
environment: Environment::KvPair(module_envs),
114122
depends_on: DependsOnOptions::Simple(vec!["cb_signer".to_owned()]),
115123
..Service::default()
@@ -131,7 +139,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
131139
container_name: Some(module_cid.clone()),
132140
image: Some(module.docker_image),
133141
networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]),
134-
volumes: vec![config_volume.clone()],
142+
volumes: vec![config_volume.clone(), log_volume.clone()],
135143
environment: Environment::KvPair(module_envs),
136144
depends_on: DependsOnOptions::Simple(vec!["cb_pbs".to_owned()]),
137145
..Service::default()
@@ -157,7 +165,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
157165
cb_config.pbs.pbs_config.port, cb_config.pbs.pbs_config.port
158166
)]),
159167
networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]),
160-
volumes: vec![config_volume.clone()],
168+
volumes: vec![config_volume.clone(), log_volume.clone()],
161169
environment: Environment::KvPair(pbs_envs),
162170
..Service::default()
163171
};
@@ -170,7 +178,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
170178

171179
if let Some(signer_config) = cb_config.signer {
172180
if needs_signer_module {
173-
let mut volumes = vec![config_volume.clone()];
181+
let mut volumes = vec![config_volume.clone(), log_volume.clone()];
174182

175183
targets.push(PrometheusTargetConfig {
176184
targets: vec![format!("cb_signer:{metrics_port}")],
@@ -288,7 +296,14 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
288296
networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]),
289297
depends_on: DependsOnOptions::Simple(vec!["cb_prometheus".to_owned()]),
290298
environment: Environment::List(vec!["GF_SECURITY_ADMIN_PASSWORD=admin".to_owned()]),
291-
volumes: vec![Volumes::Simple("./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned()), Volumes::Simple("./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned())],
299+
volumes: vec![
300+
Volumes::Simple(
301+
"./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned(),
302+
),
303+
Volumes::Simple(
304+
"./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned(),
305+
),
306+
],
292307
// TODO: re-enable logging here once we move away from docker logs
293308
logging: Some(LoggingParameters { driver: Some("none".to_owned()), options: None }),
294309
..Service::default()

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ serde_json.workspace = true
2828
# telemetry
2929
tracing.workspace = true
3030
tracing-subscriber.workspace = true
31+
tracing-appender.workspace = true
3132

3233
# crypto
3334
blst.workspace = true

crates/common/src/config/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub const METRICS_SERVER_ENV: &str = "METRICS_SERVER";
44
pub const SIGNER_SERVER_ENV: &str = "SIGNER_SERVER";
55
pub const BUILDER_SERVER_ENV: &str = "BUILDER_SERVER";
66

7+
pub const CB_BASE_LOG_PATH: &str = "/var/logs/commit-boost";
8+
79
pub const CB_CONFIG_ENV: &str = "CB_CONFIG";
810
pub const CB_CONFIG_NAME: &str = "/cb-config.toml";
911

crates/common/src/config/log.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::{
2+
fmt::{Display, Formatter},
3+
path::PathBuf,
4+
};
5+
6+
use serde::{Deserialize, Serialize};
7+
8+
#[derive(Clone, Debug, Deserialize, Serialize)]
9+
pub struct LogsSettings {
10+
#[serde(default)]
11+
pub duration: RollingDuration,
12+
#[serde(default, rename = "host-path")]
13+
pub host_path: PathBuf,
14+
#[serde(default, rename = "rust-log")]
15+
pub rust_log: String,
16+
}
17+
18+
impl Default for LogsSettings {
19+
fn default() -> Self {
20+
Self {
21+
duration: RollingDuration::Hourly,
22+
host_path: "/var/log/pbs".into(),
23+
rust_log: "info".to_string(),
24+
}
25+
}
26+
}
27+
28+
#[derive(Clone, Debug, Deserialize, Serialize)]
29+
#[serde(rename_all = "lowercase")]
30+
pub enum RollingDuration {
31+
Minutely,
32+
Hourly,
33+
Daily,
34+
Never,
35+
}
36+
37+
impl Display for RollingDuration {
38+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39+
match self {
40+
RollingDuration::Minutely => write!(f, "minutely"),
41+
RollingDuration::Hourly => write!(f, "hourly"),
42+
RollingDuration::Daily => write!(f, "daily"),
43+
RollingDuration::Never => write!(f, "never"),
44+
}
45+
}
46+
}
47+
48+
impl Default for RollingDuration {
49+
fn default() -> Self {
50+
Self::Daily
51+
}
52+
}

crates/common/src/config/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ mod pbs;
1010
mod signer;
1111
mod utils;
1212

13+
mod log;
14+
1315
pub use constants::*;
16+
pub use log::*;
1417
pub use metrics::*;
1518
pub use module::*;
1619
pub use pbs::*;
@@ -26,6 +29,8 @@ pub struct CommitBoostConfig {
2629
pub modules: Option<Vec<StaticModuleConfig>>,
2730
pub signer: Option<SignerConfig>,
2831
pub metrics: MetricsConfig,
32+
#[serde(default)]
33+
pub logs: LogsSettings,
2934
}
3035

3136
impl CommitBoostConfig {

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod config;
55
pub mod constants;
66
pub mod error;
77
pub mod loader;
8+
pub mod module_names;
89
pub mod pbs;
910
pub mod signature;
1011
pub mod signer;

0 commit comments

Comments
 (0)