Skip to content

Commit c9b08e5

Browse files
authored
Add a too_many_tenants label to cortex_rejected_queries_total metric (#6569)
Signed-off-by: SungJin1212 <[email protected]>
1 parent b48f93b commit c9b08e5

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## master / unreleased
44

55
* [FEATURE] Querier/Ruler: Add `query_partial_data` and `rules_partial_data` limits to allow queries/rules to be evaluated with data from a single zone, if other zones are not available. #6526
6+
* [ENHANCEMENT] Query Frontend: Add a `too_many_tenants` reason label value to `cortex_rejected_queries_total` metric to track the rejected query count due to the # of tenant limits. #6569
67
* [BUGFIX] Ingester: Avoid error or early throttling when READONLY ingesters are present in the ring #6517
78

89
## 1.19.0 in progress

pkg/frontend/transport/handler.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
)
4646

4747
const (
48+
reasonTooManyTenants = "too_many_tenants"
4849
reasonRequestBodySizeExceeded = "request_body_size_exceeded"
4950
reasonResponseBodySizeExceeded = "response_body_size_exceeded"
5051
reasonTooManyRequests = "too_many_requests"
@@ -191,16 +192,20 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
191192
return
192193
}
193194

195+
userID := tenant.JoinTenantIDs(tenantIDs)
196+
194197
if f.tenantFederationCfg.Enabled {
195198
maxTenant := f.tenantFederationCfg.MaxTenant
196199
if maxTenant > 0 && len(tenantIDs) > maxTenant {
200+
source := tripperware.GetSource(r.Header.Get("User-Agent"))
201+
if f.cfg.QueryStatsEnabled {
202+
f.rejectedQueries.WithLabelValues(reasonTooManyTenants, source, userID).Inc()
203+
}
197204
http.Error(w, fmt.Errorf(errTooManyTenants, maxTenant, len(tenantIDs)).Error(), http.StatusBadRequest)
198205
return
199206
}
200207
}
201208

202-
userID := tenant.JoinTenantIDs(tenantIDs)
203-
204209
// Initialise the stats in the context and make sure it's propagated
205210
// down the request chain.
206211
if f.cfg.QueryStatsEnabled {

pkg/frontend/transport/handler_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func Test_TenantFederation_MaxTenant(t *testing.T) {
589589

590590
for _, test := range tests {
591591
t.Run(test.name, func(t *testing.T) {
592-
handler := NewHandler(HandlerConfig{}, test.cfg, roundTripper, log.NewNopLogger(), nil)
592+
handler := NewHandler(HandlerConfig{QueryStatsEnabled: true}, test.cfg, roundTripper, log.NewNopLogger(), nil)
593593
handlerWithAuth := middleware.Merge(middleware.AuthenticateUser).Wrap(handler)
594594

595595
req := httptest.NewRequest("GET", "http://fake", nil)
@@ -604,6 +604,11 @@ func Test_TenantFederation_MaxTenant(t *testing.T) {
604604

605605
if test.expectedErrMsg != "" {
606606
require.Contains(t, string(body), test.expectedErrMsg)
607+
608+
if strings.Contains(test.expectedErrMsg, "too many tenants") {
609+
v := promtest.ToFloat64(handler.rejectedQueries.WithLabelValues(reasonTooManyTenants, tripperware.SourceAPI, test.orgId))
610+
assert.Equal(t, float64(1), v)
611+
}
607612
}
608613
})
609614
}

0 commit comments

Comments
 (0)