feat: serve Prometheus metrics on a port of its own #12

Merged
sophie merged 1 commit from pixel/300-add-a-metrics-endpoint-on-a-port-of-its into main 2026-11-30 21:19:45 +11:00
Collaborator

Pixel task #300 (epic E7). The service served no metrics; it can now serve them on a listener of its own.

What changes

  • TOKEN_SERVICE_METRICS_PORT (or -metrics-port) starts a second listener that serves only GET /metrics. Unset or 0: no listener, as before.
  • The main port never serves /metrics (it answers 404), and the metrics port serves nothing else.
  • The service exits at startup when the metrics port cannot be bound or equals the main port.
  • Scrapes are not written to the request log.
Metric Labels
test_token_service_tokens_issued_total alg: RS256, ES384
test_token_service_token_requests_refused_total reason: content_type, body, alg, sub, aud, expires_in, claims, server_error
test_token_service_jwks_requests_total
test_token_service_discovery_requests_total

Plus go_* and process_*. Every label value is reported from startup at 0, and no request data becomes a label value (an unsupported alg is counted under reason="alg").

Besides the feature

  • New dependency: github.com/prometheus/client_golang v1.25.0.
  • go mod tidy removed the requirement on github.com/lestrrat-go/jwx/v4, which nothing imports (the code uses v2), and raised golang.org/x/sys to v0.48.0.
  • HandleToken, HandleJWKS and HandleDiscovery take a *metrics.Metrics (nil is allowed).

Checked

  • go vet ./... and go test ./... pass. New tests cover each refusal reason, both algorithms, the two fetch counters, nil metrics, 404 for /metrics on the main router, and 404 for everything but /metrics on the metrics server.
  • Ran the binary with ports 18080 and 19090: one ES384 token, one alg: none request and one JWKS fetch showed as tokens_issued_total{alg="ES384"} 1, token_requests_refused_total{reason="alg"} 1, jwks_requests_total 1; GET :18080/metrics gave 404.
  • With the variable unset nothing listens on the metrics port; with both ports equal, or the metrics port taken, the process logs the reason and exits 1.

Not checked: the Docker build (CI builds the image only on main).

Nothing is scraped until the homelab deployment sets the variable; that is a follow-up task there.

🤖 Generated with Claude Code

Pixel task #300 (epic E7). The service served no metrics; it can now serve them on a listener of its own. ## What changes - `TOKEN_SERVICE_METRICS_PORT` (or `-metrics-port`) starts a second listener that serves only `GET /metrics`. Unset or `0`: no listener, as before. - The main port never serves `/metrics` (it answers 404), and the metrics port serves nothing else. - The service exits at startup when the metrics port cannot be bound or equals the main port. - Scrapes are not written to the request log. | Metric | Labels | |--------|--------| | `test_token_service_tokens_issued_total` | `alg`: `RS256`, `ES384` | | `test_token_service_token_requests_refused_total` | `reason`: `content_type`, `body`, `alg`, `sub`, `aud`, `expires_in`, `claims`, `server_error` | | `test_token_service_jwks_requests_total` | | | `test_token_service_discovery_requests_total` | | Plus `go_*` and `process_*`. Every label value is reported from startup at 0, and no request data becomes a label value (an unsupported `alg` is counted under `reason="alg"`). ## Besides the feature - New dependency: `github.com/prometheus/client_golang` v1.25.0. - `go mod tidy` removed the requirement on `github.com/lestrrat-go/jwx/v4`, which nothing imports (the code uses v2), and raised `golang.org/x/sys` to v0.48.0. - `HandleToken`, `HandleJWKS` and `HandleDiscovery` take a `*metrics.Metrics` (nil is allowed). ## Checked - `go vet ./...` and `go test ./...` pass. New tests cover each refusal reason, both algorithms, the two fetch counters, nil metrics, 404 for `/metrics` on the main router, and 404 for everything but `/metrics` on the metrics server. - Ran the binary with ports 18080 and 19090: one ES384 token, one `alg: none` request and one JWKS fetch showed as `tokens_issued_total{alg="ES384"} 1`, `token_requests_refused_total{reason="alg"} 1`, `jwks_requests_total 1`; `GET :18080/metrics` gave 404. - With the variable unset nothing listens on the metrics port; with both ports equal, or the metrics port taken, the process logs the reason and exits 1. Not checked: the Docker build (CI builds the image only on `main`). Nothing is scraped until the homelab deployment sets the variable; that is a follow-up task there. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
TOKEN_SERVICE_METRICS_PORT (or -metrics-port) starts a second listener
that serves only GET /metrics; unset or 0 leaves it off. The main port
never serves /metrics.

Counters: tokens issued by alg, POST /token requests refused by reason,
JWKS fetches and discovery fetches, plus the Go runtime and process
collectors, from a registry of the service's own.

go mod tidy also dropped the unused jwx/v4 requirement.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
pixel-board left a comment

Approve. The change does what #300 asks: promhttp on its own listener, off unless TOKEN_SERVICE_METRICS_PORT/-metrics-port is set, with tokens issued by alg, refusals by a fixed set of reasons (request data never becomes a label), and JWKS and discovery fetches. Every writeError path in HandleToken now goes through refuse, and TokenIssued runs only after signing succeeds.

What I ran at abfc3a4: go vet ./..., go test -count=1 ./... (all pass), go mod tidy -diff (clean; nothing imports jwx/v4) and a CGO_ENABLED=0 GOOS=linux build, which is what the Dockerfile builds. I also ran the binary: the counters moved after a POST /token and a JWKS fetch, /metrics on the main port gave 404, HEAD /metrics on the metrics port gave 200, an equal port and -metrics-port -1 both exited 1 with the reason logged, and nothing listened when the variable was unset.

There are two comments on lines, neither blocking: the startup refusals have no test, and a malformed value turns metrics off silently. The silent-off is the same envInt behaviour every other variable already has. I didn't run the Docker build itself.

Review by pixel: task #408, round 1 of 3, commit abfc3a484c.

Approve. The change does what #300 asks: `promhttp` on its own listener, off unless `TOKEN_SERVICE_METRICS_PORT`/`-metrics-port` is set, with tokens issued by `alg`, refusals by a fixed set of reasons (request data never becomes a label), and JWKS and discovery fetches. Every `writeError` path in `HandleToken` now goes through `refuse`, and `TokenIssued` runs only after signing succeeds. What I ran at abfc3a4: `go vet ./...`, `go test -count=1 ./...` (all pass), `go mod tidy -diff` (clean; nothing imports `jwx/v4`) and a `CGO_ENABLED=0 GOOS=linux` build, which is what the Dockerfile builds. I also ran the binary: the counters moved after a `POST /token` and a JWKS fetch, `/metrics` on the main port gave 404, `HEAD /metrics` on the metrics port gave 200, an equal port and `-metrics-port -1` both exited 1 with the reason logged, and nothing listened when the variable was unset. There are two comments on lines, neither blocking: the startup refusals have no test, and a malformed value turns metrics off silently. The silent-off is the same `envInt` behaviour every other variable already has. I didn't run the Docker build itself. Review by pixel: task #408, round 1 of 3, commit abfc3a484ca3b5034babc3bd0bc3712daff3fd53.
@ -21,6 +23,7 @@ func Load() *Config {
flag.StringVar(&cfg.Issuer, "issuer", os.Getenv("TOKEN_SERVICE_ISSUER"), "Issuer URL (default: http://localhost:<port>)")
flag.IntVar(&cfg.DefaultTTL, "default-ttl", envInt("TOKEN_SERVICE_DEFAULT_TTL", 3600), "Default token lifetime in seconds")
flag.IntVar(&cfg.MaxTTL, "max-ttl", envInt("TOKEN_SERVICE_MAX_TTL", 86400), "Maximum token lifetime in seconds")
flag.IntVar(&cfg.MetricsPort, "metrics-port", envInt("TOKEN_SERVICE_METRICS_PORT", 0), "Port to serve Prometheus metrics on (0 = metrics off)")
Collaborator

Not blocking, and the same as the other variables: envInt drops a value it can't parse, so TOKEN_SERVICE_METRICS_PORT=abc (or "9090 " from a sloppy manifest) starts the service with metrics silently off and no log line. I checked: nothing listens and nothing is logged. Worth knowing for the homelab follow-up; a warning in envInt would fix it for every variable, but that is outside this task.

Not blocking, and the same as the other variables: `envInt` drops a value it can't parse, so `TOKEN_SERVICE_METRICS_PORT=abc` (or `"9090 "` from a sloppy manifest) starts the service with metrics silently off and no log line. I checked: nothing listens and nothing is logged. Worth knowing for the homelab follow-up; a warning in `envInt` would fix it for every variable, but that is outside this task.
@ -16,3 +18,4 @@
func main() {
cfg := config.Load()
if cfg.MetricsPort != 0 && cfg.MetricsPort == cfg.Port {
Collaborator

Not blocking: the two startup refusals the README promises (metrics port equal to the main port, and a metrics port that can't be bound, lines 21 and 49–55) live inside main() and no test covers them. I ran both by hand and they behave as described (exit 1 with the reason logged, including -metrics-port -1). If you want them pinned down, move the check-and-listen into a function such as listenMetrics(cfg) (net.Listener, error) and test it for the same port, a taken port and 0 (nil listener).

Not blocking: the two startup refusals the README promises (metrics port equal to the main port, and a metrics port that can't be bound, lines 21 and 49–55) live inside `main()` and no test covers them. I ran both by hand and they behave as described (exit 1 with the reason logged, including `-metrics-port -1`). If you want them pinned down, move the check-and-listen into a function such as `listenMetrics(cfg) (net.Listener, error)` and test it for the same port, a taken port and 0 (nil listener).
sophie merged commit 7c0dac9245 into main 2026-11-30 21:19:45 +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/test-token-service!12
No description provided.