notifier.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package alerting
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/imguploader"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/metrics"
  10. "github.com/grafana/grafana/pkg/services/rendering"
  11. "github.com/grafana/grafana/pkg/setting"
  12. m "github.com/grafana/grafana/pkg/models"
  13. )
  14. type NotifierPlugin struct {
  15. Type string `json:"type"`
  16. Name string `json:"name"`
  17. Description string `json:"description"`
  18. OptionsTemplate string `json:"optionsTemplate"`
  19. Factory NotifierFactory `json:"-"`
  20. }
  21. type NotificationService interface {
  22. SendIfNeeded(context *EvalContext) error
  23. }
  24. func NewNotificationService(renderService rendering.Service) NotificationService {
  25. return &notificationService{
  26. log: log.New("alerting.notifier"),
  27. renderService: renderService,
  28. }
  29. }
  30. type notificationService struct {
  31. log log.Logger
  32. renderService rendering.Service
  33. }
  34. func (n *notificationService) SendIfNeeded(context *EvalContext) error {
  35. notifierStates, err := n.getNeededNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  36. if err != nil {
  37. return err
  38. }
  39. if len(notifierStates) == 0 {
  40. return nil
  41. }
  42. if notifierStates.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, notifierStates)
  48. }
  49. func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, notifierState *NotifierState) error {
  50. not := notifierState.notifier
  51. n.log.Debug("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
  52. metrics.M_Alerting_Notification_Sent.WithLabelValues(not.GetType()).Inc()
  53. err := not.Notify(evalContext)
  54. if err != nil {
  55. n.log.Error("failed to send notification", "id", not.GetNotifierId())
  56. } else {
  57. notifierState.state.SentAt = time.Now().UTC().Unix()
  58. }
  59. if evalContext.IsTestRun {
  60. return nil
  61. }
  62. cmd := &m.SetAlertNotificationStateToCompleteCommand{
  63. State: notifierState.state,
  64. }
  65. if err = bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  66. if err == m.ErrAlertNotificationStateVersionConflict {
  67. n.log.Error("notification state out of sync", "id", not.GetNotifierId())
  68. return nil
  69. }
  70. return err
  71. }
  72. return nil
  73. }
  74. func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *NotifierState) error {
  75. if !evalContext.IsTestRun {
  76. setPendingCmd := &m.SetAlertNotificationStateToPendingCommand{
  77. State: notifierState.state,
  78. }
  79. err := bus.DispatchCtx(evalContext.Ctx, setPendingCmd)
  80. if err == m.ErrAlertNotificationStateVersionConflict {
  81. return nil
  82. }
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return n.sendAndMarkAsComplete(evalContext, notifierState)
  88. }
  89. func (n *notificationService) sendNotifications(evalContext *EvalContext, notifierStates NotifierStateSlice) error {
  90. for _, notifierState := range notifierStates {
  91. err := n.sendNotification(evalContext, notifierState)
  92. if err != nil {
  93. n.log.Error("failed to send notification", "id", notifierState.notifier.GetNotifierId())
  94. }
  95. }
  96. return nil
  97. }
  98. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  99. uploader, err := imguploader.NewImageUploader()
  100. if err != nil {
  101. return err
  102. }
  103. renderOpts := rendering.Opts{
  104. Width: 1000,
  105. Height: 500,
  106. Timeout: alertTimeout / 2,
  107. OrgId: context.Rule.OrgId,
  108. OrgRole: m.ROLE_ADMIN,
  109. ConcurrentLimit: setting.AlertingRenderLimit,
  110. }
  111. ref, err := context.GetDashboardUID()
  112. if err != nil {
  113. return err
  114. }
  115. renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?panelId=%d", ref.Uid, ref.Slug, context.Rule.PanelId)
  116. result, err := n.renderService.Render(context.Ctx, renderOpts)
  117. if err != nil {
  118. return err
  119. }
  120. context.ImageOnDiskPath = result.FilePath
  121. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  122. if err != nil {
  123. return err
  124. }
  125. if context.ImagePublicUrl != "" {
  126. n.log.Info("uploaded screenshot of alert to external image store", "url", context.ImagePublicUrl)
  127. }
  128. return nil
  129. }
  130. func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, evalContext *EvalContext) (NotifierStateSlice, error) {
  131. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  132. if err := bus.Dispatch(query); err != nil {
  133. return nil, err
  134. }
  135. var result NotifierStateSlice
  136. for _, notification := range query.Result {
  137. not, err := n.createNotifierFor(notification)
  138. if err != nil {
  139. n.log.Error("Could not create notifier", "notifier", notification.Id)
  140. continue
  141. }
  142. query := &m.GetNotificationStateQuery{
  143. NotifierId: notification.Id,
  144. AlertId: evalContext.Rule.Id,
  145. OrgId: evalContext.Rule.OrgId,
  146. }
  147. err = bus.DispatchCtx(evalContext.Ctx, query)
  148. if err != nil {
  149. n.log.Error("Could not get notification state.", "notifier", notification.Id)
  150. continue
  151. }
  152. if not.ShouldNotify(evalContext.Ctx, evalContext, query.Result) {
  153. result = append(result, &NotifierState{
  154. notifier: not,
  155. state: query.Result,
  156. })
  157. }
  158. }
  159. return result, nil
  160. }
  161. func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  162. notifierPlugin, found := notifierFactories[model.Type]
  163. if !found {
  164. return nil, errors.New("Unsupported notification type")
  165. }
  166. return notifierPlugin.Factory(model)
  167. }
  168. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  169. var notifierFactories = make(map[string]*NotifierPlugin)
  170. func RegisterNotifier(plugin *NotifierPlugin) {
  171. notifierFactories[plugin.Type] = plugin
  172. }
  173. func GetNotifiers() []*NotifierPlugin {
  174. list := make([]*NotifierPlugin, 0)
  175. for _, value := range notifierFactories {
  176. list = append(list, value)
  177. }
  178. return list
  179. }