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
13 changes: 13 additions & 0 deletions .changeset/selfhost-observability.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@cloudhal/cloudflare": minor
"@cloudhal/core-ts": minor
"@cloudhal/astro-renderer": minor
"@cloudhal/storage": minor
"@cloudhal/cli": minor
---

Observability for self-host (#293; RFC-0047 §7a).
- **Traces.** `OTEL_EXPORTER_OTLP_ENDPOINT` takes `https://` too, and `OTEL_EXPORTER_OTLP_HEADERS` adds a backend's headers. Every export carries `x-cloudhal-instance` and `x-cloudhal-version` headers for the collector to turn into resource attributes.
- **Metrics.** The admin Worker gains `GET /api/metrics` (Prometheus text). It is off unless `CLOUDHAL_METRICS_TOKEN` is set, and then answers only that bearer token. It covers requests of the admin and site Workers (the site's through `/__cloudhal/metrics`), publish durations, scheduler lag and health, and store errors.
- **Supporting changes.** `D1Driver` takes an `onError` hook, and `@cloudhal/core-ts` exports the per-isolate counters (`RequestMetrics`, `Histogram`).
- **Recipes.** The compose file gains an `otel` profile (an OpenTelemetry Collector with `otel-collector.yaml`), Caddy no longer serves `/api/metrics` publicly, and the Helm chart gains `otel.headersSecret` and `metrics.enabled`.
18 changes: 17 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ name: Docker image
# first stops), scripts/failover-smoke.ts (three instances, one killed with SIGKILL while a release
# is due, then graceful handoffs under load; #290), scripts/mail-smoke.ts (an invite and a reset
# through Mailpit with STARTTLS and AUTH; #291), scripts/render-smoke.ts (render_page through
# the Chrome sidecar; #292) and Trivy, which fails the job on a CRITICAL
# the Chrome sidecar; #292), scripts/otel-smoke.ts (spans at the collector, /api/metrics; #293) and
# Trivy, which fails the job on a CRITICAL
# vulnerability with a fix;
# - `smoke-arm64` runs the same smoke test on the arm64 image, on GitHub's native arm64 runner;
# - `tag` adds the release tags to the very digest that passed, with `imagetools create`, writes
Expand Down Expand Up @@ -56,6 +57,7 @@ on:
- "scripts/failover-smoke.ts"
- "scripts/mail-smoke.ts"
- "scripts/render-smoke.ts"
- "scripts/otel-smoke.ts"
- "scripts/pack-packages.ts"
- "scripts/selfhost-edge-bundle.ts"
- "packages/cli/src/selfhost/**"
Expand Down Expand Up @@ -117,13 +119,22 @@ jobs:
docker compose -f deploy/docker/compose.yml --env-file "$RUNNER_TEMP/compose.env" --profile caddy config --quiet
docker compose -f deploy/docker/compose.yml --env-file "$RUNNER_TEMP/compose.env" --profile mail config --quiet
docker compose -f deploy/docker/compose.yml --env-file "$RUNNER_TEMP/compose.env" --profile chrome config --quiet
docker compose -f deploy/docker/compose.yml --env-file "$RUNNER_TEMP/compose.env" --profile otel config --quiet
# The otel profile's collector config, in the collector the compose file runs (a few seconds).
- name: Validate otel-collector.yaml
run: |
collector=$(sed -n 's/^ *image: \(otel\/opentelemetry-collector-contrib:.*\)$/\1/p' deploy/docker/compose.yml)
docker run --rm -v "$PWD/deploy/docker/otel-collector.yaml:/etc/otelcol-contrib/config.yaml:ro" \
"$collector" validate --config=/etc/otelcol-contrib/config.yaml
- name: Lint the Helm chart
run: |
helm lint deploy/helm/cloudhal
helm lint deploy/helm/cloudhal --set replicaCount=3 --set baseUrl=https://studio.example.com --set ingress.enabled=true
helm template t deploy/helm/cloudhal --set httpRoute.enabled=true --set baseUrl=https://studio.example.com --set 'httpRoute.parentRefs[0].name=gw' > /dev/null
helm template t deploy/helm/cloudhal --set mail.smtpUrlSecret.name=smtp --set mail.from=cloudhal@example.com --set mail.caConfigMap.name=ca \
| grep -q 'CLOUDHAL_SMTP_CA_FILE' || { echo "::error::the chart's mail values render no CLOUDHAL_SMTP_CA_FILE"; exit 1; }
helm template t deploy/helm/cloudhal --set metrics.enabled=true | grep -q 'CLOUDHAL_METRICS_TOKEN:' \
|| { echo "::error::metrics.enabled puts no CLOUDHAL_METRICS_TOKEN in the Secret"; exit 1; }
helm template t deploy/helm/cloudhal --set chrome.enabled=true \
| grep -q 'value: "http://t-cloudhal-chrome:9222"' || { echo "::error::chrome.enabled renders no CLOUDHAL_BROWSER_URL"; exit 1; }
if helm template t deploy/helm/cloudhal --set mail.smtpUrlSecret.name=smtp > /dev/null 2>&1; then
Expand Down Expand Up @@ -274,6 +285,11 @@ jobs:
- name: render_page smoke test (Chrome sidecar)
timeout-minutes: 10
run: node scripts/render-smoke.ts --image cloudhal:ci
# Observability (#293): spans reach the otel profile's collector with service.name and the
# instance; /api/metrics answers its token holder in Prometheus text, and nobody else. CI only.
- name: Observability smoke test (collector, /api/metrics)
timeout-minutes: 10
run: node scripts/otel-smoke.ts --image cloudhal:ci
- name: Trivy scan
timeout-minutes: 15
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ node scripts/release-bundle.ts build|verify … # RFC-0046 release bundle: det
- `packages/core-ts` — `SiteGraphStore` (CAS on `rev`, snapshots, diff, publish, rollback). Behavioural suite in `src/testing.ts` runs on sqlite and Miniflare D1. `./local` opens the offline store; `seedStarterSite` seeds a demo. `./surfaces` is RFC-0020's generative UI: the catalog (primitives + block types), eval-free surface checks, bindings, repairs, JSON Patch (`patchPage`), `catalogPrompt`; `./interop` converts A2UI v0.9 and json-render to and from surfaces; `./design` is RFC-0021's DESIGN.md lint and guidance. `./fields` is RFC-0041's typed custom fields (definition and value checks, `resolveFields`, `formatFieldValue`, the agents' JSON Schema, the conversion table); `src/fields-operations/` runs rename/retype/localize/remove. `search.ts` is RFC-0019's FTS5 site search (index written with the publish batch; articles, pages, terms and authors, RFC-0029); `search-provider.ts` is RFC-0029's `SearchProvider` (`fts5`, `memory`, `withFallback`), set with `SiteGraphStoreOptions.search`; `search-ai.ts` is the experimental Cloudflare AI Search provider (`hybridProvider`, `searchProviderFromEnv`, `cloudhal.json` `search`, docs/guides/search.md).
- `packages/schedules` — RFC-0008: time model (only `temporal-polyfill/implementation`, `now` injected, never `Temporal.Now`), RRULE subset, release model (`rl1:`), automation/run state, taint and budgets, `runDue`; `./sql` is `SqlScheduleStore`, `./engine` the Temporal-free part core-ts and the site Worker use. Worker-safe; conformance suites in `src/testing.ts` on sqlite and Miniflare D1.
- `packages/astro-renderer` — `GraphSource` (live/snapshot), `cloudhal()` integration (virtual config + dev route-cache watcher), `.astro` section components (`Surface.astro` for RFC-0020 surfaces; `./surfaces` renders them, `./kit` has the table of contents and reading time; `./fields` and `EntryFields.astro` render typed field values, RFC-0041). `./status-page` is RFC-0043's status pages (`statusPage(state, input) → Response`, served by the Site Worker entry before Astro and by `cloudhal dev` at `/__cloudhal/status/<state>`; the optional `statusPage` theme hook; the catalog in `src/status-strings/` where only `en.ts` is hand-edited and the other languages come from `pnpm i18n:import`). Routes live in starters, not here.
- `packages/cli` — the `cloudhal` CLI (`init`, `dev`, `studio`, `mcp`, `deploy`, `push`, `pull`, `export`, `import`, …; every command is in `src/cli/help.ts`, which `cloudhal --help` prints and the docs site's CLI reference is generated from); TS bin run by Node 24 type stripping. `src/cli.ts` parses flags and dispatches through `COMMANDS`, built from `src/cli/commands-{local,remote,content,ops,site}.ts` (a new command is one handler plus one line in `src/cli/help.ts`). `src/archive/` is RFC-0039's `.cloudhal` container (tar, gzip, manifest, limits), read and written by `src/commands/export.ts` and `import*.ts` (`archive-bundle.ts`, `archive-reassemble.ts`); the starter demo archive `test/fixtures/site-archives/starter-blog.cloudhal` is written by `pnpm demo:archive`, and `pnpm check` fails when it is stale. E2E test spawns the bin (Astro disables dev middleware under `VITEST`). Cloudflare tests run both Workers on workerd via wrangler's `createTestHarness`; test files run serially (shared starter caches). `src/commands/deploy/release/` is RFC-0046 §11b's `deploy --release`: the signed key and yank lists (`trust.ts`, the yank memory in `~/.cloudhal/release-trust.json`), the release lookup and download, the theme check against the CLI's starter (`theme-check.ts`; recheck its build-input list on every Astro or Vite major), the build keys of `src/config-build-keys.ts`, extraction and the `no_bundle` wrangler configs; tests use `@cloudhal/release-verify`'s synthetic bundle (`test/fixtures/release-deploy.ts`), never the network. `src/selfhost/` is RFC-0047's self-host image on workerd (`cloudhal selfhost fetch|check|config|secrets|start`): the settings (`_FILE` secrets), the generated Cap'n Proto config (Durable Object `uniqueKey`s are constants, never change them), the bundle check, the Node-volume guard, the first site and the SMTP sender behind `EMAIL` (`smtp-*.ts`, #291: STARTTLS or TLS only; `smtp-client.ts` has no imports because the config embeds it as a Worker module); `browser-sidecar.ts` binds the optional Chrome for `render_page` as `BROWSER_CDP` (#292, `@cloudhal/cloudflare`'s `browser-cdp.ts`); `deploy/docker/workerd/` pins workerd (an npm lockfile Dependabot bumps weekly, ADR-0047; never the Miniflare pin), `scripts/docker-smoke.ts` runs it on SeaweedFS.
- `packages/cli` — the `cloudhal` CLI (`init`, `dev`, `studio`, `mcp`, `deploy`, `push`, `pull`, `export`, `import`, …; every command is in `src/cli/help.ts`, which `cloudhal --help` prints and the docs site's CLI reference is generated from); TS bin run by Node 24 type stripping. `src/cli.ts` parses flags and dispatches through `COMMANDS`, built from `src/cli/commands-{local,remote,content,ops,site}.ts` (a new command is one handler plus one line in `src/cli/help.ts`). `src/archive/` is RFC-0039's `.cloudhal` container (tar, gzip, manifest, limits), read and written by `src/commands/export.ts` and `import*.ts` (`archive-bundle.ts`, `archive-reassemble.ts`); the starter demo archive `test/fixtures/site-archives/starter-blog.cloudhal` is written by `pnpm demo:archive`, and `pnpm check` fails when it is stale. E2E test spawns the bin (Astro disables dev middleware under `VITEST`). Cloudflare tests run both Workers on workerd via wrangler's `createTestHarness`; test files run serially (shared starter caches). `src/commands/deploy/release/` is RFC-0046 §11b's `deploy --release`: the signed key and yank lists (`trust.ts`, the yank memory in `~/.cloudhal/release-trust.json`), the release lookup and download, the theme check against the CLI's starter (`theme-check.ts`; recheck its build-input list on every Astro or Vite major), the build keys of `src/config-build-keys.ts`, extraction and the `no_bundle` wrangler configs; tests use `@cloudhal/release-verify`'s synthetic bundle (`test/fixtures/release-deploy.ts`), never the network. `src/selfhost/` is RFC-0047's self-host image on workerd (`cloudhal selfhost fetch|check|config|secrets|start`): the settings (`_FILE` secrets), the generated Cap'n Proto config (Durable Object `uniqueKey`s are constants, never change them), the bundle check, the Node-volume guard, the first site and the SMTP sender behind `EMAIL` (`smtp-*.ts`, #291: STARTTLS or TLS only; `smtp-client.ts` has no imports because the config embeds it as a Worker module); `browser-sidecar.ts` binds the optional Chrome for `render_page` as `BROWSER_CDP` (#292, `@cloudhal/cloudflare`'s `browser-cdp.ts`); `otel.ts` is the trace exporter (instance and version headers) and `CLOUDHAL_METRICS_TOKEN` for the admin Worker's `/api/metrics` (#293, `@cloudhal/cloudflare`'s `metrics.ts`, counters in `@cloudhal/core-ts`'s `request-metrics.ts`); `deploy/docker/workerd/` pins workerd (an npm lockfile Dependabot bumps weekly, ADR-0047; never the Miniflare pin), `scripts/docker-smoke.ts` runs it on SeaweedFS.
- `packages/cloudflare` — the admin Worker (`createAdminWorker`); `./playground` is RFC-0036's playground Worker (`createPlaygroundWorker`, the `PlaygroundSite` Durable Object, signed session cookies). Generated entries must import `@cloudhal/cloudflare/workerd-prelude` first (ADR-0012).
- `packages/agent-runtime` / `packages/mcp-server` — policy, skills, audit; MCP tools and transports (ADR-0011). `@cloudhal/mcp-server/render` is RFC-0021's `render_page` core: a small DevTools client, the in-page checks and limits; the CLI drives local Chrome with it (`render-chrome.ts`), the admin Worker Browser Rendering (`browser-render.ts`).
- `starters/blog` — the theme boundary: `src/pages/[...path].astro`, `src/layouts`, `src/theme.ts`.
Expand Down
1 change: 1 addition & 0 deletions deploy/docker/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
README.md
compose.yml
Caddyfile
otel-collector.yaml
.env.example
.env
.DS_Store
Expand Down
7 changes: 6 additions & 1 deletion deploy/docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@
#CLOUDHAL_BROWSER_URL=http://chrome:9222
#CLOUDHAL_RENDER_ORIGIN=http://cloudhal:8788

# Traces of every Worker invocation to an OTLP/HTTP collector.
# Traces of every Worker invocation to an OTLP/HTTP collector (http:// or https://, no path; the
# otel profile runs one), and a backend's own headers (name=value,…; also _FILE).
#OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
#OTEL_EXPORTER_OTLP_HEADERS=
# GET /api/metrics, Prometheus text of this instance, for whoever holds this token (32+ characters:
# openssl rand -hex 32). Empty: off. Scrape each instance on the private network, never the proxy.
#CLOUDHAL_METRICS_TOKEN=
2 changes: 2 additions & 0 deletions deploy/docker/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# CLOUDHAL_BEHIND_PROXY=1. Caddy appends the client's address to X-Forwarded-For, whose last hop
# the image then takes as the client's (RFC-0047 §8).
{$CLOUDHAL_BASE_URL} {
# Metrics are for a scraper on the private network (cloudhal:8787), never the public host.
respond /api/metrics 404
reverse_proxy cloudhal:8787 {
# Build chat streams server-sent events
flush_interval -1
Expand Down
3 changes: 2 additions & 1 deletion deploy/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The official self-host image, `ghcr.io/cloudhal-dev/cloudhal`, built from each r
- With the `caddy` profile, Caddy proxies both hosts with TLS and sets `CF-Connecting-IP`.
- With the `mail` profile, Mailpit catches every message at http://127.0.0.1:8025. It requires STARTTLS, with a certificate the `mail-tls` one-shot makes once.
- With the `chrome` profile, a headless Chrome serves `render_page` (`CLOUDHAL_BROWSER_URL=http://chrome:9222`). It publishes no port and shares a network with Cloudhal only.
- With the `otel` profile, an OpenTelemetry Collector (`otel-collector.yaml`) prints the traces and labels them with the instance and version. Caddy never serves `/api/metrics` publicly.

Build a release:

Expand All @@ -33,4 +34,4 @@ node scripts/pack-packages.ts deploy/docker/tarballs && pnpm --filter @cloudhal/

`node scripts/docker-smoke.ts --image cloudhal:local` runs what CI runs after every build (`.github/workflows/docker.yml`); `--platform linux/arm64` runs the other architecture where Docker can.

`scripts/failover-smoke.ts` is the cluster failover test (#290): three instances, one killed with SIGKILL while a release is due, then graceful handoffs under load. It runs in CI only and refuses to start anywhere else. So does `scripts/mail-smoke.ts` (#291), which sends an invite and a password reset through Mailpit with STARTTLS and AUTH required, and `scripts/render-smoke.ts` (#292), which calls `render_page` through the Chrome sidecar.
`scripts/failover-smoke.ts` is the cluster failover test (#290): three instances, one killed with SIGKILL while a release is due, then graceful handoffs under load. It runs in CI only and refuses to start anywhere else. So does `scripts/mail-smoke.ts` (#291), which sends an invite and a password reset through Mailpit with STARTTLS and AUTH required, `scripts/render-smoke.ts` (#292), which calls `render_page` through the Chrome sidecar, and `scripts/otel-smoke.ts` (#293), which checks spans at the collector and `/api/metrics`.
18 changes: 18 additions & 0 deletions deploy/docker/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# (http://127.0.0.1:8025; see the mailpit service)
# docker compose --profile chrome up -d + a headless Chrome for render_page (see the chrome
# service)
# docker compose --profile otel up -d + an OpenTelemetry Collector that prints Cloudhal's
# traces (otel-collector.yaml next to this file)
# Every instance of an installation needs the same .env; this file runs one instance (the cluster
# of one). More instances: deploy/swarm or deploy/helm.
x-store: &store
Expand Down Expand Up @@ -53,7 +55,11 @@ services:
CLOUDHAL_BEHIND_PROXY: ${CLOUDHAL_BEHIND_PROXY:-0}
CLOUDHAL_SITE_NAME: ${CLOUDHAL_SITE_NAME:-My site}
CLOUDHAL_AUTOMATIONS: ${CLOUDHAL_AUTOMATIONS:-}
# Traces (the otel profile: http://otel-collector:4318) and a backend's own headers.
OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT:-}
OTEL_EXPORTER_OTLP_HEADERS: ${OTEL_EXPORTER_OTLP_HEADERS:-}
# GET /api/metrics (Prometheus text) for whoever holds this token; empty keeps it off.
CLOUDHAL_METRICS_TOKEN: ${CLOUDHAL_METRICS_TOKEN:-}
# Mail (invites, password resets): off unless CLOUDHAL_SMTP_URL is set. smtp:// is STARTTLS,
# smtps:// TLS from the start; there is no unencrypted mode. CLOUDHAL_SMTP_CA_FILE adds
# certificates to trust (the mail profile's, a private relay's).
Expand Down Expand Up @@ -176,6 +182,18 @@ services:
security_opt: ["no-new-privileges:true"]
networks: [render]

# The otel profile: an OpenTelemetry Collector on this network only (no published port), with
# otel-collector.yaml next to this file. Set in .env:
# OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
otel-collector:
profiles: [otel]
image: otel/opentelemetry-collector-contrib:0.166.0@sha256:3e18fe04181de50bdd72e23ad4a4d6214b54351e63d496f1c58127cc4541ad7f
restart: unless-stopped
volumes:
- ./otel-collector.yaml:/etc/otelcol-contrib/config.yaml:ro
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]

networks:
default:
# Cloudhal and the chrome profile's browser, nothing else.
Expand Down
46 changes: 46 additions & 0 deletions deploy/docker/otel-collector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# The OpenTelemetry Collector of compose.yml's `otel` profile (docs/guides/self-host.md,
# "Observability"). Cloudhal's workerd exports every Worker invocation's spans here over OTLP/HTTP
# (OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318). workerd sets `service.name` to the
# Worker (admin, site, d1, …); each export also carries x-cloudhal-instance and
# x-cloudhal-version headers, which the resource processor below turns into
# `service.instance.id` and `service.version`.
#
# The debug exporter prints spans to the collector's log (`docker compose logs otel-collector`).
# For a real backend, add an exporter and list it in the pipeline, for example:
# otlphttp/jaeger: { endpoint: http://jaeger:4318 }
# otlphttp/tempo: { endpoint: http://tempo:4318 }
# otlphttp/honeycomb:
# endpoint: https://api.honeycomb.io
# headers: { x-honeycomb-team: ${env:HONEYCOMB_API_KEY} }
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
# keeps the request headers as client metadata, for the resource processor
include_metadata: true

processors:
resource/cloudhal:
attributes:
- key: service.namespace
value: cloudhal
action: upsert
- key: service.instance.id
from_context: metadata.x-cloudhal-instance
action: upsert
- key: service.version
from_context: metadata.x-cloudhal-version
action: upsert
batch: {}

exporters:
debug:
verbosity: detailed

service:
pipelines:
traces:
receivers: [otlp]
processors: [resource/cloudhal, batch]
exporters: [debug]
Loading