Skip to content

Commit

Permalink
feat(turbo): telemetry client
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Dec 7, 2023
1 parent 65b5f3a commit 67bf218
Show file tree
Hide file tree
Showing 18 changed files with 1,117 additions and 11 deletions.
105 changes: 103 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/turborepo-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use crate::error::{Error, Result};

pub mod analytics;
mod error;
mod retry;
pub mod retry;
pub mod spaces;

lazy_static! {
Expand Down
4 changes: 1 addition & 3 deletions crates/turborepo-api-client/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const RETRY_MAX: u32 = 2;
/// body already set. NOTE: This must be cloneable, so no streams are allowed.
///
/// returns: Result<Response, Error>
pub(crate) async fn make_retryable_request(
request_builder: RequestBuilder,
) -> Result<Response, Error> {
pub async fn make_retryable_request(request_builder: RequestBuilder) -> Result<Response, Error> {
let mut last_error = None;
for retry_count in 0..RETRY_MAX {
let builder = request_builder.try_clone().expect("cannot clone request");
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ turborepo-env = { workspace = true }
turborepo-filewatch = { path = "../turborepo-filewatch" }
turborepo-lockfiles = { workspace = true }
turborepo-scm = { workspace = true }
turborepo-telemetry = { path = "../turborepo-telemetry" }
turborepo-ui = { workspace = true }
twox-hash = "1.6.3"
wax = { workspace = true }
Expand Down
63 changes: 58 additions & 5 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ use serde::{Deserialize, Serialize};
use tracing::{debug, error};
use turbopath::AbsoluteSystemPathBuf;
use turborepo_repository::inference::{RepoMode, RepoState};
use turborepo_telemetry::{
client::AnonAPIClient,
events::{Fallback, TelemetryEvent},
init_telemetry, telem, TelemetryHandle,
};
use turborepo_ui::UI;

use crate::{
commands::{bin, daemon, generate, info, link, login, logout, prune, unlink, CommandBase},
commands::{
bin, daemon, generate, info, link, login, logout, prune, telemetry, unlink, CommandBase,
},
get_version,
tracing::TurboSubscriber,
Payload,
Expand Down Expand Up @@ -197,6 +204,17 @@ pub enum DaemonCommand {
Clean,
}

#[derive(Subcommand, Copy, Clone, Debug, Serialize, PartialEq)]
#[serde(tag = "command")]
pub enum TelemetryCommand {
/// Enables anonymous telemetry
Enable,
/// Disables anonymous telemetry
Disable,
/// Reports the status of telemetry
Status,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum LinkTarget {
RemoteCache,
Expand Down Expand Up @@ -343,6 +361,11 @@ pub enum Command {
#[serde(skip)]
command: Option<Box<GenerateCommand>>,
},
Telemetry {
#[clap(subcommand)]
#[serde(flatten)]
command: Option<Box<TelemetryCommand>>,
},
#[clap(hide = true)]
Info {
workspace: Option<String>,
Expand Down Expand Up @@ -643,6 +666,21 @@ pub async fn run(
ui: UI,
) -> Result<Payload, Error> {
let mut cli_args = Args::new();
let version = get_version();

// initialize telemetry and track handle to close at the end of the run
let mut telemetry_handle: Option<TelemetryHandle> = None;
match AnonAPIClient::new("https://telemetry.vercel.com", 250, version) {
Ok(anonymous_api_client) => {
let handle = init_telemetry(anonymous_api_client, ui);
match handle {
Ok(h) => telemetry_handle = Some(h),
Err(error) => debug!("failed to start telemetry: {:?}", error),
}
}
Err(error) => debug!("Failed to create AnonAPIClient: {:?}", error),
}

// If there is no command, we set the command to `Command::Run` with
// `self.parsed_args.run_args` as arguments.
let mut command = if let Some(command) = mem::take(&mut cli_args.command) {
Expand Down Expand Up @@ -707,12 +745,10 @@ pub async fn run(
AbsoluteSystemPathBuf::cwd()?
};

let version = get_version();

cli_args.command = Some(command);
cli_args.cwd = Some(repo_root.as_path().to_owned());

match cli_args.command.as_ref().unwrap() {
let cli_result = match cli_args.command.as_ref().unwrap() {
Command::Bin { .. } => {
bin::run()?;

Expand Down Expand Up @@ -753,6 +789,11 @@ pub async fn run(
generate::run(tag, command, &args)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Telemetry { command } => {
let mut base = CommandBase::new(cli_args.clone(), repo_root, version, ui);
telemetry::configure(command, &mut base);
Ok(Payload::Rust(Ok(0)))
}
Command::Info { workspace, json } => {
let json = *json;
let workspace = workspace.clone();
Expand Down Expand Up @@ -831,6 +872,12 @@ pub async fn run(

let should_use_go = args.go_fallback
|| env::var("EXPERIMENTAL_RUST_CODEPATH").as_deref() == Ok("false");

telem(TelemetryEvent::Fallback(Fallback {
go_arg: args.go_fallback,
rust_env_var: env::var("EXPERIMENTAL_RUST_CODEPATH").as_deref() == Ok("false"),
}));

if should_use_go {
Ok(Payload::Go(Box::new(base)))
} else {
Expand Down Expand Up @@ -858,10 +905,16 @@ pub async fn run(
}
Command::Completion { shell } => {
generate(*shell, &mut Args::command(), "turbo", &mut io::stdout());

Ok(Payload::Rust(Ok(0)))
}
};

match telemetry_handle {
Some(handle) => handle.close_with_timeout().await,
None => debug!("Skipping telemetry close - not initialized"),
}

cli_result
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) mod login;
pub(crate) mod logout;
pub(crate) mod prune;
pub(crate) mod run;
pub(crate) mod telemetry;
pub(crate) mod unlink;

#[derive(Debug)]
Expand Down
Loading

0 comments on commit 67bf218

Please sign in to comment.