Skip to content

Commit

Permalink
[Transform] Transforms health alerting rule type (#112277) (#114142)
Browse files Browse the repository at this point in the history
Co-authored-by: Dima Arnautov <[email protected]>
  • Loading branch information
kibanamachine and darnautov authored Oct 6, 2021
1 parent c6b7e9c commit 2875113
Show file tree
Hide file tree
Showing 34 changed files with 1,047 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/core/public/doc_links/doc_links_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ export class DocLinksService {
},
transforms: {
guide: `${ELASTICSEARCH_DOCS}transforms.html`,
// TODO add valid docs URL
alertingRules: `${ELASTIC_WEBSITE_URL}guide/en/machine-learning/${DOC_LINK_VERSION}/ml-configuring-alerts.html`,
},
visualize: {
guide: `${KIBANA_DOCS}dashboard.html`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const byTypeSchema: MakeSchemaFrom<AlertsUsage>['count_by_type'] = {
// Built-in
'__index-threshold': { type: 'long' },
'__es-query': { type: 'long' },
transform_health: { type: 'long' },
// APM
apm__error_rate: { type: 'long' }, // eslint-disable-line @typescript-eslint/naming-convention
apm__transaction_error_rate: { type: 'long' }, // eslint-disable-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -45,8 +46,8 @@ const byTypeSchema: MakeSchemaFrom<AlertsUsage>['count_by_type'] = {
// Maps
'__geo-containment': { type: 'long' },
// ML
xpack_ml_anomaly_detection_alert: { type: 'long' },
xpack_ml_anomaly_detection_jobs_health: { type: 'long' },
xpack__ml__anomaly_detection_alert: { type: 'long' }, // eslint-disable-line @typescript-eslint/naming-convention
xpack__ml__anomaly_detection_jobs_health: { type: 'long' }, // eslint-disable-line @typescript-eslint/naming-convention
};

export function createAlertsUsageCollector(
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/monitoring/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class MonitoringPlugin
for (const alert of alerts) {
plugins.alerting?.registerType(alert.getRuleType());
}

const config = createConfig(this.initializerContext.config.get<TypeOf<typeof configSchema>>());

// Register collector objects for stats to show up in the APIs
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/stack_alerts/server/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ describe('Stack Alerts Feature Privileges', () => {
BUILT_IN_ALERTS_FEATURE.privileges?.all?.alerting?.rule?.all ?? [];
const typesInFeaturePrivilegeRead =
BUILT_IN_ALERTS_FEATURE.privileges?.read?.alerting?.rule?.read ?? [];
expect(alertingSetup.registerType.mock.calls.length).toEqual(typesInFeaturePrivilege.length);
expect(alertingSetup.registerType.mock.calls.length).toEqual(typesInFeaturePrivilegeAll.length);
// transform alerting rule is initialized during the transform plugin setup
expect(alertingSetup.registerType.mock.calls.length).toEqual(
typesInFeaturePrivilegeRead.length
typesInFeaturePrivilege.length - 1
);
expect(alertingSetup.registerType.mock.calls.length).toEqual(
typesInFeaturePrivilegeAll.length - 1
);
expect(alertingSetup.registerType.mock.calls.length).toEqual(
typesInFeaturePrivilegeRead.length - 1
);

alertingSetup.registerType.mock.calls.forEach((call) => {
Expand Down
13 changes: 8 additions & 5 deletions x-pack/plugins/stack_alerts/server/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { GEO_CONTAINMENT_ID as GeoContainment } from './alert_types/geo_containm
import { ES_QUERY_ID as ElasticsearchQuery } from './alert_types/es_query/alert_type';
import { STACK_ALERTS_FEATURE_ID } from '../common';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server';
import { TRANSFORM_RULE_TYPE } from '../../transform/common';

const TransformHealth = TRANSFORM_RULE_TYPE.TRANSFORM_HEALTH;

export const BUILT_IN_ALERTS_FEATURE: KibanaFeatureConfig = {
id: STACK_ALERTS_FEATURE_ID,
Expand All @@ -23,7 +26,7 @@ export const BUILT_IN_ALERTS_FEATURE: KibanaFeatureConfig = {
management: {
insightsAndAlerting: ['triggersActions'],
},
alerting: [IndexThreshold, GeoContainment, ElasticsearchQuery],
alerting: [IndexThreshold, GeoContainment, ElasticsearchQuery, TransformHealth],
privileges: {
all: {
app: [],
Expand All @@ -33,10 +36,10 @@ export const BUILT_IN_ALERTS_FEATURE: KibanaFeatureConfig = {
},
alerting: {
rule: {
all: [IndexThreshold, GeoContainment, ElasticsearchQuery],
all: [IndexThreshold, GeoContainment, ElasticsearchQuery, TransformHealth],
},
alert: {
all: [IndexThreshold, GeoContainment, ElasticsearchQuery],
all: [IndexThreshold, GeoContainment, ElasticsearchQuery, TransformHealth],
},
},
savedObject: {
Expand All @@ -54,10 +57,10 @@ export const BUILT_IN_ALERTS_FEATURE: KibanaFeatureConfig = {
},
alerting: {
rule: {
read: [IndexThreshold, GeoContainment, ElasticsearchQuery],
read: [IndexThreshold, GeoContainment, ElasticsearchQuery, TransformHealth],
},
alert: {
read: [IndexThreshold, GeoContainment, ElasticsearchQuery],
read: [IndexThreshold, GeoContainment, ElasticsearchQuery, TransformHealth],
},
},
savedObject: {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/stack_alerts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{ "path": "../triggers_actions_ui/tsconfig.json" },
{ "path": "../../../src/plugins/kibana_react/tsconfig.json" },
{ "path": "../../../src/plugins/saved_objects/tsconfig.json" },
{ "path": "../../../src/plugins/data/tsconfig.json" }
{ "path": "../../../src/plugins/data/tsconfig.json" },
{ "path": "../transform/tsconfig.json" }
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@
"__es-query": {
"type": "long"
},
"transform_health": {
"type": "long"
},
"apm__error_rate": {
"type": "long"
},
Expand Down Expand Up @@ -226,10 +229,10 @@
"__geo-containment": {
"type": "long"
},
"xpack_ml_anomaly_detection_alert": {
"xpack__ml__anomaly_detection_alert": {
"type": "long"
},
"xpack_ml_anomaly_detection_jobs_health": {
"xpack__ml__anomaly_detection_jobs_health": {
"type": "long"
}
}
Expand All @@ -245,6 +248,9 @@
"__es-query": {
"type": "long"
},
"transform_health": {
"type": "long"
},
"apm__error_rate": {
"type": "long"
},
Expand Down Expand Up @@ -308,10 +314,10 @@
"__geo-containment": {
"type": "long"
},
"xpack_ml_anomaly_detection_alert": {
"xpack__ml__anomaly_detection_alert": {
"type": "long"
},
"xpack_ml_anomaly_detection_jobs_health": {
"xpack__ml__anomaly_detection_jobs_health": {
"type": "long"
}
}
Expand Down
24 changes: 24 additions & 0 deletions x-pack/plugins/transform/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';

import { LicenseType } from '../../licensing/common/types';
import { TransformHealthTests } from './types/alerting';

export const DEFAULT_REFRESH_INTERVAL_MS = 30000;
export const MINIMUM_REFRESH_INTERVAL_MS = 1000;
Expand Down Expand Up @@ -108,3 +109,26 @@ export const TRANSFORM_FUNCTION = {
} as const;

export type TransformFunction = typeof TRANSFORM_FUNCTION[keyof typeof TRANSFORM_FUNCTION];

export const TRANSFORM_RULE_TYPE = {
TRANSFORM_HEALTH: 'transform_health',
} as const;

export const ALL_TRANSFORMS_SELECTION = '*';

export const TRANSFORM_HEALTH_CHECK_NAMES: Record<
TransformHealthTests,
{ name: string; description: string }
> = {
notStarted: {
name: i18n.translate('xpack.transform.alertTypes.transformHealth.notStartedCheckName', {
defaultMessage: 'Transform is not started',
}),
description: i18n.translate(
'xpack.transform.alertTypes.transformHealth.notStartedCheckDescription',
{
defaultMessage: 'Get alerts when the transform is not started or is not indexing data.',
}
),
},
};
8 changes: 8 additions & 0 deletions x-pack/plugins/transform/common/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 { TRANSFORM_RULE_TYPE } from './constants';
22 changes: 22 additions & 0 deletions x-pack/plugins/transform/common/types/alerting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 type { AlertTypeParams } from '../../../alerting/common';

export type TransformHealthRuleParams = {
includeTransforms?: string[];
excludeTransforms?: string[] | null;
testsConfig?: {
notStarted?: {
enabled: boolean;
} | null;
} | null;
} & AlertTypeParams;

export type TransformHealthRuleTestsConfig = TransformHealthRuleParams['testsConfig'];

export type TransformHealthTests = keyof Exclude<TransformHealthRuleTestsConfig, null | undefined>;
4 changes: 4 additions & 0 deletions x-pack/plugins/transform/common/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export function dictionaryToArray<TValue>(dict: Dictionary<TValue>): TValue[] {
export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};

export function isDefined<T>(argument: T | undefined | null): argument is T {
return argument !== undefined && argument !== null;
}
16 changes: 16 additions & 0 deletions x-pack/plugins/transform/common/utils/alerts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 type { TransformHealthRuleTestsConfig } from '../types/alerting';

export function getResultTestConfig(config: TransformHealthRuleTestsConfig) {
return {
notStarted: {
enabled: config?.notStarted?.enabled ?? true,
},
};
}
10 changes: 8 additions & 2 deletions x-pack/plugins/transform/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"management",
"features",
"savedObjects",
"share"
"share",
"triggersActionsUi",
"fieldFormats"
],
"optionalPlugins": [
"security",
"usageCollection"
"usageCollection",
"alerting"
],
"configPath": ["xpack", "transform"],
"requiredBundles": [
Expand All @@ -24,6 +27,9 @@
"kibanaReact",
"ml"
],
"extraPublicDirs": [
"common"
],
"owner": {
"name": "Machine Learning UI",
"githubTeam": "ml-ui"
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/transform/public/alerting/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 { getTransformHealthRuleType } from './transform_health_rule_type';
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 { getTransformHealthRuleType } from './register_transform_health_rule';
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 { TRANSFORM_RULE_TYPE } from '../../../common';
import type { TransformHealthRuleParams } from '../../../common/types/alerting';
import type { AlertTypeModel } from '../../../../triggers_actions_ui/public';

export function getTransformHealthRuleType(): AlertTypeModel<TransformHealthRuleParams> {
return {
id: TRANSFORM_RULE_TYPE.TRANSFORM_HEALTH,
description: i18n.translate('xpack.transform.alertingRuleTypes.transformHealth.description', {
defaultMessage: 'Alert when transforms experience operational issues.',
}),
iconClass: 'bell',
documentationUrl(docLinks) {
return docLinks.links.transforms.alertingRules;
},
alertParamsExpression: lazy(() => import('./transform_health_rule_trigger')),
validate: (alertParams: TransformHealthRuleParams) => {
const validationResult = {
errors: {
includeTransforms: new Array<string>(),
} as Record<keyof TransformHealthRuleParams, string[]>,
};

if (!alertParams.includeTransforms?.length) {
validationResult.errors.includeTransforms?.push(
i18n.translate(
'xpack.transform.alertTypes.transformHealth.includeTransforms.errorMessage',
{
defaultMessage: 'At least one transform has to be selected',
}
)
);
}

return validationResult;
},
requiresAppContext: false,
defaultActionMessage: i18n.translate(
'xpack.transform.alertTypes.transformHealth.defaultActionMessage',
{
defaultMessage: `[\\{\\{rule.name\\}\\}] Transform health check result:
\\{\\{context.message\\}\\}
\\{\\{#context.results\\}\\}
Transform ID: \\{\\{transform_id\\}\\}
\\{\\{#description\\}\\}Transform description: \\{\\{description\\}\\}
\\{\\{/description\\}\\}\\{\\{#transform_state\\}\\}Transform state: \\{\\{transform_state\\}\\}
\\{\\{/transform_state\\}\\}\\{\\{#failure_reason\\}\\}Failure reason: \\{\\{failure_reason\\}\\}
\\{\\{/failure_reason\\}\\}\\{\\{#notification_message\\}\\}Notification message: \\{\\{notification_message\\}\\}
\\{\\{/notification_message\\}\\}\\{\\{#node_name\\}\\}Node name: \\{\\{node_name\\}\\}
\\{\\{/node_name\\}\\}\\{\\{#timestamp\\}\\}Timestamp: \\{\\{timestamp\\}\\}
\\{\\{/timestamp\\}\\}
\\{\\{/context.results\\}\\}
`,
}
),
};
}
Loading

0 comments on commit 2875113

Please sign in to comment.