|
|
|
|
@ -40,12 +40,17 @@ func NewAnnotationBackend(annotations annotations.Repository, dashboards dashboa |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RecordStates writes a number of state transitions for a given rule to state history.
|
|
|
|
|
func (h *AnnotationBackend) RecordStatesAsync(ctx context.Context, rule *ngmodels.AlertRule, states []state.StateTransition) { |
|
|
|
|
func (h *AnnotationBackend) RecordStatesAsync(ctx context.Context, rule *ngmodels.AlertRule, states []state.StateTransition) <-chan error { |
|
|
|
|
logger := h.log.FromContext(ctx) |
|
|
|
|
// Build annotations before starting goroutine, to make sure all data is copied and won't mutate underneath us.
|
|
|
|
|
annotations := h.buildAnnotations(rule, states, logger) |
|
|
|
|
panel := parsePanelKey(rule, logger) |
|
|
|
|
go h.recordAnnotationsSync(ctx, panel, annotations, logger) |
|
|
|
|
errCh := make(chan error, 1) |
|
|
|
|
go func() { |
|
|
|
|
defer close(errCh) |
|
|
|
|
errCh <- h.recordAnnotationsSync(ctx, panel, annotations, logger) |
|
|
|
|
}() |
|
|
|
|
return errCh |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *AnnotationBackend) QueryStates(ctx context.Context, query ngmodels.HistoryQuery) (*data.Frame, error) { |
|
|
|
|
@ -156,12 +161,12 @@ func (h *AnnotationBackend) buildAnnotations(rule *ngmodels.AlertRule, states [] |
|
|
|
|
return items |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *AnnotationBackend) recordAnnotationsSync(ctx context.Context, panel *panelKey, annotations []annotations.Item, logger log.Logger) { |
|
|
|
|
func (h *AnnotationBackend) recordAnnotationsSync(ctx context.Context, panel *panelKey, annotations []annotations.Item, logger log.Logger) error { |
|
|
|
|
if panel != nil { |
|
|
|
|
dashID, err := h.dashboards.getID(ctx, panel.orgID, panel.dashUID) |
|
|
|
|
if err != nil { |
|
|
|
|
logger.Error("Error getting dashboard for alert annotation", "dashboardUID", panel.dashUID, "error", err) |
|
|
|
|
return |
|
|
|
|
return fmt.Errorf("error getting dashboard for alert annotation: %w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i := range annotations { |
|
|
|
|
@ -172,9 +177,11 @@ func (h *AnnotationBackend) recordAnnotationsSync(ctx context.Context, panel *pa |
|
|
|
|
|
|
|
|
|
if err := h.annotations.SaveMany(ctx, annotations); err != nil { |
|
|
|
|
logger.Error("Error saving alert annotation batch", "error", err) |
|
|
|
|
return fmt.Errorf("error saving alert annotation batch: %w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
logger.Debug("Done saving alert annotation batch") |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func buildAnnotationTextAndData(rule *ngmodels.AlertRule, currentState *state.State) (string, *simplejson.Json) { |
|
|
|
|
|