Skip to content

Commit 2437007

Browse files
committed
feat: Add method to construct pre-configured Tracing instance
1 parent cd73728 commit 2437007

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-telemetry/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ license.workspace = true
66
edition.workspace = true
77
repository.workspace = true
88

9+
[features]
10+
clap = ["dep:clap"]
11+
912
[dependencies]
1013
axum.workspace = true
14+
clap = { workspace = true, optional = true }
1115
futures-util.workspace = true
1216
opentelemetry = { workspace = true, features = ["logs"] }
1317
opentelemetry-appender-tracing.workspace = true
@@ -16,6 +20,7 @@ opentelemetry-otlp = { workspace = true, features = ["grpc-tonic", "gzip-tonic",
1620
opentelemetry_sdk = { workspace = true, features = ["logs", "rt-tokio", "spec_unstable_logs_enabled"] }
1721
pin-project.workspace = true
1822
snafu.workspace = true
23+
strum.workspace = true
1924
tokio.workspace = true
2025
tower.workspace = true
2126
tracing.workspace = true

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

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//!
77
//! To get started, see [`Tracing`].
88
9+
use std::path::PathBuf;
10+
911
use opentelemetry::trace::TracerProvider;
1012
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
1113
use opentelemetry_otlp::{LogExporter, SpanExporter};
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
1416
trace::SdkTracerProvider,
1517
};
1618
use snafu::{ResultExt as _, Snafu};
17-
use tracing::subscriber::SetGlobalDefaultError;
19+
use tracing::{level_filters::LevelFilter, subscriber::SetGlobalDefaultError};
1820
use tracing_appender::rolling::{InitError, RollingFileAppender};
1921
use tracing_subscriber::{EnvFilter, Layer, Registry, filter::Directive, layer::SubscriberExt};
2022

@@ -244,11 +246,58 @@ pub struct Tracing {
244246
}
245247

246248
impl Tracing {
249+
/// The environment variable used to set the console log level filter.
250+
pub const CONSOLE_LOG_ENV_VAR: &str = "CONSOLE_LOG";
251+
/// The environment variable used to set the rolling file log level filter.
252+
pub const FILE_LOG_ENV_VAR: &str = "FILE_LOG";
253+
/// The filename used for the rolling file logs.
254+
pub const FILE_LOG_NAME: &str = "operator.log";
255+
/// The environment variable used to set the OTLP log level filter.
256+
pub const OTLP_LOG_ENV_VAR: &str = "OTLP_LOG";
257+
/// The environment variable used to set the OTLP trace level filter.
258+
pub const OTLP_TRACE_ENV_VAR: &str = "OTLP_TRACE";
259+
247260
/// Creates and returns a [`TracingBuilder`].
248261
pub fn builder() -> TracingBuilder<builder_state::PreServiceName> {
249262
TracingBuilder::default()
250263
}
251264

265+
/// Creates an returns a pre-configured [`Tracing`] instance which can be initialized by
266+
/// calling [`Tracing::init()`].
267+
///
268+
/// If `rolling_logs_period` is [`None`], this function will use a default value of
269+
/// [`RollingPeriod::Never`].
270+
pub fn pre_configured(service_name: &'static str, options: TelemetryOptions) -> Self {
271+
let TelemetryOptions {
272+
no_console_output,
273+
rolling_logs,
274+
rolling_logs_period,
275+
otlp_traces,
276+
otlp_logs,
277+
} = options;
278+
279+
let rolling_logs_period = rolling_logs_period.unwrap_or_default();
280+
281+
Self::builder()
282+
.service_name(service_name)
283+
.with_console_output((
284+
Self::CONSOLE_LOG_ENV_VAR,
285+
LevelFilter::INFO,
286+
!no_console_output,
287+
))
288+
.with_file_output(rolling_logs.map(|log_directory| {
289+
Settings::builder()
290+
.with_environment_variable(Self::FILE_LOG_ENV_VAR)
291+
.with_default_level(LevelFilter::INFO)
292+
.file_log_settings_builder(log_directory, Self::FILE_LOG_NAME)
293+
.with_rotation_period(rolling_logs_period)
294+
.build()
295+
}))
296+
.with_otlp_log_exporter((Self::OTLP_LOG_ENV_VAR, LevelFilter::DEBUG, otlp_logs))
297+
.with_otlp_trace_exporter((Self::OTLP_TRACE_ENV_VAR, LevelFilter::DEBUG, otlp_traces))
298+
.build()
299+
}
300+
252301
/// Initialise the configured tracing subscribers, returning a guard that
253302
/// will shutdown the subscribers when dropped.
254303
///
@@ -606,6 +655,69 @@ fn env_filter_builder(env_var: &str, default_directive: impl Into<Directive>) ->
606655
.from_env_lossy()
607656
}
608657

658+
/// Contains options which can be passed to [`Tracing::pre_configured()`].
659+
///
660+
/// Additionally, this struct can be used as operator CLI arguments. This functionality is only
661+
/// available if the feature `clap` is enabled.
662+
#[cfg_attr(feature = "clap", derive(clap::Args, PartialEq, Eq))]
663+
#[derive(Debug, Default)]
664+
pub struct TelemetryOptions {
665+
/// Disable console output.
666+
#[cfg_attr(feature = "clap", arg(long, env))]
667+
pub no_console_output: bool,
668+
669+
/// Enable logging to rolling files located in the specified DIRECTORY.
670+
#[cfg_attr(
671+
feature = "clap",
672+
arg(
673+
long,
674+
env = "ROLLING_LOGS_DIR",
675+
value_name = "DIRECTORY",
676+
group = "rolling_logs_group"
677+
)
678+
)]
679+
pub rolling_logs: Option<PathBuf>,
680+
681+
/// Time PERIOD after which log files are rolled over.
682+
#[cfg_attr(
683+
feature = "clap",
684+
arg(long, env, value_name = "PERIOD", requires = "rolling_logs_group")
685+
)]
686+
pub rolling_logs_period: Option<RollingPeriod>,
687+
688+
/// Enable exporting traces via OTLP.
689+
#[cfg_attr(feature = "clap", arg(long, env))]
690+
pub otlp_traces: bool,
691+
692+
/// Enable exporting logs via OTLP.
693+
#[cfg_attr(feature = "clap", arg(long, env))]
694+
pub otlp_logs: bool,
695+
}
696+
697+
/// Supported periods when the log file is rolled over.
698+
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
699+
#[derive(Clone, Debug, Default, PartialEq, Eq, strum::Display, strum::EnumString)]
700+
#[allow(missing_docs)]
701+
pub enum RollingPeriod {
702+
Minutely,
703+
Hourly,
704+
Daily,
705+
706+
#[default]
707+
Never,
708+
}
709+
710+
impl From<RollingPeriod> for Rotation {
711+
fn from(value: RollingPeriod) -> Self {
712+
match value {
713+
RollingPeriod::Minutely => Self::MINUTELY,
714+
RollingPeriod::Hourly => Self::HOURLY,
715+
RollingPeriod::Daily => Self::DAILY,
716+
RollingPeriod::Never => Self::NEVER,
717+
}
718+
}
719+
}
720+
609721
#[cfg(test)]
610722
mod test {
611723
use std::path::PathBuf;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub struct FileLogSettingsBuilder {
5757

5858
impl FileLogSettingsBuilder {
5959
/// Set file rotation period.
60-
pub fn with_rotation_period(mut self, rotation_period: Rotation) -> Self {
61-
self.rotation_period = rotation_period;
60+
pub fn with_rotation_period(mut self, rotation_period: impl Into<Rotation>) -> Self {
61+
self.rotation_period = rotation_period.into();
6262
self
6363
}
6464

0 commit comments

Comments
 (0)