-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[POC] [Response Ops] Onboard detection rules to use alerting framework summaries. #147539
Changes from 27 commits
600af4f
e64eb64
20e2ed1
0525af6
4204595
7d21829
3d137a1
7a8b83c
eda6aa3
441427f
f267e88
3728778
c0fdb1c
81b1a67
8281156
25213b6
526f376
66e1d4a
d7b8c43
b289217
b1c0cc9
e732c7b
24881f1
82fd509
9bd69a0
63393ed
acd919b
8320dd6
4d86684
6a27c94
650d275
dea0cdd
9406c5b
cf9be1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import { ExecuteOptions as EnqueueExecutionOptions } from '@kbn/actions-plugin/s | |
import { ActionsClient } from '@kbn/actions-plugin/server/actions_client'; | ||
import { chunk } from 'lodash'; | ||
import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger'; | ||
import { parseDuration, RawRule, ThrottledActions } from '../types'; | ||
import { GetRuleUrlFnOpts, parseDuration, RawRule, ThrottledActions } from '../types'; | ||
import { RuleRunMetricsStore } from '../lib/rule_run_metrics_store'; | ||
import { injectActionParams } from './inject_action_params'; | ||
import { ExecutionHandlerOptions, RuleTaskInstance } from './types'; | ||
|
@@ -201,7 +201,7 @@ export class ExecutionHandler< | |
if (isSummaryActionPerRuleRun(action) && !this.hasAlerts(alerts)) { | ||
continue; | ||
} | ||
const summarizedAlerts = await this.getSummarizedAlerts({ | ||
const { startMs, endMs, summarizedAlerts } = await this.getSummarizedAlerts({ | ||
action, | ||
spaceId, | ||
ruleId, | ||
|
@@ -222,7 +222,13 @@ export class ExecutionHandler< | |
actionsPlugin, | ||
actionTypeId, | ||
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, | ||
ruleUrl: this.buildRuleUrl(spaceId), | ||
ruleUrl: this.buildRuleUrl({ | ||
id: ruleId, | ||
spaceId, | ||
params: this.rule.params, | ||
startMs, | ||
endMs, | ||
}), | ||
}), | ||
}), | ||
}; | ||
|
@@ -267,7 +273,11 @@ export class ExecutionHandler< | |
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, | ||
alertParams: this.rule.params, | ||
actionParams: action.params, | ||
ruleUrl: this.buildRuleUrl(spaceId), | ||
ruleUrl: this.buildRuleUrl({ | ||
id: ruleId, | ||
spaceId, | ||
params: this.rule.params, | ||
}), | ||
flapping: executableAlert.getFlapping(), | ||
}), | ||
}), | ||
|
@@ -401,7 +411,24 @@ export class ExecutionHandler< | |
return alert.getScheduledActionOptions()?.actionGroup || this.ruleType.recoveryActionGroup.id; | ||
} | ||
|
||
private buildRuleUrl(spaceId: string): string | undefined { | ||
private buildRuleUrl({ | ||
id, | ||
params, | ||
spaceId, | ||
startMs, | ||
endMs, | ||
}: GetRuleUrlFnOpts<Params>): string | undefined { | ||
// Use the rule type's getRuleUrl callback if defined | ||
// This does not necessarily require `kibanaBaseUrl` to be defined | ||
const ruleTypeUrl = this.ruleType.getRuleUrl | ||
? this.ruleType?.getRuleUrl({ id, params, spaceId, startMs, endMs }) | ||
: null; | ||
|
||
if (ruleTypeUrl) { | ||
return ruleTypeUrl; | ||
} | ||
|
||
// Fallback to generic rule urle | ||
if (!this.taskRunnerContext.kibanaBaseUrl) { | ||
return; | ||
} | ||
|
@@ -543,13 +570,35 @@ export class ExecutionHandler< | |
const alerts = await this.ruleType.getSummarizedAlerts!(options); | ||
|
||
const total = alerts.new.count + alerts.ongoing.count + alerts.recovered.count; | ||
return { | ||
const summarizedAlerts = { | ||
...alerts, | ||
all: { | ||
count: total, | ||
data: [...alerts.new.data, ...alerts.ongoing.data, ...alerts.recovered.data], | ||
}, | ||
}; | ||
|
||
if (summarizedAlerts.all.count > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns the time bounds for summarized alerts that can be used for building rule URLs. This ensures that the time bounds used to load alerts in the UI matches the time bounds for the alert summary so the alert counts match. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calculating it from the summarized alerts instead of using existing time bounds because when we query for alerts per rule execution UUID, we don't have existing time bounds. |
||
// get the time bounds for this alert array | ||
const timestampMillis: number[] = summarizedAlerts.all.data | ||
.map((alert: unknown) => { | ||
const timestamp = (alert as { '@timestamp': string })['@timestamp']; | ||
if (timestamp) { | ||
return new Date(timestamp).valueOf(); | ||
} | ||
return null; | ||
}) | ||
.filter((timeInMillis: number | null) => null != timeInMillis) | ||
.sort() as number[]; | ||
|
||
return { | ||
startMs: timestampMillis[0], | ||
endMs: timestampMillis[timestampMillis.length - 1], | ||
summarizedAlerts, | ||
}; | ||
} | ||
|
||
return { summarizedAlerts }; | ||
} | ||
|
||
private async actionRunOrAddToBulk({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,16 @@ export interface SummarizedAlerts { | |
} | ||
export type GetSummarizedAlertsFn = (opts: GetSummarizedAlertsFnOpts) => Promise<SummarizedAlerts>; | ||
|
||
export interface GetRuleUrlFnOpts<Params extends RuleTypeParams> { | ||
id: string; | ||
params: Params; | ||
spaceId: string; | ||
startMs?: number; | ||
endMs?: number; | ||
} | ||
export type GetRuleUrlFn<Params extends RuleTypeParams> = ( | ||
opts: GetRuleUrlFnOpts<Params> | ||
) => string | null; | ||
export interface RuleType< | ||
Params extends RuleTypeParams = never, | ||
ExtractedParams extends RuleTypeParams = never, | ||
|
@@ -197,6 +207,13 @@ export interface RuleType< | |
cancelAlertsOnRuleTimeout?: boolean; | ||
doesSetRecoveryContext?: boolean; | ||
getSummarizedAlerts?: GetSummarizedAlertsFn; | ||
getRuleUrl?: GetRuleUrlFn<Params>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allows rule types to specify custom function for building rule URLs |
||
|
||
/** | ||
* Determines whether framework should | ||
* automatically make recovery determination. Defaults to true. | ||
*/ | ||
autoRecoverAlerts?: boolean; | ||
} | ||
export type UntypedRuleType = RuleType< | ||
RuleTypeParams, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skips this step for persistent alert rule types (slight time optimization when running a rule)