Skip to content

Commit

Permalink
fix for #5989
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Rajagopal <anrajag@amazon.com>
  • Loading branch information
rajagopalanand committed Jun 20, 2024
1 parent d664dd7 commit 9103987
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [CHANGE] Upgrade Dockerfile Node version from 14x to 18x. #5906
* [CHANGE] Ingester: Remove `-querier.query-store-for-labels-enabled` flag. Querying long-term store for labels is always enabled. #5984
* [FEATURE] Ingester: Experimental: Enable native histogram ingestion via `-blocks-storage.tsdb.enable-native-histograms` flag. #5986
* [FEATURE] Ruler: Add support for filtering out alerts in ListRules API. #6011
* [ENHANCEMENT] rulers: Add support to persist tokens in rulers. #5987
* [ENHANCEMENT] Query Frontend/Querier: Added store gateway postings touched count and touched size in Querier stats and log in Query Frontend. #5892
* [ENHANCEMENT] Query Frontend/Querier: Returns `warnings` on prometheus query responses. #5916
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ shell:
bash

configs-integration-test:
/bin/bash -c "go test -v -tags 'netgo integration' -timeout 30s ./pkg/configs/... ./pkg/ruler/..."
/bin/bash -c "go test -v -tags 'netgo integration' -timeout 60s ./pkg/configs/... ./pkg/ruler/..."

mod-check:
GO111MODULE=on go mod download
Expand Down
21 changes: 21 additions & 0 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,18 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
return
}

excludeAlerts, err := parseExcludeAlerts(req)
if err != nil {
util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("invalid parameter %q: %s", "exclude_alerts", err.Error()), http.StatusBadRequest)
return
}

rulesRequest := RulesRequest{
RuleNames: req.Form["rule_name[]"],
RuleGroupNames: req.Form["rule_group[]"],
Files: req.Form["file[]"],
Type: typ,
ExcludeAlerts: excludeAlerts,
}

w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -234,6 +241,20 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
}
}

func parseExcludeAlerts(r *http.Request) (bool, error) {
excludeAlertsParam := strings.ToLower(r.URL.Query().Get("exclude_alerts"))

if excludeAlertsParam == "" {
return false, nil
}

excludeAlerts, err := strconv.ParseBool(excludeAlertsParam)
if err != nil {
return false, fmt.Errorf("error converting exclude_alerts: %w", err)
}
return excludeAlerts, nil
}

func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) {
logger := util_log.WithContext(req.Context(), a.logger)
userID, err := tenant.TenantID(req.Context())
Expand Down
29 changes: 16 additions & 13 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,19 +927,21 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
continue
}
alerts := []*AlertStateDesc{}
for _, a := range rule.ActiveAlerts() {
alerts = append(alerts, &AlertStateDesc{
State: a.State.String(),
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
Value: a.Value,
ActiveAt: a.ActiveAt,
FiredAt: a.FiredAt,
ResolvedAt: a.ResolvedAt,
LastSentAt: a.LastSentAt,
ValidUntil: a.ValidUntil,
KeepFiringSince: a.KeepFiringSince,
})
if !rulesRequest.ExcludeAlerts {
for _, a := range rule.ActiveAlerts() {
alerts = append(alerts, &AlertStateDesc{
State: a.State.String(),
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
Value: a.Value,
ActiveAt: a.ActiveAt,
FiredAt: a.FiredAt,
ResolvedAt: a.ResolvedAt,
LastSentAt: a.LastSentAt,
ValidUntil: a.ValidUntil,
KeepFiringSince: a.KeepFiringSince,
})
}
}
ruleDesc = &RuleStateDesc{
Rule: &rulespb.RuleDesc{
Expand Down Expand Up @@ -1146,6 +1148,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest
RuleGroupNames: rulesRequest.GetRuleGroupNames(),
Files: rulesRequest.GetFiles(),
Type: rulesRequest.GetType(),
ExcludeAlerts: rulesRequest.GetExcludeAlerts(),
})

if err != nil {
Expand Down
146 changes: 97 additions & 49 deletions pkg/ruler/ruler.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/ruler/ruler.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ message RulesRequest {
repeated string ruleGroupNames = 2;
repeated string files = 3;
string type = 4;
bool excludeAlerts = 5;
}

message RulesResponse {
Expand Down
Loading

0 comments on commit 9103987

Please sign in to comment.