feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31)

* 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.

Co-authored-by: Cursor <cursoragent@cursor.com>

* 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.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: enable Grafana Assistant Cursor plugin

Co-authored-by: Cursor <cursoragent@cursor.com>

* 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: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Sully
2026-05-20 15:12:21 +08:00
committed by GitHub
parent 0d417331fd
commit fa42757916
85 changed files with 3894 additions and 405 deletions

View File

@@ -108,10 +108,32 @@ def _stdlib_emit_display(log_record: logging.LogRecord) -> tuple[str, int]:
return fn, ln
def _merge_trace_context(record: Any) -> None:
"""每条日志合并当前 OTel trace/span覆盖 Celery/后台无 HTTP middleware 的场景)。"""
try:
from app.core.telemetry import current_trace_context
ctx = current_trace_context()
if not ctx:
return
except Exception:
return
ex = record["extra"]
for k, v in ctx.items():
if not v:
continue
cur = ex.get(k)
if cur is None or str(cur).strip() in ("", "-"):
ex[k] = v
def _stderr_format(record: Any) -> str:
"""控制台 sinkrequest_id / correlation_id / user_id 有值时才显示对应列。"""
"""控制台 sinkrequest_id / correlation_id / user_id / trace_id 有值时才显示对应列。"""
rid = str(record["extra"].get("request_id") or "").strip()
rid_part = "<dim>rid={extra[request_id]}</dim> | " if rid and rid != "-" else ""
tid = str(record["extra"].get("trace_id") or "").strip()
tid_short = tid[:12] if len(tid) > 12 else tid
tid_part = f"<dim>tid={tid_short}</dim> | " if tid else ""
cid = str(record["extra"].get("correlation_id") or "").strip()
cid_part = "<dim>corr={extra[correlation_id]}</dim> | " if cid else ""
uid = str(record["extra"].get("user_id") or "").strip()
@@ -120,7 +142,7 @@ def _stderr_format(record: Any) -> str:
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level.name: <8}</level> | "
"<cyan>{extra[module]}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
f"{rid_part}{cid_part}{uid_part}"
f"{rid_part}{tid_part}{cid_part}{uid_part}"
"<level>{message}</level>\n{exception}"
)
@@ -242,8 +264,8 @@ def setup_logging() -> None:
enqueue=True,
)
logger.configure(extra={"request_id": "-", "module": "-"})
logger = logger.patch(_merge_celery_worker_extra)
logger.configure(extra={"request_id": "-", "module": "-", "trace_id": "", "span_id": ""})
logger = logger.patch(_merge_celery_worker_extra).patch(_merge_trace_context)
# 仅 root 挂 InterceptHandler避免子 logger 与 root 各处理一次导致重复行
root = logging.getLogger()