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>
This commit is contained in:
Sully
2026-05-20 15:14:13 +08:00
committed by GitHub
parent 81458c7046
commit f09ae248f9
74 changed files with 3793 additions and 375 deletions

View File

@@ -16,6 +16,7 @@ from app.core.agent_logging import (
agent_verbose_enabled,
log_agent_payload,
)
from app.core.llm_telemetry import infer_provider_model, langchain_invoke_span
from app.core.logging import get_logger
logger = get_logger(__name__)
@@ -68,29 +69,41 @@ def invoke_json_object(
sha = _prompt_sha12(prompt_for_api)
attempts = 2 if retry_empty else 1
t0 = time.perf_counter()
provider, model = infer_provider_model(llm)
last_content = ""
for attempt in range(attempts):
response = bound.invoke(prompt_for_api)
content = (getattr(response, "content", None) or "").strip()
last_content = content
if content:
if attempt > 0:
logger.info(
"json_object 空内容重试成功 agent={} prompt_sha12={}",
with langchain_invoke_span(
agent=tag,
provider=provider,
model=model,
call_type="json",
prompt_sha12=sha,
max_tokens=max_tokens,
) as tel:
for attempt in range(attempts):
response = bound.invoke(prompt_for_api)
tel["response"] = response
content = (getattr(response, "content", None) or "").strip()
last_content = content
if content:
if attempt > 0:
logger.info(
"json_object 空内容重试成功 agent={} prompt_sha12={}",
tag,
sha,
)
tel["outcome"] = "ok"
_log_json_object_done(
tag, sha, prompt_for_api, content, attempt + 1, t0, success=True
)
return content
if attempt == 0 and retry_empty:
logger.warning(
"json_object 返回空 content将重试 agent={} attempt={} prompt_sha12={}",
tag,
attempt,
sha,
)
_log_json_object_done(
tag, sha, prompt_for_api, content, attempt + 1, t0, success=True
)
return content
if attempt == 0 and retry_empty:
logger.warning(
"json_object 返回空 content将重试 agent={} attempt={} prompt_sha12={}",
tag,
attempt,
sha,
)
tel["outcome"] = "error"
logger.warning("json_object 仍为空 agent={} prompt_sha12={}", tag, sha)
_log_json_object_done(
tag, sha, prompt_for_api, last_content, attempts, t0, success=False
@@ -113,29 +126,41 @@ async def ainvoke_json_object(
sha = _prompt_sha12(prompt_for_api)
attempts = 2 if retry_empty else 1
t0 = time.perf_counter()
provider, model = infer_provider_model(llm)
last_content = ""
for attempt in range(attempts):
response = await bound.ainvoke(prompt_for_api)
content = (getattr(response, "content", None) or "").strip()
last_content = content
if content:
if attempt > 0:
logger.info(
"json_object 空内容重试成功 agent={} prompt_sha12={}",
with langchain_invoke_span(
agent=tag,
provider=provider,
model=model,
call_type="json",
prompt_sha12=sha,
max_tokens=max_tokens,
) as tel:
for attempt in range(attempts):
response = await bound.ainvoke(prompt_for_api)
tel["response"] = response
content = (getattr(response, "content", None) or "").strip()
last_content = content
if content:
if attempt > 0:
logger.info(
"json_object 空内容重试成功 agent={} prompt_sha12={}",
tag,
sha,
)
tel["outcome"] = "ok"
_log_json_object_done(
tag, sha, prompt_for_api, content, attempt + 1, t0, success=True
)
return content
if attempt == 0 and retry_empty:
logger.warning(
"json_object 返回空 content将重试 agent={} attempt={} prompt_sha12={}",
tag,
attempt,
sha,
)
_log_json_object_done(
tag, sha, prompt_for_api, content, attempt + 1, t0, success=True
)
return content
if attempt == 0 and retry_empty:
logger.warning(
"json_object 返回空 content将重试 agent={} attempt={} prompt_sha12={}",
tag,
attempt,
sha,
)
tel["outcome"] = "error"
logger.warning("json_object 仍为空 agent={} prompt_sha12={}", tag, sha)
_log_json_object_done(
tag, sha, prompt_for_api, last_content, attempts, t0, success=False