Observability
Anvio ships three observability surfaces from `@anvio/observability` at Phase 1 (all shipped v1.26.0-1.27.0):
2 min read
This is a migrated reference document from the original Anvio source repo. Content is preserved as-is; canonical documentation lives in the sections above in the sidebar.
Original path: docs/13-observability.md
Anvio ships three observability surfaces from @anvio/observability at Phase 1 (all shipped v1.26.0-1.27.0):
Structured logger (pino)
createLogger(name) returns a pino child logger namespaced by module. Env-tunable:
ANVIO_LOG_LEVEL—trace|debug|info|warn|error|fatal. Defaults:infowhenNODE_ENV=production, elsedebug.ANVIO_SERVICE_NAME— value emitted asserviceon every log record. Defaultanvio.
import { createLogger } from '@anvio/observability';
const log = createLogger('agents:runtime');
log.debug({ sessionId, agentId }, 'starting model call');Test hook: setRootLogger(logger | undefined) swaps the root; pass undefined to reset.
no-empty lint rule
Repo-root ESLint config enforces no-empty with allowEmptyCatch: false. All 46 existing catch blocks have content (verified v1.26.0); new empty catches fail CI lint.
Metrics registry
getMetricsRegistry() returns an in-process MetricsRegistry with counters, gauges, and histograms. toPrometheusText() renders a /metrics payload.
Per-model-call metrics (P1.S3, since v1.26.0)
Providers wrap chat + stream with withCallMetrics(providerId, model, fn) and recordStreamMetrics(providerId, model, usage, startedAtMs) — one call each in anthropic.provider.ts, openai-compatible.provider.ts, gemini.provider.ts. Emitted per call:
| Metric | Type | Labels |
|---|---|---|
anvio_tokens_input_total | counter | provider, model, channel |
anvio_tokens_output_total | counter | provider, model, channel |
anvio_tokens_total | counter | provider, model, channel |
anvio_tokens_cache_read_total | counter | provider, model, channel |
anvio_tokens_cache_creation_total | counter | provider, model, channel |
anvio_token_cost_usd_total | counter | provider, model, channel |
anvio_model_calls_total | counter | provider, model, channel |
anvio_model_call_latency_ms | histogram | provider, model, channel |
anvio_mcp_restarts_total | counter | server |
Histogram snapshot: registry.snapshotHistogram('anvio_model_call_latency_ms', labels) → \{ count, sum, min, max \}.
Token cost estimated via estimateModelCostUsd(provider, model, usage) from the ModelDescriptor registry. Unknown models silently emit token counts without cost.
Session-level audit
TokenUsageAudit (packages/platform/src/token-usage-audit.ts) subscribes to AGENT_RUN_COMPLETED events, appends audit/tokens.jsonl with \{ ts, sessionId, channel, agentId, userId, provider, model, usage, estimatedCostUsd, latencyMs \}, and forwards to MetricsRegistry.recordTokenUsage with cache-token and latency fields.
OpenTelemetry traces
initObservability(\{ serviceName, otlpEndpoint, enabled \}). Enable when ANVIO_OTEL_ENABLED=true or OTEL_EXPORTER_OTLP_ENDPOINT is set. Auto-instrumentations from @opentelemetry/auto-instrumentations-node (0.75.x since v1.25.1).
withSpan(name, attributes, fn) — helper that starts an active span, sets attributes, records OK on resolve or ERROR on throw, then ends the span.
shutdownObservability() — call on process shutdown to flush the OTLP exporter.
Deferred
- Prometheus
/metricsscrape endpoint served byapps/api— currently onlyMetricsRegistry.toPrometheusText()is available; not wired to an HTTP route. - Grafana dashboards for the shipped Anvio metrics —
configs/observability/langfuse-dashboard.jsonexists for LLM traces only. - Log-based cost aggregation dashboard.
- Distributed circuit-breaker state via substrate (ADR-0013 — currently process-local).
Last updated on