Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add anon-profile flag #6781

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 39 additions & 4 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{backtrace, backtrace::Backtrace, env, io, mem, process};

use camino::{Utf8Path, Utf8PathBuf};
use clap::{ArgAction, ArgGroup, CommandFactory, Parser, Subcommand, ValueEnum};
use clap::{
builder::NonEmptyStringValueParser, ArgAction, ArgGroup, CommandFactory, Parser, Subcommand,
ValueEnum,
};
use clap_complete::{generate, Shell};
pub use error::Error;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -569,8 +572,13 @@ pub struct RunArgs {
/// File to write turbo's performance profile output into.
/// You can load the file up in chrome://tracing to see
/// which parts of your build were slow.
#[clap(long)]
#[clap(long, value_parser=NonEmptyStringValueParser::new(), conflicts_with = "anon_profile")]
pub profile: Option<String>,
/// File to write turbo's performance profile output into.
/// All identifying data omitted from the profile.
#[serde(skip)]
#[clap(long, value_parser=NonEmptyStringValueParser::new(), conflicts_with = "profile")]
pub anon_profile: Option<String>,
/// Ignore the local filesystem cache for all tasks. Only
/// allow reading and caching artifacts using the remote cache.
#[clap(long, env = "TURBO_REMOTE_ONLY", value_name = "BOOL", action = ArgAction::Set, default_value = "false", default_missing_value = "true", num_args = 0..=1)]
Expand Down Expand Up @@ -626,6 +634,15 @@ impl RunArgs {
(true, true) => unreachable!(), // guaranteed by mutually exclusive `ArgGroup`
}
}

pub fn profile_file_and_include_args(&self) -> Option<(&str, bool)> {
match (self.profile.as_deref(), self.anon_profile.as_deref()) {
(Some(file), None) => Some((file, true)),
(None, Some(file)) => Some((file, false)),
(Some(_), Some(_)) => unreachable!(),
(None, None) => None,
}
}
}

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Serialize)]
Expand Down Expand Up @@ -849,9 +866,10 @@ pub async fn run(
if args.tasks.is_empty() {
return Err(Error::NoTasks(backtrace::Backtrace::capture()));
}
if let Some(file_path) = &args.profile {

if let Some((file_path, include_args)) = args.profile_file_and_include_args() {
// TODO: Do we want to handle the result / error?
let _ = logger.enable_chrome_tracing(file_path);
let _ = logger.enable_chrome_tracing(file_path, include_args);
}
let base = CommandBase::new(cli_args.clone(), repo_root, version, ui);

Expand Down Expand Up @@ -1989,4 +2007,21 @@ mod test {
assert!(Args::try_parse_from(["turbo", "build", "--go-fallback"]).is_ok(),);
assert!(Args::try_parse_from(["turbo", "build", "--remote-cache-read-only",]).is_ok(),);
}

#[test]
fn test_profile_usage() {
assert!(Args::try_parse_from(["turbo", "build", "--profile", ""]).is_err());
assert!(Args::try_parse_from(["turbo", "build", "--anon-profile", ""]).is_err());
assert!(Args::try_parse_from(["turbo", "build", "--profile", "foo.json"]).is_ok());
assert!(Args::try_parse_from(["turbo", "build", "--anon-profile", "foo.json"]).is_ok());
assert!(Args::try_parse_from([
"turbo",
"build",
"--profile",
"foo.json",
"--anon-profile",
"bar.json"
])
.is_err());
}
}
8 changes: 6 additions & 2 deletions crates/turborepo-lib/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,14 @@ impl TurboSubscriber {

/// Enables chrome tracing.
#[tracing::instrument(skip(self, to_file))]
pub fn enable_chrome_tracing<P: AsRef<Path>>(&self, to_file: P) -> Result<(), Error> {
pub fn enable_chrome_tracing<P: AsRef<Path>>(
&self,
to_file: P,
include_args: bool,
) -> Result<(), Error> {
let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
.file(to_file)
.include_args(true)
.include_args(include_args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked at the chrome traces in a while, but will this still include any package or task names?

Copy link
Contributor

@arlyon arlyon Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will only include the function names. We will not be able to get access to any function arguments so it is likely that packages and tasks will not appear. It will however let us see what parts of the run are taking time.

I mentioned in a thread it may be possible to use valuable with some type-aware filter and some newtype so we can filter out sensitive data but I have not done any proper research

tracing::log!("running task {} with absolute path {}", task_name, Sensitive(absolute_path));

.include_locations(true)
.build();

Expand Down
2 changes: 2 additions & 0 deletions turborepo-tests/integration/tests/no-args.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Make sure exit code is 2 when no args are passed
Execute all tasks in parallel
--profile <PROFILE>
File to write turbo's performance profile output into. You can load the file up in chrome://tracing to see which parts of your build were slow
--anon-profile <ANON_PROFILE>
File to write turbo's performance profile output into. All identifying data omitted from the profile
--remote-only [<BOOL>]
Ignore the local filesystem cache for all tasks. Only allow reading and caching artifacts using the remote cache [env: TURBO_REMOTE_ONLY=] [default: false] [possible values: true, false]
--remote-cache-read-only [<BOOL>]
Expand Down
4 changes: 4 additions & 0 deletions turborepo-tests/integration/tests/turbo-help.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Test help flag
Execute all tasks in parallel
--profile <PROFILE>
File to write turbo's performance profile output into. You can load the file up in chrome://tracing to see which parts of your build were slow
--anon-profile <ANON_PROFILE>
File to write turbo's performance profile output into. All identifying data omitted from the profile
--remote-only [<BOOL>]
Ignore the local filesystem cache for all tasks. Only allow reading and caching artifacts using the remote cache [env: TURBO_REMOTE_ONLY=] [default: false] [possible values: true, false]
--remote-cache-read-only [<BOOL>]
Expand Down Expand Up @@ -186,6 +188,8 @@ Test help flag
Execute all tasks in parallel
--profile <PROFILE>
File to write turbo's performance profile output into. You can load the file up in chrome://tracing to see which parts of your build were slow
--anon-profile <ANON_PROFILE>
File to write turbo's performance profile output into. All identifying data omitted from the profile
--remote-only [<BOOL>]
Ignore the local filesystem cache for all tasks. Only allow reading and caching artifacts using the remote cache [env: TURBO_REMOTE_ONLY=] [default: false] [possible values: true, false]
--remote-cache-read-only [<BOOL>]
Expand Down