Skip to content

Commit

Permalink
feat(turbo): telemetry client (#6743)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Dec 19, 2023
1 parent 6345e0e commit fff93ca
Show file tree
Hide file tree
Showing 21 changed files with 1,581 additions and 20 deletions.
114 changes: 110 additions & 4 deletions Cargo.lock

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

52 changes: 45 additions & 7 deletions crates/turborepo-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod analytics;
mod error;
mod retry;
pub mod spaces;
pub mod telemetry;

lazy_static! {
static ref AUTHORIZATION_REGEX: Regex =
Expand Down Expand Up @@ -452,13 +453,7 @@ impl APIClient {

let client = client_build.map_err(Error::TlsError)?;

let user_agent = format!(
"turbo {} {} {} {}",
version,
rustc_version_runtime::version(),
env::consts::OS,
env::consts::ARCH
);
let user_agent = build_user_agent(version);
Ok(APIClient {
client,
base_url: base_url.as_ref().to_string(),
Expand Down Expand Up @@ -536,6 +531,49 @@ impl APIAuth {
}
}

// Anon Client
#[derive(Clone)]
pub struct AnonAPIClient {
client: reqwest::Client,
base_url: String,
user_agent: String,
}

impl AnonAPIClient {
fn make_url(&self, endpoint: &str) -> String {
format!("{}{}", self.base_url, endpoint)
}

pub fn new(base_url: impl AsRef<str>, timeout: u64, version: &str) -> Result<Self> {
let client_build = if timeout != 0 {
reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(timeout))
.build()
} else {
reqwest::Client::builder().build()
};

let client = client_build.map_err(Error::TlsError)?;

let user_agent = build_user_agent(version);
Ok(AnonAPIClient {
client,
base_url: base_url.as_ref().to_string(),
user_agent,
})
}
}

fn build_user_agent(version: &str) -> String {
format!(
"turbo {} {} {} {}",
version,
rustc_version_runtime::version(),
env::consts::OS,
env::consts::ARCH
)
}

#[cfg(test)]
mod test {
use anyhow::Result;
Expand Down
43 changes: 43 additions & 0 deletions crates/turborepo-api-client/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use async_trait::async_trait;
use reqwest::Method;
use turborepo_vercel_api::TelemetryEvent;

use crate::{retry, AnonAPIClient, Error};

const TELEMETRY_ENDPOINT: &str = "/api/turborepo/v1/events";

#[async_trait]
pub trait TelemetryClient {
async fn record_telemetry(
&self,
events: Vec<TelemetryEvent>,
telemetry_id: &str,
session_id: &str,
) -> Result<(), Error>;
}

#[async_trait]
impl TelemetryClient for AnonAPIClient {
async fn record_telemetry(
&self,
events: Vec<TelemetryEvent>,
telemetry_id: &str,
session_id: &str,
) -> Result<(), Error> {
let url = self.make_url(TELEMETRY_ENDPOINT);
let telemetry_request = self
.client
.request(Method::POST, url)
.header("User-Agent", self.user_agent.clone())
.header("Content-Type", "application/json")
.header("x-turbo-telemetry-id", telemetry_id)
.header("x-turbo-session-id", session_id)
.json(&events);

retry::make_retryable_request(telemetry_request)
.await?
.error_for_status()?;

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,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

0 comments on commit fff93ca

Please sign in to comment.