Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-12741 Percona templated alerts - interval & folder uid #2919

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 102 additions & 88 deletions api/managementpb/alerting/alerting.pb.go

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions api/managementpb/alerting/alerting.pb.validate.go

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

2 changes: 2 additions & 0 deletions api/managementpb/alerting/alerting.proto
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ message CreateRuleRequest {
map<string, string> custom_labels = 8;
// Filters.
repeated Filter filters = 9;
// Evaluation Interval
google.protobuf.Duration interval = 10;
}

message CreateRuleResponse {}
Expand Down
5 changes: 5 additions & 0 deletions api/managementpb/alerting/json/alerting.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
"type": "string",
"x-order": 2
},
"interval": {
"type": "string",
"title": "Evaluation Interval",
"x-order": 9
},
"name": {
"description": "Rule name.",
"type": "string",
Expand Down

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

5 changes: 5 additions & 0 deletions api/swagger/swagger-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -26667,6 +26667,11 @@
}
},
"x-order": 8
},
"interval": {
"type": "string",
"title": "Evaluation Interval",
"x-order": 9
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -21933,6 +21933,11 @@
}
},
"x-order": 8
},
"interval": {
"type": "string",
"title": "Evaluation Interval",
"x-order": 9
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions managed/services/grafana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import (
// ErrFailedToGetToken means it failed to get user's token. Most likely due to the fact user is not logged in using Percona Account.
var ErrFailedToGetToken = errors.New("failed to get token")

const defaultEvaluationInterval = time.Minute

// Client represents a client for Grafana API.
type Client struct {
addr string
Expand Down Expand Up @@ -408,7 +406,7 @@ func (c *Client) DeleteAPIKeyByID(ctx context.Context, id int64) error {
}

// CreateAlertRule creates Grafana alert rule.
func (c *Client) CreateAlertRule(ctx context.Context, folderName, groupName string, rule *services.Rule) error {
func (c *Client) CreateAlertRule(ctx context.Context, folderUid, groupName, interval string, rule *services.Rule) error {
matejkubinec marked this conversation as resolved.
Show resolved Hide resolved
authHeaders, err := c.authHeadersFromContext(ctx)
if err != nil {
return err
Expand All @@ -421,7 +419,7 @@ func (c *Client) CreateAlertRule(ctx context.Context, folderName, groupName stri
}

var group AlertRuleGroup
if err := c.do(ctx, http.MethodGet, fmt.Sprintf("/api/ruler/grafana/api/v1/rules/%s/%s", folderName, groupName), "", authHeaders, nil, &group); err != nil {
if err := c.do(ctx, http.MethodGet, fmt.Sprintf("/api/ruler/grafana/api/v1/rules/%s/%s", folderUid, groupName), "", authHeaders, nil, &group); err != nil {
return err
}

Expand All @@ -433,8 +431,7 @@ func (c *Client) CreateAlertRule(ctx context.Context, folderName, groupName stri
group.Rules = append(group.Rules, b)

if group.Interval == "" {
// TODO: align it with grafanas default value: https://grafana.com/docs/grafana/v9.0/setup-grafana/configure-grafana/#min_interval
group.Interval = defaultEvaluationInterval.String()
group.Interval = interval
}

if err = validateDurations(group.Interval, rule.For); err != nil {
Expand All @@ -446,7 +443,7 @@ func (c *Client) CreateAlertRule(ctx context.Context, folderName, groupName stri
return err
}

if err := c.do(ctx, "POST", fmt.Sprintf("/api/ruler/grafana/api/v1/rules/%s", folderName), "", authHeaders, body, nil); err != nil {
if err := c.do(ctx, "POST", fmt.Sprintf("/api/ruler/grafana/api/v1/rules/%s", folderUid), "", authHeaders, body, nil); err != nil {
if err != nil {
if cErr, ok := errors.Cause(err).(*clientError); ok { //nolint:errorlint
return status.Error(codes.InvalidArgument, cErr.ErrorMessage)
Expand Down
2 changes: 1 addition & 1 deletion managed/services/management/alerting/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

type grafanaClient interface {
CreateAlertRule(ctx context.Context, folderName, groupName string, rule *services.Rule) error
CreateAlertRule(ctx context.Context, folderdUid, groupName, interval string, rule *services.Rule) error
matejkubinec marked this conversation as resolved.
Show resolved Hide resolved
GetDatasourceUIDByID(ctx context.Context, id int64) (string, error)
GetFolderByUID(ctx context.Context, uid string) (*gapi.Folder, error)
}

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

18 changes: 10 additions & 8 deletions managed/services/management/alerting/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ import (
)

const (
templatesDir = "/srv/alerting/templates"
portalRequestTimeout = 2 * time.Minute // time limit to get templates list from the portal
templatesDir = "/srv/alerting/templates"
portalRequestTimeout = 2 * time.Minute // time limit to get templates list from the portal
defaultEvaluationInterval = time.Minute

dirPerm = os.FileMode(0o775)
)
Expand Down Expand Up @@ -701,11 +702,6 @@ func (s *Service) CreateRule(ctx context.Context, req *alerting.CreateRuleReques
return nil, status.Error(codes.InvalidArgument, "Rule group name should be specified.")
}

folder, err := s.grafanaClient.GetFolderByUID(ctx, req.FolderUid)
if err != nil {
return nil, err
}

metricsDatasourceUID, err := s.grafanaClient.GetDatasourceUIDByID(ctx, 1) // 1 - it's id of Metrics datasource in PMM
if err != nil {
return nil, err
Expand Down Expand Up @@ -803,7 +799,13 @@ func (s *Service) CreateRule(ctx context.Context, req *alerting.CreateRuleReques
Labels: labels,
}

err = s.grafanaClient.CreateAlertRule(ctx, folder.Title, req.Group, &rule)
// TODO: align it with grafanas default value: https://grafana.com/docs/grafana/v9.0/setup-grafana/configure-grafana/#min_interval
interval := defaultEvaluationInterval.String()
if req.Interval != nil {
interval = req.Interval.AsDuration().String()
}

err = s.grafanaClient.CreateAlertRule(ctx, req.FolderUid, req.Group, interval, &rule)
if err != nil {
return nil, err
}
Expand Down
Loading