Files
life-echo/api/app/core/business_telemetry.py
Sully fa42757916 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>
2026-05-20 15:12:21 +08:00

82 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
业务链路 OpenTelemetry span回忆录阶段、WS、外部依赖等
"""
from __future__ import annotations
import time
from contextlib import contextmanager
from typing import Any, Iterator
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
from app.core.config import settings
from app.core.telemetry import get_meter, get_tracer
_meter = None
_duration_hist = None
# 仅低基数字段进入 span attribute禁止 user_id / conversation_id 等)
_ALLOWED_SPAN_ATTRS = frozenset(
{"provider", "chapter_category", "segment_count", "batch_size", "hours"}
)
def _ensure_instruments() -> None:
global _meter, _duration_hist
if _meter is not None or not settings.otel_enabled:
return
_meter = get_meter("app.business")
_duration_hist = _meter.create_histogram(
"business.operation.duration",
unit="ms",
description="Business operation wall time",
)
def _normalize_attr_value(value: Any) -> str | int | float | bool:
if isinstance(value, (str, int, float, bool)):
return value
return str(value)
@contextmanager
def business_span(
name: str,
/,
**attributes: Any,
) -> Iterator[trace.Span]:
if not settings.otel_enabled:
yield trace.INVALID_SPAN
return
tracer = get_tracer("app.business")
otel_attrs = {
f"business.{k}": _normalize_attr_value(v)
for k, v in attributes.items()
if k in _ALLOWED_SPAN_ATTRS and v is not None and v != ""
}
t0 = time.perf_counter()
outcome = "ok"
with tracer.start_as_current_span(name, attributes=otel_attrs) as span:
try:
yield span
except Exception:
outcome = "error"
if span.is_recording():
span.set_status(Status(StatusCode.ERROR))
raise
finally:
duration_ms = (time.perf_counter() - t0) * 1000
if span.is_recording():
span.set_attribute("business.duration_ms", round(duration_ms, 2))
if outcome == "ok":
span.set_status(Status(StatusCode.OK))
_ensure_instruments()
if _duration_hist is not None:
_duration_hist.record(
duration_ms,
{"operation": name, "outcome": outcome},
)