Skip to content

feat(selfhost): OTLP traces labelled per instance, and /api/metrics (#293) - #343

Merged
Madraka merged 2 commits into
mainfrom
claude/selfhost-otel-293
Nov 29, 2026
Merged

Madraka merged 2 commits into
mainfrom
claude/selfhost-otel-293

Conversation

@Madraka

@Madraka Madraka commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Observability for self-host: OTLP traces labelled per instance, an authenticated /api/metrics, and the compose otel profile. The metrics decision is written into RFC-0047 as §7a (amended 2026-11-29) and follows the PM's recommendation.

Traces

workerd already exported spans when OTEL_EXPORTER_OTLP_ENDPOINT was set (since 0.11.0). This PR makes the exporter usable with real backends (packages/cli/src/selfhost/otel.ts).

  • Endpoints. https:// endpoints now work (https = (…, tlsOptions = (trustBrowserCas = true))).
  • No path. An endpoint with a path is refused at start, because workerd always POSTs to /v1/traces and the path would be dropped silently. /v1/traces itself is still accepted, so existing settings keep working.
  • Backend headers. OTEL_EXPORTER_OTLP_HEADERS (OTel's name=value,…, URL-decoded, also _FILE) carries a backend's headers. A malformed pair is refused without echoing its value.
  • Resource attributes. workerd sets only service.name (the Worker: admin, site, d1, …) and has no setting for other attributes. So each export carries x-cloudhal-instance (the advertise name) and x-cloudhal-version through injectRequestHeaders. The collector config turns them into service.instance.id and service.version.
  • Cluster config. That header is the only per-instance difference in the config besides sockets and advertise. It labels spans and changes no Worker's behaviour, so §2's identical-config invariant holds. The RFC says so.

Metrics (the decision)

The admin Worker serves GET /api/metrics in Prometheus text format (0.0.4). The code is packages/cloudflare/src/metrics.ts, wrapped around the Worker by instrumentAdmin.

  • Off by default, never public.
    • 404 unless CLOUDHAL_METRICS_TOKEN is set: 32 characters or more, also _FILE, passed through fromEnvironment only.
    • 401 for any other bearer token, compared in constant time.
    • I chose a dedicated token so a scraper never holds the deploy token.
    • The compose file's Caddy answers 404 for /api/metrics on the public host.
  • What it reports:
    • cloudhal_http_requests_total and cloudhal_http_request_duration_seconds by Worker, area and status class. The site Worker hands its counters to the admin Worker over SITE at /__cloudhal/metrics, with the purge token, like /features.
    • cloudhal_publish_duration_seconds by via (api for snapshot publishes and publish-now, mcp for the publish tool) and outcome.
    • cloudhal_scheduler_lag_seconds, cloudhal_scheduler_seconds_since_tick and cloudhal_scheduler_healthy, read from D1 at scrape time.
    • cloudhal_store_errors_total{store="d1"|"r2"}, from the new onError hook on D1Driver (after retries) and a watched R2 bucket.
    • cloudhal_process_start_time_seconds.
  • Per instance. Counters live in the isolate (workerd runs one per Worker per instance, RequestMetrics/Histogram in core-ts), so the guide says to scrape each instance and never the load balancer. Labels are a fixed set: no paths, ids or users. The scrape itself is not counted.
  • Cloudflare. Unchanged unless someone sets the token there, and the RFC says why it isn't meant for that platform.
  • Size baseline. createAdminWorker and its fetch don't grow: the wrapper sits outside them, and adminSql(env) and watchedBucket(...) replace the old one-liners.

Recipes

  • Compose otel profile: otel/opentelemetry-collector-contrib:0.166.0, pinned by digest. It has no published port and reads deploy/docker/otel-collector.yaml: an OTLP/HTTP receiver with include_metadata, the resource processor with from_context, batch, and a debug exporter, with commented pointers to Jaeger, Tempo and Honeycomb. The mail profile came with Self-host: an SMTP sender behind the EMAIL binding (CLOUDHAL_SMTP_URL) #291.
  • Helm: otel.headersSecret, and metrics.enabled, which adds the token to the generated Secret (kept across upgrades). Defaults render unchanged, so the kustomize base is unchanged.
  • Swarm: README steps for the endpoint, the token secret and scraping tasks.cloudhal.

CI

  • Static checks. The compose check covers --profile otel. A new step runs otelcol-contrib validate on otel-collector.yaml in the pinned image. The chart lint renders metrics.enabled.
  • New CI-only scripts/otel-smoke.ts.
    • Setup: SeaweedFS, the collector with the repo's config, and an instance exporting to it.
    • Spans of admin and site must reach the collector with service.instance.id set to the instance, plus service.version and service.namespace.
    • /api/metrics must answer 401 with no token and with a wrong one. With the right token it must show both Workers' requests, the scheduler, the store errors and the publish histogram.

Docs

  • Self-host guide: a new Observability section (traces, the profile, metrics with a scrape config) and Environment rows.
  • Elsewhere: RFC-0047 §7a, deploy/docker/README.md, the Helm and Swarm READMEs, .env.example and AGENTS.md.
  • Changeset: minor for cloudflare, core-ts, astro-renderer, storage and cli.

Checked locally

  • biome check --diagnostic-level=error, pnpm size:check and actionlint.
  • tsc of storage, core-ts, astro-renderer, cloudflare, cli and the smoke script.
  • vitest: metrics and browser-cdp (cloudflare), selfhost-otel and selfhost (cli), d1-retry (storage). All pass.

There were no Docker, compose or helm runs locally. This PR's Build edge run is the first time the collector's from_context and workerd's exporter headers meet.

Closes #293

🤖 Generated with Claude Code

Madraka and others added 2 commits November 29, 2026 12:12
…293)

Traces: OTEL_EXPORTER_OTLP_ENDPOINT now takes https:// and refuses a path
(workerd posts to /v1/traces), OTEL_EXPORTER_OTLP_HEADERS (also _FILE)
adds a backend's headers, and every export carries x-cloudhal-instance (the
advertise name) and x-cloudhal-version through injectRequestHeaders: workerd
sets only service.name, so the collector turns those into
service.instance.id and service.version.

Metrics (the decision, as an RFC-0047 §7a amendment, follows the PM's
recommendation): the admin Worker's GET /api/metrics, Prometheus text of
one instance, off unless CLOUDHAL_METRICS_TOKEN is set and then answering
only that bearer token (404/401 otherwise). Requests and durations of the
admin and site Workers (the site's through SITE and /__cloudhal/metrics),
publish durations over the API and MCP, scheduler lag/last tick/health,
D1 and R2 errors (D1Driver onError, a watched R2 bucket). Counters live in
the isolate (core-ts RequestMetrics, Histogram), labels are a fixed set.

Recipes: compose's otel profile (otel-collector-contrib pinned, with
otel-collector.yaml: include_metadata and the resource processor's
from_context), Caddy answers 404 for /api/metrics on the public host, Helm
otel.headersSecret and metrics.enabled. CI validates the collector config
and the new chart values, and scripts/otel-smoke.ts checks spans of both
Workers at the collector with instance and version, and /api/metrics for
its token holder only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
…s seen

The first run found the instance, version and namespace on the spans but
read only the last 2,000 lines of the debug exporter, which workerd's own
d1 spans filled; the admin and site spans were older. The smoke now reads
the whole log, keeps requests flowing while it waits, wants a span of the
admin and of the site Worker (their own service or its front or assets
service), and names the spans seen per service.name, in the step on success
and in the error on failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka
Madraka merged commit a378fb2 into main Nov 29, 2026
12 checks passed
@Madraka
Madraka deleted the claude/selfhost-otel-293 branch November 29, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self-host: observability (OTLP traces, metrics) and the otel and mail compose profiles

1 participant