@@ -134,6 +134,7 @@ pub enum Error {
134
134
/// # use tracing_subscriber::filter::LevelFilter;
135
135
/// #[tokio::main]
136
136
/// async fn main() -> Result<(), Error> {
137
+ /// // Control the otlp_log subscriber at runtime
137
138
/// let otlp_log_flag = false;
138
139
///
139
140
/// let _tracing_guard = Tracing::builder()
@@ -142,21 +143,25 @@ pub enum Error {
142
143
/// Settings::builder()
143
144
/// .with_environment_variable("TEST_CONSOLE")
144
145
/// .with_default_level(LevelFilter::INFO)
145
- /// .enabled(true)
146
146
/// .build()
147
147
/// )
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(|| {
149
156
/// Settings::builder()
150
157
/// .with_environment_variable("TEST_OTLP_LOG")
151
158
/// .with_default_level(LevelFilter::DEBUG)
152
- /// .enabled(otlp_log_flag)
153
159
/// .build()
154
- /// )
160
+ /// }) )
155
161
/// .with_otlp_trace_exporter(
156
162
/// Settings::builder()
157
163
/// .with_environment_variable("TEST_OTLP_TRACE")
158
164
/// .with_default_level(LevelFilter::TRACE)
159
- /// .enabled(true)
160
165
/// .build()
161
166
/// )
162
167
/// .build()
@@ -253,30 +258,36 @@ impl Tracing {
253
258
pub fn init ( mut self ) -> Result < Tracing > {
254
259
let mut layers: Vec < Box < dyn Layer < Registry > + Sync + Send > > = Vec :: new ( ) ;
255
260
256
- if self . console_log_settings . enabled {
261
+ if let ConsoleLogSettings :: Enabled {
262
+ common_settings,
263
+ log_format : _,
264
+ } = & self . console_log_settings
265
+ {
257
266
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 ,
262
269
) ;
263
270
let console_output_layer =
264
271
tracing_subscriber:: fmt:: layer ( ) . with_filter ( env_filter_layer) ;
265
272
layers. push ( console_output_layer. boxed ( ) ) ;
266
273
}
267
274
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
+ {
269
280
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 ,
272
283
) ;
273
284
274
285
let file_appender = RollingFileAppender :: builder ( )
275
286
. rotation ( Rotation :: HOURLY )
276
287
. filename_prefix ( self . service_name . to_string ( ) )
277
288
. filename_suffix ( "tracing-rs.json" )
278
289
. max_log_files ( 6 )
279
- . build ( & self . file_log_settings . file_log_dir )
290
+ . build ( file_log_dir)
280
291
. context ( InitRollingFileAppenderSnafu ) ?;
281
292
282
293
layers. push (
@@ -288,10 +299,10 @@ impl Tracing {
288
299
) ;
289
300
}
290
301
291
- if self . otlp_log_settings . enabled {
302
+ if let OtlpLogSettings :: Enabled { common_settings } = & self . otlp_log_settings {
292
303
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 ,
295
306
)
296
307
// TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
297
308
. add_directive ( "h2=off" . parse ( ) . expect ( "invalid directive" ) ) ;
@@ -316,12 +327,11 @@ impl Tracing {
316
327
self . logger_provider = Some ( otel_log) ;
317
328
}
318
329
319
- if self . otlp_trace_settings . enabled {
330
+ if let OtlpTraceSettings :: Enabled { common_settings } = & self . otlp_trace_settings {
320
331
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 ,
325
335
)
326
336
// TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
327
337
. add_directive ( "h2=off" . parse ( ) . expect ( "invalid directive" ) ) ;
@@ -369,12 +379,12 @@ impl Tracing {
369
379
impl Drop for Tracing {
370
380
fn drop ( & mut self ) {
371
381
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 ( ) ,
374
384
"shutting down opentelemetry OTLP providers"
375
385
) ;
376
386
377
- if self . otlp_trace_settings . enabled {
387
+ if self . otlp_trace_settings . is_enabled ( ) {
378
388
// NOTE (@NickLarsenNZ): This might eventually be replaced with something like SdkMeterProvider::shutdown(&self)
379
389
// as has been done with the LoggerProvider (further below)
380
390
// see: https://github.com/open-telemetry/opentelemetry-rust/pull/1412/files#r1409608679
@@ -448,9 +458,9 @@ impl BuilderState for builder_state::Config {}
448
458
pub struct TracingBuilder < S : BuilderState > {
449
459
service_name : Option < & ' static str > ,
450
460
console_log_settings : ConsoleLogSettings ,
461
+ file_log_settings : FileLogSettings ,
451
462
otlp_log_settings : OtlpLogSettings ,
452
463
otlp_trace_settings : OtlpTraceSettings ,
453
- file_log_settings : FileLogSettings ,
454
464
455
465
/// Allow the generic to be used (needed for impls).
456
466
_marker : std:: marker:: PhantomData < S > ,
@@ -600,31 +610,30 @@ mod test {
600
610
Settings :: builder ( )
601
611
. with_environment_variable ( "ABC_A" )
602
612
. with_default_level ( LevelFilter :: TRACE )
603
- . enabled ( true )
604
613
. build ( ) ,
605
614
)
606
615
. with_console_output (
607
616
Settings :: builder ( )
608
617
. with_environment_variable ( "ABC_B" )
609
618
. with_default_level ( LevelFilter :: DEBUG )
610
- . enabled ( true )
611
619
. build ( ) ,
612
620
)
613
621
. build ( ) ;
614
622
615
623
assert_eq ! (
616
624
trace_guard. console_log_settings,
617
- ConsoleLogSettings {
625
+ ConsoleLogSettings :: Enabled {
618
626
common_settings: Settings {
619
- enabled: true ,
620
627
environment_variable: "ABC_B" ,
621
628
default_level: LevelFilter :: DEBUG
622
629
} ,
623
630
log_format: Default :: default ( )
624
631
}
625
632
) ;
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( ) ) ;
628
637
}
629
638
630
639
#[ test]
@@ -636,11 +645,10 @@ mod test {
636
645
637
646
assert_eq ! (
638
647
trace_guard. console_log_settings,
639
- ConsoleLogSettings {
648
+ ConsoleLogSettings :: Enabled {
640
649
common_settings: Settings {
641
650
environment_variable: "ABC_A" ,
642
651
default_level: LevelFilter :: TRACE ,
643
- enabled: true
644
652
} ,
645
653
log_format: Default :: default ( )
646
654
}
@@ -656,17 +664,18 @@ mod test {
656
664
. with_console_output ( ( "ABC_A" , LevelFilter :: TRACE , enabled) )
657
665
. build ( ) ;
658
666
659
- assert_eq ! (
660
- trace_guard. console_log_settings,
661
- ConsoleLogSettings {
667
+ let expected = match enabled {
668
+ true => ConsoleLogSettings :: Enabled {
662
669
common_settings : Settings {
663
670
environment_variable : "ABC_A" ,
664
671
default_level : LevelFilter :: TRACE ,
665
- enabled
666
672
} ,
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)
670
679
}
671
680
672
681
#[ test]
@@ -677,38 +686,33 @@ mod test {
677
686
Settings :: builder ( )
678
687
. with_environment_variable ( "ABC_CONSOLE" )
679
688
. with_default_level ( LevelFilter :: INFO )
680
- . enabled ( true )
681
689
. build ( ) ,
682
690
)
683
691
. with_file_output (
684
692
Settings :: builder ( )
685
693
. with_environment_variable ( "ABC_FILE" )
686
694
. with_default_level ( LevelFilter :: INFO )
687
- . enabled ( true )
688
695
. file_log_settings_builder ( PathBuf :: from ( "/abc_file_dir" ) )
689
696
. build ( ) ,
690
697
)
691
698
. with_otlp_log_exporter (
692
699
Settings :: builder ( )
693
700
. with_environment_variable ( "ABC_OTLP_LOG" )
694
701
. with_default_level ( LevelFilter :: DEBUG )
695
- . enabled ( true )
696
702
. build ( ) ,
697
703
)
698
704
. with_otlp_trace_exporter (
699
705
Settings :: builder ( )
700
706
. with_environment_variable ( "ABC_OTLP_TRACE" )
701
707
. with_default_level ( LevelFilter :: TRACE )
702
- . enabled ( true )
703
708
. build ( ) ,
704
709
)
705
710
. build ( ) ;
706
711
707
712
assert_eq ! (
708
713
trace_guard. console_log_settings,
709
- ConsoleLogSettings {
714
+ ConsoleLogSettings :: Enabled {
710
715
common_settings: Settings {
711
- enabled: true ,
712
716
environment_variable: "ABC_CONSOLE" ,
713
717
default_level: LevelFilter :: INFO
714
718
} ,
@@ -717,9 +721,8 @@ mod test {
717
721
) ;
718
722
assert_eq ! (
719
723
trace_guard. file_log_settings,
720
- FileLogSettings {
724
+ FileLogSettings :: Enabled {
721
725
common_settings: Settings {
722
- enabled: true ,
723
726
environment_variable: "ABC_FILE" ,
724
727
default_level: LevelFilter :: INFO
725
728
} ,
@@ -728,23 +731,59 @@ mod test {
728
731
) ;
729
732
assert_eq ! (
730
733
trace_guard. otlp_log_settings,
731
- OtlpLogSettings {
734
+ OtlpLogSettings :: Enabled {
732
735
common_settings: Settings {
733
- enabled: true ,
734
736
environment_variable: "ABC_OTLP_LOG" ,
735
737
default_level: LevelFilter :: DEBUG
736
738
} ,
737
739
}
738
740
) ;
739
741
assert_eq ! (
740
742
trace_guard. otlp_trace_settings,
741
- OtlpTraceSettings {
743
+ OtlpTraceSettings :: Enabled {
742
744
common_settings: Settings {
743
- enabled: true ,
744
745
environment_variable: "ABC_OTLP_TRACE" ,
745
746
default_level: LevelFilter :: TRACE
746
747
}
747
748
}
748
749
) ;
749
750
}
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
+ }
750
789
}
0 commit comments