notifier.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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) Notify(context *EvalContext) error {
  30. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
  31. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  32. if err != nil {
  33. return err
  34. }
  35. if len(notifiers) == 0 {
  36. return nil
  37. }
  38. err = n.uploadImage(context)
  39. if err != nil {
  40. n.log.Error("Failed to upload alert panel image", "error", err)
  41. return err
  42. }
  43. return n.sendNotifications(context, notifiers)
  44. }
  45. func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notifier) error {
  46. g, _ := errgroup.WithContext(context.Context)
  47. for _, notifier := range notifiers {
  48. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  49. g.Go(func() error { return notifier.Notify(context) })
  50. }
  51. return g.Wait()
  52. }
  53. func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
  54. uploader, err := imguploader.NewImageUploader()
  55. if err != nil {
  56. return err
  57. }
  58. renderOpts := &renderer.RenderOpts{
  59. Width: "800",
  60. Height: "400",
  61. Timeout: "30",
  62. OrgId: context.Rule.OrgId,
  63. }
  64. if slug, err := context.GetDashboardSlug(); err != nil {
  65. return err
  66. } else {
  67. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  68. }
  69. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  70. return err
  71. } else {
  72. context.ImageOnDiskPath = imagePath
  73. }
  74. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  75. if err != nil {
  76. return err
  77. }
  78. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  79. return nil
  80. }
  81. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
  82. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  83. if err := bus.Dispatch(query); err != nil {
  84. return nil, err
  85. }
  86. var result []Notifier
  87. for _, notification := range query.Result {
  88. if not, err := n.createNotifierFor(notification); err != nil {
  89. return nil, err
  90. } else {
  91. if shouldUseNotification(not, context) {
  92. result = append(result, not)
  93. }
  94. }
  95. }
  96. return result, nil
  97. }
  98. func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  99. factory, found := notifierFactories[model.Type]
  100. if !found {
  101. return nil, errors.New("Unsupported notification type")
  102. }
  103. return factory(model)
  104. }
  105. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  106. if !context.Firing {
  107. return true
  108. }
  109. if context.Error != nil {
  110. return true
  111. }
  112. return notifier.PassesFilter(context.Rule)
  113. }
  114. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  115. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  116. func RegisterNotifier(typeName string, factory NotifierFactory) {
  117. notifierFactories[typeName] = factory
  118. }