Skip to content

Commit 229012e

Browse files
committed
Move reason to iota
1 parent b705678 commit 229012e

File tree

8 files changed

+48
-49
lines changed

8 files changed

+48
-49
lines changed

pkg/distributor/distributor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ func TestDistributor_PushIngestLimits(t *testing.T) {
24622462
Tenant: "test",
24632463
Results: []*logproto.ExceedsLimitsResult{{
24642464
StreamHash: 0x90eb45def17f924,
2465-
Reason: limits.ReasonExceedsMaxStreams,
2465+
Reason: limits.ReasonExceedsMaxStreams.Humanize(),
24662466
}},
24672467
},
24682468
expectedErr: "rpc error: code = Code(429) desc = request exceeded limits: max streams exceeded",
@@ -2492,7 +2492,7 @@ func TestDistributor_PushIngestLimits(t *testing.T) {
24922492
Tenant: "test",
24932493
Results: []*logproto.ExceedsLimitsResult{{
24942494
StreamHash: 0x90eb45def17f924,
2495-
Reason: limits.ReasonExceedsRateLimit,
2495+
Reason: limits.ReasonExceedsRateLimit.Humanize(),
24962496
}},
24972497
},
24982498
expectedErr: "rpc error: code = Code(429) desc = request exceeded limits: rate limit exceeded",
@@ -2532,7 +2532,7 @@ func TestDistributor_PushIngestLimits(t *testing.T) {
25322532
Tenant: "test",
25332533
Results: []*logproto.ExceedsLimitsResult{{
25342534
StreamHash: 1,
2535-
Reason: limits.ReasonExceedsMaxStreams,
2535+
Reason: limits.ReasonExceedsMaxStreams.Humanize(),
25362536
}},
25372537
},
25382538
}, {
@@ -2562,7 +2562,7 @@ func TestDistributor_PushIngestLimits(t *testing.T) {
25622562
Tenant: "test",
25632563
Results: []*logproto.ExceedsLimitsResult{{
25642564
StreamHash: 1,
2565-
Reason: limits.ReasonExceedsMaxStreams,
2565+
Reason: limits.ReasonExceedsMaxStreams.Humanize(),
25662566
}},
25672567
},
25682568
}, {

pkg/distributor/ingest_limits.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/prometheus/client_golang/prometheus"
1212
"github.com/prometheus/client_golang/prometheus/promauto"
1313

14-
"github.com/grafana/loki/v3/pkg/limits"
1514
limits_frontend_client "github.com/grafana/loki/v3/pkg/limits/frontend/client"
1615
"github.com/grafana/loki/v3/pkg/logproto"
1716
)
@@ -159,20 +158,7 @@ func newExceedsLimitsRequest(tenant string, streams []KeyedStream) (*logproto.Ex
159158

160159
func firstReasonForHashes(reasonsForHashes map[uint64][]string) string {
161160
for _, reasons := range reasonsForHashes {
162-
return humanizeReasonForHash(reasons[0])
161+
return reasons[0]
163162
}
164163
return "unknown reason"
165164
}
166-
167-
// TODO(grobinson): Move this to the same place where the consts
168-
// are defined.
169-
func humanizeReasonForHash(s string) string {
170-
switch s {
171-
case limits.ReasonExceedsMaxStreams:
172-
return "max streams exceeded"
173-
case limits.ReasonExceedsRateLimit:
174-
return "rate limit exceeded"
175-
default:
176-
return s
177-
}
178-
}

pkg/limits/frontend/frontend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ func (f *Frontend) ExceedsLimits(ctx context.Context, req *logproto.ExceedsLimit
190190
for _, unknownStream := range resp.Response.UnknownStreams {
191191
results = append(results, &logproto.ExceedsLimitsResult{
192192
StreamHash: unknownStream,
193-
Reason: limits.ReasonExceedsMaxStreams,
193+
Reason: limits.ReasonExceedsMaxStreams.Humanize(),
194194
})
195195
}
196196
}
197197
}
198198
f.metrics.streamsExceedingLimits.WithLabelValues(
199199
req.Tenant,
200-
limits.ReasonExceedsMaxStreams,
200+
limits.ReasonExceedsMaxStreams.Humanize(),
201201
).Add(float64(len(results)))
202202

203203
// Check if rate limits would be exceeded.
@@ -208,12 +208,12 @@ func (f *Frontend) ExceedsLimits(ctx context.Context, req *logproto.ExceedsLimit
208208
for _, streamHash := range streamHashes {
209209
results = append(results, &logproto.ExceedsLimitsResult{
210210
StreamHash: streamHash,
211-
Reason: limits.ReasonExceedsRateLimit,
211+
Reason: limits.ReasonExceedsRateLimit.Humanize(),
212212
})
213213
}
214214
f.metrics.streamsExceedingLimits.WithLabelValues(
215215
req.Tenant,
216-
limits.ReasonExceedsRateLimit,
216+
limits.ReasonExceedsRateLimit.Humanize(),
217217
).Add(float64(len(streamHashes)))
218218
}
219219

pkg/limits/frontend/frontend_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
112112
maxGlobalStreams: 5,
113113
ingestionRate: 100,
114114
expected: []*logproto.ExceedsLimitsResult{
115-
{StreamHash: 0x1, Reason: limits.ReasonExceedsMaxStreams},
116-
{StreamHash: 0x2, Reason: limits.ReasonExceedsMaxStreams},
115+
{StreamHash: 0x1, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
116+
{StreamHash: 0x2, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
117117
},
118118
}, {
119119
name: "exceeds max streams limit, allows existing streams and returns the new streams",
@@ -148,8 +148,8 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
148148
maxGlobalStreams: 5,
149149
ingestionRate: 100,
150150
expected: []*logproto.ExceedsLimitsResult{
151-
{StreamHash: 6, Reason: limits.ReasonExceedsMaxStreams},
152-
{StreamHash: 7, Reason: limits.ReasonExceedsMaxStreams},
151+
{StreamHash: 6, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
152+
{StreamHash: 7, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
153153
},
154154
}, {
155155
// This test checks the case where a tenant's streams are sharded over
@@ -202,7 +202,7 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
202202
maxGlobalStreams: 1,
203203
ingestionRate: 100,
204204
expected: []*logproto.ExceedsLimitsResult{
205-
{StreamHash: 0x1, Reason: limits.ReasonExceedsMaxStreams},
205+
{StreamHash: 0x1, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
206206
},
207207
}, {
208208
name: "exceeds rate limits, returns all streams",
@@ -231,8 +231,8 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
231231
maxGlobalStreams: 10,
232232
ingestionRate: 100,
233233
expected: []*logproto.ExceedsLimitsResult{
234-
{StreamHash: 1, Reason: limits.ReasonExceedsRateLimit},
235-
{StreamHash: 2, Reason: limits.ReasonExceedsRateLimit},
234+
{StreamHash: 1, Reason: limits.ReasonExceedsRateLimit.Humanize()},
235+
{StreamHash: 2, Reason: limits.ReasonExceedsRateLimit.Humanize()},
236236
},
237237
}, {
238238
name: "exceeds rate limits, rates sharded over two instances",
@@ -274,8 +274,8 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
274274
maxGlobalStreams: 10,
275275
ingestionRate: 100,
276276
expected: []*logproto.ExceedsLimitsResult{
277-
{StreamHash: 1, Reason: limits.ReasonExceedsRateLimit},
278-
{StreamHash: 2, Reason: limits.ReasonExceedsRateLimit},
277+
{StreamHash: 1, Reason: limits.ReasonExceedsRateLimit.Humanize()},
278+
{StreamHash: 2, Reason: limits.ReasonExceedsRateLimit.Humanize()},
279279
},
280280
}, {
281281
name: "exceeds both max stream limit and rate limits",
@@ -305,10 +305,10 @@ func TestFrontend_ExceedsLimits(t *testing.T) {
305305
maxGlobalStreams: 5,
306306
ingestionRate: 100,
307307
expected: []*logproto.ExceedsLimitsResult{
308-
{StreamHash: 0x6, Reason: limits.ReasonExceedsMaxStreams},
309-
{StreamHash: 0x7, Reason: limits.ReasonExceedsMaxStreams},
310-
{StreamHash: 0x6, Reason: limits.ReasonExceedsRateLimit},
311-
{StreamHash: 0x7, Reason: limits.ReasonExceedsRateLimit},
308+
{StreamHash: 0x6, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
309+
{StreamHash: 0x7, Reason: limits.ReasonExceedsMaxStreams.Humanize()},
310+
{StreamHash: 0x6, Reason: limits.ReasonExceedsRateLimit.Humanize()},
311+
{StreamHash: 0x7, Reason: limits.ReasonExceedsRateLimit.Humanize()},
312312
},
313313
}}
314314

pkg/limits/ingest_limits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func (s *IngestLimits) ExceedsLimits(ctx context.Context, req *logproto.ExceedsL
429429
for _, streamHash := range streamHashes {
430430
results = append(results, &logproto.ExceedsLimitsResult{
431431
StreamHash: streamHash,
432-
Reason: reason,
432+
Reason: reason.Humanize(),
433433
})
434434
}
435435
}

pkg/limits/limits.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
package limits
22

3+
type Reason int
4+
35
const (
46
// ReasonExceedsMaxStreams is returned when a tenant exceeds the maximum
57
// number of active streams as per their per-tenant limit.
6-
ReasonExceedsMaxStreams = "exceeds_max_streams"
8+
ReasonExceedsMaxStreams Reason = iota
79

810
// ReasonExceedsRateLimit is returned when a tenant exceeds their maximum
911
// rate limit as per their per-tenant limit.
10-
ReasonExceedsRateLimit = "exceeds_rate_limit"
12+
ReasonExceedsRateLimit
1113
)
1214

15+
func (r Reason) Humanize() string {
16+
switch r {
17+
case ReasonExceedsMaxStreams:
18+
return "max streams exceeded"
19+
case ReasonExceedsRateLimit:
20+
return "rate limit exceeded"
21+
default:
22+
return "unknown reason"
23+
}
24+
}
25+
1326
// Limits contains all limits enforced by the limits frontend.
1427
type Limits interface {
1528
IngestionRateBytes(userID string) float64

pkg/limits/stream_metadata.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type StreamMetadata interface {
2424
// e.g. the total active streams and the total size of the streams.
2525
Usage(tenant string, fn UsageFunc)
2626

27-
// TryStore tries to store the stream metadat for a specific tenant,
28-
// until the limit is reached. It returns the stream hashes that were not stored.
29-
TryStore(tenant string, streams map[int32][]Stream, maxActiveStreams uint64, bucketStart, bucketCutOff int64) map[string][]uint64
27+
// TryStore tries to store the stream metadata for a specific tenant,
28+
// until the limit is reached. It returns the stream hashes that were not stored per reason.
29+
TryStore(tenant string, streams map[int32][]Stream, maxActiveStreams uint64, bucketStart, bucketCutOff int64) map[Reason][]uint64
3030

3131
// Store updates or creates the stream metadata for a specific tenant and partition.
3232
Store(tenant string, partitionID int32, streamHash, recTotalSize uint64, recordTime, bucketStart, bucketCutOff int64)
@@ -108,7 +108,7 @@ func (s *streamMetadata) Usage(tenant string, fn UsageFunc) {
108108
}
109109
}
110110

111-
func (s *streamMetadata) TryStore(tenant string, streams map[int32][]Stream, maxActiveStreams uint64, bucketStart, bucketCutOff int64) map[string][]uint64 {
111+
func (s *streamMetadata) TryStore(tenant string, streams map[int32][]Stream, maxActiveStreams uint64, bucketStart, bucketCutOff int64) map[Reason][]uint64 {
112112
i := s.getStripeIdx(tenant)
113113

114114
s.locks[i].Lock()
@@ -118,7 +118,7 @@ func (s *streamMetadata) TryStore(tenant string, streams map[int32][]Stream, max
118118
s.stripes[i][tenant] = make(map[int32]map[uint64]Stream)
119119
}
120120

121-
dropped := make(map[string][]uint64)
121+
dropped := make(map[Reason][]uint64)
122122
for partitionID, streams := range streams {
123123
if _, ok := s.stripes[i][tenant][partitionID]; !ok {
124124
s.stripes[i][tenant][partitionID] = make(map[uint64]Stream)

pkg/limits/stream_metadata_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ func TestStreamMetadata_TryStore(t *testing.T) {
506506
streams map[int32][]Stream
507507
maxActiveStreams int
508508
expectedStored map[string]map[int32][]Stream
509-
expectedDropped map[string][]uint64
509+
expectedDropped map[Reason][]uint64
510510
}{
511511
{
512512
name: "no streams",
@@ -584,7 +584,7 @@ func TestStreamMetadata_TryStore(t *testing.T) {
584584
},
585585
},
586586
},
587-
expectedDropped: map[string][]uint64{
587+
expectedDropped: map[Reason][]uint64{
588588
ReasonExceedsMaxStreams: {0x1},
589589
},
590590
},
@@ -615,7 +615,7 @@ func TestStreamMetadata_TryStore(t *testing.T) {
615615
},
616616
},
617617
},
618-
expectedDropped: map[string][]uint64{
618+
expectedDropped: map[Reason][]uint64{
619619
ReasonExceedsMaxStreams: {0x1, 0x3},
620620
},
621621
},
@@ -654,7 +654,7 @@ func TestStreamMetadata_TryStore(t *testing.T) {
654654
},
655655
},
656656
},
657-
expectedDropped: map[string][]uint64{
657+
expectedDropped: map[Reason][]uint64{
658658
ReasonExceedsMaxStreams: {0x3},
659659
},
660660
},
@@ -702,7 +702,7 @@ func TestStreamMetadata_TryStore(t *testing.T) {
702702
},
703703
},
704704
},
705-
expectedDropped: map[string][]uint64{
705+
expectedDropped: map[Reason][]uint64{
706706
ReasonExceedsMaxStreams: {0x2, 0x5},
707707
},
708708
},

0 commit comments

Comments
 (0)