@@ -18,7 +18,7 @@ use snafu::{ResultExt as _, Snafu};
18
18
use tracing:: subscriber:: SetGlobalDefaultError ;
19
19
use tracing_subscriber:: { filter:: Directive , layer:: SubscriberExt , EnvFilter , Layer , Registry } ;
20
20
21
- use settings:: { ConsoleLogSettings , OtlpLogSettings , OtlpTraceSettings } ;
21
+ use settings:: * ;
22
22
23
23
pub mod settings;
24
24
@@ -40,11 +40,46 @@ pub enum Error {
40
40
SetGlobalDefaultSubscriber { source : SetGlobalDefaultError } ,
41
41
}
42
42
43
- /// Easily initialize a set of preconfigured [`Subscriber`][1] layers.
43
+ /// Easily initialize a set of pre-configured [`Subscriber`][1] layers.
44
44
///
45
- /// # Usage:
45
+ /// # Usage
46
+ ///
47
+ /// There are two different styles to configure individual subscribers: Using the sophisticated
48
+ /// [`SettingsBuilder`] or the simplified tuple style for basic configuration. Currently, three
49
+ /// different subscribers are supported: console output, OTLP log export, and OTLP trace export.
50
+ ///
51
+ /// The subscribers are active as long as the tracing guard returned by [`Tracing::init`] is in
52
+ /// scope and not dropped. Dropping it results in subscribers being shut down, which can lead to
53
+ /// loss of telemetry data when done before exiting the application. This is why it is important
54
+ /// to hold onto the guard as long as required.
55
+ ///
56
+ /// <div class="warning">
57
+ /// Name the guard variable appropriately, do not just use <code>let _ =</code>, as that will drop
58
+ /// immediately.
59
+ /// </div>
60
+ ///
61
+ /// ```
62
+ /// # use stackable_telemetry::tracing::{Tracing, Error};
63
+ /// #[tokio::main]
64
+ /// async fn main() -> Result<(), Error> {
65
+ /// let _tracing_guard = Tracing::builder() // < Scope starts here
66
+ /// .service_name("test") // |
67
+ /// .build() // |
68
+ /// .init()?; // |
69
+ /// // |
70
+ /// tracing::info!("log a message"); // |
71
+ /// Ok(()) // < Scope ends here, guard is dropped
72
+ /// }
46
73
/// ```
47
- /// use stackable_telemetry::tracing::{Tracing, Error, settings::{Build as _, Settings}};
74
+ ///
75
+ /// ## Basic configuration
76
+ ///
77
+ /// A basic configuration of subscribers can be done by using 2-tuples or 3-tuples, also called
78
+ /// doubles and triples. Using tuples, the subscriber can be enabled/disabled and it's environment
79
+ /// variable and default level can be set.
80
+ ///
81
+ /// ```
82
+ /// use stackable_telemetry::tracing::{Tracing, Error, settings::Settings};
48
83
/// use tracing_subscriber::filter::LevelFilter;
49
84
///
50
85
/// #[tokio::main]
@@ -54,8 +89,36 @@ pub enum Error {
54
89
/// // runtime.
55
90
/// let otlp_log_flag = false;
56
91
///
57
- /// // IMPORTANT: Name the guard variable appropriately, do not just use
58
- /// // `let _ =`, as that will drop immediately.
92
+ /// let _tracing_guard = Tracing::builder()
93
+ /// .service_name("test")
94
+ /// .with_console_output(("TEST_CONSOLE", LevelFilter::INFO))
95
+ /// .with_otlp_log_exporter(("TEST_OTLP_LOG", LevelFilter::DEBUG, otlp_log_flag))
96
+ /// .build()
97
+ /// .init()?;
98
+ ///
99
+ /// tracing::info!("log a message");
100
+ ///
101
+ /// Ok(())
102
+ /// }
103
+ /// ```
104
+ ///
105
+ /// ## Advanced configuration
106
+ ///
107
+ /// More advanced configurations can be done via the [`Settings::builder`] function. Each
108
+ /// subscriber provides specific settings based on a common set of options. These options can be
109
+ /// customized via the following methods:
110
+ ///
111
+ /// - [`SettingsBuilder::console_log_settings_builder`]
112
+ /// - [`SettingsBuilder::otlp_log_settings_builder`]
113
+ /// - [`SettingsBuilder::otlp_trace_settings_builder`]
114
+ ///
115
+ /// ```
116
+ /// # use stackable_telemetry::tracing::{Tracing, Error, settings::Settings};
117
+ /// # use tracing_subscriber::filter::LevelFilter;
118
+ /// #[tokio::main]
119
+ /// async fn main() -> Result<(), Error> {
120
+ /// let otlp_log_flag = false;
121
+ ///
59
122
/// let _tracing_guard = Tracing::builder()
60
123
/// .service_name("test")
61
124
/// .with_console_output(
@@ -164,8 +227,10 @@ impl Tracing {
164
227
/// Initialise the configured tracing subscribers, returning a guard that
165
228
/// will shutdown the subscribers when dropped.
166
229
///
167
- /// IMPORTANT: Name the guard variable appropriately, do not just use
168
- /// `let _ =`, as that will drop immediately.
230
+ /// <div class="warning">
231
+ /// Name the guard variable appropriately, do not just use <code>let _ =</code>, as that will drop
232
+ /// immediately.
233
+ /// </div>
169
234
pub fn init ( mut self ) -> Result < Tracing > {
170
235
let mut layers: Vec < Box < dyn Layer < Registry > + Sync + Send > > = Vec :: new ( ) ;
171
236
@@ -374,11 +439,11 @@ impl TracingBuilder<builder_state::Config> {
374
439
/// [1]: tracing_subscriber::filter::LevelFilter
375
440
pub fn with_console_output (
376
441
self ,
377
- console_log_settings : ConsoleLogSettings ,
442
+ console_log_settings : impl Into < ConsoleLogSettings > ,
378
443
) -> TracingBuilder < builder_state:: Config > {
379
444
TracingBuilder {
380
445
service_name : self . service_name ,
381
- console_log_settings,
446
+ console_log_settings : console_log_settings . into ( ) ,
382
447
otlp_log_settings : self . otlp_log_settings ,
383
448
otlp_trace_settings : self . otlp_trace_settings ,
384
449
_marker : self . _marker ,
@@ -394,12 +459,12 @@ impl TracingBuilder<builder_state::Config> {
394
459
/// [1]: tracing_subscriber::filter::LevelFilter
395
460
pub fn with_otlp_log_exporter (
396
461
self ,
397
- otlp_log_settings : OtlpLogSettings ,
462
+ otlp_log_settings : impl Into < OtlpLogSettings > ,
398
463
) -> TracingBuilder < builder_state:: Config > {
399
464
TracingBuilder {
400
465
service_name : self . service_name ,
401
466
console_log_settings : self . console_log_settings ,
402
- otlp_log_settings,
467
+ otlp_log_settings : otlp_log_settings . into ( ) ,
403
468
otlp_trace_settings : self . otlp_trace_settings ,
404
469
_marker : self . _marker ,
405
470
}
@@ -414,13 +479,13 @@ impl TracingBuilder<builder_state::Config> {
414
479
/// [1]: tracing_subscriber::filter::LevelFilter
415
480
pub fn with_otlp_trace_exporter (
416
481
self ,
417
- otlp_trace_settings : OtlpTraceSettings ,
482
+ otlp_trace_settings : impl Into < OtlpTraceSettings > ,
418
483
) -> TracingBuilder < builder_state:: Config > {
419
484
TracingBuilder {
420
485
service_name : self . service_name ,
421
486
console_log_settings : self . console_log_settings ,
422
487
otlp_log_settings : self . otlp_log_settings ,
423
- otlp_trace_settings,
488
+ otlp_trace_settings : otlp_trace_settings . into ( ) ,
424
489
_marker : self . _marker ,
425
490
}
426
491
}
@@ -452,7 +517,8 @@ fn env_filter_builder(env_var: &str, default_directive: impl Into<Directive>) ->
452
517
453
518
#[ cfg( test) ]
454
519
mod test {
455
- use settings:: { Build as _, Settings } ;
520
+ use rstest:: rstest;
521
+ use settings:: Settings ;
456
522
use tracing:: level_filters:: LevelFilter ;
457
523
458
524
use super :: * ;
@@ -499,6 +565,48 @@ mod test {
499
565
assert ! ( !trace_guard. otlp_trace_settings. enabled) ;
500
566
}
501
567
568
+ #[ test]
569
+ fn builder_with_console_output_double ( ) {
570
+ let trace_guard = Tracing :: builder ( )
571
+ . service_name ( "test" )
572
+ . with_console_output ( ( "ABC_A" , LevelFilter :: TRACE ) )
573
+ . build ( ) ;
574
+
575
+ assert_eq ! (
576
+ trace_guard. console_log_settings,
577
+ ConsoleLogSettings {
578
+ common_settings: Settings {
579
+ environment_variable: "ABC_A" ,
580
+ default_level: LevelFilter :: TRACE ,
581
+ enabled: true
582
+ } ,
583
+ log_format: Default :: default ( )
584
+ }
585
+ )
586
+ }
587
+
588
+ #[ rstest]
589
+ #[ case( false ) ]
590
+ #[ case( true ) ]
591
+ fn builder_with_console_output_triple ( #[ case] enabled : bool ) {
592
+ let trace_guard = Tracing :: builder ( )
593
+ . service_name ( "test" )
594
+ . with_console_output ( ( "ABC_A" , LevelFilter :: TRACE , enabled) )
595
+ . build ( ) ;
596
+
597
+ assert_eq ! (
598
+ trace_guard. console_log_settings,
599
+ ConsoleLogSettings {
600
+ common_settings: Settings {
601
+ environment_variable: "ABC_A" ,
602
+ default_level: LevelFilter :: TRACE ,
603
+ enabled
604
+ } ,
605
+ log_format: Default :: default ( )
606
+ }
607
+ )
608
+ }
609
+
502
610
#[ test]
503
611
fn builder_with_all ( ) {
504
612
let trace_guard = Tracing :: builder ( )
0 commit comments