Skip to content

Trace control-plane requests and their database queries on Cloudflare - #289

Merged
tnkuehne merged 2 commits into
mainfrom
control-plane-tracing
Nov 22, 2026
Merged

tnkuehne merged 2 commits into
mainfrom
control-plane-tracing

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

On Cloudflare, Workers Logs show a request's wall time and CPU time, but not where the rest goes. Automatic Workers tracing covers fetch calls and bindings. PostgreSQL over Hyperdrive uses a TCP socket, so its queries do not appear as spans.

Changes

  • Tracing: wrangler.jsonc turns on Workers traces for the control plane. All requests are traced; Cloudflare documents no request overhead.
  • Database spans: createDatabase accepts a span function. The Cloudflare runtime passes one built on tracing.startSpan, and every pooled pg client then reports a postgres connect span for its connection and a postgres query span for each statement. The hook sits on the pg client, so Drizzle, Better Auth and raw SQL all report through it without changes where they run queries. Spans carry the statement text with its placeholders (up to 500 characters), never parameter values. A failed connection or statement marks its span as an error and records its SQLSTATE code as error.type, not the message, because PostgreSQL error messages can quote parameter values.
  • Node: Node installations pass no span function and keep the plain pg client.

Validation

  • pnpm check
  • New database-tracing.integration.test.ts, against Postgres: connection and query spans, including begin and commit; SQLSTATE codes on a failed statement and on a rejected connection; no parameter values, even when PostgreSQL quotes one in its error message; every span ended.
  • tracing.startSpan, setAttributes, setStatus and end run in local workerd with the control plane's compatibility date.
  • pnpm build:cloudflare and wrangler deploy --dry-run with the traces setting.
  • Browser tests: 93 passed, 1 skipped. The CLI login suite did not run locally because the CLI binary was not built.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; both previous findings are addressed.

Summary

Adds Cloudflare traces for database connections and statements while leaving Node's database client unchanged.

  • Cloudflare request traces now include PostgreSQL connections and queries.

Diagram

sequenceDiagram
  participant Request as Cloudflare request
  participant Client as Traced pg client
  participant Postgres as PostgreSQL
  participant Trace as Workers trace
  Request->>Client: Connect or query
  Client->>Trace: Start span
  Client->>Postgres: Run operation
  Postgres-->>Client: Result or error
  Client->>Trace: Record error type, if any
  Client->>Trace: End span
  Client-->>Request: Return result or original error
Loading

Reviews (2) · Last reviewed commit: "Record only the error type of a failed d..." · Reviewed by Greptile

Automatic Workers tracing records fetch calls and bindings, but not
PostgreSQL over Hyperdrive, because those queries use a TCP socket. The
control plane now traces every pg connection and statement. The hook sits
on the pooled pg client, so Drizzle, Better Auth and raw SQL all report
through it without changes at their call sites. Spans carry the statement
with its placeholders, never its values.

Node installations keep the untraced client.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 3579f3a Nov 22 2026, 12:42 PM

A PostgreSQL error message can quote a parameter value, such as the text
that failed an integer cast. Spans now record the SQLSTATE code, or the
error's name when there is none. Tests cover a failed parameterized
statement and a rejected connection.
@tnkuehne
tnkuehne merged commit 879b8f5 into main Nov 22, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the control-plane-tracing branch November 22, 2026 12:49
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.

1 participant