notifier.go 4.1 KB

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