From 29480f75ef64a8815aeab36b864411d9955f3d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Tue, 27 Jul 2021 14:06:25 +0200 Subject: [PATCH] Pass a reason to the alert instance factory --- .../inventory_metric_threshold_executor.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts index 025bc54e11cc9..ddfc575438faa 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts @@ -7,6 +7,7 @@ import { first, get, last } from 'lodash'; import { i18n } from '@kbn/i18n'; +import { ALERT_REASON } from '@kbn/rule-data-utils'; import moment from 'moment'; import { getCustomMetricLabel } from '../../../../common/formatters/get_custom_metric_label'; import { toMetricOpt } from '../../../../common/snapshot_metric_i18n'; @@ -56,6 +57,7 @@ type InventoryMetricThresholdAlertInstance = AlertInstance< >; type InventoryMetricThresholdAlertInstanceFactory = ( id: string, + reason: string, threshold?: number | undefined, value?: number | undefined ) => InventoryMetricThresholdAlertInstance; @@ -77,10 +79,12 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) = } = params as InventoryMetricThresholdParams; if (criteria.length === 0) throw new Error('Cannot execute an alert with 0 conditions'); const { alertWithLifecycle, savedObjectsClient } = services; - const alertInstanceFactory: InventoryMetricThresholdAlertInstanceFactory = (id) => + const alertInstanceFactory: InventoryMetricThresholdAlertInstanceFactory = (id, reason) => alertWithLifecycle({ id, - fields: {}, + fields: { + [ALERT_REASON]: reason, + }, }); const source = await libs.sources.getSourceConfiguration( @@ -175,7 +179,7 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) = ? WARNING_ACTIONS.id : FIRED_ACTIONS.id; - const alertInstance = alertInstanceFactory(`${item}`); + const alertInstance = alertInstanceFactory(`${item}`, reason); alertInstance.scheduleActions( /** * TODO: We're lying to the compiler here as explicitly calling `scheduleActions` on