Skip to content

Commit c98f64e

Browse files
committed
feat(rustup-init): (wip) use process().stderr() as a tracing output, pt. 2
1 parent 078795a commit c98f64e

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

src/bin/rustup-init.rs

+62-42
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,30 @@ fn main() {
4646
}
4747

4848
fn maybe_trace_rustup() -> Result<utils::ExitCode> {
49-
#[cfg(not(feature = "otel"))]
50-
{
51-
run_rustup()
52-
}
53-
#[cfg(feature = "otel")]
54-
{
55-
use std::time::Duration;
56-
57-
use opentelemetry::{global, KeyValue};
58-
use opentelemetry_otlp::WithExportConfig;
59-
use opentelemetry_sdk::{
60-
propagation::TraceContextPropagator,
61-
trace::{self, Sampler},
62-
Resource,
63-
};
64-
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Registry};
49+
use std::time::Duration;
50+
51+
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry};
52+
53+
let curr_process = process();
54+
let has_ansi = curr_process.stderr().is_a_tty();
6555

66-
let curr_process = process();
56+
// Background submission requires a runtime, and since we're probably
57+
// going to want async eventually, we just use tokio.
58+
let threaded_rt = tokio::runtime::Runtime::new()?;
6759

68-
// Background submission requires a runtime, and since we're probably
69-
// going to want async eventually, we just use tokio.
70-
let threaded_rt = tokio::runtime::Runtime::new()?;
60+
let result = threaded_rt.block_on(async move {
61+
#[cfg(feature = "otel")]
62+
let telemetry = {
63+
use opentelemetry::{global, KeyValue};
64+
use opentelemetry_otlp::WithExportConfig;
65+
use opentelemetry_sdk::{
66+
propagation::TraceContextPropagator,
67+
trace::{self, Sampler},
68+
Resource,
69+
};
7170

72-
let result = threaded_rt.block_on(async move {
7371
global::set_text_map_propagator(TraceContextPropagator::new());
72+
7473
let tracer = opentelemetry_otlp::new_pipeline()
7574
.tracing()
7675
.with_exporter(
@@ -88,30 +87,51 @@ fn maybe_trace_rustup() -> Result<utils::ExitCode> {
8887
)
8988
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
9089
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("INFO"));
91-
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
92-
let subscriber = Registry::default()
93-
.with(
94-
fmt::layer()
95-
.with_ansi(curr_process.stderr().is_a_tty())
96-
.with_writer(move || curr_process.stderr()),
97-
)
98-
.with(env_filter)
99-
.with(telemetry);
100-
tracing::subscriber::set_global_default(subscriber)?;
101-
let result = run_rustup();
102-
// We're tracing, so block until all spans are exported.
103-
opentelemetry::global::shutdown_tracer_provider();
104-
result
105-
});
106-
// default runtime behaviour is to block until nothing is running;
107-
// instead we supply a timeout, as we're either already errored and are
108-
// reporting back without care for lost threads etc... or everything
109-
// completed.
110-
threaded_rt.shutdown_timeout(Duration::from_millis(5));
90+
tracing_opentelemetry::layer()
91+
.with_tracer(tracer)
92+
.with_filter(env_filter)
93+
};
94+
let console_logger = {
95+
let is_verbose = curr_process.var_os("RUST_LOG").is_some();
96+
let logger = fmt::layer()
97+
.with_writer(move || curr_process.stderr())
98+
.with_ansi(has_ansi);
99+
if is_verbose {
100+
let env_filter =
101+
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO"));
102+
logger.compact().with_filter(env_filter).boxed()
103+
} else {
104+
let env_filter = EnvFilter::new("DEBUG");
105+
// FIXME: Add "classical" formatting
106+
logger.with_filter(env_filter).boxed()
107+
}
108+
};
109+
let subscriber = {
110+
#[cfg(feature = "otel")]
111+
{
112+
Registry::default().with(console_logger).with(telemetry)
113+
}
114+
#[cfg(not(feature = "otel"))]
115+
{
116+
Registry::default().with(console_logger)
117+
}
118+
};
119+
tracing::subscriber::set_global_default(subscriber)?;
120+
let result = run_rustup();
121+
// We're tracing, so block until all spans are exported.
122+
#[cfg(feature = "otel")]
123+
opentelemetry::global::shutdown_tracer_provider();
111124
result
112-
}
125+
});
126+
// default runtime behaviour is to block until nothing is running;
127+
// instead we supply a timeout, as we're either already errored and are
128+
// reporting back without care for lost threads etc... or everything
129+
// completed.
130+
threaded_rt.shutdown_timeout(Duration::from_millis(5));
131+
result
113132
}
114133

134+
// FIXME: Make `tracing::instrument` always run
115135
#[cfg_attr(feature = "otel", tracing::instrument)]
116136
fn run_rustup() -> Result<utils::ExitCode> {
117137
if let Ok(dir) = process().var("RUSTUP_TRACE_DIR") {

0 commit comments

Comments
 (0)