diff --git a/fleet_metrics_test.go b/fleet_metrics_test.go index d674103..8993fce 100644 --- a/fleet_metrics_test.go +++ b/fleet_metrics_test.go @@ -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) } diff --git a/limits.go b/limits.go index 799c597..9a1ec3b 100644 --- a/limits.go +++ b/limits.go @@ -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)...) } diff --git a/shared_state_review_test.go b/shared_state_review_test.go index 2cdecf9..b92b576 100644 --- a/shared_state_review_test.go +++ b/shared_state_review_test.go @@ -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) + } +}