Skip to content

Commit 9b4ca52

Browse files
committed
test: (wip) adapt unit tests to use tracing output
1 parent c78ea34 commit 9b4ca52

File tree

1 file changed

+83
-41
lines changed

1 file changed

+83
-41
lines changed

src/test.rs

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -224,71 +224,113 @@ where
224224
f(&rustup_home)
225225
}
226226

227-
#[cfg(feature = "otel")]
228227
use once_cell::sync::Lazy;
229228

230229
/// A tokio runtime for the sync tests, permitting the use of tracing. This is
231230
/// never shutdown, instead it is just dropped at end of process.
232-
#[cfg(feature = "otel")]
233231
static TRACE_RUNTIME: Lazy<tokio::runtime::Runtime> =
234232
Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
235-
/// A tracer for the tests.
233+
236234
#[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-
};
235+
type Tracer = opentelemetry_sdk::trace::Tracer;
236+
237+
#[cfg(not(feature = "otel"))]
238+
type Tracer = ();
239+
240+
/// A tracer for the tests.
241+
static TRACER: Lazy<Tracer> = Lazy::new(|| {
242+
use crate::currentprocess::{filesource::StderrSource, varsource::VarSource};
243+
247244
use tokio::runtime::Handle;
248-
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
245+
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry};
249246

250247
// Use the current runtime, or the sync test runtime otherwise.
251248
let handle = match Handle::try_current() {
252249
Ok(handle) => handle,
253250
Err(_) => TRACE_RUNTIME.handle().clone(),
254251
};
252+
255253
let _guard = handle.enter();
254+
// TODO: However this will not be set before running the actual test. What now?
255+
let curr_process = currentprocess::process();
256+
let has_ansi = curr_process.stderr().is_a_tty();
256257

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();
271-
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);
258+
#[cfg(feature = "otel")]
259+
let tracer = {
260+
use std::time::Duration;
261+
262+
use opentelemetry::KeyValue;
263+
use opentelemetry_otlp::WithExportConfig;
264+
use opentelemetry_sdk::{
265+
trace::{self, Sampler},
266+
Resource,
267+
};
268+
opentelemetry_otlp::new_pipeline()
269+
.tracing()
270+
.with_exporter(
271+
opentelemetry_otlp::new_exporter()
272+
.tonic()
273+
.with_timeout(Duration::from_secs(3)),
274+
)
275+
.with_trace_config(
276+
trace::config()
277+
.with_sampler(Sampler::AlwaysOn)
278+
.with_resource(Resource::new(vec![KeyValue::new("service.name", "rustup")])),
279+
)
280+
.install_batch(opentelemetry_sdk::runtime::Tokio)
281+
.unwrap()
282+
};
283+
#[cfg(feature = "otel")]
284+
let telemetry = {
285+
use opentelemetry::global;
286+
use opentelemetry_sdk::propagation::TraceContextPropagator;
287+
288+
global::set_text_map_propagator(TraceContextPropagator::new());
289+
290+
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("INFO"));
291+
tracing_opentelemetry::layer()
292+
.with_tracer(tracer.clone())
293+
.with_filter(env_filter)
294+
};
295+
let console_logger = {
296+
let is_verbose = curr_process.var_os("RUST_LOG").is_some();
297+
let logger = fmt::layer()
298+
.with_writer(move || curr_process.stderr())
299+
.with_ansi(has_ansi);
300+
if is_verbose {
301+
let env_filter =
302+
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO"));
303+
logger.compact().with_filter(env_filter).boxed()
304+
} else {
305+
// Receive log lines from Rustup only.
306+
let env_filter = EnvFilter::new("rustup=DEBUG");
307+
logger
308+
.event_format(crate::cli::log::EventFormatter)
309+
.with_filter(env_filter)
310+
.boxed()
311+
}
312+
};
313+
let subscriber = {
314+
#[cfg(feature = "otel")]
315+
{
316+
Registry::default().with(console_logger).with(telemetry)
317+
}
318+
#[cfg(not(feature = "otel"))]
319+
{
320+
Registry::default().with(console_logger)
321+
}
322+
};
276323
tracing::subscriber::set_global_default(subscriber).unwrap();
324+
#[cfg(feature = "otel")]
277325
tracer
278326
});
279327

280328
pub fn before_test() {
281-
#[cfg(feature = "otel")]
282-
{
283-
Lazy::force(&TRACER);
284-
}
329+
Lazy::force(&TRACER);
285330
}
286331

287332
pub async fn before_test_async() {
288-
#[cfg(feature = "otel")]
289-
{
290-
Lazy::force(&TRACER);
291-
}
333+
Lazy::force(&TRACER);
292334
}
293335

294336
pub fn after_test() {

0 commit comments

Comments
 (0)