Skip to content

feat: PostgreSQL telemetry backend and consumer header tracking#4

Open
jtalborough wants to merge 1 commit intoRelayPlane:mainfrom
jtalborough:feat/pg-telemetry-backend
Open

feat: PostgreSQL telemetry backend and consumer header tracking#4
jtalborough wants to merge 1 commit intoRelayPlane:mainfrom
jtalborough:feat/pg-telemetry-backend

Conversation

@jtalborough
Copy link

Summary

  • Adds optional PostgreSQL backend for request history (replaces JSONL when RELAYPLANE_TELEMETRY_DB is set)
  • Adds X-RelayPlane-Consumer header support for identifying request sources in multi-client deployments
  • Consumer identity threaded via AsyncLocalStorage — no function signature changes needed
  • pg added as optional dependency (dynamic import, graceful fallback)

Changes

  • src/telemetry-pg.ts (new): PostgreSQL backend module with connection pooling, auto-schema creation, fire-and-forget writes
  • src/telemetry.ts: pg integration in recordTelemetry(), consumer field on TelemetryEvent
  • src/standalone-proxy.ts: AsyncLocalStorage for consumer context, CORS update, pg wiring in history/dashboard endpoints
  • package.json: pg in optionalDependencies, @types/pg in devDependencies
  • __tests__/telemetry-pg.test.ts (new): 13 tests with mocked pg.Pool

How it works

When RELAYPLANE_TELEMETRY_DB env var is set to a PostgreSQL connection string:

  • Request history is written to a request_history table instead of history.jsonl
  • Dashboard endpoints query pg for richer aggregations (by consumer, by model)
  • JSONL files are not written (no dual-write)
  • Cloud telemetry upload continues independently

Consumer header:

  • Clients send X-RelayPlane-Consumer: my-service to identify themselves
  • Consumer appears in telemetry runs and stats (including per-consumer breakdown)

Test plan

  • npm run build compiles cleanly
  • npm test — 141 pass (1 pre-existing failure in server-sandbox)
  • Without RELAYPLANE_TELEMETRY_DB, existing JSONL behavior unchanged
  • Consumer header captured and visible in /v1/telemetry/runs and /v1/telemetry/stats
  • CORS preflight includes X-RelayPlane-Consumer in allowed headers

Add an optional PostgreSQL backend that replaces both JSONL data stores
(history.jsonl and telemetry.jsonl) when RELAYPLANE_TELEMETRY_DB is set.
When unset, existing JSONL behavior is unchanged.

PostgreSQL backend (src/telemetry-pg.ts):
- pg.Pool with max 5 connections, auto-creates schema on first connect
- Async fire-and-forget writes (errors logged, never thrown to caller)
- Rich aggregation queries for dashboard endpoints (by model, consumer)

Consumer header tracking:
- X-RelayPlane-Consumer header extracted via AsyncLocalStorage (no
  signature changes to logRequest or sendCloudTelemetry)
- Consumer identity stored in request_history and telemetry events
- Dashboard /v1/telemetry/runs and /v1/telemetry/stats include consumer
- CORS headers updated to allow the new header

Also adds pg (^8.13.0) as an optional dependency and @types/pg to
devDependencies, plus 13 new tests covering the pg backend.
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