Skip to content

Commit

Permalink
Store alert and remediation history (#3713)
Browse files Browse the repository at this point in the history
This captures the alert and remediation statuses and stores them in the
new tables.

Fixes #3556
  • Loading branch information
dmjb authored Jun 26, 2024
1 parent 6cc166f commit 37323cd
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
28 changes: 28 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,30 @@ INSERT INTO latest_evaluation_statuses(
$2
)
ON CONFLICT (rule_entity_id) DO UPDATE
SET evaluation_history_id = $2;
SET evaluation_history_id = $2;

-- name: InsertRemediationEvent :exec
INSERT INTO remediation_events(
evaluation_id,
status,
details,
metadata
) VALUES (
$1,
$2,
$3,
$4
);

-- name: InsertAlertEvent :exec
INSERT INTO alert_events(
evaluation_id,
status,
details,
metadata
) VALUES (
$1,
$2,
$3,
$4
);
63 changes: 63 additions & 0 deletions internal/db/eval_history.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions internal/engine/eval_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (e *Executor) createOrUpdateEvalStatus(
}
status := evalerrors.ErrorAsEvalStatus(params.GetEvalErr())
e.metrics.CountEvalStatus(ctx, status, entityType)

_, err = e.querier.UpsertRuleDetailsEval(ctx, db.UpsertRuleDetailsEvalParams{
RuleEvalID: evalID,
Status: evalerrors.ErrorAsEvalStatus(params.GetEvalErr()),
Details: evalerrors.ErrorAsEvalDetails(params.GetEvalErr()),
})

if err != nil {
logger.Err(err).Msg("error upserting rule evaluation details")
return err
Expand Down Expand Up @@ -204,14 +204,43 @@ func (e *Executor) createOrUpdateEvalStatus(
if flags.Bool(ctx, e.featureFlags, flags.EvalHistory) {
// Log in the evaluation history tables
_, err = db.WithTransaction(e.querier, func(qtx db.ExtendQuerier) (uuid.UUID, error) {
return e.historyService.StoreEvaluationStatus(
evalID, err := e.historyService.StoreEvaluationStatus(
ctx,
qtx,
ruleID,
params.EntityType,
entityID,
params.GetEvalErr(),
)
if err != nil {
return uuid.Nil, err
}

// These could be added into the history service, but since there
// is ongoing discussion about decoupling alerting and remediation
// from evaluation, I am leaving them here to make them easy to
// move elsewhere.
err = qtx.InsertRemediationEvent(ctx, db.InsertRemediationEventParams{
EvaluationID: evalID,
Status: remediationStatus,
Details: errorAsActionDetails(params.GetActionsErr().RemediateErr),
Metadata: params.GetActionsErr().RemediateMeta,
})
if err != nil {
return uuid.Nil, err
}

err = qtx.InsertAlertEvent(ctx, db.InsertAlertEventParams{
EvaluationID: evalID,
Status: alertStatus,
Details: errorAsActionDetails(params.GetActionsErr().AlertErr),
Metadata: params.GetActionsErr().AlertMeta,
})
if err != nil {
return uuid.Nil, err
}

return evalID, nil
})
if err != nil {
logger.Err(err).Msg("error logging evaluation status")
Expand Down

0 comments on commit 37323cd

Please sign in to comment.