6
6
//!
7
7
//! To get started, see [`Tracing`].
8
8
9
+ use std:: path:: PathBuf ;
10
+
9
11
use opentelemetry:: trace:: TracerProvider ;
10
12
use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
11
13
use opentelemetry_otlp:: { LogExporter , SpanExporter } ;
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
14
16
trace:: SdkTracerProvider ,
15
17
} ;
16
18
use snafu:: { ResultExt as _, Snafu } ;
17
- use tracing:: subscriber:: SetGlobalDefaultError ;
19
+ use tracing:: { level_filters :: LevelFilter , subscriber:: SetGlobalDefaultError } ;
18
20
use tracing_appender:: rolling:: { InitError , RollingFileAppender } ;
19
21
use tracing_subscriber:: { EnvFilter , Layer , Registry , filter:: Directive , layer:: SubscriberExt } ;
20
22
@@ -244,11 +246,58 @@ pub struct Tracing {
244
246
}
245
247
246
248
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
+
247
260
/// Creates and returns a [`TracingBuilder`].
248
261
pub fn builder ( ) -> TracingBuilder < builder_state:: PreServiceName > {
249
262
TracingBuilder :: default ( )
250
263
}
251
264
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
+
252
301
/// Initialise the configured tracing subscribers, returning a guard that
253
302
/// will shutdown the subscribers when dropped.
254
303
///
@@ -606,6 +655,69 @@ fn env_filter_builder(env_var: &str, default_directive: impl Into<Directive>) ->
606
655
. from_env_lossy ( )
607
656
}
608
657
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
+
609
721
#[ cfg( test) ]
610
722
mod test {
611
723
use std:: path:: PathBuf ;
0 commit comments