Files
ai-agent/backend/schemas/telemetry.py
gabrielvidalandClaude Opus 5.5 72a4f84902 feat(telemetry): the hub's own metrics, a browser beacon and usage events
- GET /api/metrics: per-route latency, event-loop lag, SSE fan-out, watcher
  ticks, memo builds, delta vs full reads, runs by origin (prometheus-client,
  own registry). Owner, or Prometheus with METRICS_TOKEN (scope 'metrics').
- POST /api/telemetry: web vitals, JS errors, update lag, feature events
  from the browser, bounded and checked.
- telemetry.db: per-minute hub history, hourly per-route, usage per day
  (forever) and raw events (90 d).
- GET /api/telemetry/hub and /api/telemetry/usage for the two pages.
- /api/spawn stamps meta.origin (app | script | child | mcp | …).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 16:24:24 +01:00

125 lines
3.2 KiB
Python

"""Telemetry (telemetry/): the hub's own performance (the Hub card on
/dashboards/performance) and feature usage (/dashboards/usage). Times in ms
unless named otherwise; ``t`` is epoch seconds."""
from __future__ import annotations
from typing import Optional
from .base import Schema
__all__ = [
"TelemetryIngestResult", "HubPoint", "HubTotals", "HubRoute", "HubVital",
"HubWebError", "HubNow", "TelemetryHub", "UsageFeature", "UsageWhatsNew",
"UsageOrigin", "UsagePerson", "TelemetryUsage",
]
class TelemetryIngestResult(Schema):
vitals: int
errors: int
events: int
class HubPoint(Schema):
t: int
n: int # /api requests in the bucket
err: int # of which 5xx
p50: Optional[float] = None
p95: Optional[float] = None
lagP95: Optional[float] = None # event-loop lag
lagMax: Optional[float] = None
sse: int = 0 # peak subscribers
rss: Optional[int] = None # bytes
cpu: Optional[float] = None # % of one core
class HubTotals(Schema):
n: int
err: int
p50: Optional[float] = None
p95: Optional[float] = None
max: float = 0
lagP95: Optional[float] = None
lagMax: float = 0
class HubRoute(Schema):
route: str # "GET /api/conversations"
n: int
err: int
p50: Optional[float] = None
p95: Optional[float] = None
max: float = 0
sumMs: int = 0 # total time spent answering it
class HubVital(Schema):
name: str # LCP | INP | CLS | FCP | TTFB | update
device: str # phone | tablet | desktop | all
n: int
p75: Optional[float] = None # ms (CLS: unitless)
class HubWebError(Schema):
message: str
page: str
n: int
last: Optional[float] = None
source: Optional[str] = None
class HubNow(Schema):
sse: int
class TelemetryHub(Schema):
range: str
step: int # seconds per series point
series: list[HubPoint]
totals: HubTotals
routes: list[HubRoute]
web: list[HubVital]
errors: list[HubWebError]
now: HubNow
class UsageFeature(Schema):
kind: str # view | event | api
name: str # a feature id, or "GET /api/…"
n: int
users: int
byUser: dict[str, int]
byDevice: dict[str, int] # phone | tablet | desktop | script
firstUse: dict[str, str] = {} # uid → first day ever (YYYY-MM-DD)
lastDay: Optional[str] = None
class UsageWhatsNew(Schema):
id: str
title: str
publishedAt: Optional[str] = None
seen: int
class UsageOrigin(Schema):
origin: str # app | script | cron | scheduled | child | mcp | …
n: int
byUser: dict[str, int]
class UsagePerson(Schema):
id: str
name: str
guest: bool
class TelemetryUsage(Schema):
range: str
who: str
features: list[UsageFeature]
devices: dict[str, int]
whatsNew: list[UsageWhatsNew]
runs: list[UsageOrigin]
people: list[UsagePerson]
trackedSince: Optional[str] = None