From 6555447810ca44ccc7df48f7f30227e4b4d0f42b Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 27 May 2024 12:11:28 +0200 Subject: [PATCH] make sender work with the go-kit logger, use the sender, use logger factory --- pkg/alertmanager/alertmanager.go | 26 +++++++++++++------------- pkg/alertmanager/log.go | 2 -- pkg/alertmanager/sender.go | 18 ++++++++++-------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index 14ce983a885..271442c72e1 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -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, @@ -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. @@ -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 } @@ -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 } @@ -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. diff --git a/pkg/alertmanager/log.go b/pkg/alertmanager/log.go index b64ac23938c..9b0bcafaf42 100644 --- a/pkg/alertmanager/log.go +++ b/pkg/alertmanager/log.go @@ -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...) diff --git a/pkg/alertmanager/sender.go b/pkg/alertmanager/sender.go index 77e5eea6b68..f76e9bb41f8 100644 --- a/pkg/alertmanager/sender.go +++ b/pkg/alertmanager/sender.go @@ -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" @@ -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, @@ -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 @@ -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) } }() @@ -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 }