Skip to content

Commit fb44c6f

Browse files
feat(stackable-telemetry): Support JSON console log output (#1012)
* feat(stackable-telemetry): Support JSON console log output * chore(stackable-telemetry): Update changelog * chore(stackable-operator): Update changelog * Apply suggestions from code review Co-authored-by: Techassi <git@techassi.dev> --------- Co-authored-by: Techassi <git@techassi.dev>
1 parent a6d5d65 commit fb44c6f

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

crates/stackable-operator/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9-
- Adds the `--file-log-max-files` CLI argument and `FILE_LOG_MAX_FILES` environment variable
10-
see detailed [stackable-telemetry changelog](../stackable-telemetry/CHANGELOG.md) ([#1010]).
9+
- Adds new CLI arguments and environment variables ([#1010], [#1012]).
10+
- Use `--file-log-max-files` (or `FILE_LOG_MAX_FILES`) to limit the number of log files kept.
11+
- Use `--console-log-format` (or `CONSOLE_LOG_FORMAT`) to set the format to `plain` (default) or `json`.
12+
- See detailed [stackable-telemetry changelog](../stackable-telemetry/CHANGELOG.md).
1113

1214
### Changed
1315

@@ -16,6 +18,7 @@ All notable changes to this project will be documented in this file.
1618

1719
[#1009]: https://github.com/stackabletech/operator-rs/pull/1009
1820
[#1010]: https://github.com/stackabletech/operator-rs/pull/1010
21+
[#1012]: https://github.com/stackabletech/operator-rs/pull/1012
1922

2023
## [0.91.1] - 2025-04-09
2124

crates/stackable-telemetry/CHANGELOG.md

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

55
## [Unreleased]
66

7+
### Added
8+
9+
- Add support for JSON console log output ([#1012]).
10+
- A new CLI argument was added: `--console-log-format`. It can be set to `plain` (default),
11+
or `json`.
12+
713
### Changed
814

915
- BREAKING: Update and align telemetry related CLI arguments in `TelemetryOptions` ([#1009]).
@@ -25,6 +31,7 @@ All notable changes to this project will be documented in this file.
2531

2632
[#1009]: https://github.com/stackabletech/operator-rs/pull/1009
2733
[#1010]: https://github.com/stackabletech/operator-rs/pull/1010
34+
[#1012]: https://github.com/stackabletech/operator-rs/pull/1012
2835

2936
## [0.5.0] - 2025-04-08
3037

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! To get started, see [`Tracing`].
88
9-
use std::path::PathBuf;
9+
use std::{ops::Not, path::PathBuf};
1010

1111
#[cfg_attr(feature = "clap", cfg(doc))]
1212
use clap;
@@ -105,6 +105,7 @@ pub enum Error {
105105
/// async fn main() -> Result<(), Error> {
106106
/// let options = TelemetryOptions {
107107
/// console_log_disabled: false,
108+
/// console_log_format: Default::default(),
108109
/// file_log_directory: None,
109110
/// file_log_rotation_period: None,
110111
/// file_log_max_files: Some(6),
@@ -347,6 +348,7 @@ impl Tracing {
347348
pub fn pre_configured(service_name: &'static str, options: TelemetryOptions) -> Self {
348349
let TelemetryOptions {
349350
console_log_disabled,
351+
console_log_format,
350352
file_log_directory,
351353
file_log_rotation_period,
352354
file_log_max_files,
@@ -358,11 +360,14 @@ impl Tracing {
358360

359361
Self::builder()
360362
.service_name(service_name)
361-
.with_console_output((
362-
Self::CONSOLE_LOG_LEVEL_ENV,
363-
LevelFilter::INFO,
364-
!console_log_disabled,
365-
))
363+
.with_console_output(console_log_disabled.not().then(|| {
364+
Settings::builder()
365+
.with_environment_variable(Self::CONSOLE_LOG_LEVEL_ENV)
366+
.with_default_level(LevelFilter::INFO)
367+
.console_log_settings_builder()
368+
.with_log_format(console_log_format)
369+
.build()
370+
}))
366371
.with_file_output(file_log_directory.map(|log_directory| {
367372
Settings::builder()
368373
.with_environment_variable(Self::FILE_LOG_LEVEL_ENV)
@@ -397,16 +402,29 @@ impl Tracing {
397402

398403
if let ConsoleLogSettings::Enabled {
399404
common_settings,
400-
log_format: _,
405+
log_format,
401406
} = &self.console_log_settings
402407
{
403408
let env_filter_layer = env_filter_builder(
404409
common_settings.environment_variable,
405410
common_settings.default_level,
406411
);
407-
let console_output_layer =
408-
tracing_subscriber::fmt::layer().with_filter(env_filter_layer);
409-
layers.push(console_output_layer.boxed());
412+
413+
// NOTE (@NickLarsenNZ): There is no elegant way to build the layer depending on formats because the types
414+
// returned from each subscriber "modifier" function is different (sometimes with different generics).
415+
match log_format {
416+
Format::Plain => {
417+
let console_output_layer =
418+
tracing_subscriber::fmt::layer().with_filter(env_filter_layer);
419+
layers.push(console_output_layer.boxed());
420+
}
421+
Format::Json => {
422+
let console_output_layer = tracing_subscriber::fmt::layer()
423+
.json()
424+
.with_filter(env_filter_layer);
425+
layers.push(console_output_layer.boxed());
426+
}
427+
};
410428
}
411429

412430
if let FileLogSettings::Enabled {
@@ -761,9 +779,16 @@ struct Cli {
761779
#[derive(Debug, Default)]
762780
pub struct TelemetryOptions {
763781
/// Disable console logs.
764-
#[cfg_attr(feature = "clap", arg(long, env))]
782+
#[cfg_attr(feature = "clap", arg(long, env, group = "console_log"))]
765783
pub console_log_disabled: bool,
766784

785+
/// Console log format.
786+
#[cfg_attr(
787+
feature = "clap",
788+
arg(long, env, group = "console_log", default_value_t)
789+
)]
790+
pub console_log_format: Format,
791+
767792
/// Enable logging to files located in the specified DIRECTORY.
768793
#[cfg_attr(
769794
feature = "clap",
@@ -1023,6 +1048,7 @@ mod test {
10231048
fn pre_configured() {
10241049
let tracing = Tracing::pre_configured("test", TelemetryOptions {
10251050
console_log_disabled: false,
1051+
console_log_format: Default::default(),
10261052
file_log_directory: None,
10271053
file_log_rotation_period: None,
10281054
file_log_max_files: None,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ pub enum ConsoleLogSettings {
2424
/// Console subscriber log event output formats.
2525
///
2626
/// Currently, only [Plain][Format::Plain] is supported.
27-
#[derive(Debug, Default, PartialEq)]
27+
#[derive(
28+
Clone, Debug, Default, Eq, PartialEq, strum::EnumString, strum::Display, clap::ValueEnum,
29+
)]
30+
#[strum(serialize_all = "snake_case")]
2831
pub enum Format {
2932
/// Use the plain unstructured log output.
3033
///
@@ -34,7 +37,9 @@ pub enum Format {
3437
/// See: [`Layer::with_ansi`][tracing_subscriber::fmt::Layer::with_ansi].
3538
#[default]
3639
Plain,
37-
// Json { pretty: bool },
40+
41+
/// Use structured JSON log output.
42+
Json,
3843
// LogFmt,
3944
}
4045

0 commit comments

Comments
 (0)