notifier.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/components/renderer"
  9. "github.com/grafana/grafana/pkg/log"
  10. "github.com/grafana/grafana/pkg/metrics"
  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. Send(context *EvalContext) error
  22. }
  23. func NewNotificationService() NotificationService {
  24. return newNotificationService()
  25. }
  26. type notificationService struct {
  27. log log.Logger
  28. }
  29. func newNotificationService() *notificationService {
  30. return &notificationService{
  31. log: log.New("alerting.notifier"),
  32. }
  33. }
  34. func (n *notificationService) Send(context *EvalContext) error {
  35. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  36. if err != nil {
  37. return err
  38. }
  39. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "sent count", len(notifiers))
  40. if len(notifiers) == 0 {
  41. return nil
  42. }
  43. if notifiers.ShouldUploadImage() {
  44. if err = n.uploadImage(context); err != nil {
  45. n.log.Error("Failed to upload alert panel image.", "error", err)
  46. }
  47. }
  48. return n.sendNotifications(context, notifiers)
  49. }
  50. func (n *notificationService) sendNotifications(context *EvalContext, notifiers []Notifier) error {
  51. g, _ := errgroup.WithContext(context.Ctx)
  52. for _, notifier := range notifiers {
  53. not := notifier //avoid updating scope variable in go routine
  54. n.log.Info("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
  55. metrics.M_Alerting_Notification_Sent.WithLabelValues(not.GetType()).Inc()
  56. g.Go(func() error { return not.Notify(context) })
  57. }
  58. return g.Wait()
  59. }
  60. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  61. uploader, err := imguploader.NewImageUploader()
  62. if err != nil {
  63. return err
  64. }
  65. renderOpts := &renderer.RenderOpts{
  66. Width: "800",
  67. Height: "400",
  68. Timeout: "30",
  69. OrgId: context.Rule.OrgId,
  70. IsAlertContext: true,
  71. }
  72. if slug, err := context.GetDashboardSlug(); err != nil {
  73. return err
  74. } else {
  75. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  76. }
  77. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  78. return err
  79. } else {
  80. context.ImageOnDiskPath = imagePath
  81. }
  82. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  83. if err != nil {
  84. return err
  85. }
  86. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  87. return nil
  88. }
  89. func (n *notificationService) getNotifiers(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. if not, err := n.createNotifierFor(notification); err != nil {
  97. return nil, err
  98. } else {
  99. if shouldUseNotification(not, context) {
  100. result = append(result, not)
  101. }
  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. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  114. if !context.Firing {
  115. return true
  116. }
  117. if context.Error != nil {
  118. return true
  119. }
  120. return notifier.PassesFilter(context.Rule)
  121. }
  122. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  123. var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
  124. func RegisterNotifier(plugin *NotifierPlugin) {
  125. notifierFactories[plugin.Type] = plugin
  126. }
  127. func GetNotifiers() []*NotifierPlugin {
  128. list := make([]*NotifierPlugin, 0)
  129. for _, value := range notifierFactories {
  130. list = append(list, value)
  131. }
  132. return list
  133. }