1
- use std:: cell:: Cell ;
2
1
use std:: io:: IsTerminal ;
3
- use std:: time:: Duration ;
4
- use std:: time:: Instant ;
5
2
6
3
use env:: otel_logs_enabled;
7
4
use env:: otel_metrics_enabled;
8
5
use env:: otel_tracing_enabled;
9
6
use opentelemetry_sdk:: propagation:: TraceContextPropagator ;
7
+ use tracing_subscriber:: filter:: filter_fn;
8
+ use tracing_subscriber:: fmt:: Layer as OtelFmtLayer ;
10
9
use tracing_subscriber:: { fmt, prelude:: * , registry, EnvFilter , Layer } ;
11
10
12
11
pub mod detector;
@@ -67,8 +66,15 @@ pub fn init(spin_version: String) -> anyhow::Result<ShutdownGuard> {
67
66
. add_directive ( "[{app_log_non_utf8}]=off" . parse ( ) ?) ,
68
67
) ;
69
68
70
- // Even if metrics or tracing aren't enabled we're okay to turn on the global error handler
71
- opentelemetry:: global:: set_error_handler ( otel_error_handler) ?;
69
+ let opentelemetry_layer = OtelFmtLayer :: new ( )
70
+ . with_writer ( std:: io:: stderr)
71
+ . with_filter ( filter_fn ( |metadata| {
72
+ metadata. target ( ) . starts_with ( "opentelemetry" )
73
+ } ) ) ;
74
+
75
+ // let non_opentelemetry_filter = filter_fn(|metadata| !metadata.target().starts_with("opentelemetry"));
76
+ // let otel_bridge_layer = layer::OpenTelemetryTracingBridge::new(&cloned_provider)
77
+ // .with_filter(non_opentelemetry_filter);
72
78
73
79
let otel_tracing_layer = if otel_tracing_enabled ( ) {
74
80
Some ( traces:: otel_tracing_layer ( spin_version. clone ( ) ) ?)
@@ -87,6 +93,8 @@ pub fn init(spin_version: String) -> anyhow::Result<ShutdownGuard> {
87
93
. with ( otel_tracing_layer)
88
94
. with ( otel_metrics_layer)
89
95
. with ( fmt_layer)
96
+ . with ( opentelemetry_layer)
97
+ // .with(otel_bridge_layer)
90
98
. init ( ) ;
91
99
92
100
// Used to propagate trace information in the standard W3C TraceContext format. Even if the otel
@@ -100,36 +108,36 @@ pub fn init(spin_version: String) -> anyhow::Result<ShutdownGuard> {
100
108
Ok ( ShutdownGuard )
101
109
}
102
110
103
- fn otel_error_handler ( err : opentelemetry:: global:: Error ) {
104
- // Track the error count
105
- let signal = match err {
106
- opentelemetry:: global:: Error :: Metric ( _) => "metrics" ,
107
- opentelemetry:: global:: Error :: Trace ( _) => "traces" ,
108
- opentelemetry:: global:: Error :: Log ( _) => "logs" ,
109
- _ => "unknown" ,
110
- } ;
111
- metrics:: monotonic_counter!( spin. otel_error_count = 1 , signal = signal) ;
112
-
113
- // Only log the first error at ERROR level, subsequent errors will be logged at higher levels and rate limited
114
- static FIRST_OTEL_ERROR : std:: sync:: Once = std:: sync:: Once :: new ( ) ;
115
- FIRST_OTEL_ERROR . call_once ( || {
116
- tracing:: error!( ?err, "OpenTelemetry error" ) ;
117
- tracing:: error!( "There has been an error with the OpenTelemetry system. Traces, logs or metrics are likely failing to export." ) ;
118
- tracing:: error!( "Further OpenTelemetry errors will be available at WARN level (rate limited) or at TRACE level." ) ;
119
- } ) ;
120
-
121
- // Rate limit the logging of the OTel errors to not occur more frequently on each thread than OTEL_ERROR_INTERVAL
122
- const OTEL_ERROR_INTERVAL : Duration = Duration :: from_millis ( 5000 ) ;
123
- thread_local ! {
124
- static LAST_OTEL_ERROR : Cell <Instant > = Cell :: new( Instant :: now( ) - OTEL_ERROR_INTERVAL ) ;
125
- }
126
- if LAST_OTEL_ERROR . get ( ) . elapsed ( ) > OTEL_ERROR_INTERVAL {
127
- LAST_OTEL_ERROR . set ( Instant :: now ( ) ) ;
128
- tracing:: warn!( ?err, "OpenTelemetry error" ) ;
129
- } else {
130
- tracing:: trace!( ?err, "OpenTelemetry error" ) ;
131
- }
132
- }
111
+ // fn otel_error_handler(err: opentelemetry::global::Error) {
112
+ // // Track the error count
113
+ // let signal = match err {
114
+ // opentelemetry::global::Error::Metric(_) => "metrics",
115
+ // opentelemetry::global::Error::Trace(_) => "traces",
116
+ // opentelemetry::global::Error::Log(_) => "logs",
117
+ // _ => "unknown",
118
+ // };
119
+ // metrics::monotonic_counter!(spin.otel_error_count = 1, signal = signal);
120
+
121
+ // // Only log the first error at ERROR level, subsequent errors will be logged at higher levels and rate limited
122
+ // static FIRST_OTEL_ERROR: std::sync::Once = std::sync::Once::new();
123
+ // FIRST_OTEL_ERROR.call_once(|| {
124
+ // tracing::error!(?err, "OpenTelemetry error");
125
+ // tracing::error!("There has been an error with the OpenTelemetry system. Traces, logs or metrics are likely failing to export.");
126
+ // tracing::error!("Further OpenTelemetry errors will be available at WARN level (rate limited) or at TRACE level.");
127
+ // });
128
+
129
+ // // Rate limit the logging of the OTel errors to not occur more frequently on each thread than OTEL_ERROR_INTERVAL
130
+ // const OTEL_ERROR_INTERVAL: Duration = Duration::from_millis(5000);
131
+ // thread_local! {
132
+ // static LAST_OTEL_ERROR: Cell<Instant> = Cell::new(Instant::now() - OTEL_ERROR_INTERVAL);
133
+ // }
134
+ // if LAST_OTEL_ERROR.get().elapsed() > OTEL_ERROR_INTERVAL {
135
+ // LAST_OTEL_ERROR.set(Instant::now());
136
+ // tracing::warn!(?err, "OpenTelemetry error");
137
+ // } else {
138
+ // tracing::trace!(?err, "OpenTelemetry error");
139
+ // }
140
+ // }
133
141
134
142
/// An RAII implementation for connection to open telemetry services.
135
143
///
0 commit comments