feat: add API observability — request IDs, log filtering, HTTP counters
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s

- Add request-id middleware: assigns X-Request-Id (reuses upstream header
  or generates UUID), stored on res.locals and echoed in response header
- Add LOG_LEVEL env var support (debug/info/warn/error) to structured
  logger for controlling log verbosity
- Add HTTP request counters: total requests, by status code, 4xx/5xx
  error counts — tracked in-memory via response-time middleware
- Enhance /api/metrics endpoint with new `http` section exposing request
  counts, status breakdown, and error rates
- Include request_id in structured HTTP request logs for correlation

Fixes #57

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alexander Whitestone
2026-03-22 21:05:42 -04:00
parent 5954a2fdc0
commit 2e4c3df509
6 changed files with 75 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import { db, jobs, invoices } from "@workspace/db";
import { sql } from "drizzle-orm";
import { latencyHistogram, type BucketStats } from "./histogram.js";
import { requestCounters, type RequestCountsSnapshot } from "./request-counters.js";
export interface JobStateCounts {
awaiting_eval: number;
@@ -12,6 +13,7 @@ export interface JobStateCounts {
export interface MetricsSnapshot {
uptime_s: number;
http: RequestCountsSnapshot;
jobs: {
total: number;
by_state: JobStateCounts;
@@ -94,6 +96,7 @@ export class MetricsService {
return {
uptime_s: Math.floor((Date.now() - START_TIME) / 1000),
http: requestCounters.snapshot(),
jobs: {
total: jobsTotal,
by_state: byState,