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

[ML] Alerting rule for Anomaly Detection jobs monitoring #106084

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3be0912
[ML] init job health alerting rule type
darnautov Jul 14, 2021
56f7c27
[ML] add health checks selection ui
darnautov Jul 15, 2021
7fbee4e
[ML] define schema
darnautov Jul 15, 2021
ea4b8eb
[ML] support all jobs selection
darnautov Jul 15, 2021
6e2de45
[ML] jobs health service
darnautov Jul 19, 2021
69e3559
[ML] add logger
darnautov Jul 19, 2021
5837710
[ML] add context message
darnautov Jul 19, 2021
098f890
[ML] fix default message for i18n
darnautov Jul 19, 2021
7d3963e
Merge remote-tracking branch 'upstream/master' into ml-101028-operati…
darnautov Jul 19, 2021
3ec6274
[ML] check response size
darnautov Jul 19, 2021
d3832a1
[ML] add exclude jobs control
darnautov Jul 20, 2021
4c6d17e
[ML] getResultJobsHealthRuleConfig
darnautov Jul 20, 2021
5f886cb
[ML] change naming for shared services
darnautov Jul 20, 2021
2022054
[ML] fix excluded jobs filtering
darnautov Jul 20, 2021
17e45e1
[ML] check for execution results
darnautov Jul 20, 2021
576d006
[ML] update context fields
darnautov Jul 20, 2021
353e495
[ML] unit tests for getResultJobsHealthRuleConfig
darnautov Jul 20, 2021
f752dc2
[ML] refactor and job ids check
darnautov Jul 20, 2021
b08031e
[ML] rename datafeed
darnautov Jul 20, 2021
29f547a
[ML] fix translation messages
darnautov Jul 20, 2021
f756df2
[ML] hide non-implemented tests
darnautov Jul 20, 2021
2a1eb82
[ML] remove jod ids join from the getJobs call
darnautov Jul 20, 2021
bd96959
[ML] add validation for the tests config
darnautov Jul 20, 2021
b552dda
[ML] fix excluded jobs udpate
darnautov Jul 20, 2021
c391133
[ML] update jobIdsDescription message
darnautov Jul 20, 2021
ff5db7e
[ML] allow selection all jobs only for include
darnautov Jul 20, 2021
8c7b6e6
[ML] better ux for excluded jobs setup
darnautov Jul 20, 2021
b74005d
[ML] change rule type name
darnautov Jul 20, 2021
bc86e23
[ML] fix typo
darnautov Jul 20, 2021
817fd62
[ML] change instances names
darnautov Jul 21, 2021
9c0b3ec
[ML] fix messages
darnautov Jul 21, 2021
2ada9eb
[ML] hide error callout, show health checks error in EuiFormRow
darnautov Jul 21, 2021
599d122
[ML] add check for job state
darnautov Jul 21, 2021
37865f6
[ML] add alertingRules key to the doc links
darnautov Jul 21, 2021
541140f
Merge remote-tracking branch 'upstream/master' into ml-101028-operati…
darnautov Jul 21, 2021
23e74e6
[ML] update types
darnautov Jul 21, 2021
8c3b9dd
[ML] remove redundant type
darnautov Jul 21, 2021
a6e0f92
[ML] fix job and datafeed states check
darnautov Jul 22, 2021
65dbb89
[ML] fix job and datafeed states check, add comments
darnautov Jul 22, 2021
584bcbf
Merge branch 'master' into ml-101028-operational-alerting-rule
kibanamachine Jul 22, 2021
af0e7c1
[ML] add unit tests
darnautov Jul 22, 2021
9334f32
Merge remote-tracking branch 'origin/ml-101028-operational-alerting-r…
darnautov Jul 22, 2021
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
38 changes: 3 additions & 35 deletions x-pack/plugins/ml/common/constants/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,15 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { ActionGroup } from '../../../alerting/common';
import { MINIMUM_FULL_LICENSE } from '../license';
import { PLUGIN_ID } from './app';

export const ML_ALERT_TYPES = {
ANOMALY_DETECTION: 'xpack.ml.anomaly_detection_alert',
AD_JOBS_HEALTH: 'xpack.ml.anomaly_detection_jobs_health',
} as const;

export type MlAlertType = typeof ML_ALERT_TYPES[keyof typeof ML_ALERT_TYPES];

export const ANOMALY_SCORE_MATCH_GROUP_ID = 'anomaly_score_match';
export type AnomalyScoreMatchGroupId = typeof ANOMALY_SCORE_MATCH_GROUP_ID;
export const THRESHOLD_MET_GROUP: ActionGroup<AnomalyScoreMatchGroupId> = {
id: ANOMALY_SCORE_MATCH_GROUP_ID,
name: i18n.translate('xpack.ml.anomalyDetectionAlert.actionGroupName', {
defaultMessage: 'Anomaly score matched the condition',
}),
};

export const ML_ALERT_TYPES_CONFIG: Record<
MlAlertType,
{
name: string;
actionGroups: Array<ActionGroup<AnomalyScoreMatchGroupId>>;
defaultActionGroupId: AnomalyScoreMatchGroupId;
minimumLicenseRequired: string;
producer: string;
}
> = {
[ML_ALERT_TYPES.ANOMALY_DETECTION]: {
name: i18n.translate('xpack.ml.anomalyDetectionAlert.name', {
defaultMessage: 'Anomaly detection alert',
}),
actionGroups: [THRESHOLD_MET_GROUP],
defaultActionGroupId: ANOMALY_SCORE_MATCH_GROUP_ID,
minimumLicenseRequired: MINIMUM_FULL_LICENSE,
producer: PLUGIN_ID,
},
};

export const ALERT_PREVIEW_SAMPLE_SIZE = 5;

export const TOP_N_BUCKETS_COUNT = 1;

export const ALL_JOBS_SELECTION = '*';
27 changes: 27 additions & 0 deletions x-pack/plugins/ml/common/types/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,30 @@ export type MlAnomalyDetectionAlertRule = Omit<Alert<MlAnomalyDetectionAlertPara
export interface JobAlertingRuleStats {
alerting_rules?: MlAnomalyDetectionAlertRule[];
}

interface CommonHealthCheckConfig {
enabled: boolean;
}

export type MlAnomalyDetectionJobsHealthRuleParams = {
includeJobs: {
jobIds?: string[];
groupIds?: string[];
};
excludeJobs?: {
jobIds?: string[];
groupIds?: string[];
};
testsConfig?: {
dataFeed?: CommonHealthCheckConfig;
mml?: CommonHealthCheckConfig;
delayedData?: CommonHealthCheckConfig & {
docsCount?: number;
timeInterval?: string;
};
behindRealtime?: CommonHealthCheckConfig & {
timeInterval?: string;
};
errorMessages?: CommonHealthCheckConfig;
};
} & AlertTypeParams;
63 changes: 53 additions & 10 deletions x-pack/plugins/ml/public/alerting/job_selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* 2.0.
*/

import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
import React, { FC, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiComboBox, EuiComboBoxOptionOption, EuiComboBoxProps, EuiFormRow } from '@elastic/eui';
import { JobId } from '../../common/types/anomaly_detection_jobs';
import { MlApiServices } from '../application/services/ml_api_service';
import { ALL_JOBS_SELECTION } from '../../common/constants/alerts';

interface JobSelection {
jobIds?: JobId[];
Expand All @@ -25,13 +26,18 @@ export interface JobSelectorControlProps {
* Validation is handled by alerting framework
*/
errors: string[];
/** Enables multiple selection of jobs and groups */
multiSelect?: boolean;
label?: ReactNode;
}

export const JobSelectorControl: FC<JobSelectorControlProps> = ({
jobsAndGroupIds,
onChange,
adJobsApiService,
errors,
multiSelect = false,
label,
}) => {
const [options, setOptions] = useState<Array<EuiComboBoxOptionOption<string>>>([]);
const jobIds = useMemo(() => new Set(), []);
Expand Down Expand Up @@ -60,34 +66,66 @@ export const JobSelectorControl: FC<JobSelectorControlProps> = ({
});

setOptions([
...(multiSelect
? [
{
label: i18n.translate('xpack.ml.jobSelector.selectAllGroupLabel', {
defaultMessage: 'Select all',
}),
options: [
{
label: i18n.translate('xpack.ml.jobSelector.selectAllOptionLabel', {
defaultMessage: '*',
}),
value: ALL_JOBS_SELECTION,
},
],
},
]
: []),
{
label: i18n.translate('xpack.ml.jobSelector.jobOptionsLabel', {
defaultMessage: 'Jobs',
}),
options: jobIdOptions.map((v) => ({ label: v })),
},
...(multiSelect
? [
{
label: i18n.translate('xpack.ml.jobSelector.groupOptionsLabel', {
defaultMessage: 'Groups',
}),
options: groupIdOptions.map((v) => ({ label: v })),
},
]
: []),
]);
} catch (e) {
// TODO add error handling
}
}, [adJobsApiService]);

const onSelectionChange: EuiComboBoxProps<string>['onChange'] = useCallback(
(selectionUpdate) => {
((selectionUpdate) => {
if (selectionUpdate.some((selectedOption) => selectedOption.value === ALL_JOBS_SELECTION)) {
onChange({ jobIds: [ALL_JOBS_SELECTION] });
return;
}

const selectedJobIds: JobId[] = [];
const selectedGroupIds: string[] = [];
selectionUpdate.forEach(({ label }: { label: string }) => {
if (jobIds.has(label)) {
selectedJobIds.push(label);
} else if (groupIds.has(label)) {
selectedGroupIds.push(label);
selectionUpdate.forEach(({ label: selectedLabel }: { label: string }) => {
if (jobIds.has(selectedLabel)) {
selectedJobIds.push(selectedLabel);
} else if (groupIds.has(selectedLabel)) {
selectedGroupIds.push(selectedLabel);
}
});
onChange({
...(selectedJobIds.length > 0 ? { jobIds: selectedJobIds } : {}),
...(selectedGroupIds.length > 0 ? { groupIds: selectedGroupIds } : {}),
});
},
}) as Exclude<EuiComboBoxProps<string>['onChange'], undefined>,
[jobIds, groupIds]
);

Expand All @@ -99,13 +137,18 @@ export const JobSelectorControl: FC<JobSelectorControlProps> = ({
<EuiFormRow
fullWidth
label={
<FormattedMessage id="xpack.ml.jobSelector.formControlLabel" defaultMessage="Select job" />
label ?? (
<FormattedMessage
id="xpack.ml.jobSelector.formControlLabel"
defaultMessage="Select job"
/>
)
}
isInvalid={!!errors?.length}
error={errors}
>
<EuiComboBox<string>
singleSelection
singleSelection={!multiSelect}
selectedOptions={selectedOptions}
options={options}
onChange={onSelectionChange}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 React, { FC, useCallback, useMemo } from 'react';
import { EuiForm, EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { AlertTypeParamsExpressionProps } from '../../../../triggers_actions_ui/public';
import { MlAnomalyDetectionJobsHealthRuleParams } from '../../../common/types/alerts';
import { JobSelectorControl } from '../job_selector';
import { jobsApiProvider } from '../../application/services/ml_api_service/jobs';
import { HttpService } from '../../application/services/http_service';
import { useMlKibana } from '../../application/contexts/kibana';
import { TestsSelectionControl } from './tests_selection_control';

export type MlAnomalyAlertTriggerProps = AlertTypeParamsExpressionProps<MlAnomalyDetectionJobsHealthRuleParams>;

const AnomalyDetectionJobsHealthRuleTrigger: FC<MlAnomalyAlertTriggerProps> = ({
alertParams,
setAlertParams,
errors,
}) => {
const {
services: { http },
} = useMlKibana();
const mlHttpService = useMemo(() => new HttpService(http), [http]);
const adJobsApiService = useMemo(() => jobsApiProvider(mlHttpService), [mlHttpService]);

const jobsAndGroupIds: string[] = useMemo(
() => (Object.values(alertParams.includeJobs ?? {}) as string[][]).flat(),
[alertParams.includeJobs]
);

const onAlertParamChange = useCallback(
<T extends keyof MlAnomalyDetectionJobsHealthRuleParams>(param: T) => (
update: MlAnomalyDetectionJobsHealthRuleParams[T]
) => {
setAlertParams(param, update);
},
[]
);

return (
<EuiForm data-test-subj={'mlJobsHealthAlertingRuleForm'}>
<JobSelectorControl
jobsAndGroupIds={jobsAndGroupIds}
adJobsApiService={adJobsApiService}
onChange={useCallback(onAlertParamChange('includeJobs'), [])}
errors={Array.isArray(errors.includeJobs) ? errors.includeJobs : []}
multiSelect
label={
<FormattedMessage
id="xpack.ml.alertTypes.jobsHealthAlertingRule.includeJobs.label"
defaultMessage="Include jobs or groups"
/>
}
/>

<EuiSpacer size="m" />

<TestsSelectionControl
config={alertParams.testsConfig}
onChange={useCallback(onAlertParamChange('testsConfig'), [])}
/>
</EuiForm>
);
};

// Default export is required for React.lazy loading

// eslint-disable-next-line import/no-default-export
export default AnomalyDetectionJobsHealthRuleTrigger;
8 changes: 8 additions & 0 deletions x-pack/plugins/ml/public/alerting/jobs_health_rule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { registerJobsHealthAlertingRule } from './register_jobs_health_alerting_rule';
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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';

export function registerJobsHealthAlertingRule(
triggersActionsUi: TriggersAndActionsUIPublicPluginSetup,
alerting?: AlertingSetup
) {
triggersActionsUi.alertTypeRegistry.register({
id: ML_ALERT_TYPES.AD_JOBS_HEALTH,
description: i18n.translate('xpack.ml.alertTypes.jobsHealthAlertingRule.description', {
defaultMessage: 'Alert when anomaly detection jobs experiencing realtime issues.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all of the conditions will affect real-time jobs, so I think this should read Alert when anomaly detection jobs experience operational issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9c0b3ec

}),
iconClass: 'bell',
documentationUrl(docLinks) {
return `${docLinks.ELASTIC_WEBSITE_URL}guide/en/machine-learning/${docLinks.DOC_LINK_VERSION}/ml-configuring-alerts.html`;
lcawl marked this conversation as resolved.
Show resolved Hide resolved
},
alertParamsExpression: lazy(() => import('./anomaly_detection_jobs_health_rule_trigger')),
validate: (alertParams: MlAnomalyDetectionJobsHealthRuleParams) => {
darnautov marked this conversation as resolved.
Show resolved Hide resolved
const validationResult = {
darnautov marked this conversation as resolved.
Show resolved Hide resolved
errors: {
includeJobs: 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',
lcawl marked this conversation as resolved.
Show resolved Hide resolved
})
);
}

return validationResult;
},
requiresAppContext: false,
defaultActionMessage: i18n.translate(
'xpack.ml.alertTypes.jobsHealthAlertingRule.defaultActionMessage',
{
defaultMessage: `Anomaly detection jobs health check result:
\\{\\{context.message\\}\\}
- Job IDs: \\{\\{context.jobIds\\}\\}
`,
}
),
});
}
Loading