mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Allow the notifier to log (#34232)
* Alerting: Allow the notifier to log The notifier upstream code uses go-kit as its logging library. The grafana specific logger is not compatible with this API. In this PR, I have created a wrapper that implements io.Writer to make them compatible.
This commit is contained in:
parent
7b32c5439b
commit
6384f86fb9
24
pkg/services/ngalert/logging/logging.go
Normal file
24
pkg/services/ngalert/logging/logging.go
Normal file
@ -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
|
||||
}
|
@ -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() {
|
||||
|
@ -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{}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user