Skip to content

Commit a460090

Browse files
authored
Ruler, Alertmanager: return 499 when the client cancels a list-all request (#7986)
ListAllRules and ListAllConfigs logged an error and answered 500 whenever listing the tenants failed, including when the cause was the client canceling the request. A cancellation while streaming the per-tenant results was logged as an error as well. Client cancellations polluted the 5xx SLO and the error logs. Map context.Canceled from ListAllUsers to 499 (Client Closed Request) and stop logging cancellations, the same way the distributor push path does since #7717. Signed-off-by: Sungjin1212 <tjdwls1201@gmail.com>
1 parent 3ba438e commit a460090

5 files changed

Lines changed: 132 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972
4545
* [BUGFIX] Ring: Fix shuffle-sharding subrings treating instances with a stale heartbeat as healthy when using the DynamoDB KV store. #7976
4646
* [BUGFIX] Ingester Client: Fix a gRPC connection leak when `-distributor.use-stream-push` is enabled and the push streams to an ingester cannot be opened. #7969
47+
* [BUGFIX] Ruler, Alertmanager: Return HTTP 499 (Client Closed Request) instead of 500 when a `GET /ruler/rule_groups` or `GET /multitenant_alertmanager/configs` request is canceled by the client, so client-side cancellations are no longer counted or logged as server-side errors. #7986
4748

4849
## 1.22.0 2026-11-19
4950

‎pkg/alertmanager/api.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
3434
"github.com/cortexproject/cortex/pkg/util"
35+
util_api "github.com/cortexproject/cortex/pkg/util/api"
3536
"github.com/cortexproject/cortex/pkg/util/concurrency"
3637
util_log "github.com/cortexproject/cortex/pkg/util/log"
3738
"github.com/cortexproject/cortex/pkg/util/users"
@@ -306,6 +307,10 @@ func (am *MultitenantAlertmanager) ListAllConfigs(w http.ResponseWriter, r *http
306307
logger := util_log.WithContext(r.Context(), am.logger)
307308
userIDs, err := am.store.ListAllUsers(r.Context())
308309
if err != nil {
310+
if errors.Is(err, context.Canceled) {
311+
http.Error(w, fmt.Sprintf("%s: %s", errListAllUser, err.Error()), util_api.StatusClientClosedRequest)
312+
return
313+
}
309314
level.Error(logger).Log("msg", "failed to list users of alertmanager", "err", err)
310315
http.Error(w, fmt.Sprintf("%s: %s", errListAllUser, err.Error()), http.StatusInternalServerError)
311316
return
@@ -340,7 +345,7 @@ func (am *MultitenantAlertmanager) ListAllConfigs(w http.ResponseWriter, r *http
340345

341346
return nil
342347
})
343-
if err != nil {
348+
if err != nil && !errors.Is(err, context.Canceled) {
344349
level.Error(logger).Log("msg", "failed to list all alertmanager configs", "err", err)
345350
}
346351
close(iter)

‎pkg/alertmanager/api_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
2626
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore"
2727
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore/bucketclient"
28+
util_api "github.com/cortexproject/cortex/pkg/util/api"
29+
"github.com/cortexproject/cortex/pkg/util/concurrency"
2830
"github.com/cortexproject/cortex/pkg/util/flagext"
2931
util_log "github.com/cortexproject/cortex/pkg/util/log"
3032
"github.com/cortexproject/cortex/pkg/util/services"
@@ -1348,6 +1350,64 @@ receivers:
13481350
require.YAMLEq(t, string(old), string(body))
13491351
}
13501352

1353+
// listAllUsersAlertStore overrides ListAllUsers. Other methods panic if called.
1354+
type listAllUsersAlertStore struct {
1355+
alertstore.AlertStore
1356+
userIDs []string
1357+
err error
1358+
}
1359+
1360+
func (s *listAllUsersAlertStore) ListAllUsers(_ context.Context) ([]string, error) {
1361+
return s.userIDs, s.err
1362+
}
1363+
1364+
func TestAMConfigListUserConfig_ClientCancellation(t *testing.T) {
1365+
tests := map[string]struct {
1366+
store *listAllUsersAlertStore
1367+
cancelRequest bool
1368+
expectedStatus int
1369+
expectErrorLog bool
1370+
}{
1371+
"list users canceled by the client returns 499 without logging": {
1372+
store: &listAllUsersAlertStore{err: fmt.Errorf("unable to list users in alertmanager store bucket: %w", context.Canceled)},
1373+
expectedStatus: util_api.StatusClientClosedRequest,
1374+
},
1375+
"list users failing for another reason returns 500 with error log": {
1376+
store: &listAllUsersAlertStore{err: errors.New("bucket unavailable")},
1377+
expectedStatus: http.StatusInternalServerError,
1378+
expectErrorLog: true,
1379+
},
1380+
"client canceling after the users are listed does not log": {
1381+
store: &listAllUsersAlertStore{userIDs: []string{"user-1", "user-2"}},
1382+
cancelRequest: true,
1383+
expectedStatus: http.StatusOK,
1384+
},
1385+
}
1386+
1387+
for name, tc := range tests {
1388+
t.Run(name, func(t *testing.T) {
1389+
logs := &concurrency.SyncBuffer{}
1390+
am := &MultitenantAlertmanager{store: tc.store, logger: log.NewLogfmtLogger(logs)}
1391+
1392+
req := httptest.NewRequest(http.MethodGet, "/multitenant_alertmanager/configs", nil)
1393+
if tc.cancelRequest {
1394+
ctx, cancel := context.WithCancel(req.Context())
1395+
cancel()
1396+
req = req.WithContext(ctx)
1397+
}
1398+
w := httptest.NewRecorder()
1399+
am.ListAllConfigs(w, req)
1400+
1401+
assert.Equal(t, tc.expectedStatus, w.Code)
1402+
if tc.expectErrorLog {
1403+
assert.Contains(t, logs.String(), "level=error")
1404+
} else {
1405+
assert.Empty(t, logs.String())
1406+
}
1407+
})
1408+
}
1409+
}
1410+
13511411
func TestValidateAlertmanagerConfig(t *testing.T) {
13521412
tests := map[string]struct {
13531413
input any

‎pkg/ruler/ruler.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,10 @@ func (r *Ruler) ListAllRules(w http.ResponseWriter, req *http.Request) {
17211721

17221722
userIDs, err := r.store.ListAllUsers(req.Context())
17231723
if err != nil {
1724+
if errors.Is(err, context.Canceled) {
1725+
http.Error(w, fmt.Sprintf("%s: %s", errListAllUser, err.Error()), util_api.StatusClientClosedRequest)
1726+
return
1727+
}
17241728
level.Error(logger).Log("msg", errListAllUser, "err", err)
17251729
http.Error(w, fmt.Sprintf("%s: %s", errListAllUser, err.Error()), http.StatusInternalServerError)
17261730
return
@@ -1752,7 +1756,7 @@ func (r *Ruler) ListAllRules(w http.ResponseWriter, req *http.Request) {
17521756

17531757
return nil
17541758
})
1755-
if err != nil {
1759+
if err != nil && !errors.Is(err, context.Canceled) {
17561760
level.Error(logger).Log("msg", "failed to list all ruler configs", "err", err)
17571761
}
17581762
close(iter)

‎pkg/ruler/ruler_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import (
5555
"github.com/cortexproject/cortex/pkg/ruler/rulestore"
5656
"github.com/cortexproject/cortex/pkg/ruler/rulestore/bucketclient"
5757
"github.com/cortexproject/cortex/pkg/util"
58+
util_api "github.com/cortexproject/cortex/pkg/util/api"
59+
"github.com/cortexproject/cortex/pkg/util/concurrency"
5860
"github.com/cortexproject/cortex/pkg/util/flagext"
5961
"github.com/cortexproject/cortex/pkg/util/services"
6062
"github.com/cortexproject/cortex/pkg/util/test"
@@ -2744,6 +2746,64 @@ func TestRuler_ListAllRules(t *testing.T) {
27442746
require.YAMLEq(t, string(expectedResponse), string(body))
27452747
}
27462748

2749+
// listAllUsersRuleStore overrides ListAllUsers. Other methods panic if called.
2750+
type listAllUsersRuleStore struct {
2751+
rulestore.RuleStore
2752+
userIDs []string
2753+
err error
2754+
}
2755+
2756+
func (s *listAllUsersRuleStore) ListAllUsers(_ context.Context) ([]string, error) {
2757+
return s.userIDs, s.err
2758+
}
2759+
2760+
func TestRuler_ListAllRules_ClientCancellation(t *testing.T) {
2761+
tests := map[string]struct {
2762+
store *listAllUsersRuleStore
2763+
cancelRequest bool
2764+
expectedStatus int
2765+
expectErrorLog bool
2766+
}{
2767+
"list users canceled by the client returns 499 without logging": {
2768+
store: &listAllUsersRuleStore{err: fmt.Errorf("unable to list users in rule store bucket: %w", context.Canceled)},
2769+
expectedStatus: util_api.StatusClientClosedRequest,
2770+
},
2771+
"list users failing for another reason returns 500 with error log": {
2772+
store: &listAllUsersRuleStore{err: errors.New("bucket unavailable")},
2773+
expectedStatus: http.StatusInternalServerError,
2774+
expectErrorLog: true,
2775+
},
2776+
"client canceling after the users are listed does not log": {
2777+
store: &listAllUsersRuleStore{userIDs: []string{"user-1", "user-2"}},
2778+
cancelRequest: true,
2779+
expectedStatus: http.StatusOK,
2780+
},
2781+
}
2782+
2783+
for name, tc := range tests {
2784+
t.Run(name, func(t *testing.T) {
2785+
logs := &concurrency.SyncBuffer{}
2786+
r := &Ruler{store: tc.store, logger: log.NewLogfmtLogger(logs)}
2787+
2788+
req := httptest.NewRequest(http.MethodGet, "/ruler/rule_groups", nil)
2789+
if tc.cancelRequest {
2790+
ctx, cancel := context.WithCancel(req.Context())
2791+
cancel()
2792+
req = req.WithContext(ctx)
2793+
}
2794+
w := httptest.NewRecorder()
2795+
r.ListAllRules(w, req)
2796+
2797+
assert.Equal(t, tc.expectedStatus, w.Code)
2798+
if tc.expectErrorLog {
2799+
assert.Contains(t, logs.String(), "level=error")
2800+
} else {
2801+
assert.Empty(t, logs.String())
2802+
}
2803+
})
2804+
}
2805+
}
2806+
27472807
type senderFunc func(alerts ...*notifier.Alert)
27482808

27492809
func (s senderFunc) Send(alerts ...*notifier.Alert) {

0 commit comments

Comments
 (0)