Skip to content

Commit

Permalink
More updates
Browse files Browse the repository at this point in the history
- Fix delete notification template status code should be 204
- Add annotations to alerts
- Derive notification templates name from file name
  • Loading branch information
leeyikjiun committed Feb 4, 2025
1 parent 6d6c525 commit 77c2cdf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion observability-lib/api/notification-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) DeleteNotificationTemplate(name string) (DeleteNotificationTemp
}

statusCode := resp.StatusCode()
if statusCode != 200 {
if statusCode != 204 {
return DeleteNotificationTemplateResponse{}, resp, fmt.Errorf("error deleting notification template, received unexpected status code %d: %s", statusCode, resp.String())
}

Expand Down
8 changes: 6 additions & 2 deletions observability-lib/grafana/alerts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package grafana

import (
"maps"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"github.com/grafana/grafana-foundation-sdk/go/cog"
"github.com/grafana/grafana-foundation-sdk/go/expr"
Expand Down Expand Up @@ -171,6 +173,7 @@ type AlertOptions struct {
For string
NoDataState alerting.RuleNoDataState
RuleExecErrState alerting.RuleExecErrState
Annotations map[string]string
Tags map[string]string
Query []RuleQuery
QueryRefCondition string
Expand All @@ -196,11 +199,12 @@ func NewAlertRule(options *AlertOptions) *alerting.RuleBuilder {
options.QueryRefCondition = "A"
}

annotations := map[string]string{
annotations := maps.Clone(options.Annotations)
maps.Copy(annotations, map[string]string{
"summary": options.Summary,
"description": options.Description,
"runbook_url": options.RunbookURL,
}
})

if options.PanelTitle != "" {
annotations["panel_title"] = options.PanelTitle
Expand Down
13 changes: 10 additions & 3 deletions observability-lib/grafana/notification-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ package grafana
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"gopkg.in/yaml.v3"
)

func NewNotificationTemplatesFromFile(filepath string) (map[string]alerting.NotificationTemplate, error) {
func NewNotificationTemplatesFromFile(filePath string) (map[string]alerting.NotificationTemplate, error) {
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
if fileName == "notification-templates" {
fileName = "chainlink"
}

notificationTemplates := make(map[string]alerting.NotificationTemplate)
yamlFileToMapRes, errFileToMap := yamlFileToMap(filepath)
yamlFileToMapRes, errFileToMap := yamlFileToMap(filePath)
if errFileToMap != nil {
return nil, errFileToMap
}

for typeTemplate, template := range yamlFileToMapRes {
newTemplate, err := alerting.NewNotificationTemplateBuilder().
Name(fmt.Sprintf("chainlink-%s-notification-template", typeTemplate)).
Name(fmt.Sprintf("%s-%s-notification-template", fileName, typeTemplate)).
Template(template).Build()
if err != nil {
return nil, err
Expand Down

0 comments on commit 77c2cdf

Please sign in to comment.