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

[RAM] Update rule status #140882

Merged
merged 42 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1dd3687
re-structure migrations code
XavierM Sep 13, 2022
d1ca8f0
change mappings to match new design
XavierM Sep 13, 2022
0b74e72
wip
XavierM Sep 14, 2022
bbfc35a
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 16, 2022
5579627
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Sep 16, 2022
defdf85
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Oct 17, 2022
1707107
wip to add a monitoring service
XavierM Oct 28, 2022
eb8ce8b
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Oct 28, 2022
7ca2fc2
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Oct 28, 2022
c882426
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 28, 2022
0fe91eb
jiawei work on backend of rule
JiaweiWu Nov 1, 2022
6b4531f
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Nov 1, 2022
4a1de41
Fix conflicts
JiaweiWu Nov 1, 2022
6430691
Fix some of the missing functions used in rules_client
JiaweiWu Nov 1, 2022
4c57e09
Fix types and unit tests
JiaweiWu Nov 3, 2022
2151d4f
Removing running and fix associated tests
JiaweiWu Nov 3, 2022
aa1977c
Remove more references to running
JiaweiWu Nov 3, 2022
992cccb
Address comments and update task runner tests
JiaweiWu Nov 4, 2022
a907217
Fix and add more tests
JiaweiWu Nov 7, 2022
55f2cae
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 7, 2022
74269ac
Migration unit and integration tests, fixed some more tests
JiaweiWu Nov 7, 2022
6fdf08f
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 7, 2022
be1feb3
Fix types
JiaweiWu Nov 7, 2022
d404fd8
Fix tests and types
JiaweiWu Nov 8, 2022
8da8345
Fix SO migration snapshot, add more task runner tests
JiaweiWu Nov 8, 2022
1345ac2
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 8, 2022
daac93e
Fix type check
JiaweiWu Nov 9, 2022
9a70231
Fix type check
JiaweiWu Nov 9, 2022
f0b1c66
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 9, 2022
897ec3f
Fix more types
JiaweiWu Nov 10, 2022
cae7541
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Nov 10, 2022
2ce6b6e
Address comments
JiaweiWu Nov 14, 2022
d0b5298
Update rule monitoring mock
JiaweiWu Nov 14, 2022
bbef4eb
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
1bc1db7
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Nov 14, 2022
d9a6b9d
add more test
XavierM Nov 14, 2022
1869e17
unit test
XavierM Nov 14, 2022
c8e1da5
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
f2e4735
fix type
XavierM Nov 14, 2022
fad9da5
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Nov 14, 2022
49d07a1
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
367b3d8
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
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
53 changes: 45 additions & 8 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const RuleExecutionStatusValues = [
] as const;
export type RuleExecutionStatuses = typeof RuleExecutionStatusValues[number];

export const RuleLastRunOutcomeValues = ['succeeded', 'warning', 'failed'] as const;
export type RuleLastRunOutcomes = typeof RuleLastRunOutcomeValues[number];

export enum RuleExecutionStatusErrorReasons {
Read = 'read',
Decrypt = 'decrypt',
Expand Down Expand Up @@ -76,12 +79,25 @@ export interface RuleAction {

export interface RuleAggregations {
alertExecutionStatus: { [status: string]: number };
ruleLastRunOutcome: { [status: string]: number };
ruleEnabledStatus: { enabled: number; disabled: number };
ruleMutedStatus: { muted: number; unmuted: number };
ruleSnoozedStatus: { snoozed: number };
ruleTags: string[];
}

export interface RuleLastRun {
outcome: RuleLastRunOutcomes;
warning?: RuleExecutionStatusErrorReasons | RuleExecutionStatusWarningReasons | null;
outcomeMsg?: string | null;
alertsCount: {
active?: number | null;
new?: number | null;
recovered?: number | null;
ignored?: number | null;
};
}

export interface MappedParamsProperties {
risk_score?: number;
severity?: string;
Expand Down Expand Up @@ -116,6 +132,9 @@ export interface Rule<Params extends RuleTypeParams = never> {
snoozeSchedule?: RuleSnooze; // Remove ? when this parameter is made available in the public API
activeSnoozes?: string[];
isSnoozedUntil?: Date | null;
lastRun?: RuleLastRun | null;
nextRun?: Date | null;
running: boolean;
}

export type SanitizedRule<Params extends RuleTypeParams = never> = Omit<Rule<Params>, 'apiKey'>;
Expand Down Expand Up @@ -174,16 +193,34 @@ export interface RuleMonitoringHistory extends SavedObjectAttributes {
success: boolean;
timestamp: number;
duration?: number;
outcome?: RuleLastRunOutcomes;
}

export interface RuleMonitoringCalculatedMetrics extends SavedObjectAttributes {
p50?: number;
p95?: number;
p99?: number;
success_ratio: number;
}

export interface RuleMonitoringLastRunMetrics extends SavedObjectAttributes {
duration: number;
total_search_duration_ms?: number | null;
total_indexing_duration_ms?: number | null;
total_alerts_detected?: number | null;
total_alerts_created?: number | null;
gap_duration_s?: number | null;
}

export interface RuleMonitoringLastRun extends SavedObjectAttributes {
timestamp: string;
metrics: RuleMonitoringLastRunMetrics;
}

export interface RuleMonitoring extends SavedObjectAttributes {
execution: {
export interface RuleMonitoring {
run: {
history: RuleMonitoringHistory[];
calculated_metrics: {
p50?: number;
p95?: number;
p99?: number;
success_ratio: number;
};
calculated_metrics: RuleMonitoringCalculatedMetrics;
last_run: RuleMonitoringLastRun;
};
}
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/public/alert_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe('loadRule', () => {
"params": Object {
"x": 42,
},
"running": false,
"schedule": Object {
"interval": "1s",
},
Expand Down Expand Up @@ -289,6 +290,7 @@ function getApiRule() {
last_execution_date: RuleExecuteDate.toISOString(),
last_duration: 1194,
},
running: false,
};
}

Expand Down Expand Up @@ -330,5 +332,6 @@ function getRule(): Rule<{ x: number }> {
lastExecutionDate: RuleExecuteDate,
lastDuration: 1194,
},
running: false,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('common_transformations', () => {
message: 'this is just a test',
},
},
running: false,
};
expect(transformRule(apiRule)).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -105,6 +106,7 @@ describe('common_transformations', () => {
],
},
},
"running": false,
"schedule": Object {
"interval": "1s",
},
Expand Down Expand Up @@ -152,6 +154,7 @@ describe('common_transformations', () => {
last_execution_date: dateExecuted.toISOString(),
status: 'error',
},
running: false,
};
expect(transformRule(apiRule)).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -184,6 +187,7 @@ describe('common_transformations', () => {
"name": "some-name",
"notifyWhen": "onActiveAlert",
"params": Object {},
"running": false,
"schedule": Object {
"interval": "1s",
},
Expand Down
42 changes: 41 additions & 1 deletion x-pack/plugins/alerting/public/lib/common_transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
* 2.0.
*/
import { AsApiContract } from '@kbn/actions-plugin/common';
import { RuleExecutionStatus, Rule, RuleAction, RuleType } from '../../common';
import {
RuleExecutionStatus,
RuleMonitoring,
Rule,
RuleLastRun,
RuleAction,
RuleType,
} from '../../common';

function transformAction(input: AsApiContract<RuleAction>): RuleAction {
const { connector_type_id: actionTypeId, ...rest } = input;
Expand All @@ -27,6 +34,31 @@ function transformExecutionStatus(input: ApiRuleExecutionStatus): RuleExecutionS
};
}

function transformMonitoring(input: RuleMonitoring): RuleMonitoring {
const { run } = input;
const { last_run: lastRun, ...rest } = run;
const { timestamp, ...restLastRun } = lastRun;

return {
run: {
last_run: {
timestamp: input.run.last_run.timestamp,
...restLastRun,
},
...rest,
},
};
}

function transformLastRun(input: AsApiContract<RuleLastRun>): RuleLastRun {
const { outcome_msg: outcomeMsg, alerts_count: alertsCount, ...rest } = input;
return {
outcomeMsg,
alertsCount,
...rest,
};
}

// AsApiContract does not deal with object properties that also
// need snake -> camel conversion, Dates, are renamed, etc, so we do by hand
export type ApiRule = Omit<
Expand All @@ -37,13 +69,15 @@ export type ApiRule = Omit<
| 'updated_at'
| 'alert_type_id'
| 'muted_instance_ids'
| 'last_run'
> & {
execution_status: ApiRuleExecutionStatus;
actions: Array<AsApiContract<RuleAction>>;
created_at: string;
updated_at: string;
rule_type_id: string;
muted_alert_ids: string[];
last_run?: AsApiContract<RuleLastRun>;
};

export function transformRule(input: ApiRule): Rule {
XavierM marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -61,6 +95,9 @@ export function transformRule(input: ApiRule): Rule {
scheduled_task_id: scheduledTaskId,
execution_status: executionStatusAPI,
actions: actionsAPI,
next_run: nextRun,
last_run: lastRun,
monitoring: monitoring,
...rest
} = input;

Expand All @@ -78,6 +115,9 @@ export function transformRule(input: ApiRule): Rule {
executionStatus: transformExecutionStatus(executionStatusAPI),
actions: actionsAPI ? actionsAPI.map((action) => transformAction(action)) : [],
scheduledTaskId,
...(nextRun ? { nextRun: new Date(nextRun) } : {}),
...(monitoring ? { monitoring: transformMonitoring(monitoring) } : {}),
...(lastRun ? { lastRun: transformLastRun(lastRun) } : {}),
...rest,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,5 @@ const BaseRule: SanitizedRule<{ bar: boolean }> = {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
running: false,
};
7 changes: 7 additions & 0 deletions x-pack/plugins/alerting/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export {
ruleExecutionStatusToRaw,
ruleExecutionStatusFromRaw,
} from './rule_execution_status';
export { lastRunFromState, lastRunFromError, lastRunToRaw } from './last_run_status';
export {
updateMonitoring,
getDefaultMonitoring,
convertMonitoringFromRawAndVerify,
} from './monitoring';
export { getNextRunDate, getNextRunString } from './next_run';
export { processAlerts } from './process_alerts';
export { createWrappedScopedClusterClientFactory } from './wrap_scoped_cluster_client';
export { isRuleSnoozed, getRuleSnoozeEndTime } from './is_rule_snoozed';
Expand Down
87 changes: 87 additions & 0 deletions x-pack/plugins/alerting/server/lib/last_run_status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RuleTaskStateAndMetrics } from '../task_runner/types';
import { getReasonFromError } from './error_with_reason';
import { getEsErrorMessage } from './errors';
import { ActionsCompletion } from '../../common';
import {
RuleLastRunOutcomeValues,
RuleLastRunOutcomes,
RuleExecutionStatusWarningReasons,
RawRuleLastRun,
RuleLastRun,
} from '../types';
import { translations } from '../constants/translations';
import { RuleRunMetrics } from './rule_run_metrics_store';

export interface ILastRun {
lastRun: RuleLastRun;
metrics: RuleRunMetrics | null;
}

export const lastRunFromState = (stateWithMetrics: RuleTaskStateAndMetrics): ILastRun => {
XavierM marked this conversation as resolved.
Show resolved Hide resolved
const { metrics } = stateWithMetrics;
let outcome: RuleLastRunOutcomes = RuleLastRunOutcomeValues[0];
// Check for warning states
let warning = null;
let outcomeMsg = null;

// We only have a single warning field so prioritizing the alert circuit breaker over the actions circuit breaker
if (metrics.hasReachedAlertLimit) {
outcome = RuleLastRunOutcomeValues[1];
warning = RuleExecutionStatusWarningReasons.MAX_ALERTS;
outcomeMsg = translations.taskRunner.warning.maxAlerts;
} else if (metrics.triggeredActionsStatus === ActionsCompletion.PARTIAL) {
outcome = RuleLastRunOutcomeValues[1];
warning = RuleExecutionStatusWarningReasons.MAX_EXECUTABLE_ACTIONS;
outcomeMsg = translations.taskRunner.warning.maxExecutableActions;
}

return {
lastRun: {
outcome,
alertsCount: {
active: metrics.numberOfActiveAlerts,
new: metrics.numberOfNewAlerts,
recovered: metrics.numberOfRecoveredAlerts,
ignored: 0,
},
...(warning ? { warning } : {}),
...(outcomeMsg ? { outcome_msg: outcomeMsg } : {}),
},
metrics,
};
};

export const lastRunFromError = (error: Error): ILastRun => {
return {
lastRun: {
outcome: RuleLastRunOutcomeValues[2],
warning: getReasonFromError(error),
outcomeMsg: getEsErrorMessage(error),
alertsCount: {},
},
metrics: null,
};
};

export const lastRunToRaw = (lastRun: ILastRun['lastRun']): RawRuleLastRun => {
const { warning, alertsCount, outcomeMsg } = lastRun;

return {
...lastRun,
alertsCount: {
active: alertsCount.active || 0,
new: alertsCount.new || 0,
recovered: alertsCount.recovered || 0,
ignored: alertsCount.ignored || 0,
},
warning: warning ?? null,
outcomeMsg: outcomeMsg ?? null,
};
};
10 changes: 5 additions & 5 deletions x-pack/plugins/alerting/server/lib/monitoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const mockHistory = [
];

const mockRuleMonitoring = {
execution: {
run: {
history: mockHistory,
calculated_metrics: {
success_ratio: 0,
Expand All @@ -52,7 +52,7 @@ const mockRuleMonitoring = {

describe('getExecutionDurationPercentiles', () => {
it('Calculates the percentile given partly undefined durations', () => {
const percentiles = getExecutionDurationPercentiles(mockRuleMonitoring);
const percentiles = getExecutionDurationPercentiles(mockRuleMonitoring.run.history);
expect(percentiles.p50).toEqual(250);
expect(percentiles.p95).toEqual(500);
expect(percentiles.p99).toEqual(500);
Expand All @@ -66,13 +66,13 @@ describe('getExecutionDurationPercentiles', () => {

const newMockRuleMonitoring = {
...mockRuleMonitoring,
execution: {
...mockRuleMonitoring.execution,
run: {
...mockRuleMonitoring.run,
history: nullDurationHistory,
},
} as RuleMonitoring;

const percentiles = getExecutionDurationPercentiles(newMockRuleMonitoring);
const percentiles = getExecutionDurationPercentiles(newMockRuleMonitoring.run.history);
expect(Object.keys(percentiles).length).toEqual(0);
});
});
Loading