Skip to content

Commit 34ec305

Browse files
authored
Move TRACING_DURATIONS_FILE to EnvironmentOptions (#16109)
<!-- Thank you for contributing to uv! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Fixes a part of #14720 Add `TRACING_DURATIONS_FILE` to EnvironmentOptions. ## Test Plan <!-- How was it tested? -->
1 parent 241ad88 commit 34ec305

5 files changed

Lines changed: 29 additions & 8 deletions

File tree

crates/uv-settings/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ url = { workspace = true }
5050
ignored = ["uv-options-metadata", "clap"]
5151

5252
[dev-dependencies]
53+
54+
[features]
55+
tracing-durations-export = []

crates/uv-settings/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ pub struct EnvironmentOptions {
574574
pub python_install_registry: Option<bool>,
575575
pub install_mirrors: PythonInstallMirrors,
576576
pub log_context: Option<bool>,
577+
#[cfg(feature = "tracing-durations-export")]
578+
pub tracing_durations_file: Option<PathBuf>,
577579
}
578580

579581
impl EnvironmentOptions {
@@ -599,6 +601,10 @@ impl EnvironmentOptions {
599601
)?,
600602
},
601603
log_context: parse_boolish_environment_variable(EnvVars::UV_LOG_CONTEXT)?,
604+
#[cfg(feature = "tracing-durations-export")]
605+
tracing_durations_file: parse_path_environment_variable(
606+
EnvVars::TRACING_DURATIONS_FILE,
607+
),
602608
})
603609
}
604610
}
@@ -676,6 +682,18 @@ fn parse_string_environment_variable(name: &'static str) -> Result<Option<String
676682
}
677683
}
678684

685+
#[cfg(feature = "tracing-durations-export")]
686+
/// Parse a path environment variable.
687+
fn parse_path_environment_variable(name: &'static str) -> Option<PathBuf> {
688+
let value = std::env::var_os(name)?;
689+
690+
if value.is_empty() {
691+
return None;
692+
}
693+
694+
Some(PathBuf::from(value))
695+
}
696+
679697
/// Populate the [`EnvironmentFlags`] from the given [`EnvironmentOptions`].
680698
impl From<&EnvironmentOptions> for EnvironmentFlags {
681699
fn from(options: &EnvironmentOptions) -> Self {

crates/uv/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ performance-memory-allocator = ["dep:uv-performance-memory-allocator"]
167167
self-update = ["axoupdater", "uv-cli/self-update"]
168168

169169
# Features for development only.
170-
tracing-durations-export = ["dep:tracing-durations-export", "uv-resolver/tracing-durations-export"]
170+
tracing-durations-export = ["dep:tracing-durations-export", "uv-resolver/tracing-durations-export", "uv-settings/tracing-durations-export"]
171171

172172
# Features that only apply when running tests, no-ops otherwise.
173173
default-tests = [

crates/uv/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
360360

361361
// Configure the `tracing` crate, which controls internal logging.
362362
#[cfg(feature = "tracing-durations-export")]
363-
let (durations_layer, _duration_guard) = logging::setup_durations()?;
363+
let (durations_layer, _duration_guard) =
364+
logging::setup_durations(environment.tracing_durations_file.as_ref())?;
364365
#[cfg(not(feature = "tracing-durations-export"))]
365366
let durations_layer = None::<tracing_subscriber::layer::Identity>;
366367
logging::setup_logging(

crates/uv/src/logging.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use tracing_tree::time::Uptime;
1414

1515
use uv_cli::ColorChoice;
1616
use uv_logging::UvFormat;
17-
#[cfg(feature = "tracing-durations-export")]
18-
use uv_static::EnvVars;
1917

2018
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
2119
pub(crate) enum Level {
@@ -120,12 +118,13 @@ pub(crate) fn setup_logging(
120118

121119
/// Setup the `TRACING_DURATIONS_FILE` environment variable to enable tracing durations.
122120
#[cfg(feature = "tracing-durations-export")]
123-
pub(crate) fn setup_durations() -> anyhow::Result<(
121+
pub(crate) fn setup_durations(
122+
tracing_durations_file: Option<&std::path::PathBuf>,
123+
) -> anyhow::Result<(
124124
Option<DurationsLayer<Registry>>,
125125
Option<DurationsLayerDropGuard>,
126126
)> {
127-
if let Ok(location) = std::env::var(EnvVars::TRACING_DURATIONS_FILE) {
128-
let location = std::path::PathBuf::from(location);
127+
if let Some(location) = tracing_durations_file {
129128
if let Some(parent) = location.parent() {
130129
fs_err::create_dir_all(parent)
131130
.context("Failed to create parent of TRACING_DURATIONS_FILE")?;
@@ -141,7 +140,7 @@ pub(crate) fn setup_durations() -> anyhow::Result<(
141140
..PlotConfig::default()
142141
};
143142
let (layer, guard) = DurationsLayerBuilder::default()
144-
.durations_file(&location)
143+
.durations_file(location)
145144
.plot_file(location.with_extension("svg"))
146145
.plot_config(plot_config)
147146
.build()

0 commit comments

Comments
 (0)