feat(admin): live tokens-per-minute usage per scope

This commit is contained in:
2026-11-25 11:52:35 +00:00
parent af3628d13d
commit c6da612215
5 changed files with 51 additions and 4 deletions
+4 -1
View File
@@ -960,6 +960,7 @@ type adminUsageEntryView struct {
MCPServers []string `json:"mcpServers,omitempty"`
Agents []string `json:"agents,omitempty"`
RequestsPerMinute int64 `json:"requestsPerMinute"`
TokensPerMinute int64 `json:"tokensPerMinute"`
RequestsPerDay int64 `json:"requestsPerDay"`
TokensInPerDay int64 `json:"tokensInPerDay"`
TokensOutPerDay int64 `json:"tokensOutPerDay"`
@@ -1003,6 +1004,7 @@ func usageEntryView(su scopeUsage, limits *LimitsConfig) adminUsageEntryView {
ID: su.id,
Limits: limits,
RequestsPerMinute: su.requestsPerMinute,
TokensPerMinute: su.tokensPerMinute,
RequestsPerDay: su.requestsPerDay,
TokensInPerDay: su.tokensInPerDay,
TokensOutPerDay: su.tokensOutPerDay,
@@ -1063,7 +1065,8 @@ func usageEntryView(su scopeUsage, limits *LimitsConfig) adminUsageEntryView {
// chunk, keeping this endpoint's per-round-trip key count at the same
// order of magnitude this constant has always targeted, rather than
// letting it grow every time a future per-scope counter is added here.
const adminUsageChunkScopes = 145
// 123 scopes * 13 keys/scope = 1,599 keys, within usageModelsChunkKeys.
const adminUsageChunkScopes = 123
// chunkedCurrentUsage calls limiter.currentUsage in batches of at most
// adminUsageChunkScopes scopes at a time, concatenating the results in
+2 -2
View File
@@ -3288,8 +3288,8 @@ func TestAdminUsage_LastSeen(t *testing.T) {
// without a test catching it.
func TestAdminUsage_ChunkScopesLoweredForLastSeenKey(t *testing.T) {
t.Parallel()
if adminUsageChunkScopes != 145 {
t.Errorf("adminUsageChunkScopes = %d, want 145", adminUsageChunkScopes)
if adminUsageChunkScopes != 123 {
t.Errorf("adminUsageChunkScopes = %d, want 123", adminUsageChunkScopes)
}
if adminUsageChunkScopes*usageKeysPerScope > 1600 {
t.Errorf("adminUsageChunkScopes*usageKeysPerScope = %d, want <= 1600", adminUsageChunkScopes*usageKeysPerScope)
+37
View File
@@ -0,0 +1,37 @@
package traefikllmgateway
import (
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func tpmUsage(t *testing.T, limits *LimitsConfig) adminUsageEntryView {
t.Helper()
h, gw := exemptGateway(t, nil, limits, nil)
require.Equal(t, http.StatusOK, memberLimitsChat(t, h, "sk-a").Code)
return findUsageEntry(t, gw.buildAdminUsage().Users, "a")
}
func TestAdminUsage_TokensPerMinuteLive(t *testing.T) {
e := tpmUsage(t, &LimitsConfig{TokensPerMinute: 1000})
assert.GreaterOrEqual(t, e.TokensPerMinute, int64(1), "minute token counter must surface")
raw, err := json.Marshal(e)
require.NoError(t, err)
var m map[string]any
require.NoError(t, json.Unmarshal(raw, &m))
assert.Contains(t, m, "tokensPerMinute")
}
func TestAdminUsage_TokensPerMinuteZeroWithoutLimit(t *testing.T) {
e := tpmUsage(t, &LimitsConfig{RequestsPerDay: 100})
assert.EqualValues(t, 0, e.TokensPerMinute)
}
func TestAdminUsageChunk_FitsKeyBudget(t *testing.T) {
assert.LessOrEqual(t, adminUsageChunkScopes*usageKeysPerScope, usageModelsChunkKeys)
assert.Equal(t, 13, usageKeysPerScope)
}
+6 -1
View File
@@ -2950,7 +2950,7 @@ func (l *limiter) providerUsage(scopes []limitScope) []providerCounters {
// rejectionsPerDay counter (metricRej) usageWindowKeys now appends. Grows
// once more, 9 to 10, for the last-seen counter (Q6, DECISIONS) — see
// usageWindowKeys' own doc comment for the appended field's position.
const usageKeysPerScope = 11
const usageKeysPerScope = 13
// scopeUsage is one scope's (a user's, a group's, or the synthetic total
// scope's — totalScopeKind) current-window counter values — the admin
@@ -2985,6 +2985,8 @@ type scopeUsage struct {
lastSeen int64
// exemptPerMonth is the scope's limit-exempt request count this month.
exemptPerMonth int64
// tokensPerMinute is the minute-window uncached input plus output; 0 for scopes with no tokensPerMinute limit (nothing is written).
tokensPerMinute int64
// storeDown reports whether reading any of the usageKeysPerScope
// counters above failed closed (a configured store errored and
// failOpen is false) — mirrors limitViolation.storeDown. Every value
@@ -3017,6 +3019,8 @@ func usageWindowKeys(sc limitScope, now time.Time) []string {
windowKey(sc.kind, sc.id, metricRej, windowDay, now),
lastSeenKey(sc.kind, sc.id),
windowKey(sc.kind, sc.id, metricExempt, windowMonth, now),
windowKey(sc.kind, sc.id, metricTokIn, windowMin, now),
windowKey(sc.kind, sc.id, metricTokOut, windowMin, now),
}
}
@@ -3092,6 +3096,7 @@ func (l *limiter) currentUsage(scopes []limitScope) []scopeUsage {
tokensOutPerDay: v[4], tokensOutPerMonth: v[5],
costPerDayMicros: v[6], costPerMonthMicros: v[7],
rejectionsPerDay: v[8], lastSeen: v[9], exemptPerMonth: v[10],
tokensPerMinute: v[11] + v[12],
}
}
return out
+2
View File
@@ -443,6 +443,8 @@ export interface AdminUsageEntryView {
mcpServers?: string[]
agents?: string[]
requestsPerMinute: number
/** Live minute-window tokens (uncached input + output); 0 when the scope has no tokensPerMinute limit. */
tokensPerMinute?: number
requestsPerDay: number
tokensInPerDay: number
tokensOutPerDay: number