Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ The `tracing` library is used for:
`--org-url-slug` (env `TRUNK_ORG_URL_SLUG`) is declared as a non-optional `String` on `UploadArgs`, which makes it a hard requirement for both `upload` and `test`. Keep it that way: the slug is what attributes a run to an organization across every kind of telemetry we collect.

- **Sentry**: `setup_logger` in `cli/src/main.rs` attaches it as an `org_url_slug` tag on every `tracing::error!()` event forwarded to Sentry, alongside `command_name` and `repo_root`. Those tags are how Trunk narrows Sentry to a single customer's CI runs when someone reports a failing upload — without the slug, an error report can't be tied back to an organization and we have to ask the user to reproduce.
- **Our own upload telemetry**: the slug is stored on the bundle as `base_props.org` and scopes the `UploadMetrics` we report to the telemetry endpoint at the end of a run (`cli/src/upload_command.rs`, `ApiClient::telemetry_upload_metrics`). Timing, quarantine outcome, and failure-reason metrics are only useful if we know which org they came from.
- **Our own upload telemetry**: at the end of a run the CLI sends one OTLP metrics request (`api/src/otlp.rs`, `ApiClient::telemetry_upload_metrics`) to public-api's CLI telemetry metrics route, `/v2/telemetry/v1/metrics`, with the run's API key; a run with no key (a fork pull request) sends nothing. It holds an upload count and the upload and run duration histograms (delta, one data point each), with trunk1's telemetry-service labels and buckets, and each label value bounded the way trunk1's `boundedLabel` bounds it. The route stamps the org from the key rather than from the slug, and keeps only the metric and attribute names it lists for `trunk-analytics-cli` (trunk2 `ts/apps/public-api/src/products/telemetry`), so a new metric or attribute is dropped until it is added there too.

The slug also travels with the upload requests, builds the org-scoped links we print for uploads and tests (`api/src/urls.rs`), and backs the settings-page hint shown on unauthorized errors (`cli/src/error_report.rs`).

Expand Down
66 changes: 29 additions & 37 deletions api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl std::error::Error for ApiErrorEndpoint {}

pub struct ApiClient {
pub api_host: String,
pub telemetry_host: String,
s3_client: Client,
trunk_api_client: Client,
/// Holds the run's v2 credential: an API token from the start, or a fork pull request's Trunk
Expand All @@ -60,7 +59,9 @@ pub struct ApiClient {
credential_free: bool,
/// A credential-free GitHub Actions fork PR run, which on v2 swaps its `GITHUB_TOKEN` for a login.
github_fork_pull_request: Option<GithubForkPullRequest>,
telemetry_client: Client,
/// The run's API token, for the CLI telemetry route; `None` without one (a fork pull request
/// has no token, and the gateway refuses its GitHub Actions login there).
telemetry_client: Option<Client>,
version_path_prefix: String,
org_url_slug: String,
render_sender: Option<Sender<DisplayMessage>>,
Expand Down Expand Up @@ -88,6 +89,10 @@ impl V2Credential {
const TRUNK_TOKEN_HEADER: HeaderName = HeaderName::from_static("x-trunk-token");

fn client(&self) -> anyhow::Result<Client> {
self.client_with_timeout(ApiClient::TRUNK_API_TIMEOUT)
}

fn client_with_timeout(&self, timeout: std::time::Duration) -> anyhow::Result<Client> {
let (name, value, error) = match self {
Self::ApiKey(token) => (
header::AUTHORIZATION,
Expand All @@ -109,7 +114,7 @@ impl V2Credential {
);
headers.append(name, value);
Ok(Client::builder()
.timeout(ApiClient::TRUNK_API_TIMEOUT)
.timeout(timeout)
.default_headers(headers)
.build()?)
}
Expand Down Expand Up @@ -160,18 +165,6 @@ impl ApiClient {
let api_host = get_api_host();
tracing::debug!("Using public api address {}", api_host);

let telemetry_host = if api_host.contains("https://") {
format!(
"https://telemetry.{}",
api_host.split("https://").nth(1).unwrap_or_default()
)
} else {
// If the api_host is not https, we default to the api host
// this happens when the api_host is localhost and we are running
// inside of test environments
api_host.clone()
};

let mut trunk_api_client_default_headers = HeaderMap::new();
trunk_api_client_default_headers.append(
header::CONTENT_TYPE,
Expand Down Expand Up @@ -199,21 +192,12 @@ impl ApiClient {
.transpose()?;
let credential_free = allow_forked_pr_uploads && api_token.is_none();

let mut telemetry_client_default_headers = HeaderMap::new();
telemetry_client_default_headers.append(
header::CONTENT_TYPE,
HeaderValue::from_static("application/x-protobuf"),
);

if let Some(api_token_header_value) = api_token_header_value {
telemetry_client_default_headers
.append(Self::TRUNK_API_TOKEN_HEADER, api_token_header_value);
}

let telemetry_client = Client::builder()
.timeout(Self::TRUNK_TELEMETRY_TIMEOUT)
.default_headers(telemetry_client_default_headers)
.build()?;
let telemetry_client = api_token
.clone()
.map(|token| {
V2Credential::ApiKey(token).client_with_timeout(Self::TRUNK_TELEMETRY_TIMEOUT)
})
.transpose()?;

let mut s3_client_default_headers = HeaderMap::new();
s3_client_default_headers.append(
Expand All @@ -231,7 +215,6 @@ impl ApiClient {
};

Ok(ApiClient {
telemetry_host,
api_host,
s3_client,
trunk_api_client,
Expand Down Expand Up @@ -443,18 +426,27 @@ impl ApiClient {
&self,
request: &message::TelemetryUploadMetricsRequest,
) -> anyhow::Result<()> {
let Some(telemetry_client) = &self.telemetry_client else {
return Ok(());
};
let environment = if self.api_host == DEFAULT_ORIGIN {
"production"
} else {
"staging"
};
CallApi {
action: || async {
if std::env::var("DISABLE_TELEMETRY").is_ok() {
return Ok(());
}
let response = self
.telemetry_client
.post(format!(
"{}{}/flakytests-cli/upload-metrics",
self.telemetry_host, self.version_path_prefix
let now = chrono::Utc::now();
let response = telemetry_client
.post(format!("{}{}", self.api_host, crate::otlp::CLI_TELEMETRY_METRICS_PATH))
.json(&crate::otlp::upload_metrics_request(
&request.upload_metrics,
environment,
crate::otlp::unix_nanos(now.timestamp(), now.timestamp_subsec_nanos() as i32),
))
.body(prost::Message::encode_to_vec(&request.upload_metrics))
.send()
.await?;

Expand Down
1 change: 1 addition & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod call_api;
pub mod client;
pub mod message;
pub mod otlp;
pub mod quarantine;
pub mod urls;
Loading
Loading