notifier.go 6.4 KB

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