stackable_telemetry/tracing/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
//! This module contains functionality to initialize tracing Subscribers for
//! console output, file output, and OpenTelemetry OTLP export for traces and logs.
//!
//! It is intended to be used by the Stackable Data Platform operators and
//! webhooks, but it should be generic enough to be used in any application.
//!
//! To get started, see [`Tracing`].

use std::{ops::Not, path::PathBuf};

#[cfg_attr(feature = "clap", cfg(doc))]
use clap;
use opentelemetry::trace::TracerProvider;
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_otlp::{ExporterBuildError, LogExporter, SpanExporter};
use opentelemetry_sdk::{
    Resource, logs::SdkLoggerProvider, propagation::TraceContextPropagator,
    trace::SdkTracerProvider,
};
use snafu::{ResultExt as _, Snafu};
use tracing::{level_filters::LevelFilter, subscriber::SetGlobalDefaultError};
use tracing_appender::rolling::{InitError, RollingFileAppender};
use tracing_subscriber::{EnvFilter, Layer, Registry, filter::Directive, layer::SubscriberExt};

use crate::tracing::settings::*;

pub mod settings;

type Result<T, E = Error> = std::result::Result<T, E>;

/// Errors which can be encountered when initialising [`Tracing`].
#[derive(Debug, Snafu)]
pub enum Error {
    /// Indicates that [`Tracing`] failed to install the OpenTelemetry trace exporter.
    #[snafu(display("unable to install opentelemetry trace exporter"))]
    InstallOtelTraceExporter {
        #[allow(missing_docs)]
        source: ExporterBuildError,
    },

    /// Indicates that [`Tracing`] failed to install the OpenTelemetry log exporter.
    #[snafu(display("unable to install opentelemetry log exporter"))]
    InstallOtelLogExporter {
        #[allow(missing_docs)]
        source: ExporterBuildError,
    },

    /// Indicates that [`Tracing`] failed to install the rolling file appender.
    #[snafu(display("failed to initialize rolling file appender"))]
    InitRollingFileAppender {
        #[allow(missing_docs)]
        source: InitError,
    },

    /// Indicates that [`Tracing`] failed to set the global default subscriber.
    #[snafu(display("unable to set the global default subscriber"))]
    SetGlobalDefaultSubscriber {
        #[allow(missing_docs)]
        source: SetGlobalDefaultError,
    },
}

/// Easily initialize a set of pre-configured [`Subscriber`][1] layers.
///
/// # Usage
///
/// ## Tracing Guard
///
/// The configured subscribers are active as long as the tracing guard returned by [`Tracing::init`]
/// is in scope and not dropped. Dropping it results in subscribers being shut down, which can lead
/// to loss of telemetry data when done before exiting the application. This is why it is important
/// to hold onto the guard as long as required.
///
/// <div class="warning">
///
/// Name the guard variable appropriately, do not just use `let _ = ...`, as that will drop
/// immediately.
///
/// </div>
///
/// ```
/// # use stackable_telemetry::tracing::{Tracing, Error};
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
///     let _tracing_guard = Tracing::builder() // < Scope starts here
///         .service_name("test")               // |
///         .build()                            // |
///         .init()?;                           // |
///                                             // |
///     tracing::info!("log a message");        // |
///     Ok(())                                  // < Scope ends here, guard is dropped
/// }
/// ```
///
/// ## Pre-configured Tracing Instance
///
/// There are two different styles to configure a [`Tracing`] instance: Using an opinionated pre-
/// configured instance or a fully customizable builder. The first option should be suited for
/// pretty much all operators by using sane defaults and applying best practices out-of-the-box.
/// [`Tracing::pre_configured`] lists details about environment variables, filter levels and
/// defaults used.
///
/// ```
/// use stackable_telemetry::tracing::{Tracing, TelemetryOptions, Error};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
///     let options = TelemetryOptions {
///          console_log_disabled: false,
///          console_log_format: Default::default(),
///          file_log_directory: None,
///          file_log_rotation_period: None,
///          file_log_max_files: Some(6),
///          otel_trace_exporter_enabled: true,
///          otel_log_exporter_enabled: true,
///      };
///
///     let _tracing_guard = Tracing::pre_configured("test", options).init()?;
///
///     tracing::info!("log a message");
///
///     Ok(())
/// }
/// ```
///
/// Also see the documentation for [`TelemetryOptions`] which details how it can be used as CLI
/// arguments via [`clap`]. Additionally see [this section](#environment-variables-and-cli-arguments)
/// in the docs for a full list of environment variables and CLI arguments used by the pre-configured
/// instance.
///
/// ## Builders
///
/// When choosing the builder, there are two different styles to configure individual subscribers:
/// Using the sophisticated [`SettingsBuilder`] or the simplified tuple style for basic
/// configuration. Currently, three different subscribers are supported: console output, OTLP log
/// export, and OTLP trace export.
///
/// ### Basic Configuration
///
/// A basic configuration of subscribers can be done by using 2-tuples or 3-tuples, also called
/// doubles and triples. Using tuples, the subscriber can be enabled/disabled and it's environment
/// variable and default level can be set.
///
/// ```
/// use stackable_telemetry::tracing::{Tracing, Error, settings::Settings};
/// use tracing_subscriber::filter::LevelFilter;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
///     // This can come from a Clap argument for example. The enabled builder
///     // function below allows enabling/disabling certain subscribers during
///     // runtime.
///     let otlp_log_flag = false;
///
///     let _tracing_guard = Tracing::builder()
///         .service_name("test")
///         .with_console_output(("TEST_CONSOLE", LevelFilter::INFO))
///         .with_otlp_log_exporter(("TEST_OTLP_LOG", LevelFilter::DEBUG, otlp_log_flag))
///         .build()
///         .init()?;
///
///     tracing::info!("log a message");
///
///     Ok(())
/// }
/// ```
///
/// ### Advanced Configuration
///
/// More advanced configurations can be done via the [`Settings::builder`] function. Each
/// subscriber provides specific settings based on a common set of options. These options can be
/// customized via the following methods:
///
/// - [`SettingsBuilder::console_log_settings_builder`]
/// - [`SettingsBuilder::otlp_log_settings_builder`]
/// - [`SettingsBuilder::otlp_trace_settings_builder`]
///
/// ```
/// # use stackable_telemetry::tracing::{Tracing, Error, settings::Settings};
/// # use tracing_subscriber::filter::LevelFilter;
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
///     // Control the otlp_log subscriber at runtime
///     let otlp_log_flag = false;
///
///     let _tracing_guard = Tracing::builder()
///         .service_name("test")
///         .with_console_output(
///             Settings::builder()
///                 .with_environment_variable("CONSOLE_LOG")
///                 .with_default_level(LevelFilter::INFO)
///                 .build()
///         )
///         .with_file_output(
///             Settings::builder()
///                 .with_environment_variable("FILE_LOG")
///                 .with_default_level(LevelFilter::INFO)
///                 .file_log_settings_builder("/tmp/logs", "operator.log")
///                 .build()
///         )
///         .with_otlp_log_exporter(otlp_log_flag.then(|| {
///             Settings::builder()
///                 .with_environment_variable("OTLP_LOG")
///                 .with_default_level(LevelFilter::DEBUG)
///                 .build()
///         }))
///         .with_otlp_trace_exporter(
///             Settings::builder()
///                 .with_environment_variable("OTLP_TRACE")
///                 .with_default_level(LevelFilter::TRACE)
///                 .build()
///         )
///         .build()
///         .init()?;
///
///     tracing::info!("log a message");
///
///     Ok(())
/// }
/// ```
///
/// ## Environment Variables and CLI Arguments
///
/// <div class="warning">
///
/// It should be noted that the CLI arguments (listed in parentheses) are only available when the
/// `clap` feature is enabled.
///
/// </div>
///
/// ### Console logs
///
/// - `CONSOLE_LOG_DISABLED` (`--console-log-disabled`): Disables console logs when set to `true`.
/// - `CONSOLE_LOG_FORMAT` (`--console-log-format`): Set the format for the console logs.
/// - `CONSOLE_LOG_LEVEL`: Set the log level for the console logs.
///
/// ### File logs
///
/// - `FILE_LOG_DIRECTORY` (`--file-log-directory`): Enable the file logs and set the file log directory.
/// - `FILE_LOG_ROTATION_PERIOD` (`--file-log-rotation-period`): Set the rotation period of log files.
/// - `FILE_LOG_MAX_FILES` (`--file-log-max-files`): Set the maximum number of log files to keep.
/// - `FILE_LOG_LEVEL`: Set the log level for file logs.
///
/// ### OTEL logs
///
/// - `OTEL_LOG_EXPORTER_ENABLED` (`--otel-log-exporter-enabled`): Enable exporting OTEL logs.
/// - `OTEL_LOG_EXPORTER_LEVEL`: Set the log level for OTEL logs.
///
/// ### OTEL traces
///
/// - `OTEL_TRACE_EXPORTER_ENABLED` (`--otel-trace-exporter-enabled`): Enable exporting OTEL traces.
/// - `OTEL_TRACE_EXPORTER_LEVEL`: Set the log level for OTEL traces.
///
/// # Additional Configuration
///
/// You can configure the OTLP trace and log exports through the variables defined in the opentelemetry crates:
///
/// - `OTEL_EXPORTER_OTLP_COMPRESSION` (defaults to none, but can be set to `gzip`).
/// - `OTEL_EXPORTER_OTLP_ENDPOINT` (defaults to `http://localhost:4317`, with the `grpc-tonic` feature (default)).
/// - `OTEL_EXPORTER_OTLP_TIMEOUT`
/// - `OTEL_EXPORTER_OTLP_HEADERS`
///
/// _See the defaults in the [opentelemetry-otlp][2] crate._
///
/// ## Tracing exporter overrides
///
/// OTLP Exporter settings:
///
/// - `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`
/// - `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT`
/// - `OTEL_EXPORTER_OTLP_TRACES_COMPRESSION`
/// - `OTEL_EXPORTER_OTLP_TRACES_HEADERS`
///
/// General Span and Trace settings:
///
/// - `OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT`
/// - `OTEL_SPAN_EVENT_COUNT_LIMIT`
/// - `OTEL_SPAN_LINK_COUNT_LIMIT`
/// - `OTEL_TRACES_SAMPLER` (Defaults to `parentbased_always_on`. If "traceidratio" or "parentbased_traceidratio", then `OTEL_TRACES_SAMPLER_ARG`)
///
/// Batch Span Processor settings:
///
/// - `OTEL_BSP_MAX_QUEUE_SIZE`
/// - `OTEL_BSP_SCHEDULE_DELAY`
/// - `OTEL_BSP_MAX_EXPORT_BATCH_SIZE`
/// - `OTEL_BSP_EXPORT_TIMEOUT`
/// - `OTEL_BSP_MAX_CONCURRENT_EXPORTS`
///
/// _See defaults in the opentelemetry_sdk crate under [trace::config][3] and [trace::span_processor][4]._
///
/// ## Log exporter overrides
///
/// OTLP exporter settings:
///
/// - `OTEL_EXPORTER_OTLP_LOGS_COMPRESSION`
/// - `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`
/// - `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT`
/// - `OTEL_EXPORTER_OTLP_LOGS_HEADERS`
///
/// Batch Log Record Processor settings:
///
/// - `OTEL_BLRP_MAX_QUEUE_SIZE`
/// - `OTEL_BLRP_SCHEDULE_DELAY`
/// - `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`
/// - `OTEL_BLRP_EXPORT_TIMEOUT`
///
/// _See defaults in the opentelemetry_sdk crate under [log::log_processor][5]._
///
/// [1]: tracing::Subscriber
/// [2]: https://docs.rs/opentelemetry-otlp/latest/src/opentelemetry_otlp/exporter/mod.rs.html
/// [3]: https://docs.rs/opentelemetry_sdk/latest/src/opentelemetry_sdk/trace/config.rs.html
/// [4]: https://docs.rs/opentelemetry_sdk/latest/src/opentelemetry_sdk/trace/span_processor.rs.html
/// [5]: https://docs.rs/opentelemetry_sdk/latest/src/opentelemetry_sdk/logs/log_processor.rs.html
pub struct Tracing {
    service_name: &'static str,
    console_log_settings: ConsoleLogSettings,
    file_log_settings: FileLogSettings,
    otlp_log_settings: OtlpLogSettings,
    otlp_trace_settings: OtlpTraceSettings,

    logger_provider: Option<SdkLoggerProvider>,
    tracer_provider: Option<SdkTracerProvider>,
}

impl Tracing {
    /// The environment variable used to set the console log level filter.
    pub const CONSOLE_LOG_LEVEL_ENV: &str = "CONSOLE_LOG_LEVEL";
    /// The environment variable used to set the rolling file log level filter.
    pub const FILE_LOG_LEVEL_ENV: &str = "FILE_LOG_LEVEL";
    /// The filename used for the rolling file logs.
    pub const FILE_LOG_SUFFIX: &str = "tracing-rs.json";
    /// The environment variable used to set the OTEL log level filter.
    pub const OTEL_LOG_EXPORTER_LEVEL_ENV: &str = "OTEL_LOG_EXPORTER_LEVEL";
    /// The environment variable used to set the OTEL trace level filter.
    pub const OTEL_TRACE_EXPORTER_LEVEL_ENV: &str = "OTEL_TRACE_EXPORTER_LEVEL";

    /// Creates and returns a [`TracingBuilder`].
    pub fn builder() -> TracingBuilder<builder_state::PreServiceName> {
        TracingBuilder::default()
    }

    /// Creates an returns a pre-configured [`Tracing`] instance which can be initialized by
    /// calling [`Tracing::init()`].
    ///
    /// Also see [this section](#environment-variables-and-cli-arguments) in the docs for all full
    /// list of environment variables and CLI arguments used by the pre-configured instance.
    ///
    /// ### Default Levels
    ///
    /// - Console logs: INFO
    /// - File logs: INFO
    /// - OTEL logs: INFO
    /// - OTEL traces: INFO
    ///
    /// ### Default Values
    ///
    /// - If `rolling_logs_period` is [`None`], this function will use a default value of
    ///   [`RotationPeriod::Never`].
    pub fn pre_configured(service_name: &'static str, options: TelemetryOptions) -> Self {
        let TelemetryOptions {
            console_log_disabled,
            console_log_format,
            file_log_directory,
            file_log_rotation_period,
            file_log_max_files,
            otel_trace_exporter_enabled,
            otel_log_exporter_enabled,
        } = options;

        let file_log_rotation_period = file_log_rotation_period.unwrap_or_default();

        Self::builder()
            .service_name(service_name)
            .with_console_output(console_log_disabled.not().then(|| {
                Settings::builder()
                    .with_environment_variable(Self::CONSOLE_LOG_LEVEL_ENV)
                    .with_default_level(LevelFilter::INFO)
                    .console_log_settings_builder()
                    .with_log_format(console_log_format)
                    .build()
            }))
            .with_file_output(file_log_directory.map(|log_directory| {
                Settings::builder()
                    .with_environment_variable(Self::FILE_LOG_LEVEL_ENV)
                    .with_default_level(LevelFilter::INFO)
                    .file_log_settings_builder(log_directory, Self::FILE_LOG_SUFFIX)
                    .with_rotation_period(file_log_rotation_period)
                    .with_max_files(file_log_max_files)
                    .build()
            }))
            .with_otlp_log_exporter((
                Self::OTEL_LOG_EXPORTER_LEVEL_ENV,
                LevelFilter::INFO,
                otel_log_exporter_enabled,
            ))
            .with_otlp_trace_exporter((
                Self::OTEL_TRACE_EXPORTER_LEVEL_ENV,
                LevelFilter::INFO,
                otel_trace_exporter_enabled,
            ))
            .build()
    }

    /// Initialize the configured tracing subscribers, returning a guard that
    /// will shutdown the subscribers when dropped.
    ///
    /// <div class="warning">
    /// Name the guard variable appropriately, do not just use <code>let _ =</code>, as that will drop
    /// immediately.
    /// </div>
    pub fn init(mut self) -> Result<Tracing> {
        let mut layers: Vec<Box<dyn Layer<Registry> + Sync + Send>> = Vec::new();

        if let ConsoleLogSettings::Enabled {
            common_settings,
            log_format,
        } = &self.console_log_settings
        {
            let env_filter_layer = env_filter_builder(
                common_settings.environment_variable,
                common_settings.default_level,
            );

            // NOTE (@NickLarsenNZ): There is no elegant way to build the layer depending on formats because the types
            // returned from each subscriber "modifier" function is different (sometimes with different generics).
            match log_format {
                Format::Plain => {
                    let console_output_layer =
                        tracing_subscriber::fmt::layer().with_filter(env_filter_layer);
                    layers.push(console_output_layer.boxed());
                }
                Format::Json => {
                    let console_output_layer = tracing_subscriber::fmt::layer()
                        .json()
                        .with_filter(env_filter_layer);
                    layers.push(console_output_layer.boxed());
                }
            };
        }

        if let FileLogSettings::Enabled {
            common_settings,
            file_log_dir,
            rotation_period,
            filename_suffix,
            max_log_files,
        } = &self.file_log_settings
        {
            let env_filter_layer = env_filter_builder(
                common_settings.environment_variable,
                common_settings.default_level,
            );

            let file_appender = RollingFileAppender::builder()
                .rotation(rotation_period.clone())
                .filename_prefix(self.service_name.to_string())
                .filename_suffix(filename_suffix);

            let file_appender = if let Some(max_log_files) = max_log_files {
                file_appender.max_log_files(*max_log_files)
            } else {
                file_appender
            };

            let file_appender = file_appender
                .build(file_log_dir)
                .context(InitRollingFileAppenderSnafu)?;

            layers.push(
                tracing_subscriber::fmt::layer()
                    .json()
                    .with_writer(file_appender)
                    .with_filter(env_filter_layer)
                    .boxed(),
            );
        }

        if let OtlpLogSettings::Enabled { common_settings } = &self.otlp_log_settings {
            let env_filter_layer = env_filter_builder(
                common_settings.environment_variable,
                common_settings.default_level,
            )
            // TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
            .add_directive("h2=off".parse().expect("invalid directive"));

            let log_exporter = LogExporter::builder()
                .with_tonic()
                .build()
                .context(InstallOtelLogExporterSnafu)?;

            let logger_provider = SdkLoggerProvider::builder()
                .with_batch_exporter(log_exporter)
                .with_resource(
                    Resource::builder()
                        .with_service_name(self.service_name)
                        .build(),
                )
                .build();

            // Convert `tracing::Event` to OpenTelemetry logs
            layers.push(
                OpenTelemetryTracingBridge::new(&logger_provider)
                    .with_filter(env_filter_layer)
                    .boxed(),
            );
            self.logger_provider = Some(logger_provider);
        }

        if let OtlpTraceSettings::Enabled { common_settings } = &self.otlp_trace_settings {
            let env_filter_layer = env_filter_builder(
                // todo, deref?
                common_settings.environment_variable,
                common_settings.default_level,
            )
            // TODO (@NickLarsenNZ): Remove this directive once https://github.com/open-telemetry/opentelemetry-rust/issues/761 is resolved
            .add_directive("h2=off".parse().expect("invalid directive"));

            let trace_exporter = SpanExporter::builder()
                .with_tonic()
                .build()
                .context(InstallOtelTraceExporterSnafu)?;

            let tracer_provider = SdkTracerProvider::builder()
                .with_batch_exporter(trace_exporter)
                .with_resource(
                    Resource::builder()
                        .with_service_name(self.service_name)
                        .build(),
                )
                .build();

            let tracer = tracer_provider.tracer(self.service_name);

            layers.push(
                tracing_opentelemetry::layer()
                    .with_tracer(tracer)
                    .with_filter(env_filter_layer)
                    .boxed(),
            );
            self.tracer_provider = Some(tracer_provider);

            opentelemetry::global::set_text_map_propagator(
                // NOTE (@NickLarsenNZ): There are various propagators. Eg: TraceContextPropagator
                // standardises HTTP headers to propagate trace-id, parent-id, etc... while the
                // BaggagePropagator sets a "baggage" header with the value being key=value pairs. There
                // are other kinds too. There is also B3 and Jaeger, and some legacy stuff like OT Trace
                // and OpenCensus.
                // See: https://opentelemetry.io/docs/specs/otel/context/api-propagators/
                TraceContextPropagator::new(),
            );
        }

        if !layers.is_empty() {
            // Add the layers to the tracing_subscriber Registry (console,
            // tracing (OTLP), logging (OTLP))
            tracing::subscriber::set_global_default(tracing_subscriber::registry().with(layers))
                .context(SetGlobalDefaultSubscriberSnafu)?;
        }

        // IMPORTANT: we must return self, otherwise Drop will be called and uninitialise tracing
        Ok(self)
    }
}

impl Drop for Tracing {
    fn drop(&mut self) {
        tracing::debug!(
            opentelemetry.tracing.enabled = self.otlp_trace_settings.is_enabled(),
            opentelemetry.logger.enabled = self.otlp_log_settings.is_enabled(),
            "shutting down opentelemetry OTLP providers"
        );

        if let Some(tracer_provider) = &self.tracer_provider {
            if let Err(error) = tracer_provider.shutdown() {
                tracing::error!(%error, "unable to shutdown TracerProvider")
            }
        }

        if let Some(logger_provider) = &self.logger_provider {
            if let Err(error) = logger_provider.shutdown() {
                tracing::error!(%error, "unable to shutdown LoggerProvider");
            }
        }
    }
}

/// This trait is only used for the typestate builder and cannot be implemented
/// outside of this crate.
///
/// The only reason it has pub visibility is because it needs to be at least as
/// visible as the types that use it.
#[doc(hidden)]
pub trait BuilderState: private::Sealed {}

/// This private module holds the [`Sealed`][1] trait that is used by the
/// [`BuilderState`], so that it cannot be implemented outside of this crate.
///
/// We impl Sealed for any types that will use the trait that we want to
/// restrict impls on. In this case, the [`BuilderState`] trait.
///
/// [1]: private::Sealed
#[doc(hidden)]
mod private {
    use super::*;

    pub trait Sealed {}

    impl Sealed for builder_state::PreServiceName {}
    impl Sealed for builder_state::Config {}
}

/// This module holds the possible states that the builder is in.
///
/// Each state will implement [`BuilderState`] (with no methods), and the
/// Builder struct ([`TracingBuilder`]) itself will be implemented with
/// each state as a generic parameter.
/// This allows only the methods to be called when the builder is in the
/// applicable state.
#[doc(hidden)]
mod builder_state {
    /// The initial state, before the service name is set.
    #[derive(Default)]
    pub struct PreServiceName;

    /// The state that allows you to configure the supported [`Subscriber`][1]
    /// [`Layer`][2].
    ///
    /// [1]: tracing::Subscriber
    /// [2]: tracing_subscriber::layer::Layer
    #[derive(Default)]
    pub struct Config;
}

// Make the states usable
#[doc(hidden)]
impl BuilderState for builder_state::PreServiceName {}

#[doc(hidden)]
impl BuilderState for builder_state::Config {}

/// Makes it easy to build a valid [`Tracing`] instance.
#[derive(Default)]
pub struct TracingBuilder<S: BuilderState> {
    service_name: Option<&'static str>,
    console_log_settings: ConsoleLogSettings,
    file_log_settings: FileLogSettings,
    otlp_log_settings: OtlpLogSettings,
    otlp_trace_settings: OtlpTraceSettings,

    /// Allow the generic to be used (needed for impls).
    _marker: std::marker::PhantomData<S>,
}

impl TracingBuilder<builder_state::PreServiceName> {
    /// Set the service name used in OTLP exports, and console output.
    ///
    /// A service name is required for valid OTLP telemetry.
    pub fn service_name(self, service_name: &'static str) -> TracingBuilder<builder_state::Config> {
        TracingBuilder {
            service_name: Some(service_name),
            ..Default::default()
        }
    }
}

impl TracingBuilder<builder_state::Config> {
    /// Enable the console output tracing subscriber and set the default
    /// [`LevelFilter`] which is overridable through the given environment
    /// variable.
    pub fn with_console_output(
        self,
        console_log_settings: impl Into<ConsoleLogSettings>,
    ) -> TracingBuilder<builder_state::Config> {
        TracingBuilder {
            service_name: self.service_name,
            console_log_settings: console_log_settings.into(),
            otlp_log_settings: self.otlp_log_settings,
            otlp_trace_settings: self.otlp_trace_settings,
            file_log_settings: self.file_log_settings,
            _marker: self._marker,
        }
    }

    /// Enable the file output tracing subscriber and set the default
    /// [`LevelFilter`] which is overridable through the given environment
    /// variable.
    pub fn with_file_output(
        self,
        file_log_settings: impl Into<FileLogSettings>,
    ) -> TracingBuilder<builder_state::Config> {
        TracingBuilder {
            service_name: self.service_name,
            console_log_settings: self.console_log_settings,
            file_log_settings: file_log_settings.into(),
            otlp_log_settings: self.otlp_log_settings,
            otlp_trace_settings: self.otlp_trace_settings,
            _marker: self._marker,
        }
    }

    /// Enable the OTLP logging subscriber and set the default [`LevelFilter`]
    /// which is overridable through the given environment variable.
    ///
    /// You can configure the OTLP log exports through the variables defined
    /// in the opentelemetry crates. See [`Tracing`].
    pub fn with_otlp_log_exporter(
        self,
        otlp_log_settings: impl Into<OtlpLogSettings>,
    ) -> TracingBuilder<builder_state::Config> {
        TracingBuilder {
            service_name: self.service_name,
            console_log_settings: self.console_log_settings,
            otlp_log_settings: otlp_log_settings.into(),
            otlp_trace_settings: self.otlp_trace_settings,
            file_log_settings: self.file_log_settings,
            _marker: self._marker,
        }
    }

    /// Enable the OTLP tracing subscriber and set the default [`LevelFilter`]
    /// which is overridable through the given environment variable.
    ///
    /// You can configure the OTLP trace exports through the variables defined
    /// in the opentelemetry crates. See [`Tracing`].
    pub fn with_otlp_trace_exporter(
        self,
        otlp_trace_settings: impl Into<OtlpTraceSettings>,
    ) -> TracingBuilder<builder_state::Config> {
        TracingBuilder {
            service_name: self.service_name,
            console_log_settings: self.console_log_settings,
            otlp_log_settings: self.otlp_log_settings,
            otlp_trace_settings: otlp_trace_settings.into(),
            file_log_settings: self.file_log_settings,
            _marker: self._marker,
        }
    }

    /// Consumes self and returns a valid [`Tracing`] instance.
    ///
    /// Once built, you can call [`Tracing::init`] to enable the configured
    /// tracing subscribers.
    pub fn build(self) -> Tracing {
        Tracing {
            service_name: self
                .service_name
                .expect("service_name must be configured at this point"),
            console_log_settings: self.console_log_settings,
            otlp_log_settings: self.otlp_log_settings,
            otlp_trace_settings: self.otlp_trace_settings,
            file_log_settings: self.file_log_settings,
            logger_provider: None,
            tracer_provider: None,
        }
    }
}

/// Create an [`EnvFilter`] configured with the given environment variable and default [`Directive`].
fn env_filter_builder(env_var: &str, default_directive: impl Into<Directive>) -> EnvFilter {
    EnvFilter::builder()
        .with_env_var(env_var)
        .with_default_directive(default_directive.into())
        .from_env_lossy()
}

/// Contains options which can be passed to [`Tracing::pre_configured()`].
///
/// Additionally, this struct can be used as operator CLI arguments. This functionality is only
/// available if the feature `clap` is enabled.
///
#[cfg_attr(
    feature = "clap",
    doc = r#"
```
# use stackable_telemetry::tracing::TelemetryOptions;
use clap::Parser;

#[derive(Parser)]
struct Cli {
    #[arg(short, long)]
    namespace: String,

    #[clap(flatten)]
    telemetry_arguments: TelemetryOptions,
}
```
"#
)]
#[cfg_attr(feature = "clap", derive(clap::Args, PartialEq, Eq))]
#[derive(Debug, Default)]
pub struct TelemetryOptions {
    /// Disable console logs.
    #[cfg_attr(feature = "clap", arg(long, env, group = "console_log"))]
    pub console_log_disabled: bool,

    /// Console log format.
    #[cfg_attr(
        feature = "clap",
        arg(long, env, group = "console_log", default_value_t)
    )]
    pub console_log_format: Format,

    /// Enable logging to files located in the specified DIRECTORY.
    #[cfg_attr(
        feature = "clap",
        arg(long, env, value_name = "DIRECTORY", group = "file_log")
    )]
    pub file_log_directory: Option<PathBuf>,

    /// Time PERIOD after which log files are rolled over.
    #[cfg_attr(
        feature = "clap",
        arg(long, env, value_name = "PERIOD", requires = "file_log")
    )]
    pub file_log_rotation_period: Option<RotationPeriod>,

    /// Maximum NUMBER of log files to keep.
    #[cfg_attr(
        feature = "clap",
        arg(long, env, value_name = "NUMBER", requires = "file_log")
    )]
    pub file_log_max_files: Option<usize>,

    /// Enable exporting OpenTelemetry traces via OTLP.
    #[cfg_attr(feature = "clap", arg(long, env))]
    pub otel_trace_exporter_enabled: bool,

    /// Enable exporting OpenTelemetry logs via OTLP.
    #[cfg_attr(feature = "clap", arg(long, env))]
    pub otel_log_exporter_enabled: bool,
}

/// Supported periods when the log file is rolled over.
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Clone, Debug, Default, PartialEq, Eq, strum::Display, strum::EnumString)]
#[strum(serialize_all = "snake_case")]
#[allow(missing_docs)]
pub enum RotationPeriod {
    Minutely,
    Hourly,
    Daily,

    #[default]
    Never,
}

impl From<RotationPeriod> for Rotation {
    fn from(value: RotationPeriod) -> Self {
        match value {
            RotationPeriod::Minutely => Self::MINUTELY,
            RotationPeriod::Hourly => Self::HOURLY,
            RotationPeriod::Daily => Self::DAILY,
            RotationPeriod::Never => Self::NEVER,
        }
    }
}

#[cfg(test)]
mod test {
    use std::path::PathBuf;

    use rstest::rstest;
    use settings::Settings;
    use tracing::level_filters::LevelFilter;
    use tracing_appender::rolling::Rotation;

    use super::*;

    #[test]
    fn builder_basic_construction() {
        let trace_guard = Tracing::builder().service_name("test").build();

        assert_eq!(trace_guard.service_name, "test");
    }

    #[test]
    fn builder_with_console_output() {
        let trace_guard = Tracing::builder()
            .service_name("test")
            .with_console_output(
                Settings::builder()
                    .with_environment_variable("ABC_A")
                    .with_default_level(LevelFilter::TRACE)
                    .build(),
            )
            .with_console_output(
                Settings::builder()
                    .with_environment_variable("ABC_B")
                    .with_default_level(LevelFilter::DEBUG)
                    .build(),
            )
            .build();

        assert_eq!(
            trace_guard.console_log_settings,
            ConsoleLogSettings::Enabled {
                common_settings: Settings {
                    environment_variable: "ABC_B",
                    default_level: LevelFilter::DEBUG
                },
                log_format: Default::default()
            }
        );

        assert!(trace_guard.file_log_settings.is_disabled());
        assert!(trace_guard.otlp_log_settings.is_disabled());
        assert!(trace_guard.otlp_trace_settings.is_disabled());
    }

    #[test]
    fn builder_with_console_output_double() {
        let trace_guard = Tracing::builder()
            .service_name("test")
            .with_console_output(("ABC_A", LevelFilter::TRACE))
            .build();

        assert_eq!(
            trace_guard.console_log_settings,
            ConsoleLogSettings::Enabled {
                common_settings: Settings {
                    environment_variable: "ABC_A",
                    default_level: LevelFilter::TRACE,
                },
                log_format: Default::default()
            }
        )
    }

    #[rstest]
    #[case(false)]
    #[case(true)]
    fn builder_with_console_output_triple(#[case] enabled: bool) {
        let trace_guard = Tracing::builder()
            .service_name("test")
            .with_console_output(("ABC_A", LevelFilter::TRACE, enabled))
            .build();

        let expected = match enabled {
            true => ConsoleLogSettings::Enabled {
                common_settings: Settings {
                    environment_variable: "ABC_A",
                    default_level: LevelFilter::TRACE,
                },
                log_format: Default::default(),
            },
            false => ConsoleLogSettings::Disabled,
        };

        assert_eq!(trace_guard.console_log_settings, expected)
    }

    #[test]
    fn builder_with_all() {
        let trace_guard = Tracing::builder()
            .service_name("test")
            .with_console_output(
                Settings::builder()
                    .with_environment_variable("ABC_CONSOLE")
                    .with_default_level(LevelFilter::INFO)
                    .build(),
            )
            .with_file_output(
                Settings::builder()
                    .with_environment_variable("ABC_FILE")
                    .with_default_level(LevelFilter::INFO)
                    .file_log_settings_builder(PathBuf::from("/abc_file_dir"), "tracing-rs.json")
                    .build(),
            )
            .with_otlp_log_exporter(
                Settings::builder()
                    .with_environment_variable("ABC_OTLP_LOG")
                    .with_default_level(LevelFilter::DEBUG)
                    .build(),
            )
            .with_otlp_trace_exporter(
                Settings::builder()
                    .with_environment_variable("ABC_OTLP_TRACE")
                    .with_default_level(LevelFilter::TRACE)
                    .build(),
            )
            .build();

        assert_eq!(
            trace_guard.console_log_settings,
            ConsoleLogSettings::Enabled {
                common_settings: Settings {
                    environment_variable: "ABC_CONSOLE",
                    default_level: LevelFilter::INFO
                },
                log_format: Default::default()
            }
        );
        assert_eq!(trace_guard.file_log_settings, FileLogSettings::Enabled {
            common_settings: Settings {
                environment_variable: "ABC_FILE",
                default_level: LevelFilter::INFO
            },
            file_log_dir: PathBuf::from("/abc_file_dir"),
            rotation_period: Rotation::NEVER,
            filename_suffix: "tracing-rs.json".to_owned(),
            max_log_files: None,
        });
        assert_eq!(trace_guard.otlp_log_settings, OtlpLogSettings::Enabled {
            common_settings: Settings {
                environment_variable: "ABC_OTLP_LOG",
                default_level: LevelFilter::DEBUG
            },
        });
        assert_eq!(
            trace_guard.otlp_trace_settings,
            OtlpTraceSettings::Enabled {
                common_settings: Settings {
                    environment_variable: "ABC_OTLP_TRACE",
                    default_level: LevelFilter::TRACE
                }
            }
        );
    }

    #[test]
    fn builder_with_options() {
        let enable_console_output = true;
        let enable_filelog_output = true;
        let enable_otlp_trace = true;
        let enable_otlp_log = false;

        let tracing_guard = Tracing::builder()
            .service_name("test")
            .with_console_output(enable_console_output.then(|| {
                Settings::builder()
                    .with_environment_variable("ABC_CONSOLE")
                    .build()
            }))
            .with_file_output(enable_filelog_output.then(|| {
                Settings::builder()
                    .with_environment_variable("ABC_FILELOG")
                    .file_log_settings_builder("/dev/null", "tracing-rs.json")
                    .build()
            }))
            .with_otlp_trace_exporter(enable_otlp_trace.then(|| {
                Settings::builder()
                    .with_environment_variable("ABC_OTLP_TRACE")
                    .build()
            }))
            .with_otlp_log_exporter(enable_otlp_log.then(|| {
                Settings::builder()
                    .with_environment_variable("ABC_OTLP_LOG")
                    .build()
            }))
            .build();

        assert!(tracing_guard.console_log_settings.is_enabled());
        assert!(tracing_guard.file_log_settings.is_enabled());
        assert!(tracing_guard.otlp_trace_settings.is_enabled());
        assert!(tracing_guard.otlp_log_settings.is_disabled());
    }

    #[test]
    fn pre_configured() {
        let tracing = Tracing::pre_configured("test", TelemetryOptions {
            console_log_disabled: false,
            console_log_format: Default::default(),
            file_log_directory: None,
            file_log_rotation_period: None,
            file_log_max_files: None,
            otel_trace_exporter_enabled: true,
            otel_log_exporter_enabled: false,
        });

        assert!(tracing.otlp_trace_settings.is_enabled());
    }
}