@@ -32,46 +32,76 @@ use tracing_subscriber::prelude::*;
32
32
use tracing_subscriber:: registry:: LookupSpan ;
33
33
use tracing_subscriber:: { EnvFilter , Layer } ;
34
34
35
- use crate :: environment:: { LOG_SPAN_BOUNDARIES , OPENTELEMETRY_AUTHORIZATION , OPENTELEMETRY_URL } ;
35
+ use crate :: environment:: {
36
+ ENABLE_VERBOSE_JSON_LOGS , OPENTELEMETRY_AUTHORIZATION , OPENTELEMETRY_URL ,
37
+ } ;
36
38
37
39
static TRACER_PROVIDER : OnceCell < Option < TracerProvider > > = OnceCell :: new ( ) ;
38
40
pub ( crate ) const RUNTIME_CONTEXT_SPAN : & str = "runtime_context" ;
39
41
40
- fn fmt_layer < S > ( level : Level ) -> impl Layer < S >
41
- where
42
- S : for < ' a > LookupSpan < ' a > ,
43
- S : tracing:: Subscriber ,
44
- {
42
+ fn fmt_env_filter ( level : Level ) -> EnvFilter {
45
43
let default_filter = format ! ( "quickwit={level}" )
46
44
. parse ( )
47
45
. expect ( "Invalid default filter" ) ;
48
- let env_filter = EnvFilter :: builder ( )
46
+ EnvFilter :: builder ( )
49
47
. with_default_directive ( default_filter)
50
- . from_env_lossy ( ) ;
48
+ . from_env_lossy ( )
49
+ }
50
+
51
+ fn fmt_time_format ( ) -> UtcTime < std:: vec:: Vec < time:: format_description:: FormatItem < ' static > > > {
52
+ // We do not rely on the Rfc3339 implementation, because it has a nanosecond precision.
53
+ // See discussion here: https://github.com/time-rs/time/discussions/418
54
+ UtcTime :: new (
55
+ time:: format_description:: parse (
56
+ "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z" ,
57
+ )
58
+ . expect ( "Time format invalid." ) ,
59
+ )
60
+ }
61
+
62
+ fn compact_fmt_layer < S > ( level : Level ) -> impl Layer < S >
63
+ where
64
+ S : for < ' a > LookupSpan < ' a > ,
65
+ S : tracing:: Subscriber ,
66
+ {
51
67
let event_format = tracing_subscriber:: fmt:: format ( )
52
68
. with_target ( true )
53
- . with_timer (
54
- // We do not rely on the Rfc3339 implementation, because it has a nanosecond precision.
55
- // See discussion here: https://github.com/time-rs/time/discussions/418
56
- UtcTime :: new (
57
- time:: format_description:: parse (
58
- "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z" ,
59
- )
60
- . expect ( "Time format invalid." ) ,
61
- ) ,
62
- )
69
+ . with_timer ( fmt_time_format ( ) )
70
+ . compact ( ) ;
71
+
72
+ tracing_subscriber:: fmt:: layer :: < S > ( )
73
+ . event_format ( event_format)
74
+ . with_ansi ( false )
75
+ . with_filter ( fmt_env_filter ( level) )
76
+ }
77
+
78
+ fn json_fmt_layer < S > ( level : Level ) -> impl Layer < S >
79
+ where
80
+ S : for < ' a > LookupSpan < ' a > ,
81
+ S : tracing:: Subscriber ,
82
+ {
83
+ let event_format = tracing_subscriber:: fmt:: format ( )
84
+ . with_target ( true )
85
+ . with_timer ( fmt_time_format ( ) )
63
86
. json ( ) ;
64
- let fmt_span = if * LOG_SPAN_BOUNDARIES {
65
- FmtSpan :: NEW | FmtSpan :: CLOSE
66
- } else {
67
- FmtSpan :: NONE
68
- } ;
69
87
tracing_subscriber:: fmt:: layer :: < S > ( )
70
- . with_span_events ( fmt_span )
88
+ . with_span_events ( FmtSpan :: NEW | FmtSpan :: CLOSE )
71
89
. event_format ( event_format)
72
90
. fmt_fields ( JsonFields :: default ( ) )
73
91
. with_ansi ( false )
74
- . with_filter ( env_filter)
92
+ . with_filter ( fmt_env_filter ( level) )
93
+ }
94
+
95
+ fn fmt_layer < S > ( level : Level ) -> Box < dyn Layer < S > + Send + Sync + ' static >
96
+ where
97
+ S : for < ' a > LookupSpan < ' a > ,
98
+ S : tracing:: Subscriber ,
99
+ {
100
+ if * ENABLE_VERBOSE_JSON_LOGS {
101
+ json_fmt_layer ( level) . boxed ( )
102
+ } else {
103
+ compact_fmt_layer ( level) . boxed ( )
104
+ }
75
105
}
76
106
77
107
fn otlp_layer < S > (
0 commit comments