Skip to content

Commit 6d5f0f6

Browse files
committed
refactor(test): setup tracing subscriber in before_test_async()
1 parent 5f34b12 commit 6d5f0f6

File tree

2 files changed

+18
-55
lines changed

2 files changed

+18
-55
lines changed

rustup-macros/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn test_inner(mod_path: String, mut input: ItemFn) -> syn::Result<TokenStream> {
9595
let name = input.sig.ident.clone();
9696
let new_block: Block = parse_quote! {
9797
{
98-
#before_ident().await;
98+
let _guard = #before_ident().await;
9999
// Define a function with same name we can instrument inside the
100100
// tracing enablement logic.
101101
#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]

src/test.rs

+17-54
Original file line numberDiff line numberDiff line change
@@ -224,69 +224,32 @@ where
224224
f(&rustup_home)
225225
}
226226

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};
256231

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;
271235

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+
};
279239

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"))]
282244
{
283-
Lazy::force(&TRACER);
245+
None
284246
}
285247
}
286248

287249
pub async fn after_test_async() {
288250
#[cfg(feature = "otel")]
289251
{
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();
291254
}
292255
}

0 commit comments

Comments
 (0)