feat(multiplayer): expose allocator quota metrics

This commit is contained in:
Josh Creek
2026-09-01 18:43:55 +01:00
parent fc000e71d5
commit 9a72dd5eab
8 changed files with 254 additions and 5 deletions
+21
View File
@@ -5,6 +5,7 @@ import (
"database/sql"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
@@ -27,6 +28,7 @@ func main() {
workloadSecret := flag.String("workload-secret", os.Getenv("COSMIC_CLASH_WORKLOAD_SECRET"), "HMAC secret for control-plane-issued workload tokens (see workload/signed_token.go); must match cmd/control-plane's own --workload-secret. Unset skips minting a cosmic-clash.io/workload-token annotation entirely")
allocationQuota := flag.Int("allocation-quota", 0, "optional per-replica allocation attempts per region per quota window; zero disables this local guard")
allocationQuotaWindow := flag.Duration("allocation-quota-window", time.Minute, "window for --allocation-quota")
metricsAddr := flag.String("metrics-addr", envOrDefault("COSMIC_CLASH_ALLOCATOR_METRICS_ADDR", ":9091"), "allocator Prometheus metrics address; empty disables metrics")
flag.Parse()
if *dsn == "" || *agonesURL == "" {
fatalf("--dsn/COSMIC_CLASH_POSTGRES_DSN and --agones-url/COSMIC_CLASH_AGONES_URL are required")
@@ -62,6 +64,7 @@ func main() {
}
log.Printf("allocator: enabled per-replica regional allocation quota=%d window=%s", *allocationQuota, *allocationQuotaWindow)
}
metrics := allocator.NewMetrics()
client := agones.Client{BaseURL: *agonesURL, Namespace: *namespace, WorkloadSecret: []byte(*workloadSecret)}
worker := allocator.Worker{
Claims: store.AllocatingMatchClaims{DB: db, Transport: *transport},
@@ -70,12 +73,30 @@ func main() {
Durable: store.AllocationRegistry{DB: db},
Quota: store.AllocationQuota{DB: db},
Budget: budget,
Metrics: metrics,
Now: now,
},
Now: now,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var metricsServer *http.Server
if *metricsAddr != "" {
metricsServer = &http.Server{Addr: *metricsAddr, Handler: allocator.MetricsHandler(metrics)}
go func() {
if err := metricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("allocator: metrics server: %v", err)
}
}()
log.Printf("allocator: metrics listening on %s", *metricsAddr)
}
defer func() {
if metricsServer != nil {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = metricsServer.Shutdown(shutdownCtx)
}
}()
ticker := time.NewTicker(*interval)
defer ticker.Stop()
for {