Skip to content

feat: expose Prometheus /metrics for the agent itself (#125) - #156

Merged
OkiriGabriel merged 1 commit into
mainfrom
feat/metrics-125
Nov 3, 2026
Merged

OkiriGabriel merged 1 commit into
mainfrom
feat/metrics-125

Conversation

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Description

Fixes #125.

prometheus-client was already a dependency, but nothing exposed metrics, so operators couldn't monitor or alert on the on-call agent itself.

GET /metrics (Prometheus text format, dedicated registry in services/metrics.py):

Metric Labels Recorded in
devops_agent_incidents_total type, source, outcome (resolved/unresolved/failed) queue worker, after agent.run
devops_agent_incident_duration_seconds (histogram) type same
devops_agent_queue_depth state computed at scrape time
devops_agent_tool_calls_total tool, result (ok/error/blocked/requires_approval/emergency_stop) agent loop and public execute_tool (MCP)
devops_agent_approvals_total decision Slack decision recorder (#148)
devops_agent_llm_tokens_total model, kind (incl. cache read/creation) _call_claude
devops_agent_fix_verification_total status after verify_fix
devops_agent_http_requests_total route template, method, status HTTP middleware (excludes /metrics)
devops_agent_emergency_stop none computed at scrape time

Design notes

  • Cardinality is bounded: no incident IDs, orgs, repos or hosts. Request-supplied type/source values and unknown tool names collapse to other, so a crafted webhook can't create unbounded series. HTTP requests are labelled by route template (/orgs/{org_id}/docs), not the raw path.
  • An incident counts as failed only when agent.run itself raised. If a later step (audit, escalation) fails, the incident was already counted with its real outcome, so nothing is double-counted.
  • Tool calls are counted at both call sites of _execute_tool, not inside it, because tests/test_host_mutation_approval.py parses that dispatcher's source.
  • A storage error while computing queue depth is logged and never breaks the scrape.
  • METRICS_TOKEN (optional) requires Authorization: Bearer <token>. Unset leaves /metrics open, as is usual for internal scraping; this is documented.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Documentation update

Testing

  • New tests/test_metrics.py (14 tests):
    • endpoint serves all 9 metric families; token enforced when set; emergency-stop gauge; queue-depth gauge from a real queue (1 pending, 1 processing); scrape survives a storage error
    • HTTP counted by route template, /metrics not counted
    • result labels; unknown tools and untrusted type/source become other; LLM usage; verification
    • _process_incident_with_org_creds counts resolved, unresolved and failed; public execute_tool counts requires_approval; approval decisions counted
  • Full suite: 1020 passed, 4 skipped. Flake8 critical: 0. Bandit -ll clean. mkdocs build --strict plus link check: 0 errors.

Docs

docs/GRAFANA_PROMETHEUS.md has a new "Monitoring the agent itself" section (scrape config, metric table, and alert rules for a stuck queue, high failure rate, fixes not holding, and emergency stop). docs/API_REFERENCE.md documents GET /metrics. .env.example adds METRICS_TOKEN.

🤖 Generated with Claude Code

Operators could not monitor or alert on the agent (#125).

GET /metrics serves a dedicated registry (services/metrics.py):
incidents by type/source/outcome and their duration, queue depth,
tool calls by tool and result, Slack approval decisions, Claude token
usage, fix-verification results, HTTP requests by route template and
status, and an emergency-stop gauge. Labels never carry incident IDs,
orgs, repos or hosts; request-supplied types, sources and unknown tool
names collapse to "other" so a crafted webhook cannot create unbounded
series. Queue depth and emergency stop are computed at scrape time, and
a storage error there never breaks the scrape. METRICS_TOKEN optionally
requires a bearer token.

Tool calls are counted at both call sites of _execute_tool (agent loop
and public execute_tool) rather than inside it, since tests parse that
dispatcher's source.

Docs: scrape config, metric table and example alert rules in
docs/GRAFANA_PROMETHEUS.md; endpoint in docs/API_REFERENCE.md.

Fixes #125

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@OkiriGabriel
OkiriGabriel merged commit d49d3f5 into main Nov 3, 2026
16 checks passed
@OkiriGabriel
OkiriGabriel deleted the feat/metrics-125 branch November 26, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Expose Prometheus /metrics for the agent itself (incidents, queue, tools, approvals, tokens)

1 participant