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
+14 -2
View File
@@ -3,6 +3,7 @@ package allocator
import (
"context"
"errors"
"strings"
"testing"
"time"
@@ -56,11 +57,16 @@ func (d *durableSpy) RecordProviderAllocation(_ context.Context, allocation doma
func TestServiceDurablyRecordsProviderAllocationBeforeReturning(t *testing.T) {
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", MatchID: "m", ServerID: "gs", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
durable := &durableSpy{}
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
metrics := NewMetrics()
service := Service{Provider: provider, Durable: durable, Metrics: metrics, Now: func() time.Time { return time.Unix(1000, 0) }}
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
if err != nil || result.Endpoint == "" || durable.calls != 1 || durable.allocation.ServerID != "gs" {
t.Fatalf("result=%+v err=%v durable=%+v", result, err, durable)
}
var output strings.Builder
if err := metrics.WritePrometheus(&output); err != nil || !strings.Contains(output.String(), `allocations_total{region="EU"} 1`) {
t.Fatalf("success metric err=%v output=%s", err, output.String())
}
}
func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
@@ -76,13 +82,19 @@ func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
func TestServiceConsumesSharedQuotaBeforeFreshProviderCall(t *testing.T) {
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
quota := &quotaSpy{err: errors.New("quota exhausted")}
service := Service{Provider: provider, Durable: &durableSpy{}, Quota: quota, Now: func() time.Time { return time.Unix(1000, 0) }}
metrics := NewMetrics()
service := Service{Provider: provider, Durable: &durableSpy{}, Quota: quota, Metrics: metrics, Now: func() time.Time { return time.Unix(1000, 0) }}
if _, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, nil); err == nil {
t.Fatal("quota rejection was ignored")
}
if quota.calls != 1 || provider.calls != 0 {
t.Fatalf("quota/provider calls = %d/%d, want 1/0", quota.calls, provider.calls)
}
var output strings.Builder
_ = metrics.WritePrometheus(&output)
if !strings.Contains(output.String(), `quota_denials_total{region="EU"} 1`) {
t.Fatalf("quota denial metric missing: %s", output.String())
}
}
func TestServiceConsumesSharedQuotaOnceWhenReconcilingProviderResult(t *testing.T) {