66//!
77//! To get started, see [`Tracing`].
88
9+ use std:: path:: PathBuf ;
10+
911use opentelemetry:: trace:: TracerProvider ;
1012use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
1113use opentelemetry_otlp:: { LogExporter , SpanExporter } ;
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
1416 trace:: SdkTracerProvider ,
1517} ;
1618use snafu:: { ResultExt as _, Snafu } ;
17- use tracing:: subscriber:: SetGlobalDefaultError ;
19+ use tracing:: { level_filters :: LevelFilter , subscriber:: SetGlobalDefaultError } ;
1820use tracing_appender:: rolling:: { InitError , RollingFileAppender } ;
1921use tracing_subscriber:: { EnvFilter , Layer , Registry , filter:: Directive , layer:: SubscriberExt } ;
2022
@@ -244,11 +246,58 @@ pub struct Tracing {
244246}
245247
246248impl 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) ]
610722mod test {
611723 use std:: path:: PathBuf ;
0 commit comments