Skip to content

Commit 0d23102

Browse files
refactor(stackable-telemetry): Support Option<T> in subscriber methods (#951)
* wip: Make builder functions take Option * wip: Make builder functions not take Option * wip: remove derefs * wip: restore the tuple impls and tests * wip: fix example in docs * chore: fix word tense in past entry * chore: add changelog entries * Apply suggestions from code review Co-authored-by: Techassi <[email protected]> * chore: use default impl for SettingToggle::is_disabled() * chore: fix clippy warning * chore(stackable-telemetry): add missing docs * chore)stackable-telemetry): add missing docs --------- Co-authored-by: Nick Larsen <[email protected]> Co-authored-by: Nick <[email protected]>
1 parent 53ccc1e commit 0d23102

File tree

7 files changed

+272
-171
lines changed

7 files changed

+272
-171
lines changed

crates/stackable-telemetry/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ All notable changes to this project will be documented in this file.
88

99
### Added
1010

11+
- Allow `Option<_>` to be used to enable/disable a subscriber ([#951]).
1112
- Introduce common `Settings` and subscriber specific settings ([#901]).
1213
- Add support for logging to files ([#933]).
1314

1415
### Changed
1516

16-
- BREAKING: Renamed `TracingBuilder` methods with long names, and prefix with `with_` ([#901]).
17+
- BREAKING: Change subscriber settings into an enum to indicate if the subscriber is enabled/disabled ([#951]).
18+
- BREAKING: Rename `TracingBuilder` methods with long names, and prefix with `with_` ([#901]).
1719
- BREAKING: Use the new subscriber settings in the `TracingBuilder` ([#901]).
1820

21+
### Removed
22+
23+
- BREAKING: Remove `Deref` impls for subscriber settings and removed the `enabled` fields and `enabled()` methods ([#951]).
24+
1925
[#901]: https://github.com/stackabletech/operator-rs/pull/901
2026
[#933]: https://github.com/stackabletech/operator-rs/pull/933
27+
[#951]: https://github.com/stackabletech/operator-rs/pull/951
2128

2229
## [0.2.0] - 2024-07-10
2330

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

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub enum Error {
134134
/// # use tracing_subscriber::filter::LevelFilter;
135135
/// #[tokio::main]
136136
/// async fn main() -> Result<(), Error> {
137+
/// // Control the otlp_log subscriber at runtime
137138
/// let otlp_log_flag = false;
138139
///
139140
/// let _tracing_guard = Tracing::builder()
@@ -142,21 +143,25 @@ pub enum Error {
142143
/// Settings::builder()
143144
/// .with_environment_variable("TEST_CONSOLE")
144145
/// .with_default_level(LevelFilter::INFO)
145-
/// .enabled(true)
146146
/// .build()
147147
/// )
148-
/// .with_otlp_log_exporter(
148+
/// .with_file_output(
149+
/// Settings::builder()
150+
/// .with_environment_variable("TEST_FILE_LOG")
151+
/// .with_default_level(LevelFilter::INFO)
152+
/// .file_log_settings_builder("/tmp/logs")
153+
/// .build()
154+
/// )
155+
/// .with_otlp_log_exporter(otlp_log_flag.then(|| {
149156
/// Settings::builder()
150157
/// .with_environment_variable("TEST_OTLP_LOG")
151158
/// .with_default_level(LevelFilter::DEBUG)
152-
/// .enabled(otlp_log_flag)
153159
/// .build()
154-
/// )
160+
/// }))
155161
/// .with_otlp_trace_exporter(
156162
/// Settings::builder()
157163
/// .with_environment_variable("TEST_OTLP_TRACE")
158164
/// .with_default_level(LevelFilter::TRACE)
159-
/// .enabled(true)
160165
/// .build()
161166
/// )
162167
/// .build()
@@ -253,30 +258,36 @@ impl Tracing {
253258
pub fn init(mut self) -> Result<Tracing> {
254259
let mut layers: Vec<Box<dyn Layer<Registry> + Sync + Send>> = Vec::new();
255260

256-
if self.console_log_settings.enabled {
261+
if let ConsoleLogSettings::Enabled {
262+
common_settings,
263+
log_format: _,
264+
} = &self.console_log_settings
265+
{
257266
let env_filter_layer = env_filter_builder(
258-
self.console_log_settings
259-
.common_settings
260-
.environment_variable,
261-
self.console_log_settings.default_level,
267+
common_settings.environment_variable,
268+
common_settings.default_level,
262269
);
263270
let console_output_layer =
264271
tracing_subscriber::fmt::layer().with_filter(env_filter_layer);
265272
layers.push(console_output_layer.boxed());
266273
}
267274

268-
if self.file_log_settings.enabled {
275+
if let FileLogSettings::Enabled {
276+
common_settings,
277+
file_log_dir,
278+
} = &self.file_log_settings
279+
{
269280
let env_filter_layer = env_filter_builder(
270-
self.file_log_settings.common_settings.environment_variable,
271-
self.file_log_settings.default_level,
281+
common_settings.environment_variable,
282+
common_settings.default_level,
272283
);
273284

274285
let file_appender = RollingFileAppender::builder()
275286
.rotation(Rotation::HOURLY)
276287
.filename_prefix(self.service_name.to_string())
277288
.filename_suffix("tracing-rs.json")
278289
.max_log_files(6)
279-
.build(&self.file_log_settings.file_log_dir)
290+
.build(file_log_dir)
280291
.context(InitRollingFileAppenderSnafu)?;
281292

282293
layers.push(
@@ -288,10 +299,10 @@ impl Tracing {
288299
);
289300
}
290301

291-
if self.otlp_log_settings.enabled {
302+
if let OtlpLogSettings::Enabled { common_settings } = &self.otlp_log_settings {
292303
let env_filter_layer = env_filter_builder(
293-
self.otlp_log_settings.environment_variable,
294-
self.otlp_log_settings.default_level,
304+
common_settings.environment_variable,
305+
common_settings.default_level,
295306
)
296307
// TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
297308
.add_directive("h2=off".parse().expect("invalid directive"));
@@ -316,12 +327,11 @@ impl Tracing {
316327
self.logger_provider = Some(otel_log);
317328
}
318329

319-
if self.otlp_trace_settings.enabled {
330+
if let OtlpTraceSettings::Enabled { common_settings } = &self.otlp_trace_settings {
320331
let env_filter_layer = env_filter_builder(
321-
self.otlp_trace_settings
322-
.common_settings
323-
.environment_variable,
324-
self.otlp_trace_settings.default_level,
332+
// todo, deref?
333+
common_settings.environment_variable,
334+
common_settings.default_level,
325335
)
326336
// TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
327337
.add_directive("h2=off".parse().expect("invalid directive"));
@@ -369,12 +379,12 @@ impl Tracing {
369379
impl Drop for Tracing {
370380
fn drop(&mut self) {
371381
tracing::debug!(
372-
opentelemetry.tracing.enabled = self.otlp_trace_settings.enabled,
373-
opentelemetry.logger.enabled = self.otlp_log_settings.enabled,
382+
opentelemetry.tracing.enabled = self.otlp_trace_settings.is_enabled(),
383+
opentelemetry.logger.enabled = self.otlp_log_settings.is_enabled(),
374384
"shutting down opentelemetry OTLP providers"
375385
);
376386

377-
if self.otlp_trace_settings.enabled {
387+
if self.otlp_trace_settings.is_enabled() {
378388
// NOTE (@NickLarsenNZ): This might eventually be replaced with something like SdkMeterProvider::shutdown(&self)
379389
// as has been done with the LoggerProvider (further below)
380390
// see: https://github.com/open-telemetry/opentelemetry-rust/pull/1412/files#r1409608679
@@ -448,9 +458,9 @@ impl BuilderState for builder_state::Config {}
448458
pub struct TracingBuilder<S: BuilderState> {
449459
service_name: Option<&'static str>,
450460
console_log_settings: ConsoleLogSettings,
461+
file_log_settings: FileLogSettings,
451462
otlp_log_settings: OtlpLogSettings,
452463
otlp_trace_settings: OtlpTraceSettings,
453-
file_log_settings: FileLogSettings,
454464

455465
/// Allow the generic to be used (needed for impls).
456466
_marker: std::marker::PhantomData<S>,
@@ -600,31 +610,30 @@ mod test {
600610
Settings::builder()
601611
.with_environment_variable("ABC_A")
602612
.with_default_level(LevelFilter::TRACE)
603-
.enabled(true)
604613
.build(),
605614
)
606615
.with_console_output(
607616
Settings::builder()
608617
.with_environment_variable("ABC_B")
609618
.with_default_level(LevelFilter::DEBUG)
610-
.enabled(true)
611619
.build(),
612620
)
613621
.build();
614622

615623
assert_eq!(
616624
trace_guard.console_log_settings,
617-
ConsoleLogSettings {
625+
ConsoleLogSettings::Enabled {
618626
common_settings: Settings {
619-
enabled: true,
620627
environment_variable: "ABC_B",
621628
default_level: LevelFilter::DEBUG
622629
},
623630
log_format: Default::default()
624631
}
625632
);
626-
assert!(!trace_guard.otlp_log_settings.enabled);
627-
assert!(!trace_guard.otlp_trace_settings.enabled);
633+
634+
assert!(trace_guard.file_log_settings.is_disabled());
635+
assert!(trace_guard.otlp_log_settings.is_disabled());
636+
assert!(trace_guard.otlp_trace_settings.is_disabled());
628637
}
629638

630639
#[test]
@@ -636,11 +645,10 @@ mod test {
636645

637646
assert_eq!(
638647
trace_guard.console_log_settings,
639-
ConsoleLogSettings {
648+
ConsoleLogSettings::Enabled {
640649
common_settings: Settings {
641650
environment_variable: "ABC_A",
642651
default_level: LevelFilter::TRACE,
643-
enabled: true
644652
},
645653
log_format: Default::default()
646654
}
@@ -656,17 +664,18 @@ mod test {
656664
.with_console_output(("ABC_A", LevelFilter::TRACE, enabled))
657665
.build();
658666

659-
assert_eq!(
660-
trace_guard.console_log_settings,
661-
ConsoleLogSettings {
667+
let expected = match enabled {
668+
true => ConsoleLogSettings::Enabled {
662669
common_settings: Settings {
663670
environment_variable: "ABC_A",
664671
default_level: LevelFilter::TRACE,
665-
enabled
666672
},
667-
log_format: Default::default()
668-
}
669-
)
673+
log_format: Default::default(),
674+
},
675+
false => ConsoleLogSettings::Disabled,
676+
};
677+
678+
assert_eq!(trace_guard.console_log_settings, expected)
670679
}
671680

672681
#[test]
@@ -677,38 +686,33 @@ mod test {
677686
Settings::builder()
678687
.with_environment_variable("ABC_CONSOLE")
679688
.with_default_level(LevelFilter::INFO)
680-
.enabled(true)
681689
.build(),
682690
)
683691
.with_file_output(
684692
Settings::builder()
685693
.with_environment_variable("ABC_FILE")
686694
.with_default_level(LevelFilter::INFO)
687-
.enabled(true)
688695
.file_log_settings_builder(PathBuf::from("/abc_file_dir"))
689696
.build(),
690697
)
691698
.with_otlp_log_exporter(
692699
Settings::builder()
693700
.with_environment_variable("ABC_OTLP_LOG")
694701
.with_default_level(LevelFilter::DEBUG)
695-
.enabled(true)
696702
.build(),
697703
)
698704
.with_otlp_trace_exporter(
699705
Settings::builder()
700706
.with_environment_variable("ABC_OTLP_TRACE")
701707
.with_default_level(LevelFilter::TRACE)
702-
.enabled(true)
703708
.build(),
704709
)
705710
.build();
706711

707712
assert_eq!(
708713
trace_guard.console_log_settings,
709-
ConsoleLogSettings {
714+
ConsoleLogSettings::Enabled {
710715
common_settings: Settings {
711-
enabled: true,
712716
environment_variable: "ABC_CONSOLE",
713717
default_level: LevelFilter::INFO
714718
},
@@ -717,9 +721,8 @@ mod test {
717721
);
718722
assert_eq!(
719723
trace_guard.file_log_settings,
720-
FileLogSettings {
724+
FileLogSettings::Enabled {
721725
common_settings: Settings {
722-
enabled: true,
723726
environment_variable: "ABC_FILE",
724727
default_level: LevelFilter::INFO
725728
},
@@ -728,23 +731,59 @@ mod test {
728731
);
729732
assert_eq!(
730733
trace_guard.otlp_log_settings,
731-
OtlpLogSettings {
734+
OtlpLogSettings::Enabled {
732735
common_settings: Settings {
733-
enabled: true,
734736
environment_variable: "ABC_OTLP_LOG",
735737
default_level: LevelFilter::DEBUG
736738
},
737739
}
738740
);
739741
assert_eq!(
740742
trace_guard.otlp_trace_settings,
741-
OtlpTraceSettings {
743+
OtlpTraceSettings::Enabled {
742744
common_settings: Settings {
743-
enabled: true,
744745
environment_variable: "ABC_OTLP_TRACE",
745746
default_level: LevelFilter::TRACE
746747
}
747748
}
748749
);
749750
}
751+
752+
#[test]
753+
fn builder_with_options() {
754+
let enable_console_output = true;
755+
let enable_filelog_output = true;
756+
let enable_otlp_trace = true;
757+
let enable_otlp_log = false;
758+
759+
let tracing_builder = Tracing::builder()
760+
.service_name("test")
761+
.with_console_output(enable_console_output.then(|| {
762+
Settings::builder()
763+
.with_environment_variable("ABC_CONSOLE")
764+
.build()
765+
}))
766+
.with_file_output(enable_filelog_output.then(|| {
767+
Settings::builder()
768+
.with_environment_variable("ABC_FILELOG")
769+
.file_log_settings_builder("/dev/null")
770+
.build()
771+
}))
772+
.with_otlp_trace_exporter(enable_otlp_trace.then(|| {
773+
Settings::builder()
774+
.with_environment_variable("ABC_OTLP_TRACE")
775+
.build()
776+
}))
777+
.with_otlp_log_exporter(enable_otlp_log.then(|| {
778+
Settings::builder()
779+
.with_environment_variable("ABC_OTLP_LOG")
780+
.build()
781+
}))
782+
.build();
783+
784+
assert!(tracing_builder.console_log_settings.is_enabled());
785+
assert!(tracing_builder.file_log_settings.is_enabled());
786+
assert!(tracing_builder.otlp_trace_settings.is_enabled());
787+
assert!(tracing_builder.otlp_log_settings.is_disabled());
788+
}
750789
}

0 commit comments

Comments
 (0)