notifier.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package alerting
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/imguploader"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. "github.com/grafana/grafana/pkg/infra/metrics"
  9. "github.com/grafana/grafana/pkg/services/rendering"
  10. "github.com/grafana/grafana/pkg/setting"
  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(renderService rendering.Service) NotificationService {
  24. return &notificationService{
  25. log: log.New("alerting.notifier"),
  26. renderService: renderService,
  27. }
  28. }
  29. type notificationService struct {
  30. log log.Logger
  31. renderService rendering.Service
  32. }
  33. func (n *notificationService) SendIfNeeded(context *EvalContext) error {
  34. notifierStates, err := n.getNeededNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  35. if err != nil {
  36. return err
  37. }
  38. if len(notifierStates) == 0 {
  39. return nil
  40. }
  41. if notifierStates.ShouldUploadImage() {
  42. if err = n.uploadImage(context); err != nil {
  43. n.log.Error("Failed to upload alert panel image.", "error", err)
  44. }
  45. }
  46. return n.sendNotifications(context, notifierStates)
  47. }
  48. func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, notifierState *notifierState) error {
  49. notifier := notifierState.notifier
  50. n.log.Debug("Sending notification", "type", notifier.GetType(), "uid", notifier.GetNotifierUid(), "isDefault", notifier.GetIsDefault())
  51. metrics.M_Alerting_Notification_Sent.WithLabelValues(notifier.GetType()).Inc()
  52. err := notifier.Notify(evalContext)
  53. if err != nil {
  54. n.log.Error("failed to send notification", "uid", notifier.GetNotifierUid(), "error", err)
  55. }
  56. if evalContext.IsTestRun {
  57. return nil
  58. }
  59. cmd := &m.SetAlertNotificationStateToCompleteCommand{
  60. Id: notifierState.state.Id,
  61. Version: notifierState.state.Version,
  62. }
  63. return bus.DispatchCtx(evalContext.Ctx, cmd)
  64. }
  65. func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error {
  66. if !evalContext.IsTestRun {
  67. setPendingCmd := &m.SetAlertNotificationStateToPendingCommand{
  68. Id: notifierState.state.Id,
  69. Version: notifierState.state.Version,
  70. AlertRuleStateUpdatedVersion: evalContext.Rule.StateChanges,
  71. }
  72. err := bus.DispatchCtx(evalContext.Ctx, setPendingCmd)
  73. if err == m.ErrAlertNotificationStateVersionConflict {
  74. return nil
  75. }
  76. if err != nil {
  77. return err
  78. }
  79. // We need to update state version to be able to log
  80. // unexpected version conflicts when marking notifications as ok
  81. notifierState.state.Version = setPendingCmd.ResultVersion
  82. }
  83. return n.sendAndMarkAsComplete(evalContext, notifierState)
  84. }
  85. func (n *notificationService) sendNotifications(evalContext *EvalContext, notifierStates notifierStateSlice) error {
  86. for _, notifierState := range notifierStates {
  87. err := n.sendNotification(evalContext, notifierState)
  88. if err != nil {
  89. n.log.Error("failed to send notification", "uid", notifierState.notifier.GetNotifierUid(), "error", err)
  90. }
  91. }
  92. return nil
  93. }
  94. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  95. uploader, err := imguploader.NewImageUploader()
  96. if err != nil {
  97. return err
  98. }
  99. renderOpts := rendering.Opts{
  100. Width: 1000,
  101. Height: 500,
  102. Timeout: setting.AlertingEvaluationTimeout,
  103. OrgId: context.Rule.OrgId,
  104. OrgRole: m.ROLE_ADMIN,
  105. ConcurrentLimit: setting.AlertingRenderLimit,
  106. }
  107. ref, err := context.GetDashboardUID()
  108. if err != nil {
  109. return err
  110. }
  111. renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?orgId=%d&panelId=%d", ref.Uid, ref.Slug, context.Rule.OrgId, context.Rule.PanelId)
  112. result, err := n.renderService.Render(context.Ctx, renderOpts)
  113. if err != nil {
  114. return err
  115. }
  116. context.ImageOnDiskPath = result.FilePath
  117. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  118. if err != nil {
  119. return err
  120. }
  121. if context.ImagePublicUrl != "" {
  122. n.log.Info("uploaded screenshot of alert to external image store", "url", context.ImagePublicUrl)
  123. }
  124. return nil
  125. }
  126. func (n *notificationService) getNeededNotifiers(orgId int64, notificationUids []string, evalContext *EvalContext) (notifierStateSlice, error) {
  127. query := &m.GetAlertNotificationsWithUidToSendQuery{OrgId: orgId, Uids: notificationUids}
  128. if err := bus.Dispatch(query); err != nil {
  129. return nil, err
  130. }
  131. var result notifierStateSlice
  132. for _, notification := range query.Result {
  133. not, err := InitNotifier(notification)
  134. if err != nil {
  135. n.log.Error("Could not create notifier", "notifier", notification.Uid, "error", err)
  136. continue
  137. }
  138. query := &m.GetOrCreateNotificationStateQuery{
  139. NotifierId: notification.Id,
  140. AlertId: evalContext.Rule.Id,
  141. OrgId: evalContext.Rule.OrgId,
  142. }
  143. err = bus.DispatchCtx(evalContext.Ctx, query)
  144. if err != nil {
  145. n.log.Error("Could not get notification state.", "notifier", notification.Id, "error", err)
  146. continue
  147. }
  148. if not.ShouldNotify(evalContext.Ctx, evalContext, query.Result) {
  149. result = append(result, &notifierState{
  150. notifier: not,
  151. state: query.Result,
  152. })
  153. }
  154. }
  155. return result, nil
  156. }
  157. // InitNotifier instantiate a new notifier based on the model
  158. func InitNotifier(model *m.AlertNotification) (Notifier, error) {
  159. notifierPlugin, found := notifierFactories[model.Type]
  160. if !found {
  161. return nil, errors.New("Unsupported notification type")
  162. }
  163. return notifierPlugin.Factory(model)
  164. }
  165. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  166. var notifierFactories = make(map[string]*NotifierPlugin)
  167. // RegisterNotifier register an notifier
  168. func RegisterNotifier(plugin *NotifierPlugin) {
  169. notifierFactories[plugin.Type] = plugin
  170. }
  171. func GetNotifiers() []*NotifierPlugin {
  172. list := make([]*NotifierPlugin, 0)
  173. for _, value := range notifierFactories {
  174. list = append(list, value)
  175. }
  176. return list
  177. }