diff --git a/pkg/services/ngalert/logging/logging.go b/pkg/services/ngalert/logging/logging.go new file mode 100644 index 00000000000..7e5b695c59c --- /dev/null +++ b/pkg/services/ngalert/logging/logging.go @@ -0,0 +1,24 @@ +package logging + +import ( + "strings" + + glog "github.com/grafana/grafana/pkg/infra/log" +) + +// GoKitWrapper wraps around the grafana-specific logger to make a compatible logger for go-kit. +type GoKitWrapper struct { + logger glog.Logger +} + +// NewWrapper creates a new go-kit wrapper for a grafana-specific logger +func NewWrapper(l glog.Logger) *GoKitWrapper { + return &GoKitWrapper{logger: l} +} + +// Write implements io.Writer +func (w *GoKitWrapper) Write(p []byte) (n int, err error) { + withoutNewline := strings.TrimSuffix(string(p), "\n") + w.logger.Info(withoutNewline) + return len(p), nil +} diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 3392bb8bb5b..e83b06d66f6 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -29,6 +29,7 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/models" apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" + "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/services/ngalert/metrics" ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/services/ngalert/notifier/channels" @@ -75,7 +76,9 @@ const ( ) type Alertmanager struct { - logger log.Logger + logger log.Logger + gokitLogger gokit_log.Logger + Settings *setting.Cfg `inject:""` SQLStore *sqlstore.SQLStore `inject:""` Store store.AlertingStore @@ -116,6 +119,8 @@ func New(cfg *setting.Cfg, store store.AlertingStore, m *metrics.Metrics) (*Aler Metrics: m, } + am.gokitLogger = gokit_log.NewLogfmtLogger(logging.NewWrapper(am.logger)) + // Initialize the notification log am.wg.Add(1) var err error @@ -144,7 +149,7 @@ func New(cfg *setting.Cfg, store store.AlertingStore, m *metrics.Metrics) (*Aler }() // Initialize in-memory alerts - am.alerts, err = mem.NewAlerts(context.Background(), am.marker, memoryAlertsGCInterval, gokit_log.NewNopLogger()) + am.alerts, err = mem.NewAlerts(context.Background(), am.marker, memoryAlertsGCInterval, am.gokitLogger) if err != nil { return nil, fmt.Errorf("unable to initialize the alert provider component of alerting: %w", err) } @@ -324,8 +329,8 @@ func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig am.dispatcher.Stop() } - am.inhibitor = inhibit.NewInhibitor(am.alerts, cfg.AlertmanagerConfig.InhibitRules, am.marker, gokit_log.NewNopLogger()) - am.silencer = silence.NewSilencer(am.silences, am.marker, gokit_log.NewNopLogger()) + am.inhibitor = inhibit.NewInhibitor(am.alerts, cfg.AlertmanagerConfig.InhibitRules, am.marker, am.gokitLogger) + am.silencer = silence.NewSilencer(am.silences, am.marker, am.gokitLogger) inhibitionStage := notify.NewMuteStage(am.inhibitor) silencingStage := notify.NewMuteStage(am.silencer) @@ -335,7 +340,7 @@ func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig } am.route = dispatch.NewRoute(cfg.AlertmanagerConfig.Route, nil) - am.dispatcher = dispatch.NewDispatcher(am.alerts, am.route, routingStage, am.marker, timeoutFunc, gokit_log.NewNopLogger(), am.dispatcherMetrics) + am.dispatcher = dispatch.NewDispatcher(am.alerts, am.route, routingStage, am.marker, timeoutFunc, am.gokitLogger, am.dispatcherMetrics) am.wg.Add(1) go func() { diff --git a/pkg/services/ngalert/notifier/alertmanager_test.go b/pkg/services/ngalert/notifier/alertmanager_test.go index c6d0cec0665..c074b91f06d 100644 --- a/pkg/services/ngalert/notifier/alertmanager_test.go +++ b/pkg/services/ngalert/notifier/alertmanager_test.go @@ -19,6 +19,7 @@ import ( "github.com/stretchr/testify/require" apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" + "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/services/ngalert/metrics" "github.com/grafana/grafana/pkg/services/ngalert/store" "github.com/grafana/grafana/pkg/services/sqlstore" @@ -276,7 +277,7 @@ func TestPutAlert(t *testing.T) { t.Run(c.title, func(t *testing.T) { r := prometheus.NewRegistry() am.marker = types.NewMarker(r) - am.alerts, err = mem.NewAlerts(context.Background(), am.marker, 15*time.Minute, gokit_log.NewNopLogger()) + am.alerts, err = mem.NewAlerts(context.Background(), am.marker, 15*time.Minute, gokit_log.NewLogfmtLogger(logging.NewWrapper(am.logger))) require.NoError(t, err) alerts := []*types.Alert{} diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go index 0938720a497..83bf3e444b7 100644 --- a/pkg/services/ngalert/notifier/channels/dingding.go +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -17,6 +17,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) const defaultDingdingMsgType = "link" @@ -67,7 +68,7 @@ func (dd *DingDingNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9 messageURL := "dingtalk://dingtalkclient/page/link?" + q.Encode() - data := notify.GetTemplateData(ctx, dd.tmpl, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, dd.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(dd.log))) var tmplErr error tmpl := notify.TmplText(dd.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/email.go b/pkg/services/ngalert/notifier/channels/email.go index ae18b2ec1ee..d10b9b00d64 100644 --- a/pkg/services/ngalert/notifier/channels/email.go +++ b/pkg/services/ngalert/notifier/channels/email.go @@ -15,6 +15,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/util" ) @@ -59,7 +60,7 @@ func NewEmailNotifier(model *models.AlertNotification, t *template.Template) (*E // Notify sends the alert notification. func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { // We only need ExternalURL from this template object. This hack should go away with https://github.com/prometheus/alertmanager/pull/2508. - data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: en.tmpl.ExternalURL}, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: en.tmpl.ExternalURL}, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(en.log))) var tmplErr error tmpl := notify.TmplText(en.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/pagerduty.go b/pkg/services/ngalert/notifier/channels/pagerduty.go index f72fb338d1b..9b0d3059488 100644 --- a/pkg/services/ngalert/notifier/channels/pagerduty.go +++ b/pkg/services/ngalert/notifier/channels/pagerduty.go @@ -17,6 +17,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) const ( @@ -118,7 +119,7 @@ func (pn *PagerdutyNotifier) buildPagerdutyMessage(ctx context.Context, alerts m eventType = pagerDutyEventResolve } - data := notify.GetTemplateData(ctx, pn.tmpl, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, pn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(pn.log))) var tmplErr error tmpl := notify.TmplText(pn.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 08506e7468f..91b318e5164 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -25,6 +25,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" "github.com/grafana/grafana/pkg/setting" ) @@ -239,7 +240,7 @@ var sendSlackRequest = func(request *http.Request, logger log.Logger) error { } func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, as []*types.Alert) (*slackMessage, error) { - data := notify.GetTemplateData(ctx, sn.tmpl, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, sn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(sn.log))) alerts := types.Alerts(as...) var tmplErr error tmpl := notify.TmplText(sn.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/teams.go b/pkg/services/ngalert/notifier/channels/teams.go index f291c88859b..fe196ad9693 100644 --- a/pkg/services/ngalert/notifier/channels/teams.go +++ b/pkg/services/ngalert/notifier/channels/teams.go @@ -16,6 +16,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) // TeamsNotifier is responsible for sending @@ -50,7 +51,7 @@ func NewTeamsNotifier(model *models.AlertNotification, t *template.Template) (*T // Notify send an alert notification to Microsoft teams. func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { - data := notify.GetTemplateData(ctx, tn.tmpl, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, tn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(tn.log))) var tmplErr error tmpl := notify.TmplText(tn.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index 9d7cbb9daf0..12a9f583851 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -16,6 +16,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) var ( @@ -111,7 +112,7 @@ func (tn *TelegramNotifier) buildTelegramMessage(ctx context.Context, as []*type msg["chat_id"] = tn.ChatID msg["parse_mode"] = "html" - data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: tn.tmpl.ExternalURL}, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: tn.tmpl.ExternalURL}, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(tn.log))) var tmplErr error tmpl := notify.TmplText(tn.tmpl, data, &tmplErr) diff --git a/pkg/services/ngalert/notifier/channels/webhook.go b/pkg/services/ngalert/notifier/channels/webhook.go index e6ceb67a37f..6b1c7eefc7f 100644 --- a/pkg/services/ngalert/notifier/channels/webhook.go +++ b/pkg/services/ngalert/notifier/channels/webhook.go @@ -16,6 +16,7 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/services/ngalert/logging" ) // WebhookNotifier is responsible for sending @@ -74,7 +75,7 @@ func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool } as, numTruncated := truncateAlerts(wn.MaxAlerts, as) - data := notify.GetTemplateData(ctx, wn.tmpl, as, gokit_log.NewNopLogger()) + data := notify.GetTemplateData(ctx, wn.tmpl, as, gokit_log.NewLogfmtLogger(logging.NewWrapper(wn.log))) var tmplErr error tmpl := notify.TmplText(wn.tmpl, data, &tmplErr)