perf(limits): add fleet latency counters only with a store

accountWith appended the fleet latency sums even for an in-memory
limiter, which nothing reads. It now appends them only when a store is
configured.
This commit is contained in:
2026-11-26 04:14:08 +00:00
parent 0753a8cb4c
commit d90514cd55
3 changed files with 30 additions and 5 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ func TestFleetMetricsHistogramSumAndBuckets(t *testing.T) {
{duration: 700 * time.Second, hasTTFB: false},
}
for _, s := range samples {
_, ok := gw.limiter.storeIncrMulti(latencyCounterEntries(kindProvider, "alpha", s, wb, et))
_, ok := gw.limiter.storeIncrMulti(latencyCounterEntries(kindProvider, "alpha", s, wb, et, true))
require.True(t, ok)
}
+7 -4
View File
@@ -2237,10 +2237,10 @@ func (l *limiter) accountWith(scopes []limitScope, u usage, costMicros int64, x
if x.hasLat && l.statsLatency {
if x.provider != "" {
entries = append(entries, latencyCounterEntries(kindProvider, x.provider, x.lat, wb, et)...)
entries = append(entries, latencyCounterEntries(kindProvider, x.provider, x.lat, wb, et, l.store != nil)...)
}
if hasModelScope && !x.noModelHistograms {
entries = append(entries, latencyCounterEntries(kindModel, modelCanonical, x.lat, wb, et)...)
entries = append(entries, latencyCounterEntries(kindModel, modelCanonical, x.lat, wb, et, l.store != nil)...)
}
}
@@ -2272,8 +2272,8 @@ func (l *limiter) accountWith(scopes []limitScope, u usage, costMicros int64, x
// provider and, separately, for the served model. latencyBucketIndex
// (metrics.go) already folds "past every configured bound" into its own
// returned index (len(latencyBucketBounds)), so no separate overflow
// branch is needed here.
func latencyCounterEntries(kind, id string, lat latencySample, wb windowBuckets, et enforceTTLSet) []counterIncr {
// branch is needed here. fleet adds the shared-store sum counters, which only a configured store reads.
func latencyCounterEntries(kind, id string, lat latencySample, wb windowBuckets, et enforceTTLSet, fleet bool) []counterIncr {
durIdx, _ := latencyBucketIndex(lat.duration)
durMetric := latencyDurationMetric(durIdx)
out := []counterIncr{
@@ -2288,6 +2288,9 @@ func latencyCounterEntries(kind, id string, lat latencySample, wb windowBuckets,
counterIncr{key: windowKeyForBucket(kind, id, ttfbMetric, windowDay, wb.day), delta: 1, ttl: dayWindowTTL, enforceTTL: et.day},
)
}
if !fleet {
return out
}
return append(out, fleetLatencyEntries(kind, id, lat, wb, et)...)
}
+22
View File
@@ -419,3 +419,25 @@ func TestSharedCalibratorCountersRideTheAccountBatch(t *testing.T) {
t.Errorf("calibrator counters in %d batches (%d shared with usage counters), want 1 batch shared with them", calBatches, mixed)
}
}
func TestFleetLatencyEntriesOnlyWithStore(t *testing.T) {
a, _, store, _, _ := sharedGatewayPair(t, nil)
a.limiter.statsLatency = true
lat := latencySample{duration: 300 * time.Millisecond, ttfb: 100 * time.Millisecond, hasTTFB: true}
sumKey := func() string {
return windowKeyForBucket(kindProvider, "alpha", metricLatDurSum, windowDay, bucketsFor(a.limiter.now()).day)
}
read := func(s counterStore) int64 {
got, _ := s.getMulti([]string{sumKey()})
return got[0]
}
a.limiter.accountWith(nil, usage{}, 0, accountExtras{provider: "alpha", lat: lat, hasLat: true})
if got := read(a.limiter.fallback); got != 0 {
t.Errorf("fleet latency sum in the in-memory fallback = %d, want 0 without a store", got)
}
shareLimiterStore(a, store)
a.limiter.accountWith(nil, usage{}, 0, accountExtras{provider: "alpha", lat: lat, hasLat: true})
if got := read(store); got != 300 {
t.Errorf("fleet latency sum in the store = %d, want 300", got)
}
}