2026-03-18 17:18:23 +08:00
|
|
|
|
"""
|
|
|
|
|
|
HTTP 中间件:request_id 注入。
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
2026-05-22 13:44:50 +08:00
|
|
|
|
from starlette.datastructures import State
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from starlette.requests import Request
|
2026-05-22 13:44:50 +08:00
|
|
|
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.core.logging import logger
|
feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31) (#32)
* add staging ios app build script
* feat(api): add OpenTelemetry LGTM stack for local observability
Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.
* feat: expand observability, harden dev tooling, and fix expo staging UX
Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.
Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.
Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.
* chore: enable Grafana Assistant Cursor plugin
* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp
Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.
---------
Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:14:13 +08:00
|
|
|
|
from app.core.telemetry import current_trace_context
|
2026-04-08 15:37:09 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-05-22 13:44:50 +08:00
|
|
|
|
class RequestIdMiddleware:
|
|
|
|
|
|
"""Inject request_id into request.state and response headers, bind to loguru context.
|
|
|
|
|
|
|
|
|
|
|
|
Pure ASGI middleware (not BaseHTTPMiddleware) so FastAPI exception handlers still run.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, app: ASGIApp) -> None:
|
|
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
|
|
|
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
|
|
|
|
if scope["type"] != "http":
|
|
|
|
|
|
await self.app(scope, receive, send)
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
request_id = None
|
|
|
|
|
|
for name, value in scope.get("headers", []):
|
|
|
|
|
|
if name == b"x-request-id":
|
|
|
|
|
|
request_id = value.decode("latin-1")
|
|
|
|
|
|
break
|
|
|
|
|
|
if not request_id:
|
|
|
|
|
|
request_id = str(uuid.uuid4())
|
|
|
|
|
|
existing_state = scope.get("state")
|
|
|
|
|
|
if isinstance(existing_state, State):
|
|
|
|
|
|
existing_state.request_id = request_id
|
|
|
|
|
|
elif isinstance(existing_state, dict):
|
|
|
|
|
|
existing_state["request_id"] = request_id
|
|
|
|
|
|
scope["state"] = State(existing_state)
|
|
|
|
|
|
else:
|
|
|
|
|
|
scope["state"] = State({"request_id": request_id})
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31) (#32)
* add staging ios app build script
* feat(api): add OpenTelemetry LGTM stack for local observability
Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.
* feat: expand observability, harden dev tooling, and fix expo staging UX
Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.
Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.
Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.
* chore: enable Grafana Assistant Cursor plugin
* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp
Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.
---------
Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:14:13 +08:00
|
|
|
|
bind = {"request_id": request_id, **current_trace_context()}
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-05-22 13:44:50 +08:00
|
|
|
|
async def send_with_request_id(message: Message) -> None:
|
|
|
|
|
|
if message["type"] == "http.response.start":
|
|
|
|
|
|
headers = list(message.get("headers", []))
|
|
|
|
|
|
headers.append((b"x-request-id", request_id.encode("latin-1")))
|
|
|
|
|
|
message = {**message, "headers": headers}
|
|
|
|
|
|
await send(message)
|
|
|
|
|
|
|
|
|
|
|
|
with logger.contextualize(**bind):
|
|
|
|
|
|
await self.app(scope, receive, send_with_request_id)
|