Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store alert and remediation history #3713

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading