From 0e7c95a4d2a85643b5c5eac73f6482e6547bbf41 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Wed, 14 Dec 2022 16:21:55 -0500 Subject: [PATCH] Alerting: Remove reference to global models package in channels package (#60358) * remove intermediate struct to create Base struct * fix alertmanager --- .../ngalert/notifier/channels/alertmanager.go | 8 +------- .../ngalert/notifier/channels/base.go | 20 +++++-------------- .../ngalert/notifier/channels/dingding.go | 9 +-------- .../ngalert/notifier/channels/discord.go | 10 +--------- .../ngalert/notifier/channels/email.go | 9 +-------- .../ngalert/notifier/channels/googlechat.go | 9 +-------- .../ngalert/notifier/channels/kafka.go | 8 +------- .../ngalert/notifier/channels/line.go | 9 +-------- .../ngalert/notifier/channels/opsgenie.go | 9 +-------- .../ngalert/notifier/channels/pagerduty.go | 9 +-------- .../ngalert/notifier/channels/pushover.go | 10 +--------- .../ngalert/notifier/channels/sensugo.go | 10 +--------- .../ngalert/notifier/channels/slack.go | 9 +-------- .../ngalert/notifier/channels/teams.go | 9 +-------- .../ngalert/notifier/channels/telegram.go | 9 +-------- .../ngalert/notifier/channels/threema.go | 9 +-------- .../ngalert/notifier/channels/victorops.go | 9 +-------- .../ngalert/notifier/channels/webex.go | 9 +-------- .../ngalert/notifier/channels/webhook.go | 8 +------- .../ngalert/notifier/channels/wecom.go | 9 +-------- 20 files changed, 24 insertions(+), 167 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/alertmanager.go b/pkg/services/ngalert/notifier/channels/alertmanager.go index 0430653a4e3..b8f4efad65b 100644 --- a/pkg/services/ngalert/notifier/channels/alertmanager.go +++ b/pkg/services/ngalert/notifier/channels/alertmanager.go @@ -13,7 +13,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) // GetDecryptedValueFn is a function that returns the decrypted value of @@ -70,12 +69,7 @@ func AlertmanagerFactory(fc FactoryConfig) (NotificationChannel, error) { // NewAlertmanagerNotifier returns a new Alertmanager notifier. func NewAlertmanagerNotifier(config *AlertmanagerConfig, images ImageStore, _ *template.Template, fn GetDecryptedValueFn) *AlertmanagerNotifier { return &AlertmanagerNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: config.UID, - Name: config.Name, - DisableResolveMessage: config.DisableResolveMessage, - Settings: config.Settings, - }), + Base: NewBase(config.NotificationChannelConfig), images: images, urls: config.URLs, basicAuthUser: config.BasicAuthUser, diff --git a/pkg/services/ngalert/notifier/channels/base.go b/pkg/services/ngalert/notifier/channels/base.go index 6f261bc7ef0..99a1aaeef11 100644 --- a/pkg/services/ngalert/notifier/channels/base.go +++ b/pkg/services/ngalert/notifier/channels/base.go @@ -1,32 +1,22 @@ package channels -import ( - "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" -) - // Base is the base implementation of a notifier. It contains the common fields across all notifier types. type Base struct { Name string Type string UID string - IsDefault bool DisableResolveMessage bool - - log log.Logger } func (n *Base) GetDisableResolveMessage() bool { return n.DisableResolveMessage } -func NewBase(model *models.AlertNotification) *Base { +func NewBase(cfg *NotificationChannelConfig) *Base { return &Base{ - UID: model.Uid, - Name: model.Name, - IsDefault: model.IsDefault, - Type: model.Type, - DisableResolveMessage: model.DisableResolveMessage, - log: log.New("alerting.notifier." + model.Name), + UID: cfg.UID, + Name: cfg.Name, + Type: cfg.Type, + DisableResolveMessage: cfg.DisableResolveMessage, } } diff --git a/pkg/services/ngalert/notifier/channels/dingding.go b/pkg/services/ngalert/notifier/channels/dingding.go index f5ce6f90d10..5d90e24b078 100644 --- a/pkg/services/ngalert/notifier/channels/dingding.go +++ b/pkg/services/ngalert/notifier/channels/dingding.go @@ -11,7 +11,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const defaultDingdingMsgType = "link" @@ -54,13 +53,7 @@ func newDingDingNotifier(fc FactoryConfig) (*DingDingNotifier, error) { return nil, err } return &DingDingNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.dingding"), ns: fc.NotificationService, tmpl: fc.Template, diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go index cf3a50c1b96..3e3d597fe3c 100644 --- a/pkg/services/ngalert/notifier/channels/discord.go +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -18,7 +18,6 @@ import ( "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -67,14 +66,7 @@ func newDiscordNotifier(fc FactoryConfig) (*DiscordNotifier, error) { } return &DiscordNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - SecureSettings: fc.Config.SecureSettings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.discord"), ns: fc.NotificationService, images: fc.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/email.go b/pkg/services/ngalert/notifier/channels/email.go index 56ea5534c06..438c0462ce9 100644 --- a/pkg/services/ngalert/notifier/channels/email.go +++ b/pkg/services/ngalert/notifier/channels/email.go @@ -12,7 +12,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/util" ) @@ -69,13 +68,7 @@ func NewEmailConfig(config *NotificationChannelConfig) (*EmailConfig, error) { // for the EmailNotifier. func NewEmailNotifier(config *EmailConfig, ns EmailSender, images ImageStore, t *template.Template) *EmailNotifier { return &EmailNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: config.UID, - Name: config.Name, - Type: config.Type, - DisableResolveMessage: config.DisableResolveMessage, - Settings: config.Settings, - }), + Base: NewBase(config.NotificationChannelConfig), Addresses: config.Addresses, SingleEmail: config.SingleEmail, Message: config.Message, diff --git a/pkg/services/ngalert/notifier/channels/googlechat.go b/pkg/services/ngalert/notifier/channels/googlechat.go index f5f2f93039a..039a759d8bd 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat.go +++ b/pkg/services/ngalert/notifier/channels/googlechat.go @@ -12,7 +12,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -57,13 +56,7 @@ func newGoogleChatNotifier(fc FactoryConfig) (*GoogleChatNotifier, error) { } return &GoogleChatNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.googlechat"), ns: fc.NotificationService, images: fc.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/kafka.go b/pkg/services/ngalert/notifier/channels/kafka.go index c433ce00c1c..513d5ec27d5 100644 --- a/pkg/services/ngalert/notifier/channels/kafka.go +++ b/pkg/services/ngalert/notifier/channels/kafka.go @@ -58,13 +58,7 @@ func newKafkaNotifier(fc FactoryConfig) (*KafkaNotifier, error) { details := fc.Config.Settings.Get("details").MustString(DefaultMessageEmbed) return &KafkaNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.kafka"), images: fc.ImageStore, ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/line.go b/pkg/services/ngalert/notifier/channels/line.go index 99231acb773..eaad6a05e53 100644 --- a/pkg/services/ngalert/notifier/channels/line.go +++ b/pkg/services/ngalert/notifier/channels/line.go @@ -11,7 +11,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) var ( @@ -55,13 +54,7 @@ func newLineNotifier(fc FactoryConfig) (*LineNotifier, error) { description := fc.Config.Settings.Get("description").MustString(DefaultMessageEmbed) return &LineNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.line"), ns: fc.NotificationService, tmpl: fc.Template, diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index 0a4e38a4ec2..f82191e6476 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -16,7 +16,6 @@ import ( ptr "github.com/xorcare/pointer" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const ( @@ -125,13 +124,7 @@ func NewOpsgenieNotifier(fc FactoryConfig) (*OpsgenieNotifier, error) { return nil, err } return &OpsgenieNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), tmpl: fc.Template, log: log.New("alerting.notifier.opsgenie"), ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/pagerduty.go b/pkg/services/ngalert/notifier/channels/pagerduty.go index 519be1c185c..e9df556f0ff 100644 --- a/pkg/services/ngalert/notifier/channels/pagerduty.go +++ b/pkg/services/ngalert/notifier/channels/pagerduty.go @@ -14,7 +14,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const ( @@ -130,13 +129,7 @@ func newPagerdutyNotifier(fc FactoryConfig) (*PagerdutyNotifier, error) { } return &PagerdutyNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), tmpl: fc.Template, log: log.New("alerting.notifier." + fc.Config.Name), ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/pushover.go b/pkg/services/ngalert/notifier/channels/pushover.go index 1827f7c0472..a5009b3c0ef 100644 --- a/pkg/services/ngalert/notifier/channels/pushover.go +++ b/pkg/services/ngalert/notifier/channels/pushover.go @@ -18,7 +18,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const ( @@ -147,14 +146,7 @@ func NewPushoverNotifier(fc FactoryConfig) (*PushoverNotifier, error) { return nil, err } return &PushoverNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - SecureSettings: fc.Config.SecureSettings, - }), + Base: NewBase(fc.Config), tmpl: fc.Template, log: log.New("alerting.notifier.pushover"), images: fc.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/sensugo.go b/pkg/services/ngalert/notifier/channels/sensugo.go index 4f8b953151b..4f5ebb920ed 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo.go +++ b/pkg/services/ngalert/notifier/channels/sensugo.go @@ -12,7 +12,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) type SensuGoNotifier struct { @@ -71,14 +70,7 @@ func NewSensuGoNotifier(fc FactoryConfig) (*SensuGoNotifier, error) { return nil, err } return &SensuGoNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - SecureSettings: fc.Config.SecureSettings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.sensugo"), images: fc.ImageStore, ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 4b75dbdbef2..fabe3a37e89 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -23,7 +23,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -156,13 +155,7 @@ func buildSlackNotifier(factoryConfig FactoryConfig) (*SlackNotifier, error) { settings.Title = DefaultMessageTitleEmbed } return &SlackNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: factoryConfig.Config.UID, - Name: factoryConfig.Config.Name, - Type: factoryConfig.Config.Type, - DisableResolveMessage: factoryConfig.Config.DisableResolveMessage, - Settings: factoryConfig.Config.Settings, - }), + Base: NewBase(factoryConfig.Config), settings: settings, images: factoryConfig.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/teams.go b/pkg/services/ngalert/notifier/channels/teams.go index 5fd8ed2e685..1489466648f 100644 --- a/pkg/services/ngalert/notifier/channels/teams.go +++ b/pkg/services/ngalert/notifier/channels/teams.go @@ -12,7 +12,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const ( @@ -263,13 +262,7 @@ func NewTeamsNotifier(fc FactoryConfig) (*TeamsNotifier, error) { return nil, err } return &TeamsNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.teams"), ns: fc.NotificationService, images: fc.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index fe68845a09e..1ff215e1fa7 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -15,7 +15,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) var ( @@ -101,13 +100,7 @@ func NewTelegramNotifier(fc FactoryConfig) (*TelegramNotifier, error) { return nil, err } return &TelegramNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), tmpl: fc.Template, log: log.New("alerting.notifier.telegram"), images: fc.ImageStore, diff --git a/pkg/services/ngalert/notifier/channels/threema.go b/pkg/services/ngalert/notifier/channels/threema.go index 2f093324fc9..0890438f60e 100644 --- a/pkg/services/ngalert/notifier/channels/threema.go +++ b/pkg/services/ngalert/notifier/channels/threema.go @@ -13,7 +13,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) var ( @@ -95,13 +94,7 @@ func NewThreemaNotifier(fc FactoryConfig) (*ThreemaNotifier, error) { return nil, err } return &ThreemaNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.threema"), images: fc.ImageStore, ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/victorops.go b/pkg/services/ngalert/notifier/channels/victorops.go index ace7ed59c41..fd30b98dee5 100644 --- a/pkg/services/ngalert/notifier/channels/victorops.go +++ b/pkg/services/ngalert/notifier/channels/victorops.go @@ -14,7 +14,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -76,13 +75,7 @@ func NewVictoropsNotifier(fc FactoryConfig) (*VictoropsNotifier, error) { return nil, err } return &VictoropsNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: fc.Config.UID, - Name: fc.Config.Name, - Type: fc.Config.Type, - DisableResolveMessage: fc.Config.DisableResolveMessage, - Settings: fc.Config.Settings, - }), + Base: NewBase(fc.Config), log: log.New("alerting.notifier.victorops"), images: fc.ImageStore, ns: fc.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/webex.go b/pkg/services/ngalert/notifier/channels/webex.go index d0522b8f175..2fd46600ada 100644 --- a/pkg/services/ngalert/notifier/channels/webex.go +++ b/pkg/services/ngalert/notifier/channels/webex.go @@ -11,7 +11,6 @@ import ( "github.com/prometheus/alertmanager/types" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) const webexAPIURL = "https://webexapis.com/v1/messages" @@ -84,13 +83,7 @@ func buildWebexNotifier(factoryConfig FactoryConfig) (*WebexNotifier, error) { logger := log.New("alerting.notifier.webex") return &WebexNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: factoryConfig.Config.UID, - Name: factoryConfig.Config.Name, - Type: factoryConfig.Config.Type, - DisableResolveMessage: factoryConfig.Config.DisableResolveMessage, - Settings: factoryConfig.Config.Settings, - }), + Base: NewBase(factoryConfig.Config), orgID: factoryConfig.Config.OrgID, log: logger, ns: factoryConfig.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/webhook.go b/pkg/services/ngalert/notifier/channels/webhook.go index 76df97f0e47..87dc5243a99 100644 --- a/pkg/services/ngalert/notifier/channels/webhook.go +++ b/pkg/services/ngalert/notifier/channels/webhook.go @@ -116,13 +116,7 @@ func buildWebhookNotifier(factoryConfig FactoryConfig) (*WebhookNotifier, error) return nil, err } return &WebhookNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: factoryConfig.Config.UID, - Name: factoryConfig.Config.Name, - Type: factoryConfig.Config.Type, - DisableResolveMessage: factoryConfig.Config.DisableResolveMessage, - Settings: factoryConfig.Config.Settings, - }), + Base: NewBase(factoryConfig.Config), orgID: factoryConfig.Config.OrgID, log: log.New("alerting.notifier.webhook"), ns: factoryConfig.NotificationService, diff --git a/pkg/services/ngalert/notifier/channels/wecom.go b/pkg/services/ngalert/notifier/channels/wecom.go index c841d2cc64e..e0fff7db513 100644 --- a/pkg/services/ngalert/notifier/channels/wecom.go +++ b/pkg/services/ngalert/notifier/channels/wecom.go @@ -13,7 +13,6 @@ import ( "golang.org/x/sync/singleflight" "github.com/grafana/grafana/pkg/infra/log" - "github.com/grafana/grafana/pkg/models" ) var weComEndpoint = "https://qyapi.weixin.qq.com" @@ -110,13 +109,7 @@ func buildWecomNotifier(factoryConfig FactoryConfig) (*WeComNotifier, error) { return nil, err } return &WeComNotifier{ - Base: NewBase(&models.AlertNotification{ - Uid: factoryConfig.Config.UID, - Name: factoryConfig.Config.Name, - Type: factoryConfig.Config.Type, - DisableResolveMessage: factoryConfig.Config.DisableResolveMessage, - Settings: factoryConfig.Config.Settings, - }), + Base: NewBase(factoryConfig.Config), tmpl: factoryConfig.Template, log: log.New("alerting.notifier.wecom"), ns: factoryConfig.NotificationService,