# Observability and tracing

Read structured errors, durable audit records and Cloudflare spans as separate signals.

## Application errors and logs [#application-errors-and-logs]

The web middleware integrates evlog. Shared error codes and localized messages come from `packages/errors`; its server handler logs unexpected failures while the client resolver returns a safe user message. During development, inspect `.evlog/logs/` when available. Do not copy credentials, prompts or private payloads into logs.

Each RPC request has a request ID: `cf-ray` when Cloudflare set a well-formed one, otherwise a UUID. It is `context.requestId` on the server, the `x-request-id` header on every `/api/rpc` response (including the 500 when building the context fails), and a field of the error log. The MCP context derives its own `requestId` for the logs of its document tools; MCP responses do not carry the header. It serves only to correlate logs, never to decide anything: outside Cloudflare (local `wrangler dev`, Node) a client can send a well-formed `cf-ray` and it is used as is, and [Cloudflare's header reference](https://developers.cloudflare.com/fundamentals/reference/http-headers/) does not say whether a client-sent value is replaced. The error handler writes expected 4xx failures at debug level and unexpected ones (5xx, unknown codes, non-oRPC errors) as errors, both with the request ID, so a user's quoted header finds the matching line ([error handling](https://github.com/CarlosZiegler/varu/blob/main/docs/error-handling.md)).

An Audit Event is a durable domain record, not a log line. The Audit Log is the admin table; an Activity Timeline is one User's security history. Release notes belong to the public [changelog](/docs/changelog).

## Workers Logs and sampling [#workers-logs-and-sampling]

Workers Logs keeps what the Worker writes for seven days: one invocation event per request plus one event per `console` line (for example the evlog error handler's). `observability.logs.head_sampling_rate` in `apps/web/wrangler.jsonc` decides which share of requests is kept, and it is set explicitly to `1` (every request).

Sampling is decided per request before the Worker runs. An unsampled request keeps none of its lines, errors included: at `0.1`, nine of ten failures leave no trace, and a failure that happens once a day may never show up. Lower it only when the log bill matters more than that, and watch errors through the Worker's metrics (requests and errors per status in the dashboard), which this setting does not sample. Traces have their own rate (`observability.traces.head_sampling_rate`, default `1`); changing the logs rate does not change it.

What the logs cost on Workers Paid until 30 November 2026 (20 million events a month included, then US$ 0.60 per million), with one event per request and 15 ms of CPU per server render (an estimate from local workerd; every extra log line per request adds the logs column again). Prices read on 2026-11-05; the comparison is in [the 2026-11-06 measurement](https://github.com/CarlosZiegler/varu/blob/main/docs/measurements/2026-11-06-web-quick-wins.md).

| SSR requests / month | Workers total (base + requests + CPU + logs) | Logs at rate 1 | Logs at rate 0.1 |
| -------------------: | -------------------------------------------: | -------------: | ---------------: |
|                 10 M |                                        US$ 7 |          US$ 0 |            US$ 0 |
|                 50 M |                                       US$ 49 |         US$ 18 |            US$ 0 |
|                100 M |                                      US$ 109 |         US$ 48 |            US$ 0 |
|                500 M |                                      US$ 589 |        US$ 288 |           US$ 18 |

From 1 December 2026 Workers Logs moves to [Cloudflare Observability pricing](https://developers.cloudflare.com/observability/pricing/): on Workers Paid, 50 GB of ingestion and 10 GB-month of storage per billing cycle are included, then US$ 0.25 per GB ingested and US$ 0.10 per GB-month stored. Sampling reduces both. Measure the bytes per event in the dashboard before choosing a rate under that model.

## No visitor analytics [#no-visitor-analytics]

Varu tracks nothing in the browser: no Web Vitals beacon, no analytics or marketing script, no optional cookie, so there is no consent banner. The only cookies are strictly necessary ones (the session of a signed-in municipality or editor user, the language), explained on the privacy page. Server-side observability above is all there is; do not add a third-party script or a visitor-measuring endpoint without a product decision. `src/platform/app/root-document.test.tsx` fails if the document renders a consent dialog or loads a script from another origin.

## Worker traces [#worker-traces]

Wrangler enables observability and traces. The scheduled handler passes `ctx.tracing` into `runScheduledJobs`; `workersTracer` adapts Effect spans to Cloudflare spans. It records scalar attributes, failures and interruption.

The native API cannot assign an explicit parent to each Effect span. Those spans appear as siblings under `jobs.scheduled`, alongside platform fetch and Hyperdrive spans. Node execution runs without this tracer. Local successful jobs do not prove that production custom spans are visible in the Cloudflare dashboard.

## Diagnose a failure [#diagnose-a-failure]

Trace the entry point, transport result and durable row together. For an import, inspect its state and persisted failure reason before assuming the queue lost it. For a webhook, inspect the delivery lease and next attempt. For a client error, keep the stable error code and correlation context rather than exposing an exception message.

Bundle and fault experiments are recorded in `docs/measurements`. Their local timings are not production latency or billing measurements. Add a dated report when changing runtime ownership, deadlines or a heavy dependency.

## Key files [#key-files]

- [`packages/api/src/platform/rpc/request-id.ts`](https://github.com/CarlosZiegler/varu/blob/main/packages/api/src/platform/rpc/request-id.ts)
- [`packages/errors/src/server.ts`](https://github.com/CarlosZiegler/varu/blob/main/packages/errors/src/server.ts)
- [`packages/api/src/platform/effect/tracer.ts`](https://github.com/CarlosZiegler/varu/blob/main/packages/api/src/platform/effect/tracer.ts)
- [`packages/db/src/audit.ts`](https://github.com/CarlosZiegler/varu/blob/main/packages/db/src/audit.ts)
- [`apps/web/wrangler.jsonc`](https://github.com/CarlosZiegler/varu/blob/main/apps/web/wrangler.jsonc)

