api: add metrics-only listener - #1637
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The man page example formatting is inconsistent and the new HEAD metrics unit test asserts a response body for HEAD, which contradicts correct HTTP semantics and the system test expectations.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds support for running a dedicated “metrics-only” HTTP listener alongside the existing Aptly API listener, enabling operators to expose /metrics on a separate address without also exposing the full REST API.
Changes:
- Adds
-metrics-listen=host:porttoaptly api serveand coordinates startup/shutdown of API + metrics servers. - Restricts the dedicated listener to exact
/metricswith onlyGET/HEAD, while preserving/api/metrics(now with explicitHEADsupport). - Adds unit tests plus an end-to-end system test, and updates docs/completions.
File summaries
| File | Description |
|---|---|
| system/t12_api/metrics_listener.py | System test covering dual listeners, routing restrictions, methods, and shutdown behavior. |
| man/aptly.1 | Documents -metrics-listen and adds an example invocation. |
| completion.d/aptly | Bash completion: adds -metrics-listen= for aptly api serve. |
| completion.d/_aptly | Zsh completion: adds -metrics-listen argument. |
| cmd/api_serve.go | Implements metrics-only listener wiring, routing adapter, and coordinated shutdown. |
| cmd/api_serve_test.go | Unit tests for handler routing, listener validation/selection, shutdown, and sibling-stop behavior. |
| AUTHORS | Adds contributor entry. |
| api/router.go | Adds explicit HEAD /api/metrics route when metrics are enabled. |
| api/api_test.go | Adds API suite coverage for HEAD /api/metrics. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| .P | ||
| $ aptly api serve \-listen=:8080 $ aptly api serve \-listen=unix:///tmp/aptly\.sock | ||
| $ aptly api serve \-listen=:8080 $ aptly api serve \-listen=unix:///tmp/aptly\.sock $ aptly api serve \-listen=:8080 \-metrics\-listen=127\.0\.0\.1:9090 | ||
| . |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1637 +/- ##
==========================================
- Coverage 77.37% 77.14% -0.24%
==========================================
Files 165 165
Lines 15747 15891 +144
==========================================
+ Hits 12185 12259 +74
- Misses 2356 2421 +65
- Partials 1206 1211 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The remaining failures are all Debian 11/Bullseye jobs. Bullseye LTS ended 8-31, and its security repo metadata expired 9-7, so apt-get update fails before the project builds. Looks like all other lint and coverage build passes! |
Motivation
Aptly currently exposes Prometheus metrics through the REST API listener. Operators who need a loopback or monitoring-only scrape address must expose the API on that address too.
Changes
-metrics-listen=host:portflag toaptly api serve.GETandHEADrequests to/metricson the dedicated plain-HTTP TCP listener.enableMetricsEndpointand preserve the existing/api/metricsendpoint, includingHEADsupport.The listener intentionally provides no TLS or authentication; operators can apply those controls through network policy or a proxy.
Testing
go test ./cmd -count=10go test -race ./cmdgo test -race ./api -check.f APISuite.TestHeadMetricsgo vet ./cmd ./apigo mod verifygolangci-lintandflake8checks