Frontend (17 items): - Virtualized message list with batch loading - CSS split with skeleton, drawer, search filter, message action styles - Code splitting via React.lazy + Suspense for Admin/Ethics/Settings pages - Skeleton loading components (Skeleton, SkeletonCard, SkeletonGrid) - Debounced search/filter component (SearchFilter) - Error boundary with fallback UI - Keyboard shortcuts (Ctrl+K search, Ctrl+Enter send, Escape dismiss) - Page transition animations (fade-in) - PWA support (manifest.json + service worker) - WebSocket auto-reconnect with exponential backoff (10 retries) - Chat history persistence to localStorage (500 msg limit) - Message edit/delete on hover - Copy-to-clipboard on code blocks - Mobile drawer (bottom-sheet for consensus panel) - File upload support - User preferences sync to backend Testing (8 items): - Component tests: Toast, Markdown, ChatMessage, Avatar, ErrorBoundary, Skeleton - Hook tests: useChatHistory - E2E smoke tests (5 tests) - Accessibility audit utility Backend (12 items): - Vector memory with cosine similarity search - TTS/STT adapter factory wiring - Geometry kernel with orphan detection - Tenant registry with CRUD operations - Response cache with TTL - Connection pool (async) - Background task queue - Health check endpoints (/health, /ready) - Request tracing middleware (X-Request-ID) - API key rotation mechanism - Environment-based config (settings.py) - API route documentation improvements Infrastructure (4 items): - Grafana dashboard template - Database migration system - Storybook configuration Documentation (3 items): - ADR-001: Advisory Governance Model - ADR-002: Twelve-Head Architecture - ADR-003: Consequence Engine 552 Python tests + 45 frontend tests passing, 0 ruff errors. Co-Authored-By: Nakamoto, S <defi@defi-oracle.io>
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""Request tracing middleware for structured logging with correlation IDs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextvars
|
|
import uuid
|
|
from typing import Any
|
|
|
|
trace_id_var: contextvars.ContextVar[str] = contextvars.ContextVar("trace_id", default="")
|
|
|
|
|
|
def get_trace_id() -> str:
|
|
"""Get current trace ID from context."""
|
|
return trace_id_var.get() or ""
|
|
|
|
|
|
def set_trace_id(trace_id: str) -> None:
|
|
"""Set trace ID in current context."""
|
|
trace_id_var.set(trace_id)
|
|
|
|
|
|
def generate_trace_id() -> str:
|
|
"""Generate a new trace ID."""
|
|
return str(uuid.uuid4())[:8]
|
|
|
|
|
|
class TracingMiddleware:
|
|
"""ASGI middleware that sets/propagates request trace IDs.
|
|
|
|
Extracts trace ID from X-Request-ID header or generates a new one.
|
|
Injects trace ID into response headers and logging context.
|
|
"""
|
|
|
|
def __init__(self, app: Any, header_name: str = "X-Request-ID") -> None:
|
|
self.app = app
|
|
self.header_name = header_name.lower()
|
|
|
|
async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None:
|
|
"""ASGI entrypoint."""
|
|
if scope["type"] not in ("http", "websocket"):
|
|
await self.app(scope, receive, send)
|
|
return
|
|
|
|
headers = dict(scope.get("headers", []))
|
|
trace_id = ""
|
|
for k, v in headers.items():
|
|
if isinstance(k, bytes) and k.decode("latin-1").lower() == self.header_name:
|
|
trace_id = v.decode("latin-1") if isinstance(v, bytes) else str(v)
|
|
break
|
|
|
|
if not trace_id:
|
|
trace_id = generate_trace_id()
|
|
|
|
set_trace_id(trace_id)
|
|
|
|
async def send_with_trace(message: dict[str, Any]) -> None:
|
|
if message["type"] == "http.response.start":
|
|
headers_list = list(message.get("headers", []))
|
|
headers_list.append((b"x-request-id", trace_id.encode()))
|
|
headers_list.append((b"x-trace-id", trace_id.encode()))
|
|
message["headers"] = headers_list
|
|
await send(message)
|
|
|
|
await self.app(scope, receive, send_with_trace)
|