notifier.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package alerting
  2. import (
  3. "errors"
  4. "fmt"
  5. "golang.org/x/sync/errgroup"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/imguploader"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/metrics"
  10. "github.com/grafana/grafana/pkg/services/rendering"
  11. m "github.com/grafana/grafana/pkg/models"
  12. )
  13. type NotifierPlugin struct {
  14. Type string `json:"type"`
  15. Name string `json:"name"`
  16. Description string `json:"description"`
  17. OptionsTemplate string `json:"optionsTemplate"`
  18. Factory NotifierFactory `json:"-"`
  19. }
  20. type NotificationService interface {
  21. SendIfNeeded(context *EvalContext) error
  22. }
  23. func NewNotificationService(renderService rendering.Service) NotificationService {
  24. return &notificationService{
  25. log: log.New("alerting.notifier"),
  26. renderService: renderService,
  27. }
  28. }
  29. type notificationService struct {
  30. log log.Logger
  31. renderService rendering.Service
  32. }
  33. func (n *notificationService) SendIfNeeded(context *EvalContext) error {
  34. notifiers, err := n.getNeededNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  35. if err != nil {
  36. return err
  37. }
  38. if len(notifiers) == 0 {
  39. return nil
  40. }
  41. if notifiers.ShouldUploadImage() {
  42. if err = n.uploadImage(context); err != nil {
  43. n.log.Error("Failed to upload alert panel image.", "error", err)
  44. }
  45. }
  46. return n.sendNotifications(context, notifiers)
  47. }
  48. func (n *notificationService) sendNotifications(context *EvalContext, notifiers []Notifier) error {
  49. g, _ := errgroup.WithContext(context.Ctx)
  50. for _, notifier := range notifiers {
  51. not := notifier //avoid updating scope variable in go routine
  52. n.log.Debug("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
  53. metrics.M_Alerting_Notification_Sent.WithLabelValues(not.GetType()).Inc()
  54. g.Go(func() error { return not.Notify(context) })
  55. }
  56. return g.Wait()
  57. }
  58. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  59. uploader, err := imguploader.NewImageUploader()
  60. if err != nil {
  61. return err
  62. }
  63. renderOpts := rendering.Opts{
  64. Width: 1000,
  65. Height: 500,
  66. Timeout: alertTimeout / 2,
  67. OrgId: context.Rule.OrgId,
  68. OrgRole: m.ROLE_ADMIN,
  69. }
  70. ref, err := context.GetDashboardUID()
  71. if err != nil {
  72. return err
  73. }
  74. renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?panelId=%d", ref.Uid, ref.Slug, context.Rule.PanelId)
  75. result, err := n.renderService.Render(context.Ctx, renderOpts)
  76. if err != nil {
  77. return err
  78. }
  79. context.ImageOnDiskPath = result.FilePath
  80. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  81. if err != nil {
  82. return err
  83. }
  84. if context.ImagePublicUrl != "" {
  85. n.log.Info("uploaded screenshot of alert to external image store", "url", context.ImagePublicUrl)
  86. }
  87. return nil
  88. }
  89. func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
  90. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  91. if err := bus.Dispatch(query); err != nil {
  92. return nil, err
  93. }
  94. var result []Notifier
  95. for _, notification := range query.Result {
  96. not, err := n.createNotifierFor(notification)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if not.ShouldNotify(context) {
  101. result = append(result, not)
  102. }
  103. }
  104. return result, nil
  105. }
  106. func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  107. notifierPlugin, found := notifierFactories[model.Type]
  108. if !found {
  109. return nil, errors.New("Unsupported notification type")
  110. }
  111. return notifierPlugin.Factory(model)
  112. }
  113. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  114. var notifierFactories = make(map[string]*NotifierPlugin)
  115. func RegisterNotifier(plugin *NotifierPlugin) {
  116. notifierFactories[plugin.Type] = plugin
  117. }
  118. func GetNotifiers() []*NotifierPlugin {
  119. list := make([]*NotifierPlugin, 0)
  120. for _, value := range notifierFactories {
  121. list = append(list, value)
  122. }
  123. return list
  124. }