Skip to content

Commit

Permalink
Fixed push of PrometheusRuleGroup (#469)
Browse files Browse the repository at this point in the history
fixed error messages received from the API and the import of PrometheusRuleGroup
  • Loading branch information
vladshub authored Oct 17, 2024
1 parent d736558 commit 95f9a2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 7 additions & 5 deletions pkg/mimir/client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/mimir/rules-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 95f9a2c

Please sign in to comment.