notifier.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. m "github.com/grafana/grafana/pkg/models"
  11. )
  12. type NotifierPlugin struct {
  13. Type string `json:"type"`
  14. Name string `json:"name"`
  15. Description string `json:"description"`
  16. OptionsTemplate string `json:"optionsTemplate"`
  17. Factory NotifierFactory `json:"-"`
  18. }
  19. type NotificationService interface {
  20. Send(context *EvalContext) error
  21. }
  22. func NewNotificationService() NotificationService {
  23. return newNotificationService()
  24. }
  25. type notificationService struct {
  26. log log.Logger
  27. }
  28. func newNotificationService() *notificationService {
  29. return &notificationService{
  30. log: log.New("alerting.notifier"),
  31. }
  32. }
  33. func (n *notificationService) Send(context *EvalContext) error {
  34. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  35. if err != nil {
  36. return err
  37. }
  38. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "sent count", len(notifiers))
  39. if len(notifiers) == 0 {
  40. return nil
  41. }
  42. if notifiers.ShouldUploadImage() {
  43. if err = n.uploadImage(context); err != nil {
  44. n.log.Error("Failed to upload alert panel image.", "error", err)
  45. }
  46. }
  47. return n.sendNotifications(context, notifiers)
  48. }
  49. func (n *notificationService) sendNotifications(context *EvalContext, notifiers []Notifier) error {
  50. g, _ := errgroup.WithContext(context.Ctx)
  51. for _, notifier := range notifiers {
  52. not := notifier //avoid updating scope variable in go routine
  53. n.log.Info("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
  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 := &renderer.RenderOpts{
  64. Width: "800",
  65. Height: "400",
  66. Timeout: "30",
  67. OrgId: context.Rule.OrgId,
  68. IsAlertContext: true,
  69. }
  70. if slug, err := context.GetDashboardSlug(); err != nil {
  71. return err
  72. } else {
  73. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  74. }
  75. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  76. return err
  77. } else {
  78. context.ImageOnDiskPath = imagePath
  79. }
  80. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  81. if err != nil {
  82. return err
  83. }
  84. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  85. return nil
  86. }
  87. func (n *notificationService) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
  88. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  89. if err := bus.Dispatch(query); err != nil {
  90. return nil, err
  91. }
  92. var result []Notifier
  93. for _, notification := range query.Result {
  94. if not, err := n.createNotifierFor(notification); err != nil {
  95. return nil, err
  96. } else {
  97. if shouldUseNotification(not, context) {
  98. result = append(result, not)
  99. }
  100. }
  101. }
  102. return result, nil
  103. }
  104. func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  105. notifierPlugin, found := notifierFactories[model.Type]
  106. if !found {
  107. return nil, errors.New("Unsupported notification type")
  108. }
  109. return notifierPlugin.Factory(model)
  110. }
  111. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  112. if !context.Firing {
  113. return true
  114. }
  115. if context.Error != nil {
  116. return true
  117. }
  118. return notifier.PassesFilter(context.Rule)
  119. }
  120. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  121. var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
  122. func RegisterNotifier(plugin *NotifierPlugin) {
  123. notifierFactories[plugin.Type] = plugin
  124. }
  125. func GetNotifiers() []*NotifierPlugin {
  126. list := make([]*NotifierPlugin, 0)
  127. for _, value := range notifierFactories {
  128. list = append(list, value)
  129. }
  130. return list
  131. }