From 95f9a2c34e7c09f1bf4d2419bff68c231269c33a Mon Sep 17 00:00:00 2001 From: Vladislav Shub <1196390+vladshub@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:56:25 +0300 Subject: [PATCH] Fixed push of PrometheusRuleGroup (#469) fixed error messages received from the API and the import of PrometheusRuleGroup --- pkg/mimir/client/http_client.go | 12 +++++++----- pkg/mimir/rules-handler.go | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/mimir/client/http_client.go b/pkg/mimir/client/http_client.go index 46ce8939..87adc2b8 100644 --- a/pkg/mimir/client/http_client.go +++ b/pkg/mimir/client/http_client.go @@ -10,10 +10,12 @@ import ( "net/http" "os" "strconv" + "strings" "time" "github.com/grafana/grizzly/pkg/config" "github.com/grafana/grizzly/pkg/mimir/models" + "github.com/hashicorp/go-multierror" "gopkg.in/yaml.v3" ) @@ -73,7 +75,7 @@ func (c *Client) CreateRules(resource models.PrometheusRuleGrouping) error { } if _, err = c.doRequest(http.MethodPost, url, out); err != nil { - return fmt.Errorf("error found creating rule group: %s", group.Name) + return multierror.Append(fmt.Errorf("error found creating rule group: %s", group.Name), err) } } @@ -109,15 +111,15 @@ func (c *Client) doRequest(method string, url string, body []byte) ([]byte, erro return nil, fmt.Errorf("request to load rules failed: %s", err) } - if res.StatusCode >= 300 { - return nil, fmt.Errorf("error loading rules: %d", res.StatusCode) - } - b, err := io.ReadAll(res.Body) if err != nil { return nil, fmt.Errorf("cannot read response body: %s", err) } + if res.StatusCode >= 300 { + return nil, fmt.Errorf("error loading rules: %d, error: %s", res.StatusCode, strings.TrimSpace(string(b))) + } + return b, nil } diff --git a/pkg/mimir/rules-handler.go b/pkg/mimir/rules-handler.go index a3e2bf35..032b5288 100644 --- a/pkg/mimir/rules-handler.go +++ b/pkg/mimir/rules-handler.go @@ -4,10 +4,9 @@ import ( "fmt" "strings" + "github.com/grafana/grizzly/pkg/grizzly" "github.com/grafana/grizzly/pkg/mimir/client" "github.com/grafana/grizzly/pkg/mimir/models" - - "github.com/grafana/grizzly/pkg/grizzly" ) // RuleHandler is a Grizzly Handler for Prometheus Rules @@ -136,6 +135,20 @@ func (h *RuleHandler) writeRuleGroup(resource grizzly.Resource) error { rules := resource.Spec()["rules"].([]interface{}) for _, ruleIf := range rules { rule := ruleIf.(map[string]interface{}) + // In case that the field "type" is recording, we need to change the field "name" to "record" + // In case that the field "type" is alerting, we need to change the field "name" to "alert" + if rule["type"] == "recording" { + rule["record"] = rule["name"] + delete(rule, "name") + } else if rule["type"] == "alerting" { + rule["alert"] = rule["name"] + delete(rule, "name") + } + // The field query, should be changed to expr + if rule["query"] != nil { + rule["expr"] = rule["query"] + delete(rule, "query") + } newGroup.Rules = append(newGroup.Rules, rule) } grouping := models.PrometheusRuleGrouping{