Repository navigation
feat(selfhost): OTLP traces labelled per instance, and /api/metrics (#293) - #343
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Observability for self-host: OTLP traces labelled per instance, an authenticated
/api/metrics, and the composeotelprofile. 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_ENDPOINTwas set (since 0.11.0). This PR makes the exporter usable with real backends (packages/cli/src/selfhost/otel.ts).https://endpoints now work (https = (…, tlsOptions = (trustBrowserCas = true)))./v1/tracesand the path would be dropped silently./v1/tracesitself is still accepted, so existing settings keep working.OTEL_EXPORTER_OTLP_HEADERS(OTel'sname=value,…, URL-decoded, also_FILE) carries a backend's headers. A malformed pair is refused without echoing its value.service.name(the Worker:admin,site,d1, …) and has no setting for other attributes. So each export carriesx-cloudhal-instance(the advertise name) andx-cloudhal-versionthroughinjectRequestHeaders. The collector config turns them intoservice.instance.idandservice.version.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/metricsin Prometheus text format (0.0.4). The code ispackages/cloudflare/src/metrics.ts, wrapped around the Worker byinstrumentAdmin.CLOUDHAL_METRICS_TOKENis set: 32 characters or more, also_FILE, passed throughfromEnvironmentonly./api/metricson the public host.cloudhal_http_requests_totalandcloudhal_http_request_duration_secondsby Worker, area and status class. The site Worker hands its counters to the admin Worker overSITEat/__cloudhal/metrics, with the purge token, like/features.cloudhal_publish_duration_secondsbyvia(apifor snapshot publishes and publish-now,mcpfor thepublishtool) and outcome.cloudhal_scheduler_lag_seconds,cloudhal_scheduler_seconds_since_tickandcloudhal_scheduler_healthy, read from D1 at scrape time.cloudhal_store_errors_total{store="d1"|"r2"}, from the newonErrorhook onD1Driver(after retries) and a watched R2 bucket.cloudhal_process_start_time_seconds.RequestMetrics/Histogramin 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.createAdminWorkerand itsfetchdon't grow: the wrapper sits outside them, andadminSql(env)andwatchedBucket(...)replace the old one-liners.Recipes
otelprofile:otel/opentelemetry-collector-contrib:0.166.0, pinned by digest. It has no published port and readsdeploy/docker/otel-collector.yaml: an OTLP/HTTP receiver withinclude_metadata, theresourceprocessor withfrom_context, batch, and a debug exporter, with commented pointers to Jaeger, Tempo and Honeycomb. Themailprofile came with Self-host: an SMTP sender behind the EMAIL binding (CLOUDHAL_SMTP_URL) #291.otel.headersSecret, andmetrics.enabled, which adds the token to the generated Secret (kept across upgrades). Defaults render unchanged, so the kustomize base is unchanged.tasks.cloudhal.CI
--profile otel. A new step runsotelcol-contrib validateonotel-collector.yamlin the pinned image. The chart lint rendersmetrics.enabled.scripts/otel-smoke.ts.adminandsitemust reach the collector withservice.instance.idset to the instance, plusservice.versionandservice.namespace./api/metricsmust 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
deploy/docker/README.md, the Helm and Swarm READMEs,.env.exampleand AGENTS.md.Checked locally
biome check --diagnostic-level=error,pnpm size:checkandactionlint.tscof storage, core-ts, astro-renderer, cloudflare, cli and the smoke script.metricsandbrowser-cdp(cloudflare),selfhost-otelandselfhost(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_contextand workerd's exporter headers meet.Closes #293
🤖 Generated with Claude Code