@@ -224,69 +224,32 @@ where
224
224
f ( & rustup_home)
225
225
}
226
226
227
- #[ cfg( feature = "otel" ) ]
228
- use once_cell:: sync:: Lazy ;
229
-
230
- /// A tokio runtime for the sync tests, permitting the use of tracing. This is
231
- /// never shutdown, instead it is just dropped at end of process.
232
- #[ cfg( feature = "otel" ) ]
233
- static TRACE_RUNTIME : Lazy < tokio:: runtime:: Runtime > =
234
- Lazy :: new ( || tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ) ;
235
- /// A tracer for the tests.
236
- #[ cfg( feature = "otel" ) ]
237
- static TRACER : Lazy < opentelemetry_sdk:: trace:: Tracer > = Lazy :: new ( || {
238
- use std:: time:: Duration ;
239
-
240
- use opentelemetry:: { global, KeyValue } ;
241
- use opentelemetry_otlp:: WithExportConfig ;
242
- use opentelemetry_sdk:: {
243
- propagation:: TraceContextPropagator ,
244
- trace:: { self , Sampler } ,
245
- Resource ,
246
- } ;
247
- use tokio:: runtime:: Handle ;
248
- use tracing_subscriber:: { layer:: SubscriberExt , EnvFilter , Registry } ;
249
-
250
- // Use the current runtime, or the sync test runtime otherwise.
251
- let handle = match Handle :: try_current ( ) {
252
- Ok ( handle) => handle,
253
- Err ( _) => TRACE_RUNTIME . handle ( ) . clone ( ) ,
254
- } ;
255
- let _guard = handle. enter ( ) ;
227
+ pub async fn before_test_async ( ) -> Option < tracing:: dispatcher:: DefaultGuard > {
228
+ #[ cfg( feature = "otel" ) ]
229
+ {
230
+ use tracing_subscriber:: { layer:: SubscriberExt , Registry } ;
256
231
257
- let tracer = opentelemetry_otlp:: new_pipeline ( )
258
- . tracing ( )
259
- . with_exporter (
260
- opentelemetry_otlp:: new_exporter ( )
261
- . tonic ( )
262
- . with_timeout ( Duration :: from_secs ( 3 ) ) ,
263
- )
264
- . with_trace_config (
265
- trace:: config ( )
266
- . with_sampler ( Sampler :: AlwaysOn )
267
- . with_resource ( Resource :: new ( vec ! [ KeyValue :: new( "service.name" , "rustup" ) ] ) ) ,
268
- )
269
- . install_batch ( opentelemetry_sdk:: runtime:: Tokio )
270
- . unwrap ( ) ;
232
+ let telemetry = {
233
+ use opentelemetry:: global;
234
+ use opentelemetry_sdk:: propagation:: TraceContextPropagator ;
271
235
272
- global:: set_text_map_propagator ( TraceContextPropagator :: new ( ) ) ;
273
- let env_filter = EnvFilter :: try_from_default_env ( ) . unwrap_or ( EnvFilter :: new ( "INFO" ) ) ;
274
- let telemetry = tracing_opentelemetry:: layer ( ) . with_tracer ( tracer. clone ( ) ) ;
275
- let subscriber = Registry :: default ( ) . with ( env_filter) . with ( telemetry) ;
276
- tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
277
- tracer
278
- } ) ;
236
+ global:: set_text_map_propagator ( TraceContextPropagator :: new ( ) ) ;
237
+ crate :: cli:: log:: telemetry ( )
238
+ } ;
279
239
280
- pub async fn before_test_async ( ) {
281
- #[ cfg( feature = "otel" ) ]
240
+ let subscriber = Registry :: default ( ) . with ( telemetry) ;
241
+ Some ( tracing:: subscriber:: set_default ( subscriber) )
242
+ }
243
+ #[ cfg( not( feature = "otel" ) ) ]
282
244
{
283
- Lazy :: force ( & TRACER ) ;
245
+ None
284
246
}
285
247
}
286
248
287
249
pub async fn after_test_async ( ) {
288
250
#[ cfg( feature = "otel" ) ]
289
251
{
290
- TRACER . provider ( ) . map ( |p| p. force_flush ( ) ) ;
252
+ // We're tracing, so block until all spans are exported.
253
+ opentelemetry:: global:: shutdown_tracer_provider ( ) ;
291
254
}
292
255
}
0 commit comments