notifier.go 3.6 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/components/renderer"
  9. "github.com/grafana/grafana/pkg/log"
  10. m "github.com/grafana/grafana/pkg/models"
  11. )
  12. type RootNotifier struct {
  13. log log.Logger
  14. }
  15. func NewRootNotifier() *RootNotifier {
  16. return &RootNotifier{
  17. log: log.New("alerting.notifier"),
  18. }
  19. }
  20. func (n *RootNotifier) GetType() string {
  21. return "root"
  22. }
  23. func (n *RootNotifier) NeedsImage() bool {
  24. return false
  25. }
  26. func (n *RootNotifier) PassesFilter(rule *Rule) bool {
  27. return false
  28. }
  29. func (n *RootNotifier) GetNotifierId() int64 {
  30. return 0
  31. }
  32. func (n *RootNotifier) GetIsDefault() bool {
  33. return false
  34. }
  35. func (n *RootNotifier) Notify(context *EvalContext) error {
  36. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  37. if err != nil {
  38. return err
  39. }
  40. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "sent count", len(notifiers))
  41. if len(notifiers) == 0 {
  42. return nil
  43. }
  44. if err = n.uploadImage(context); err != nil {
  45. n.log.Error("Failed to upload alert panel image.", "error", err)
  46. }
  47. return n.sendNotifications(context, notifiers)
  48. }
  49. func (n *RootNotifier) 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 *RootNotifier) 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. }
  69. if slug, err := context.GetDashboardSlug(); err != nil {
  70. return err
  71. } else {
  72. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  73. }
  74. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  75. return err
  76. } else {
  77. context.ImageOnDiskPath = imagePath
  78. }
  79. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  80. if err != nil {
  81. return err
  82. }
  83. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  84. return nil
  85. }
  86. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
  87. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  88. if err := bus.Dispatch(query); err != nil {
  89. return nil, err
  90. }
  91. var result []Notifier
  92. for _, notification := range query.Result {
  93. if not, err := n.createNotifierFor(notification); err != nil {
  94. return nil, err
  95. } else {
  96. if shouldUseNotification(not, context) {
  97. result = append(result, not)
  98. }
  99. }
  100. }
  101. return result, nil
  102. }
  103. func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  104. factory, found := notifierFactories[model.Type]
  105. if !found {
  106. return nil, errors.New("Unsupported notification type")
  107. }
  108. return factory(model)
  109. }
  110. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  111. if !context.Firing {
  112. return true
  113. }
  114. if context.Error != nil {
  115. return true
  116. }
  117. return notifier.PassesFilter(context.Rule)
  118. }
  119. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  120. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  121. func RegisterNotifier(typeName string, factory NotifierFactory) {
  122. notifierFactories[typeName] = factory
  123. }