forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_jobs_health_alerting_rule.ts
103 lines (95 loc) · 4.13 KB
/
register_jobs_health_alerting_rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* 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 { lazy } from 'react';
import { i18n } from '@kbn/i18n';
import { TriggersAndActionsUIPublicPluginSetup } from '../../../../triggers_actions_ui/public';
import { PluginSetupContract as AlertingSetup } from '../../../../alerting/public';
import { ML_ALERT_TYPES } from '../../../common/constants/alerts';
import { MlAnomalyDetectionJobsHealthRuleParams } from '../../../common/types/alerts';
import { getResultJobsHealthRuleConfig } from '../../../common/util/alerts';
import { validateLookbackInterval } from '../validators';
export function registerJobsHealthAlertingRule(
triggersActionsUi: TriggersAndActionsUIPublicPluginSetup,
alerting?: AlertingSetup
) {
triggersActionsUi.ruleTypeRegistry.register({
id: ML_ALERT_TYPES.AD_JOBS_HEALTH,
description: i18n.translate('xpack.ml.alertTypes.jobsHealthAlertingRule.description', {
defaultMessage: 'Alert when anomaly detection jobs experience operational issues.',
}),
iconClass: 'bell',
documentationUrl(docLinks) {
return docLinks.links.ml.alertingRules;
},
alertParamsExpression: lazy(() => import('./anomaly_detection_jobs_health_rule_trigger')),
validate: (alertParams: MlAnomalyDetectionJobsHealthRuleParams) => {
const validationResult = {
errors: {
includeJobs: new Array<string>(),
testsConfig: new Array<string>(),
delayedData: new Array<string>(),
} as Record<keyof MlAnomalyDetectionJobsHealthRuleParams, string[]>,
};
if (!alertParams.includeJobs?.jobIds?.length && !alertParams.includeJobs?.groupIds?.length) {
validationResult.errors.includeJobs.push(
i18n.translate('xpack.ml.alertTypes.jobsHealthAlertingRule.includeJobs.errorMessage', {
defaultMessage: 'Job selection is required',
})
);
}
const resultTestConfig = getResultJobsHealthRuleConfig(alertParams.testsConfig);
if (Object.values(resultTestConfig).every((v) => v?.enabled === false)) {
validationResult.errors.testsConfig.push(
i18n.translate('xpack.ml.alertTypes.jobsHealthAlertingRule.testsConfig.errorMessage', {
defaultMessage: 'At least one health check must be enabled.',
})
);
}
if (
!!resultTestConfig.delayedData.timeInterval &&
validateLookbackInterval(resultTestConfig.delayedData.timeInterval)
) {
validationResult.errors.delayedData.push(
i18n.translate(
'xpack.ml.alertTypes.jobsHealthAlertingRule.testsConfig.delayedData.timeIntervalErrorMessage',
{
defaultMessage: 'Invalid time interval',
}
)
);
}
if (resultTestConfig.delayedData.docsCount === 0) {
validationResult.errors.delayedData.push(
i18n.translate(
'xpack.ml.alertTypes.jobsHealthAlertingRule.testsConfig.delayedData.docsCountErrorMessage',
{
defaultMessage: 'Invalid number of documents',
}
)
);
}
return validationResult;
},
requiresAppContext: false,
defaultActionMessage: i18n.translate(
'xpack.ml.alertTypes.jobsHealthAlertingRule.defaultActionMessage',
{
defaultMessage: `Anomaly detection jobs health check result:
\\{\\{context.message\\}\\}
\\{\\{#context.results\\}\\}
Job ID: \\{\\{job_id\\}\\}
\\{\\{#datafeed_id\\}\\}Datafeed ID: \\{\\{datafeed_id\\}\\} \\{\\{/datafeed_id\\}\\}
\\{\\{#datafeed_state\\}\\}Datafeed state: \\{\\{datafeed_state\\}\\} \\{\\{/datafeed_state\\}\\}
\\{\\{#memory_status\\}\\}Memory status: \\{\\{memory_status\\}\\} \\{\\{/memory_status\\}\\}
\\{\\{#log_time\\}\\}Memory logging time: \\{\\{log_time\\}\\} \\{\\{/log_time\\}\\}
\\{\\{#failed_category_count\\}\\}Failed category count: \\{\\{failed_category_count\\}\\} \\{\\{/failed_category_count\\}\\}
\\{\\{/context.results\\}\\}
`,
}
),
});
}