feat: serve Prometheus metrics on a port of its own #12
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/test-token-service!12
Loading…
Reference in a new issue
No description provided.
Delete branch "pixel/300-add-a-metrics-endpoint-on-a-port-of-its"
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 #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 onlyGET /metrics. Unset or0: no listener, as before./metrics(it answers 404), and the metrics port serves nothing else.test_token_service_tokens_issued_totalalg:RS256,ES384test_token_service_token_requests_refused_totalreason:content_type,body,alg,sub,aud,expires_in,claims,server_errortest_token_service_jwks_requests_totaltest_token_service_discovery_requests_totalPlus
go_*andprocess_*. Every label value is reported from startup at 0, and no request data becomes a label value (an unsupportedalgis counted underreason="alg").Besides the feature
github.com/prometheus/client_golangv1.25.0.go mod tidyremoved the requirement ongithub.com/lestrrat-go/jwx/v4, which nothing imports (the code uses v2), and raisedgolang.org/x/systo v0.48.0.HandleToken,HandleJWKSandHandleDiscoverytake a*metrics.Metrics(nil is allowed).Checked
go vet ./...andgo test ./...pass. New tests cover each refusal reason, both algorithms, the two fetch counters, nil metrics, 404 for/metricson the main router, and 404 for everything but/metricson the metrics server.alg: nonerequest and one JWKS fetch showed astokens_issued_total{alg="ES384"} 1,token_requests_refused_total{reason="alg"} 1,jwks_requests_total 1;GET :18080/metricsgave 404.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
Approve. The change does what #300 asks:
promhttpon its own listener, off unlessTOKEN_SERVICE_METRICS_PORT/-metrics-portis set, with tokens issued byalg, refusals by a fixed set of reasons (request data never becomes a label), and JWKS and discovery fetches. EverywriteErrorpath inHandleTokennow goes throughrefuse, andTokenIssuedruns only after signing succeeds.What I ran at
abfc3a4:go vet ./...,go test -count=1 ./...(all pass),go mod tidy -diff(clean; nothing importsjwx/v4) and aCGO_ENABLED=0 GOOS=linuxbuild, which is what the Dockerfile builds. I also ran the binary: the counters moved after aPOST /tokenand a JWKS fetch,/metricson the main port gave 404,HEAD /metricson the metrics port gave 200, an equal port and-metrics-port -1both 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
envIntbehaviour every other variable already has. I didn't run the Docker build itself.Review by pixel: task #408, round 1 of 3, commit
abfc3a484c.@ -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)")Not blocking, and the same as the other variables:
envIntdrops a value it can't parse, soTOKEN_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 inenvIntwould 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 {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 aslistenMetrics(cfg) (net.Listener, error)and test it for the same port, a taken port and 0 (nil listener).