notifier.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. SendIfNeeded(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) SendIfNeeded(context *EvalContext) error {
  35. notifiers, err := n.getNeededNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  36. if err != nil {
  37. return err
  38. }
  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.Debug("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
  54. metrics.M_Alerting_Notification_Sent.WithLabelValues(not.GetType()).Inc()
  55. g.Go(func() error { return not.Notify(context) })
  56. }
  57. return g.Wait()
  58. }
  59. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  60. uploader, err := imguploader.NewImageUploader()
  61. if err != nil {
  62. return err
  63. }
  64. renderOpts := &renderer.RenderOpts{
  65. Width: "800",
  66. Height: "400",
  67. Timeout: "30",
  68. OrgId: context.Rule.OrgId,
  69. IsAlertContext: true,
  70. }
  71. if ref, err := context.GetDashboardUID(); err != nil {
  72. return err
  73. } else {
  74. renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?panelId=%d", ref.Uid, ref.Slug, context.Rule.PanelId)
  75. }
  76. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  77. return err
  78. } else {
  79. context.ImageOnDiskPath = imagePath
  80. }
  81. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  82. if err != nil {
  83. return err
  84. }
  85. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  86. return nil
  87. }
  88. func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
  89. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  90. if err := bus.Dispatch(query); err != nil {
  91. return nil, err
  92. }
  93. var result []Notifier
  94. for _, notification := range query.Result {
  95. if not, err := n.createNotifierFor(notification); err != nil {
  96. return nil, err
  97. } else {
  98. if not.ShouldNotify(context) {
  99. result = append(result, not)
  100. }
  101. }
  102. }
  103. return result, nil
  104. }
  105. func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  106. notifierPlugin, found := notifierFactories[model.Type]
  107. if !found {
  108. return nil, errors.New("Unsupported notification type")
  109. }
  110. return notifierPlugin.Factory(model)
  111. }
  112. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  113. var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
  114. func RegisterNotifier(plugin *NotifierPlugin) {
  115. notifierFactories[plugin.Type] = plugin
  116. }
  117. func GetNotifiers() []*NotifierPlugin {
  118. list := make([]*NotifierPlugin, 0)
  119. for _, value := range notifierFactories {
  120. list = append(list, value)
  121. }
  122. return list
  123. }