Skip to content

Commit 3374ae1

Browse files
authored
feat(stackable-telemetry)!: Allow customization of the rolling file appender (#995)
* feat(stackable-telemetry): Allow customization of the rolling file appender rotation period * chore(stackable-telemetry): Update changelog * feat(stackable-telemetry): Add required file appender filename_suffix field * feat(stackable-telemetry): Allow customization of the rolling file appender max log files * chore(stackable-telemetry): Mark change as breaking * feat(stackable-telemetry): Complete the sentence * feat(stackable-telemetry): Add punctuation * chore(stackable-telemetry): Replace generic parameters with impls
1 parent 1815bc1 commit 3374ae1

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

crates/stackable-telemetry/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- BREAKING: Allow customization of the rolling file appender [#995].
10+
- Add required `filename_suffix` field.
11+
- Add `with_rotation_period` method.
12+
- Add `with_max_log_files` method.
13+
14+
[#995]: https://github.com/stackabletech/operator-rs/pull/995
15+
716
## [0.3.0] - 2025-01-30
817

918
### Added

crates/stackable-telemetry/src/tracing/mod.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use opentelemetry_sdk::{
1616
use opentelemetry_semantic_conventions::resource;
1717
use snafu::{ResultExt as _, Snafu};
1818
use tracing::subscriber::SetGlobalDefaultError;
19-
use tracing_appender::rolling::{InitError, RollingFileAppender, Rotation};
19+
use tracing_appender::rolling::{InitError, RollingFileAppender};
2020
use tracing_subscriber::{filter::Directive, layer::SubscriberExt, EnvFilter, Layer, Registry};
2121

2222
use crate::tracing::settings::*;
@@ -275,6 +275,9 @@ impl Tracing {
275275
if let FileLogSettings::Enabled {
276276
common_settings,
277277
file_log_dir,
278+
rotation_period,
279+
filename_suffix,
280+
max_log_files,
278281
} = &self.file_log_settings
279282
{
280283
let env_filter_layer = env_filter_builder(
@@ -283,10 +286,17 @@ impl Tracing {
283286
);
284287

285288
let file_appender = RollingFileAppender::builder()
286-
.rotation(Rotation::HOURLY)
289+
.rotation(rotation_period.clone())
287290
.filename_prefix(self.service_name.to_string())
288-
.filename_suffix("tracing-rs.json")
289-
.max_log_files(6)
291+
.filename_suffix(filename_suffix);
292+
293+
let file_appender = if let Some(max_log_files) = max_log_files {
294+
file_appender.max_log_files(*max_log_files)
295+
} else {
296+
file_appender
297+
};
298+
299+
let file_appender = file_appender
290300
.build(file_log_dir)
291301
.context(InitRollingFileAppenderSnafu)?;
292302

@@ -592,6 +602,7 @@ mod test {
592602
use rstest::rstest;
593603
use settings::Settings;
594604
use tracing::level_filters::LevelFilter;
605+
use tracing_appender::rolling::Rotation;
595606

596607
use super::*;
597608

@@ -692,7 +703,7 @@ mod test {
692703
Settings::builder()
693704
.with_environment_variable("ABC_FILE")
694705
.with_default_level(LevelFilter::INFO)
695-
.file_log_settings_builder(PathBuf::from("/abc_file_dir"))
706+
.file_log_settings_builder(PathBuf::from("/abc_file_dir"), "tracing-rs.json")
696707
.build(),
697708
)
698709
.with_otlp_log_exporter(
@@ -726,7 +737,10 @@ mod test {
726737
environment_variable: "ABC_FILE",
727738
default_level: LevelFilter::INFO
728739
},
729-
file_log_dir: PathBuf::from("/abc_file_dir")
740+
file_log_dir: PathBuf::from("/abc_file_dir"),
741+
rotation_period: Rotation::NEVER,
742+
filename_suffix: "tracing-rs.json".to_owned(),
743+
max_log_files: None,
730744
}
731745
);
732746
assert_eq!(
@@ -766,7 +780,7 @@ mod test {
766780
.with_file_output(enable_filelog_output.then(|| {
767781
Settings::builder()
768782
.with_environment_variable("ABC_FILELOG")
769-
.file_log_settings_builder("/dev/null")
783+
.file_log_settings_builder("/dev/null", "tracing-rs.json")
770784
.build()
771785
}))
772786
.with_otlp_trace_exporter(enable_otlp_trace.then(|| {

crates/stackable-telemetry/src/tracing/settings/file_log.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
use std::path::PathBuf;
44

5+
/// Re-export to save the end crate from an extra import.
6+
pub use tracing_appender::rolling::Rotation;
7+
58
use super::{Settings, SettingsToggle};
69

710
/// Configure specific settings for the File Log subscriber.
@@ -18,6 +21,15 @@ pub enum FileLogSettings {
1821

1922
/// Path to directory for log files.
2023
file_log_dir: PathBuf,
24+
25+
/// Log rotation frequency.
26+
rotation_period: Rotation,
27+
28+
/// Suffix for log filenames.
29+
filename_suffix: String,
30+
31+
/// Keep the last `n` files on disk.
32+
max_log_files: Option<usize>,
2133
},
2234
}
2335

@@ -38,14 +50,32 @@ impl SettingsToggle for FileLogSettings {
3850
pub struct FileLogSettingsBuilder {
3951
pub(crate) common_settings: Settings,
4052
pub(crate) file_log_dir: PathBuf,
53+
pub(crate) rotation_period: Rotation,
54+
pub(crate) filename_suffix: String,
55+
pub(crate) max_log_files: Option<usize>,
4156
}
4257

4358
impl FileLogSettingsBuilder {
59+
/// Set file rotation period.
60+
pub fn with_rotation_period(mut self, rotation_period: Rotation) -> Self {
61+
self.rotation_period = rotation_period;
62+
self
63+
}
64+
65+
/// Set maximum number of log files to keep.
66+
pub fn with_max_log_files(mut self, max_log_files: usize) -> Self {
67+
self.max_log_files = Some(max_log_files);
68+
self
69+
}
70+
4471
/// Consumes self and returns a valid [`FileLogSettings`] instance.
4572
pub fn build(self) -> FileLogSettings {
4673
FileLogSettings::Enabled {
4774
common_settings: self.common_settings,
4875
file_log_dir: self.file_log_dir,
76+
rotation_period: self.rotation_period,
77+
filename_suffix: self.filename_suffix,
78+
max_log_files: self.max_log_files,
4979
}
5080
}
5181
}
@@ -76,11 +106,16 @@ mod test {
76106
default_level: LevelFilter::DEBUG,
77107
},
78108
file_log_dir: PathBuf::from("/logs"),
109+
rotation_period: Rotation::HOURLY,
110+
filename_suffix: "tracing-rs.log".to_owned(),
111+
max_log_files: Some(6),
79112
};
80113
let result = Settings::builder()
81114
.with_environment_variable("hello")
82115
.with_default_level(LevelFilter::DEBUG)
83-
.file_log_settings_builder(PathBuf::from("/logs"))
116+
.file_log_settings_builder(PathBuf::from("/logs"), "tracing-rs.json")
117+
.with_rotation_period(Rotation::HOURLY)
118+
.with_max_log_files(6)
84119
.build();
85120

86121
assert_eq!(expected, result);

crates/stackable-telemetry/src/tracing/settings/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ impl SettingsBuilder {
8585
}
8686

8787
/// Set specific [`FileLogSettings`].
88-
pub fn file_log_settings_builder<P>(self, path: P) -> FileLogSettingsBuilder
89-
where
90-
P: AsRef<Path>,
91-
{
88+
pub fn file_log_settings_builder(
89+
self,
90+
path: impl AsRef<Path>,
91+
filename_suffix: impl Into<String>,
92+
) -> FileLogSettingsBuilder {
9293
FileLogSettingsBuilder {
9394
common_settings: self.build(),
9495
file_log_dir: path.as_ref().to_path_buf(),
96+
rotation_period: Rotation::NEVER,
97+
filename_suffix: filename_suffix.into(),
98+
max_log_files: None,
9599
}
96100
}
97101

0 commit comments

Comments
 (0)