Skip to content

Commit 025a93a

Browse files
committed
Add limit breached metric + wrap error with 429
Signed-off-by: Justin Jung <[email protected]>
1 parent c68bbd2 commit 025a93a

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

pkg/ingester/ingester.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ func (i *Ingester) trackInflightQueryRequest() (func(), error) {
21722172
if i.resourceBasedLimiter != nil {
21732173
if err := i.resourceBasedLimiter.AcceptNewRequest(); err != nil {
21742174
level.Warn(i.logger).Log("msg", "failed to accept request", "err", err)
2175-
return nil, fmt.Errorf("failed to query: %s", limiter.ErrResourceLimitReachedStr)
2175+
return nil, httpgrpc.Errorf(http.StatusTooManyRequests, "failed to query: %s", limiter.ErrResourceLimitReachedStr)
21762176
}
21772177
}
21782178

pkg/storegateway/gateway.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/thanos-io/objstore"
1717
"github.com/thanos-io/thanos/pkg/extprom"
1818
"github.com/thanos-io/thanos/pkg/store/storepb"
19+
"github.com/weaveworks/common/httpgrpc"
1920
"github.com/weaveworks/common/logging"
2021

2122
"github.com/cortexproject/cortex/pkg/configs"
@@ -436,7 +437,7 @@ func (g *StoreGateway) checkResourceUtilization() error {
436437

437438
if err := g.resourceBasedLimiter.AcceptNewRequest(); err != nil {
438439
level.Warn(g.logger).Log("msg", "failed to accept request", "err", err)
439-
return fmt.Errorf("failed to query: %s", util_limiter.ErrResourceLimitReachedStr)
440+
return httpgrpc.Errorf(http.StatusTooManyRequests, "failed to query: %s", util_limiter.ErrResourceLimitReachedStr)
440441
}
441442

442443
return nil

pkg/util/limiter/resource_based_limiter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ func (e *ResourceLimitReachedError) Error() string {
1818
}
1919

2020
type ResourceBasedLimiter struct {
21-
resourceMonitor resource.IMonitor
22-
limits map[resource.Type]float64
21+
resourceMonitor resource.IMonitor
22+
limits map[resource.Type]float64
23+
limitBreachedCount *prometheus.CounterVec
2324
}
2425

2526
func NewResourceBasedLimiter(resourceMonitor resource.IMonitor, limits map[resource.Type]float64, registerer prometheus.Registerer) (*ResourceBasedLimiter, error) {
@@ -38,6 +39,13 @@ func NewResourceBasedLimiter(resourceMonitor resource.IMonitor, limits map[resou
3839
return &ResourceBasedLimiter{
3940
resourceMonitor: resourceMonitor,
4041
limits: limits,
42+
limitBreachedCount: promauto.With(registerer).NewCounterVec(
43+
prometheus.CounterOpts{
44+
Name: "cortex_resource_based_limiter_limit_breached",
45+
Help: "The total number of times resource based limiter was throttled.",
46+
},
47+
[]string{"resource"},
48+
),
4149
}, nil
4250
}
4351

@@ -53,6 +61,7 @@ func (l *ResourceBasedLimiter) AcceptNewRequest() error {
5361
}
5462

5563
if utilization >= limit {
64+
l.limitBreachedCount.WithLabelValues(string(resType)).Inc()
5665
return fmt.Errorf("%s utilization limit reached (limit: %.3f, utilization: %.3f)", resType, limit, utilization)
5766
}
5867
}

0 commit comments

Comments
 (0)