Skip to content

Commit 422af37

Browse files
authored
fix: ensure ruler returns 404 when getting missing rule group (#4013)
* fix: ensure ruler returns 404 when getting missing rule group Signed-off-by: Jacob Lisi <[email protected]> * test(TestRulerAPI): add check for proper get rulegroup 404 behaviour Signed-off-by: Jacob Lisi <[email protected]> * chore: update changelog Signed-off-by: Jacob Lisi <[email protected]>
1 parent 3bc862c commit 422af37

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [ENHANCEMENT] Querier / ruler: some optimizations to PromQL query engine. #3934 #3989
2222
* [ENHANCEMENT] Ingester: reduce CPU and memory when an high number of errors are returned by the ingester on the write path with the blocks storage. #3969 #3971 #3973
2323
* [ENHANCEMENT] Distributor: reduce CPU and memory when an high number of errors are returned by the distributor on the write path. #3990
24+
* [BUGFIX] Ruler-API: fix bug where `/api/v1/rules/<namespace>/<group_name>` endpoint return `400` instead of `404`. #4013
2425
* [BUGFIX] Distributor: reverted changes done to rate limiting in #3825. #3948
2526
* [BUGFIX] Ingester: Fix race condition when opening and closing tsdb concurrently. #3959
2627
* [BUGFIX] Querier: streamline tracing spans. #3924

integration/e2ecortex/client.go

+18
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,24 @@ func (c *Client) SetRuleGroup(rulegroup rulefmt.RuleGroup, namespace string) err
321321
return nil
322322
}
323323

324+
// GetRuleGroup gets a rule group.
325+
func (c *Client) GetRuleGroup(namespace string, groupName string) (*http.Response, error) {
326+
// Create HTTP request
327+
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/rules/%s/%s", c.rulerAddress, url.PathEscape(namespace), url.PathEscape(groupName)), nil)
328+
if err != nil {
329+
return nil, err
330+
}
331+
332+
req.Header.Set("Content-Type", "application/yaml")
333+
req.Header.Set("X-Scope-OrgID", c.orgID)
334+
335+
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
336+
defer cancel()
337+
338+
// Execute HTTP request
339+
return c.httpClient.Do(req.WithContext(ctx))
340+
}
341+
324342
// DeleteRuleGroup deletes a rule group.
325343
func (c *Client) DeleteRuleGroup(namespace string, groupName string) error {
326344
// Create HTTP request

integration/ruler_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ func TestRulerAPI(t *testing.T) {
123123
require.NoError(t, c.DeleteRuleGroup(namespaceOne, ruleGroup.Name))
124124
require.NoError(t, c.DeleteRuleNamespace(namespaceTwo))
125125

126+
// Get the rule group and ensure it returns a 404
127+
resp, err := c.GetRuleGroup(namespaceOne, ruleGroup.Name)
128+
require.NoError(t, err)
129+
defer resp.Body.Close()
130+
require.Equal(t, http.StatusNotFound, resp.StatusCode)
131+
126132
// Wait until the users manager has been terminated
127133
require.NoError(t, ruler.WaitSumMetrics(e2e.Equals(0), "cortex_ruler_managers_total"))
128134

pkg/ruler/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func (a *API) GetRuleGroup(w http.ResponseWriter, req *http.Request) {
427427

428428
rg, err := a.store.GetRuleGroup(req.Context(), userID, namespace, groupName)
429429
if err != nil {
430-
if err == rulestore.ErrGroupNotFound {
430+
if errors.Is(err, rulestore.ErrGroupNotFound) {
431431
http.Error(w, err.Error(), http.StatusNotFound)
432432
return
433433
}

0 commit comments

Comments
 (0)