Skip to content

Request context propagation across HTTP, jobs, and DB layer (#213)#222

Merged
Polliog merged 27 commits into
developfrom
feature/213-request-context-propagation
May 8, 2026
Merged

Request context propagation across HTTP, jobs, and DB layer (#213)#222
Polliog merged 27 commits into
developfrom
feature/213-request-context-propagation

Conversation

@Polliog
Copy link
Copy Markdown
Collaborator

@Polliog Polliog commented May 8, 2026

Closes #213.

Summary

  • New RequestContext primitive in @logtide/shared/context (AsyncLocalStorage-backed).
  • Fastify plugin establishes context after auth resolves (org, project, user/apiKey, requestId, ip, ua).
  • BullMQ producers transparently piggy-back _ctx on payloads; consumers wrap processors in context.run / runAsSystem.
  • Backend pg.Pool.query and pool.connect() patched to prepend /* req=... */ SQL comments — visible in slow-query logs and pg_stat_activity.
  • Reservoir engines (Timescale, ClickHouse, MongoDB) inject the same correlation in their native form (SQL comment / query_id / $comment).
  • 7 worker cron callbacks wrapped in runAsSystem('cron:<name>').
  • fetchWithContext helper for outbound HTTP (injects X-Logtide-Request-Id).

Out of scope

No callsite migration. Audit log / queue producers / webhook fetch keep their current argument-based form. Tracked as a follow-up.

Bonus fix

  • apikeys: debounce last_used updates 60s per key — eliminates ~100 update/s on a single hot row under load. Pre-existing bug surfaced during validation.

Test plan

  • Backend tests: 3941/3941 passing
  • Shared: 31/31 passing
  • Reservoir: Timescale 130/130, ClickHouse 57/57, MongoDB 35/35
  • Integration test: HTTP -> producer -> consumer requestId correlation
  • Smoke load test: 180/180 (5ms avg, 0% errors)
  • Ingestion load test (5min sustained @ 100 req/s + burst): performance-equivalent to main (avg 568ms vs 590ms, error rate 0% vs 0.47%, P95 +7.8% within noise)

Polliog added 26 commits May 7, 2026 14:00
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

@Polliog Polliog merged commit 6f2bddf into develop May 8, 2026
6 checks passed
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