Skip to content

feat: propagate request_id through pgSettings and metering context#1205

Merged
pyramation merged 1 commit into
mainfrom
feat/request-id-infrastructure
May 21, 2026
Merged

feat: propagate request_id through pgSettings and metering context#1205
pyramation merged 1 commit into
mainfrom
feat/request-id-infrastructure

Conversation

@pyramation
Copy link
Copy Markdown
Contributor

Summary

Propagates the per-request request_id (already generated by request-logger.ts middleware via X-Request-Id header or crypto.randomUUID()) into PostgreSQL session settings and the LLM metering pipeline.

Changes:

  • graphile.ts: adds request.id to pgSettings for both authenticated and anonymous GraphQL requests
  • llm-api.ts: adds request.id to getPgSettings() for REST LLM endpoints
  • metering.ts: adds requestId to MeteringContext interface; includes request_id in record_usage() billing metadata for both embed and chat operations
  • metering-plugin.ts: extracts request.id from pgSettings into the metering context

After this change, request.id is available in SQL via current_setting('request.id', true) — enabling distributed tracing and correlation across Express → GraphQL → billing → inference log layers.

Companion PR: constructive-io/constructive-db#1276 (adds request_id uuid column to inference_log_module)

Review & Testing Checklist for Human

  • Verify request.id appears in pgSettings during a real GraphQL request (check via current_setting('request.id', true) in a PG function or trigger)
  • Confirm billing record_usage metadata includes request_id — check the billing ledger jsonb after an LLM call

Notes

No new dependencies. The request_id generation already existed in request-logger.ts — this PR just wires it through to pgSettings and metering. The request.id pgSetting is the standard Constructive convention (matching jwt.claims.* pattern).

Link to Devin session: https://app.devin.ai/sessions/2b5a29d83d3f478e8d3d972653b4879c
Requested by: @pyramation

- graphile.ts: add request.id to pgSettings for both authenticated and
  anonymous requests (sourced from req.requestId, already generated by
  request-logger middleware via X-Request-Id header or crypto.randomUUID)
- llm-api.ts: add request.id to getPgSettings() for REST LLM endpoints
- metering.ts: add requestId to MeteringContext, include in record_usage
  billing metadata for correlation
- metering-plugin.ts: extract request.id from pgSettings into context

Available in SQL via current_setting('request.id', true). Enables
distributed tracing across Express → GraphQL → billing → inference log.
@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@pyramation pyramation merged commit 6e1908d into main May 21, 2026
37 checks passed
@pyramation pyramation deleted the feat/request-id-infrastructure branch May 21, 2026 02:57
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