Alerting: Remove reference to global models package in channels package (#60358)

* remove intermediate struct to create Base struct
* fix alertmanager
This commit is contained in:
Yuri Tseretyan
2022-12-14 16:21:55 -05:00
committed by GitHub
parent 342b0f3668
commit 0e7c95a4d2
20 changed files with 24 additions and 167 deletions
@@ -13,7 +13,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
// GetDecryptedValueFn is a function that returns the decrypted value of // 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. // NewAlertmanagerNotifier returns a new Alertmanager notifier.
func NewAlertmanagerNotifier(config *AlertmanagerConfig, images ImageStore, _ *template.Template, fn GetDecryptedValueFn) *AlertmanagerNotifier { func NewAlertmanagerNotifier(config *AlertmanagerConfig, images ImageStore, _ *template.Template, fn GetDecryptedValueFn) *AlertmanagerNotifier {
return &AlertmanagerNotifier{ return &AlertmanagerNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(config.NotificationChannelConfig),
Uid: config.UID,
Name: config.Name,
DisableResolveMessage: config.DisableResolveMessage,
Settings: config.Settings,
}),
images: images, images: images,
urls: config.URLs, urls: config.URLs,
basicAuthUser: config.BasicAuthUser, basicAuthUser: config.BasicAuthUser,
+5 -15
View File
@@ -1,32 +1,22 @@
package channels 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. // Base is the base implementation of a notifier. It contains the common fields across all notifier types.
type Base struct { type Base struct {
Name string Name string
Type string Type string
UID string UID string
IsDefault bool
DisableResolveMessage bool DisableResolveMessage bool
log log.Logger
} }
func (n *Base) GetDisableResolveMessage() bool { func (n *Base) GetDisableResolveMessage() bool {
return n.DisableResolveMessage return n.DisableResolveMessage
} }
func NewBase(model *models.AlertNotification) *Base { func NewBase(cfg *NotificationChannelConfig) *Base {
return &Base{ return &Base{
UID: model.Uid, UID: cfg.UID,
Name: model.Name, Name: cfg.Name,
IsDefault: model.IsDefault, Type: cfg.Type,
Type: model.Type, DisableResolveMessage: cfg.DisableResolveMessage,
DisableResolveMessage: model.DisableResolveMessage,
log: log.New("alerting.notifier." + model.Name),
} }
} }
@@ -11,7 +11,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const defaultDingdingMsgType = "link" const defaultDingdingMsgType = "link"
@@ -54,13 +53,7 @@ func newDingDingNotifier(fc FactoryConfig) (*DingDingNotifier, error) {
return nil, err return nil, err
} }
return &DingDingNotifier{ return &DingDingNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.dingding"), log: log.New("alerting.notifier.dingding"),
ns: fc.NotificationService, ns: fc.NotificationService,
tmpl: fc.Template, tmpl: fc.Template,
@@ -18,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
@@ -67,14 +66,7 @@ func newDiscordNotifier(fc FactoryConfig) (*DiscordNotifier, error) {
} }
return &DiscordNotifier{ return &DiscordNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
SecureSettings: fc.Config.SecureSettings,
}),
log: log.New("alerting.notifier.discord"), log: log.New("alerting.notifier.discord"),
ns: fc.NotificationService, ns: fc.NotificationService,
images: fc.ImageStore, images: fc.ImageStore,
@@ -12,7 +12,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
@@ -69,13 +68,7 @@ func NewEmailConfig(config *NotificationChannelConfig) (*EmailConfig, error) {
// for the EmailNotifier. // for the EmailNotifier.
func NewEmailNotifier(config *EmailConfig, ns EmailSender, images ImageStore, t *template.Template) *EmailNotifier { func NewEmailNotifier(config *EmailConfig, ns EmailSender, images ImageStore, t *template.Template) *EmailNotifier {
return &EmailNotifier{ return &EmailNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(config.NotificationChannelConfig),
Uid: config.UID,
Name: config.Name,
Type: config.Type,
DisableResolveMessage: config.DisableResolveMessage,
Settings: config.Settings,
}),
Addresses: config.Addresses, Addresses: config.Addresses,
SingleEmail: config.SingleEmail, SingleEmail: config.SingleEmail,
Message: config.Message, Message: config.Message,
@@ -12,7 +12,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
@@ -57,13 +56,7 @@ func newGoogleChatNotifier(fc FactoryConfig) (*GoogleChatNotifier, error) {
} }
return &GoogleChatNotifier{ return &GoogleChatNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.googlechat"), log: log.New("alerting.notifier.googlechat"),
ns: fc.NotificationService, ns: fc.NotificationService,
images: fc.ImageStore, images: fc.ImageStore,
@@ -58,13 +58,7 @@ func newKafkaNotifier(fc FactoryConfig) (*KafkaNotifier, error) {
details := fc.Config.Settings.Get("details").MustString(DefaultMessageEmbed) details := fc.Config.Settings.Get("details").MustString(DefaultMessageEmbed)
return &KafkaNotifier{ return &KafkaNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.kafka"), log: log.New("alerting.notifier.kafka"),
images: fc.ImageStore, images: fc.ImageStore,
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -11,7 +11,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
var ( var (
@@ -55,13 +54,7 @@ func newLineNotifier(fc FactoryConfig) (*LineNotifier, error) {
description := fc.Config.Settings.Get("description").MustString(DefaultMessageEmbed) description := fc.Config.Settings.Get("description").MustString(DefaultMessageEmbed)
return &LineNotifier{ return &LineNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.line"), log: log.New("alerting.notifier.line"),
ns: fc.NotificationService, ns: fc.NotificationService,
tmpl: fc.Template, tmpl: fc.Template,
@@ -16,7 +16,6 @@ import (
ptr "github.com/xorcare/pointer" ptr "github.com/xorcare/pointer"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const ( const (
@@ -125,13 +124,7 @@ func NewOpsgenieNotifier(fc FactoryConfig) (*OpsgenieNotifier, error) {
return nil, err return nil, err
} }
return &OpsgenieNotifier{ return &OpsgenieNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
tmpl: fc.Template, tmpl: fc.Template,
log: log.New("alerting.notifier.opsgenie"), log: log.New("alerting.notifier.opsgenie"),
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -14,7 +14,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const ( const (
@@ -130,13 +129,7 @@ func newPagerdutyNotifier(fc FactoryConfig) (*PagerdutyNotifier, error) {
} }
return &PagerdutyNotifier{ return &PagerdutyNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
tmpl: fc.Template, tmpl: fc.Template,
log: log.New("alerting.notifier." + fc.Config.Name), log: log.New("alerting.notifier." + fc.Config.Name),
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -18,7 +18,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const ( const (
@@ -147,14 +146,7 @@ func NewPushoverNotifier(fc FactoryConfig) (*PushoverNotifier, error) {
return nil, err return nil, err
} }
return &PushoverNotifier{ return &PushoverNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
SecureSettings: fc.Config.SecureSettings,
}),
tmpl: fc.Template, tmpl: fc.Template,
log: log.New("alerting.notifier.pushover"), log: log.New("alerting.notifier.pushover"),
images: fc.ImageStore, images: fc.ImageStore,
@@ -12,7 +12,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
type SensuGoNotifier struct { type SensuGoNotifier struct {
@@ -71,14 +70,7 @@ func NewSensuGoNotifier(fc FactoryConfig) (*SensuGoNotifier, error) {
return nil, err return nil, err
} }
return &SensuGoNotifier{ return &SensuGoNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
SecureSettings: fc.Config.SecureSettings,
}),
log: log.New("alerting.notifier.sensugo"), log: log.New("alerting.notifier.sensugo"),
images: fc.ImageStore, images: fc.ImageStore,
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -23,7 +23,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
@@ -156,13 +155,7 @@ func buildSlackNotifier(factoryConfig FactoryConfig) (*SlackNotifier, error) {
settings.Title = DefaultMessageTitleEmbed settings.Title = DefaultMessageTitleEmbed
} }
return &SlackNotifier{ return &SlackNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(factoryConfig.Config),
Uid: factoryConfig.Config.UID,
Name: factoryConfig.Config.Name,
Type: factoryConfig.Config.Type,
DisableResolveMessage: factoryConfig.Config.DisableResolveMessage,
Settings: factoryConfig.Config.Settings,
}),
settings: settings, settings: settings,
images: factoryConfig.ImageStore, images: factoryConfig.ImageStore,
@@ -12,7 +12,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const ( const (
@@ -263,13 +262,7 @@ func NewTeamsNotifier(fc FactoryConfig) (*TeamsNotifier, error) {
return nil, err return nil, err
} }
return &TeamsNotifier{ return &TeamsNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.teams"), log: log.New("alerting.notifier.teams"),
ns: fc.NotificationService, ns: fc.NotificationService,
images: fc.ImageStore, images: fc.ImageStore,
@@ -15,7 +15,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
var ( var (
@@ -101,13 +100,7 @@ func NewTelegramNotifier(fc FactoryConfig) (*TelegramNotifier, error) {
return nil, err return nil, err
} }
return &TelegramNotifier{ return &TelegramNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
tmpl: fc.Template, tmpl: fc.Template,
log: log.New("alerting.notifier.telegram"), log: log.New("alerting.notifier.telegram"),
images: fc.ImageStore, images: fc.ImageStore,
@@ -13,7 +13,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
var ( var (
@@ -95,13 +94,7 @@ func NewThreemaNotifier(fc FactoryConfig) (*ThreemaNotifier, error) {
return nil, err return nil, err
} }
return &ThreemaNotifier{ return &ThreemaNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.threema"), log: log.New("alerting.notifier.threema"),
images: fc.ImageStore, images: fc.ImageStore,
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -14,7 +14,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
@@ -76,13 +75,7 @@ func NewVictoropsNotifier(fc FactoryConfig) (*VictoropsNotifier, error) {
return nil, err return nil, err
} }
return &VictoropsNotifier{ return &VictoropsNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(fc.Config),
Uid: fc.Config.UID,
Name: fc.Config.Name,
Type: fc.Config.Type,
DisableResolveMessage: fc.Config.DisableResolveMessage,
Settings: fc.Config.Settings,
}),
log: log.New("alerting.notifier.victorops"), log: log.New("alerting.notifier.victorops"),
images: fc.ImageStore, images: fc.ImageStore,
ns: fc.NotificationService, ns: fc.NotificationService,
@@ -11,7 +11,6 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
const webexAPIURL = "https://webexapis.com/v1/messages" const webexAPIURL = "https://webexapis.com/v1/messages"
@@ -84,13 +83,7 @@ func buildWebexNotifier(factoryConfig FactoryConfig) (*WebexNotifier, error) {
logger := log.New("alerting.notifier.webex") logger := log.New("alerting.notifier.webex")
return &WebexNotifier{ return &WebexNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(factoryConfig.Config),
Uid: factoryConfig.Config.UID,
Name: factoryConfig.Config.Name,
Type: factoryConfig.Config.Type,
DisableResolveMessage: factoryConfig.Config.DisableResolveMessage,
Settings: factoryConfig.Config.Settings,
}),
orgID: factoryConfig.Config.OrgID, orgID: factoryConfig.Config.OrgID,
log: logger, log: logger,
ns: factoryConfig.NotificationService, ns: factoryConfig.NotificationService,
@@ -116,13 +116,7 @@ func buildWebhookNotifier(factoryConfig FactoryConfig) (*WebhookNotifier, error)
return nil, err return nil, err
} }
return &WebhookNotifier{ return &WebhookNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(factoryConfig.Config),
Uid: factoryConfig.Config.UID,
Name: factoryConfig.Config.Name,
Type: factoryConfig.Config.Type,
DisableResolveMessage: factoryConfig.Config.DisableResolveMessage,
Settings: factoryConfig.Config.Settings,
}),
orgID: factoryConfig.Config.OrgID, orgID: factoryConfig.Config.OrgID,
log: log.New("alerting.notifier.webhook"), log: log.New("alerting.notifier.webhook"),
ns: factoryConfig.NotificationService, ns: factoryConfig.NotificationService,
@@ -13,7 +13,6 @@ import (
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
) )
var weComEndpoint = "https://qyapi.weixin.qq.com" var weComEndpoint = "https://qyapi.weixin.qq.com"
@@ -110,13 +109,7 @@ func buildWecomNotifier(factoryConfig FactoryConfig) (*WeComNotifier, error) {
return nil, err return nil, err
} }
return &WeComNotifier{ return &WeComNotifier{
Base: NewBase(&models.AlertNotification{ Base: NewBase(factoryConfig.Config),
Uid: factoryConfig.Config.UID,
Name: factoryConfig.Config.Name,
Type: factoryConfig.Config.Type,
DisableResolveMessage: factoryConfig.Config.DisableResolveMessage,
Settings: factoryConfig.Config.Settings,
}),
tmpl: factoryConfig.Template, tmpl: factoryConfig.Template,
log: log.New("alerting.notifier.wecom"), log: log.New("alerting.notifier.wecom"),
ns: factoryConfig.NotificationService, ns: factoryConfig.NotificationService,