Serve Prometheus metrics on a METRICS_PORT listener of its own #16
No reviewers
Labels
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
sophie/simpleplayer-svc!16
Loading…
Reference in a new issue
No description provided.
Delete branch "pixel/297-serve-prometheus-metrics-on-a-metrics"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pixel task #297. The server serves no metrics today, and
/metricson 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 VaporHTTPServeron that port that serves onlyGET /metrics; everything else there is a plain 404. Validated with the other variables (1 to 65535, notPORT). A port that cannot be bound fails startup with a readable message./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.Package.resolved.Series
http_requests_total{method,path,status},http_request_errors_total,http_request_duration_seconds(pathis the route pattern). Soto addsaws_requests_total,aws_request_errors,aws_request_durationby 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), histogramsimpleplayer_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 inMetricsTests, 4 inConfigTests), run repeatedly without a flaky run.PORT=28081 METRICS_PORT=29465:/metricson the main port 404,/healthz200; on the metrics port/healthzandPOST /metrics404,GET /metrics200 astext/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'shttp_requests_totalwith route patterns. Scrapes are not in the info log and not inhttp_requests_total.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
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 buildandswift test: 125 tests in 26 suites pass.PORTandMETRICS_PORTon a fresh database. On the main port,/metricsgives 404, while SPA routes, static files and/apigive 200. The metrics port renders everysimpleplayer_*series at 0, and Vapor'shttp_requests_total/http_request_duration_secondscarry route-pattern labels (/**,vapor_route_undefined), so theMetricsSystembootstrap reaches the same registry.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.@ -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 aNit, not blocking: "Every series is there from the start, at 0" is true of the
simpleplayer_*series only. Vapor'shttp_*and Soto'saws_*series appear when the first request (or S3 call) creates them. I scraped a fresh server and saw exactly that. Suggest: "Everysimpleplayer_*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() }Worth knowing, not blocking: these counts go through
services.db, the single connection. While a scan'sLibraryScan.applytransaction holds that connection, a scrape waits for the whole transaction. On a large bucket, if that takes longer than the scraper'sscrape_timeout(10 s by default), the scrape fails andupdrops 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.