[Technical Debt] Request ID Middleware Not Implemented #9

Closed
opened 2026-03-10 03:41:46 +00:00 by Mark · 0 comments
Owner

Overview

According to docs/error_handling.md, request ID middleware is documented but not implemented. This is important for request tracing and debugging.

Current State

  • ❌ No request ID middleware
  • ❌ No X-Request-Id response header
  • ❌ No structured logging with correlation IDs
  • ⚠️ Error responses don't include request_id

Required Implementation

1. Request ID Middleware

Create middleware that:

  • Checks for X-Request-Id header from client
  • Generates new UUID if not provided
  • Sets X-Request-Id response header
  • Stores request_id in request state

2. Integration Points

  • Add to middleware stack (outermost layer)
  • Integrate with logging
  • Include in error responses

3. Structured Logging

  • Add request_id to all log entries
  • Include method, path, status_code, duration_ms
  • Include user_id and role if authenticated

4. Error Response Updates

Update error envelope to include:

{
  "detail": "human readable message",
  "code": "error_code",
  "request_id": "uuid-string"
}

Middleware Order (Target)

  1. CORS (existing)
  2. Request ID middleware (NEW)
  3. Authn/Authz
  4. Exception handler
  5. Router handlers

Acceptance Criteria

  • Request ID middleware implemented
  • X-Request-Id header echoed in responses
  • Structured logging with correlation IDs
  • Error responses include request_id
  • All log entries include request context
  • Documentation updated
  • docs/error_handling.md - Phase 1 Implementation Checklist

Priority

🟡 MEDIUM

Effort Estimate

Low (1 day)

## Overview According to `docs/error_handling.md`, request ID middleware is documented but not implemented. This is important for request tracing and debugging. ## Current State - ❌ No request ID middleware - ❌ No `X-Request-Id` response header - ❌ No structured logging with correlation IDs - ⚠️ Error responses don't include request_id ## Required Implementation ### 1. Request ID Middleware Create middleware that: - Checks for `X-Request-Id` header from client - Generates new UUID if not provided - Sets `X-Request-Id` response header - Stores request_id in request state ### 2. Integration Points - Add to middleware stack (outermost layer) - Integrate with logging - Include in error responses ### 3. Structured Logging - Add request_id to all log entries - Include method, path, status_code, duration_ms - Include user_id and role if authenticated ### 4. Error Response Updates Update error envelope to include: ```json { "detail": "human readable message", "code": "error_code", "request_id": "uuid-string" } ``` ## Middleware Order (Target) 1. CORS (existing) 2. **Request ID middleware** (NEW) 3. Authn/Authz 4. Exception handler 5. Router handlers ## Acceptance Criteria - [ ] Request ID middleware implemented - [ ] `X-Request-Id` header echoed in responses - [ ] Structured logging with correlation IDs - [ ] Error responses include request_id - [ ] All log entries include request context - [ ] Documentation updated ## Related Documents - `docs/error_handling.md` - Phase 1 Implementation Checklist ## Priority 🟡 MEDIUM ## Effort Estimate Low (1 day)
Mark 2026-05-28 02:38:41 +00:00
Sign in to join this conversation.
No description provided.