Serve Prometheus metrics on a METRICS_PORT listener of its own #16

Merged
sophie merged 2 commits from pixel/297-serve-prometheus-metrics-on-a-metrics into main 2026-11-30 21:40:05 +11:00
Collaborator

Pixel task #297. The server serves no metrics today, and /metrics on 8080 is the SPA's catch-all page. It has no login, so metrics get a listener of their own.

What changes

  • METRICS_PORT (unset by default): a second Vapor HTTPServer on that port that serves only GET /metrics; everything else there is a plain 404. Validated with the other variables (1 to 65535, not PORT). A port that cannot be bound fails startup with a readable message.
  • Unset: no listener, and swift-metrics is not bootstrapped.
  • On the public port, /metrics (and anything under it) is now a plain 404 instead of the SPA page, so a scraper pointed at the wrong port does not get a 200 with HTML. docs/ADMIN_API.md, "Other routes", says so.
  • New dependencies: swift-prometheus 2.4.1; swift-metrics moves from 2.11.0 to 2.13.1 in Package.resolved.

Series

  • From Vapor, for the public port only: http_requests_total{method,path,status}, http_request_errors_total, http_request_duration_seconds (path is the route pattern). Soto adds aws_requests_total, aws_request_errors, aws_request_duration by service and operation.
  • simpleplayer_devices_connected, simpleplayer_admin_clients, simpleplayer_devices_registered, simpleplayer_media, simpleplayer_media_unprobed: counted at scrape time (three small queries on the one database connection).
  • simpleplayer_device_messages_total{type}: hello, status, playlistRequest, downloadUrlRequest, unknown, invalid.
  • simpleplayer_library_scans_total{outcome} (success, failed), histogram simpleplayer_library_scan_duration_seconds, simpleplayer_library_scan_last_objects, simpleplayer_library_scan_last_success_timestamp_seconds. A scan cut short by shutdown is not counted.
  • simpleplayer_outbox_queued_frames: text frames queued for device and admin WebSockets and not yet written.
  • simpleplayer_s3_errors_total{operation}: list, presign.

Label values are fixed sets; nothing from a request, a Mac or S3 becomes one. Every series exists at 0 from the first scrape.

Checked

  • swift test: 125 tests in 26 suites pass (13 new in MetricsTests, 4 in ConfigTests), run repeatedly without a flaky run.
  • The built binary with PORT=28081 METRICS_PORT=29465: /metrics on the main port 404, /healthz 200; on the metrics port /healthz and POST /metrics 404, GET /metrics 200 as text/plain; version=0.0.4. After a registration and a scan against an S3 that refuses the credentials: simpleplayer_devices_registered 1, simpleplayer_library_scans_total{outcome="failed"} 1, simpleplayer_s3_errors_total{operation="list"} 1, and Vapor's http_requests_total with route patterns. Scrapes are not in the info log and not in http_requests_total.
  • A second instance on the same metrics port exits with Startup failed: cannot listen for metrics on 0.0.0.0:29464 (METRICS_PORT): … Address already in use.

Not checked here: the Docker image build and Linux (the pull request's CI builds the image and runs the tests in it).

Nothing is scraped until the deployment in sophie/homelab sets METRICS_PORT; that is a follow-up task.

🤖 Generated with Claude Code

Pixel task #297. The server serves no metrics today, and `/metrics` on 8080 is the SPA's catch-all page. It has no login, so metrics get a listener of their own. ## What changes - `METRICS_PORT` (unset by default): a second Vapor `HTTPServer` on that port that serves only `GET /metrics`; everything else there is a plain 404. Validated with the other variables (1 to 65535, not `PORT`). A port that cannot be bound fails startup with a readable message. - Unset: no listener, and swift-metrics is not bootstrapped. - On the public port, `/metrics` (and anything under it) is now a plain 404 instead of the SPA page, so a scraper pointed at the wrong port does not get a 200 with HTML. `docs/ADMIN_API.md`, "Other routes", says so. - New dependencies: swift-prometheus 2.4.1; swift-metrics moves from 2.11.0 to 2.13.1 in `Package.resolved`. ## Series - From Vapor, for the public port only: `http_requests_total{method,path,status}`, `http_request_errors_total`, `http_request_duration_seconds` (`path` is the route pattern). Soto adds `aws_requests_total`, `aws_request_errors`, `aws_request_duration` by service and operation. - `simpleplayer_devices_connected`, `simpleplayer_admin_clients`, `simpleplayer_devices_registered`, `simpleplayer_media`, `simpleplayer_media_unprobed`: counted at scrape time (three small queries on the one database connection). - `simpleplayer_device_messages_total{type}`: `hello`, `status`, `playlistRequest`, `downloadUrlRequest`, `unknown`, `invalid`. - `simpleplayer_library_scans_total{outcome}` (`success`, `failed`), histogram `simpleplayer_library_scan_duration_seconds`, `simpleplayer_library_scan_last_objects`, `simpleplayer_library_scan_last_success_timestamp_seconds`. A scan cut short by shutdown is not counted. - `simpleplayer_outbox_queued_frames`: text frames queued for device and admin WebSockets and not yet written. - `simpleplayer_s3_errors_total{operation}`: `list`, `presign`. Label values are fixed sets; nothing from a request, a Mac or S3 becomes one. Every series exists at 0 from the first scrape. ## Checked - `swift test`: 125 tests in 26 suites pass (13 new in `MetricsTests`, 4 in `ConfigTests`), run repeatedly without a flaky run. - The built binary with `PORT=28081 METRICS_PORT=29465`: `/metrics` on the main port 404, `/healthz` 200; on the metrics port `/healthz` and `POST /metrics` 404, `GET /metrics` 200 as `text/plain; version=0.0.4`. After a registration and a scan against an S3 that refuses the credentials: `simpleplayer_devices_registered 1`, `simpleplayer_library_scans_total{outcome="failed"} 1`, `simpleplayer_s3_errors_total{operation="list"} 1`, and Vapor's `http_requests_total` with route patterns. Scrapes are not in the info log and not in `http_requests_total`. - A second instance on the same metrics port exits with `Startup failed: cannot listen for metrics on 0.0.0.0:29464 (METRICS_PORT): … Address already in use`. Not checked here: the Docker image build and Linux (the pull request's CI builds the image and runs the tests in it). Nothing is scraped until the deployment in sophie/homelab sets `METRICS_PORT`; that is a follow-up task. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With METRICS_PORT set, a second Vapor HTTPServer serves only GET /metrics;
the public port answers /metrics with a plain 404 instead of the SPA page.
Unset, there is no listener and swift-metrics is not bootstrapped.

The simpleplayer_* series: devices connected and registered, admin clients,
device messages by type, library scans (outcome, duration, objects, last
success), outbox frames queued, media and media waiting for a probe, and
S3 errors by operation. Vapor's http_* series come with the bootstrap.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Describe the metrics listener in the README and CLAUDE.md
All checks were successful
PR Checks / build-and-test (pull_request) Successful in 8m19s
548fba223e
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
pixel-board left a comment

Approve. Read the whole diff and the Vapor (HTTPServer configuration storage, DefaultResponder metrics, asyncBoot idempotence) and swift-prometheus (label sanitising, registration checks) code it relies on.

Ran:

  • swift build and swift test: 125 tests in 26 suites pass.
  • The built binary with PORT and METRICS_PORT on a fresh database. On the main port, /metrics gives 404, while SPA routes, static files and /api give 200. The metrics port renders every simpleplayer_* series at 0, and Vapor's http_requests_total / http_request_duration_seconds carry route-pattern labels (/**, vapor_route_undefined), so the MetricsSystem bootstrap reaches the same registry.
  • With the metrics port already bound, startup exits 1 with Startup failed: cannot listen for metrics on 0.0.0.0:<port> (METRICS_PORT): … Address already in use.

Nothing must change. Two line comments, both optional: the README overstates which series exist from the start, and scrapes wait on the single DB connection while a scan transaction holds it. The entrypoint wiring (bootstrap onto the registry handed to configure) has no automated test. I checked it by hand as above.

Review by pixel: task #423, round 1 of 3, commit 548fba223e.

Approve. Read the whole diff and the Vapor (HTTPServer configuration storage, DefaultResponder metrics, asyncBoot idempotence) and swift-prometheus (label sanitising, registration checks) code it relies on. Ran: - `swift build` and `swift test`: 125 tests in 26 suites pass. - The built binary with `PORT` and `METRICS_PORT` on a fresh database. On the main port, `/metrics` gives 404, while SPA routes, static files and `/api` give 200. The metrics port renders every `simpleplayer_*` series at 0, and Vapor's `http_requests_total` / `http_request_duration_seconds` carry route-pattern labels (`/**`, `vapor_route_undefined`), so the `MetricsSystem` bootstrap reaches the same registry. - With the metrics port already bound, startup exits 1 with `Startup failed: cannot listen for metrics on 0.0.0.0:<port> (METRICS_PORT): … Address already in use`. Nothing must change. Two line comments, both optional: the README overstates which series exist from the start, and scrapes wait on the single DB connection while a scan transaction holds it. The entrypoint wiring (bootstrap onto the registry handed to `configure`) has no automated test. I checked it by hand as above. Review by pixel: task #423, round 1 of 3, commit 548fba223e5cd76f4574241765a3964c50dea472.
@ -67,0 +92,4 @@
written
- `simpleplayer_s3_errors_total{operation}`: `list`, `presign`
Every series is there from the start, at 0. Label values are fixed sets; nothing from a
Collaborator

Nit, not blocking: "Every series is there from the start, at 0" is true of the simpleplayer_* series only. Vapor's http_* and Soto's aws_* series appear when the first request (or S3 call) creates them. I scraped a fresh server and saw exactly that. Suggest: "Every simpleplayer_* series is there from the start, at 0."

Nit, not blocking: "Every series is there from the start, at 0" is true of the `simpleplayer_*` series only. Vapor's `http_*` and Soto's `aws_*` series appear when the first request (or S3 call) creates them. I scraped a fresh server and saw exactly that. Suggest: "Every `simpleplayer_*` series is there from the start, at 0."
@ -0,0 +208,4 @@
logger.warning("Metrics: counting \(what) failed: \(String(reflecting: error))")
}
}
await count("devices", into: metrics.devicesRegistered) { try await Device.query(on: db).count() }
Collaborator

Worth knowing, not blocking: these counts go through services.db, the single connection. While a scan's LibraryScan.apply transaction holds that connection, a scrape waits for the whole transaction. On a large bucket, if that takes longer than the scraper's scrape_timeout (10 s by default), the scrape fails and up drops for the length of the scan. If that shows up in practice, one fix is to keep the last counts and render those whenever the connection is busy. A short note in the README would also do.

Worth knowing, not blocking: these counts go through `services.db`, the single connection. While a scan's `LibraryScan.apply` transaction holds that connection, a scrape waits for the whole transaction. On a large bucket, if that takes longer than the scraper's `scrape_timeout` (10 s by default), the scrape fails and `up` drops for the length of the scan. If that shows up in practice, one fix is to keep the last counts and render those whenever the connection is busy. A short note in the README would also do.
sophie merged commit 0e86050011 into main 2026-11-30 21:40:05 +11:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sophie/simpleplayer-svc!16
No description provided.