Skip to content

Commit

Permalink
Merge branch 'alertmanager-controller' into alertmanager-test
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoBrigitte committed Dec 17, 2024
2 parents ec33d05 + 10a85c1 commit df3f51f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ global:
slack_api_url: {{ .Values.alerting.slackAPIURL }}
{{- end }}

templates:
- notification-template.tmpl
- url-template.tmpl

route:
group_by: [alertname, cluster_id, installation, status, team]
group_interval: 15m
Expand Down
17 changes: 6 additions & 11 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"context"
"fmt"
"io"
"maps"
"net/http"
"path"
"slices"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -94,35 +92,32 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte
logger := log.FromContext(ctx)

// Load alertmanager configuration
alertmanagerConfig, err := config.Load(string(alertmanagerConfigContent))
_, err := config.Load(string(alertmanagerConfigContent))
if err != nil {
return errors.WithStack(fmt.Errorf("alertmanager: failed to load configuration: %w", err))
}

// Set template names
// Values set here must match the keys set in requestData.TemplateFiles
alertmanagerConfig.Templates = slices.Collect(maps.Keys(templates))
alertmanagerConfigString := alertmanagerConfig.String()

// Prepare request for Alertmanager API
requestData := configRequest{
AlertmanagerConfig: alertmanagerConfigString,
requestData := &configRequest{
AlertmanagerConfig: string(alertmanagerConfigContent),
TemplateFiles: templates,
}
data, err := yaml.Marshal(requestData)
if err != nil {
return errors.WithStack(fmt.Errorf("alertmanager: failed to marshal yaml: %w", err))
}
dateLen := len(data)

url := s.alertmanagerURL + alertmanagerAPIPath
logger.WithValues("url", url, "data_size", len(data), "config_size", len(alertmanagerConfigString), "templates_count", len(templates)).Info("Alertmanager: sending configuration")
logger.WithValues("url", url, "data_size", dataLen, "config_size", len(alertmanagerConfigContent), "templates_count", len(templates)).Info("Alertmanager: sending configuration")

// Send request to Alertmanager's API
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return errors.WithStack(fmt.Errorf("alertmanager: failed to create request: %w", err))
}
req.Header.Set(common.OrgIDHeader, tenantID)
req.ContentLength = int64(dateLen)

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down

0 comments on commit df3f51f

Please sign in to comment.