Skip to content

feat(observability): expose Prometheus /metrics for the API and grading pipeline (#488) - #491

Merged
ArthurCRodrigues merged 7 commits into
mainfrom
feat/488
Nov 22, 2026
Merged

ArthurCRodrigues merged 7 commits into
mainfrom
feat/488

Conversation

@jaoppb

@jaoppb jaoppb commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Closes #488
Part of webtech-network/prisma-infrastructure#92

What

GET /metrics (Prometheus text format) on autograder-api and autograder-sandbox-manager. Both are public like /health and must stay on the private network. They carry counts and durations with closed-set labels only: no IDs, paths, tokens or student data. Full reference: docs/operate/metrics.md.

Area Metrics
HTTP (both services) http_server_requests_total{method,route,status}, http_server_request_duration_seconds{method,route}, http_server_requests_in_progress{method}; route is the matched template or unmatched; a client disconnect counts as 499
Grading (api) autograder_submissions_total{status,language,error_category} once per final outcome, autograder_grading_failures_total{category,code,retryable} per failed attempt, autograder_grading_duration_seconds{language,status} (0.5–600 s), autograder_grading_in_progress
Queue (api) autograder_queue_submissions{state=pending,processing,retry_wait}, autograder_queue_oldest_age_seconds, autograder_queue_up
DB pool (api) autograder_db_pool_{size,checked_out,checked_in,overflow,max_overflow}, autograder_db_pool_connections_created_total, autograder_db_pool_connect_duration_seconds
Sandboxes (sandbox-manager; api in local mode) autograder_sandboxes{language,state}, autograder_sandbox_pool_target{language}, autograder_sandbox_budget_{limit,held,waiting}

Design notes

  • Uses prometheus_client only, added to the sandbox extra. Each app has its own CollectorRegistry; there is no global registry.
  • One pure-ASGI middleware, sandbox_manager/metrics.py, serves both apps and follows the existing dependency direction. It is registered outermost, so rejections by other middleware (413, 401) are counted.
  • The queue is read with one grouped query over the indexed open statuses:
    • Concurrent scrapes share one read and reuse it for 5 s, so the database sees at most one query per 5 s per process, however often the route is requested.
    • A 5 s client timeout plus PostgreSQL SET LOCAL statement_timeout bound the query.
    • Ages use the database clock.
    • A failed read reports autograder_queue_up 0 and logs a warning once per outage.
  • /metrics stays out of OpenAPI. The auth contract test classifies it as public.
  • No checkout-timeout counter: SQLAlchemy has no hook for it without overriding private pool internals. Saturation shows as checked_out / (size + max_overflow).
  • Edits to sandbox_manager/api.py, web/main.py and web/service/worker.py only add the route, the middleware and the metric calls. Logging setup is untouched (AG-1 [Observability][AG-1] Sanitize Logs & Emit Structured JSON Logs from autograder-sandbox-manager #487 also edits those files).

Deployment (for INF-5)

  • Two scrape targets: api:8000/metrics and sandbox-manager:8001/metrics.
  • The TLS proxy in front of api must not forward /metrics. See docs/operate/deployment.md.

Verification

  • make test: 1676 passed.
  • make test-web: 512 passed.
  • PostgreSQL tier: 35 passed, including a queue-query test on PostgreSQL 17.
  • make contracts docs: OK.
  • pylint: the remaining E1136 errors are pre-existing repo-wide. The CI Python fix PR handles them.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 21, 2026 19:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@ArthurCRodrigues

Copy link
Copy Markdown
Member

Merged main into this branch twice to clear the conflicts: with #490 (7c9b22b), then with #494 after it merged.

make verify passes locally on the result (1727 unit, 528 web tests, lint, contracts, docs). The pushes dismissed the earlier approval, so this needs a new one.

🤖 Generated with Claude Code

…ng pipeline (#488)

GET /metrics on autograder-api and autograder-sandbox-manager (public like
/health, private network only): HTTP golden signals by route template,
grading throughput/failures/duration, the database queue backlog, SQLAlchemy
pool gauges and connect timing, and sandbox pool/budget gauges.
…oaden metrics tests (#488)

Concurrent scrapes share one queue read reused for 5 s, with a PostgreSQL
statement_timeout and the database clock; queue gauges come from one snapshot;
one 'other' label spelling; 499 for client disconnects; derived error
categories; docs, CHANGELOG and tests for review round 1.
Share the <unmatched> route label with logs and spans, log successful
/metrics scrapes at DEBUG, and exclude /metrics from the control-API client
route-template check (the API never calls it).
@ArthurCRodrigues
ArthurCRodrigues merged commit 8130d8b into main Nov 22, 2026
15 of 16 checks passed
@ArthurCRodrigues
ArthurCRodrigues deleted the feat/488 branch November 22, 2026 01:10
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.

[Observability][AG-2] Expose Prometheus /metrics for Autograder API & Grading Pipeline

3 participants