Skip to content

feat(telemetry): send upload telemetry as OTLP metrics to public-api (TRUNK-23773) - #1328

Merged
trunk-io[bot] merged 3 commits into
mainfrom
tylerbeebe/trunk-23773-otlp-upload-telemetry
Nov 16, 2026
Merged

trunk-io[bot] merged 3 commits into
mainfrom
tylerbeebe/trunk-23773-otlp-upload-telemetry

Conversation

@acatxnamedvirtue

@acatxnamedvirtue acatxnamedvirtue commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

TRUNK-23773: analytics-cli's upload telemetry moves off trunk1 to trunk2's public-api, so it no longer needs the legacy org key. It's sent as OTLP metrics (decided 2026-11-16). public-api validates each data point and records its own counter and histograms, as trunk1's telemetry service did (trunk-io/trunk2#8568).

  • Before: after each upload, ApiClient::telemetry_upload_metrics POSTed the protobuf UploadMetrics to telemetry.<host>/v1/flakytests-cli/upload-metrics on trunk1, with the raw key on x-api-token. That works only for a legacy key. trunk1 turned each event into its upload_metrics counter and its upload_duration and cli_run_duration histograms.
  • After: it POSTs one OTLP JSON ExportMetricsServiceRequest to <api host>/v2/telemetry/v1/metrics (trunk-io/trunk2#8568), with the run's API key on Authorization.
    • The gateway exchanges a v2 or legacy key for a JWT, and the route stamps the org from it.
    • The send keeps its 1 s timeout and is still best-effort: it never fails a run.
    • A run with no API key sends nothing. That covers a fork pull request, since the gateway refuses its GitHub Actions login on this path.

api/src/otlp.rs builds three delta metrics, each with one data point per run:

Metric Type Labels
trunk.analytics_cli.uploads monotonic int sum, {upload} upload.status (success/failed), upload.failure_reason, quarantine.query_result, trunk.vcs.repository.owner / .name
trunk.analytics_cli.upload.duration histogram, s (upload start → finished) upload.status
trunk.analytics_cli.run.duration histogram, s (CLI start → upload finished) upload.status
  • Histogram buckets: trunk1's [0.05 … 600]. A histogram is skipped when a timestamp is missing or runs backwards.
  • Label values: bounded like trunk1's boundedLabel. Each value is cut to 32 characters (owner 39, name 100). It becomes invalid if it has characters outside [A-Za-z0-9._+/-], and unknown when missing; a successful upload sends failure_reason = unknown.
  • Not sent: quarantine.resolution_mode and the repository host are dropped.
  • What public-api records: it doesn't keep every label as sent (trunk2#8568, TRD-0011 §The CLI telemetry route).
    • Repository owner and name are accepted but not recorded, so per-repository outcomes aren't in metrics.
    • Each label is mapped to a fixed set, and anything else becomes other:
      • status: success or failed.
      • quarantine_query_result: the six values above.
      • failure_reason: this CLI's error_reason values (unknown, connection, the four unknown_<endpoint> values). An HTTP status such as 400_bad_request is recorded as http_400.
      • client_version: a semver of at most 32 characters.
    • Contract: the failure_reason list in trunk2 has to change with error_reason here.

The resource has service.name, service.version, deployment.environment.name (production on https://api.trunk.io, else staging), os.type and host.arch. The route rejects any other metric, type, unit, temporality or bucket bounds, so a change here needs a matching public-api change in trunk2. CONTRIBUTING.md says so.

Deploy order

This merges to main, which only ships as 1.x. trunk-io/trunk2#8568 (the metrics route, and the gateway lane covering it) must be in production before 1.0.0 is promoted. Otherwise every upload's telemetry is refused and lost silently.

The route was first built in sawmill-api (trunk-io/trunk-cloud#5923, closed). It moved to trunk2 on 2026-11-13, as the logs route (#8502), and on 2026-11-16 the metrics route took over analytics-cli's telemetry.

#1313, #1314 and #1329 are restacked on this PR.

Test plan

  • The mock server serves /v2/telemetry/v1/metrics and decodes the request strictly. It rejects any deviation in service, metric names, units, delta temporality, the monotonic flag, counts, or a bucket array without exactly one 1.
  • The telemetry tests assert on what goes over the wire: status, failure reason, quarantine result, repository labels, and both durations' bucket placement.
  • Unit tests cover bucket boundaries, label bounding, and missing or backwards durations.
  • cargo test -p api and -p test_report --test telemetry pass.
  • -p trunk-analytics-cli --test upload --test test: everything passes except the 3 Swift xunit tests, which fail the same way on unmodified main on this machine.
  • trunk fmt and trunk check report no new issues.

🤖 Generated with Claude Code

…te (TRUNK-23773)

The upload metrics went to trunk1's telemetry service
(telemetry.<host>/v1/flakytests-cli/upload-metrics) as protobuf with the
raw key on x-api-token, which only a legacy key could authenticate. They
now go to sawmill-api's CLI telemetry route, /v2/cloud/telemetry/v1/logs
on the API host, as one OTLP JSON log record, with the run's API key on
Authorization: the gateway exchanges a v2 or a legacy key for a JWT, and
the route stamps the org from it.

The record carries what UploadMetrics did, under the names the route keeps
for trunk-analytics-cli (trunk-io/trunk-cloud#5923): whether the upload
failed and its failure category, the CLI and upload durations, the
quarantine query result and resolution mode, and the repository's host,
owner and name. The send keeps its 1 s timeout and stays best-effort; a run
with no API key (a fork pull request, whose GitHub Actions login the
gateway refuses on this path) sends nothing.

The mock server serves the new path and reads the OTLP record back into
UploadMetrics, so the existing telemetry tests check what is sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@trunk-io

trunk-io Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

😎 Merged successfully - details.

acatxnamedvirtue and others added 2 commits November 13, 2026 20:20
…773)

The CLI telemetry route moved from trunk-cloud's sawmill-api into trunk2's
public-api (trunk-io/trunk2#8502).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the single OTLP log record with one OTLP/HTTP JSON
ExportMetricsServiceRequest to public-api's /v2/telemetry/v1/metrics:
an upload count and the upload and run duration histograms, delta
temporality, with trunk1's telemetry-service labels, duration buckets and
boundedLabel rule. The mock server decodes the metrics back so the
telemetry tests assert status, failure reason, quarantine result, repo
labels and each duration's bucket.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@acatxnamedvirtue acatxnamedvirtue changed the title feat(telemetry): send upload metrics as OTLP to the CLI telemetry route (TRUNK-23773) feat(telemetry): send upload telemetry as OTLP metrics to public-api (TRUNK-23773) Nov 16, 2026
@trunk-io
trunk-io Bot merged commit a4d5979 into main Nov 16, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants