notifier.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.UpdatedAt = time.Now().UTC().Unix()
  58. }
  59. if evalContext.IsTestRun {
  60. return nil
  61. }
  62. cmd := &m.SetAlertNotificationStateToCompleteCommand{
  63. Id: not.GetNotifierId(),
  64. Version: notifierState.state.Version,
  65. }
  66. if err = bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  67. if err == m.ErrAlertNotificationStateVersionConflict {
  68. n.log.Error("notification state out of sync", "id", not.GetNotifierId())
  69. return nil
  70. }
  71. return err
  72. }
  73. return nil
  74. }
  75. func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error {
  76. if !evalContext.IsTestRun {
  77. setPendingCmd := &m.SetAlertNotificationStateToPendingCommand{
  78. Id: notifierState.state.NotifierId,
  79. Version: notifierState.state.Version,
  80. AlertRuleStateUpdatedVersion: evalContext.Rule.StateChanges,
  81. }
  82. err := bus.DispatchCtx(evalContext.Ctx, setPendingCmd)
  83. if err == m.ErrAlertNotificationStateVersionConflict {
  84. return nil
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. // We need to update state version to be able to log
  90. // unexpected version conflicts when marking notifications as ok
  91. notifierState.state.Version = setPendingCmd.ResultVersion
  92. }
  93. return n.sendAndMarkAsComplete(evalContext, notifierState)
  94. }
  95. func (n *notificationService) sendNotifications(evalContext *EvalContext, notifierStates notifierStateSlice) error {
  96. for _, notifierState := range notifierStates {
  97. err := n.sendNotification(evalContext, notifierState)
  98. if err != nil {
  99. n.log.Error("failed to send notification", "id", notifierState.notifier.GetNotifierId())
  100. }
  101. }
  102. return nil
  103. }
  104. func (n *notificationService) uploadImage(context *EvalContext) (err error) {
  105. uploader, err := imguploader.NewImageUploader()
  106. if err != nil {
  107. return err
  108. }
  109. renderOpts := rendering.Opts{
  110. Width: 1000,
  111. Height: 500,
  112. Timeout: alertTimeout / 2,
  113. OrgId: context.Rule.OrgId,
  114. OrgRole: m.ROLE_ADMIN,
  115. ConcurrentLimit: setting.AlertingRenderLimit,
  116. }
  117. ref, err := context.GetDashboardUID()
  118. if err != nil {
  119. return err
  120. }
  121. renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?panelId=%d", ref.Uid, ref.Slug, context.Rule.PanelId)
  122. result, err := n.renderService.Render(context.Ctx, renderOpts)
  123. if err != nil {
  124. return err
  125. }
  126. context.ImageOnDiskPath = result.FilePath
  127. context.ImagePublicUrl, err = uploader.Upload(context.Ctx, context.ImageOnDiskPath)
  128. if err != nil {
  129. return err
  130. }
  131. if context.ImagePublicUrl != "" {
  132. n.log.Info("uploaded screenshot of alert to external image store", "url", context.ImagePublicUrl)
  133. }
  134. return nil
  135. }
  136. func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, evalContext *EvalContext) (notifierStateSlice, error) {
  137. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  138. if err := bus.Dispatch(query); err != nil {
  139. return nil, err
  140. }
  141. var result notifierStateSlice
  142. for _, notification := range query.Result {
  143. not, err := n.createNotifierFor(notification)
  144. if err != nil {
  145. n.log.Error("Could not create notifier", "notifier", notification.Id, "error", err)
  146. continue
  147. }
  148. query := &m.GetOrCreateNotificationStateQuery{
  149. NotifierId: notification.Id,
  150. AlertId: evalContext.Rule.Id,
  151. OrgId: evalContext.Rule.OrgId,
  152. }
  153. err = bus.DispatchCtx(evalContext.Ctx, query)
  154. if err != nil {
  155. n.log.Error("Could not get notification state.", "notifier", notification.Id, "error", err)
  156. continue
  157. }
  158. if not.ShouldNotify(evalContext.Ctx, evalContext, query.Result) {
  159. result = append(result, &notifierState{
  160. notifier: not,
  161. state: query.Result,
  162. })
  163. }
  164. }
  165. return result, nil
  166. }
  167. func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  168. notifierPlugin, found := notifierFactories[model.Type]
  169. if !found {
  170. return nil, errors.New("Unsupported notification type")
  171. }
  172. return notifierPlugin.Factory(model)
  173. }
  174. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  175. var notifierFactories = make(map[string]*NotifierPlugin)
  176. func RegisterNotifier(plugin *NotifierPlugin) {
  177. notifierFactories[plugin.Type] = plugin
  178. }
  179. func GetNotifiers() []*NotifierPlugin {
  180. list := make([]*NotifierPlugin, 0)
  181. for _, value := range notifierFactories {
  182. list = append(list, value)
  183. }
  184. return list
  185. }