Skip to content

Commit

Permalink
make sender work with the go-kit logger, use the sender, use logger f…
Browse files Browse the repository at this point in the history
…actory
  • Loading branch information
santihernandezc committed May 27, 2024
1 parent 9aadec6 commit 6555447
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
26 changes: 13 additions & 13 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (am *Alertmanager) ApplyConfig(userID string, conf *definition.PostableApiA
// Create a firewall binded to the per-tenant config.
firewallDialer := util_net.NewFirewallDialer(newFirewallDialerConfigProvider(userID, am.cfg.Limits))

integrationsMap, err := buildIntegrationsMap(am.logger, userID, conf.Receivers, tmpl, firewallDialer, am.logger, func(integrationName string, notifier notify.Notifier) notify.Notifier {
integrationsMap, err := buildIntegrationsMap(am.logger, userID, conf.Receivers, tmpl, firewallDialer, func(integrationName string, notifier notify.Notifier) notify.Notifier {
if am.cfg.Limits != nil {
rl := &tenantRateLimits{
tenant: userID,
Expand Down Expand Up @@ -456,9 +456,16 @@ func (am *Alertmanager) getFullState() (*clusterpb.FullState, error) {
return am.state.GetFullState()
}

// buildIntegrationsMap builds a map of name to the list of integration notifiers off of a
// list of receiver config.
func buildIntegrationsMap(l log.Logger, userID string, nc []*definition.PostableApiReceiver, tmpl *template.Template, firewallDialer *util_net.FirewallDialer, logger log.Logger, notifierWrapper func(string, notify.Notifier) notify.Notifier) (map[string][]notify.Integration, error) {
// buildIntegrationsMap builds a map of name to the list of integration notifiers off of a list of receiver config.
func buildIntegrationsMap(l log.Logger, userID string, nc []*definition.PostableApiReceiver, tmpl *template.Template, firewallDialer *util_net.FirewallDialer, notifierWrapper func(string, notify.Notifier) notify.Notifier) (map[string][]notify.Integration, error) {
loggerFactory := newLoggerFactory(l)
whFn := func(n alertingReceivers.Metadata) (alertingReceivers.WebhookSender, error) {
return NewSender(l), nil
}
emailFn := func(n alertingReceivers.Metadata) (alertingReceivers.EmailSender, error) {
return NewSender(l), nil
}

integrationsMap := make(map[string][]notify.Integration, len(nc))
for _, rcv := range nc {
// Check if it's a Grafana or an upstream receiver and build the integrations accordingly.
Expand All @@ -475,7 +482,7 @@ func buildIntegrationsMap(l log.Logger, userID string, nc []*definition.Postable
orgID = 0
}
// TODO: email sender is no-op for now.
integrations, err := alertingNotify.BuildReceiverIntegrations(rCfg, tmpl, &images.UnavailableProvider{}, newLoggerFactory(l), whSenderFn, emailSenderFn, orgID, version.Version)
integrations, err := alertingNotify.BuildReceiverIntegrations(rCfg, tmpl, &images.UnavailableProvider{}, loggerFactory, whFn, emailFn, orgID, version.Version)
if err != nil {
return nil, err
}
Expand All @@ -488,7 +495,7 @@ func buildIntegrationsMap(l log.Logger, userID string, nc []*definition.Postable
}
integrationsMap[rcv.Name] = finalIntegrations
} else {
integrations, err := buildReceiverIntegrations(rcv.Receiver, tmpl, firewallDialer, logger, notifierWrapper)
integrations, err := buildReceiverIntegrations(rcv.Receiver, tmpl, firewallDialer, l, notifierWrapper)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -567,13 +574,6 @@ func buildReceiverIntegrations(nc config.Receiver, tmpl *template.Template, fire
return integrations, nil
}

func whSenderFn(n alertingReceivers.Metadata) (alertingReceivers.WebhookSender, error) {
return &Sender{}, nil
}
func emailSenderFn(n alertingReceivers.Metadata) (alertingReceivers.EmailSender, error) {
return &Sender{}, nil
}

func md5HashAsMetricValue(data []byte) float64 {
sum := md5.Sum(data)
// We only want 48 bits as a float64 only has a 53 bit mantissa.
Expand Down
2 changes: 0 additions & 2 deletions pkg/alertmanager/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type alertingLogger struct {
}

// newLoggerFactory returns a function that implements the alertingLogging.LoggerFactory interface.
//
//lint:ignore U1000 Ignore unused functions for now, they will be used to create the Grafana notifiers.
func newLoggerFactory(logger log.Logger) alertingLogging.LoggerFactory {
return func(loggerName string, ctx ...any) alertingLogging.Logger {
keyvals := append([]any{"logger", loggerName}, ctx...)
Expand Down
18 changes: 10 additions & 8 deletions pkg/alertmanager/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"net/http"
"time"

alertingLogging "github.com/grafana/alerting/logging"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
alertingReceivers "github.com/grafana/alerting/receivers"
"github.com/pkg/errors"

Expand All @@ -29,10 +30,10 @@ var (

type Sender struct {
c *http.Client
log alertingLogging.Logger
log log.Logger
}

func NewSender(log alertingLogging.Logger) *Sender {
func NewSender(log log.Logger) *Sender {
netTransport := &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateFreelyAsClient,
Expand All @@ -59,7 +60,7 @@ func (s *Sender) SendWebhook(ctx context.Context, cmd *alertingReceivers.SendWeb
cmd.HTTPMethod = http.MethodPost
}

s.log.Debug("Sending webhook", "url", cmd.URL, "http method", cmd.HTTPMethod)
level.Debug(s.log).Log("msg", "Sending webhook", "url", cmd.URL, "http method", cmd.HTTPMethod)

if cmd.HTTPMethod != http.MethodPost && cmd.HTTPMethod != http.MethodPut {
return ErrInvalidMethod
Expand Down Expand Up @@ -91,7 +92,7 @@ func (s *Sender) SendWebhook(ctx context.Context, cmd *alertingReceivers.SendWeb
}
defer func() {
if err := resp.Body.Close(); err != nil {
s.log.Warn("Failed to close response body", "err", err)
level.Warn(s.log).Log("msg", "Failed to close response body", "err", err)
}
}()

Expand All @@ -103,21 +104,22 @@ func (s *Sender) SendWebhook(ctx context.Context, cmd *alertingReceivers.SendWeb
if cmd.Validation != nil {
err := cmd.Validation(body, resp.StatusCode)
if err != nil {
s.log.Debug("Webhook failed validation", "url", cmd.URL, "statuscode", resp.Status, "body", string(body))
level.Debug(s.log).Log("msg", "Webhook failed validation", "url", cmd.URL, "statuscode", resp.Status, "body", string(body))
return fmt.Errorf("webhook failed validation: %w", err)
}
}

if resp.StatusCode/100 == 2 {
s.log.Debug("Webhook succeeded", "url", cmd.URL, "statuscode", resp.Status)
level.Debug(s.log).Log("msg", "Webhook succeeded", "url", cmd.URL, "statuscode", resp.Status)
return nil
}

s.log.Debug("Webhook failed", "url", cmd.URL, "statuscode", resp.Status, "body", string(body))
level.Debug(s.log).Log("msg", "Webhook failed", "url", cmd.URL, "statuscode", resp.Status, "body", string(body))
return fmt.Errorf("webhook response status %v", resp.Status)
}

// SendEmail implements alertingReceivers.EmailSender.
// TODO: no-op for now, implement.
func (s *Sender) SendEmail(ctx context.Context, cmd *alertingReceivers.SendEmailSettings) error {
return nil
}

0 comments on commit 6555447

Please sign in to comment.