From 61905cf92e8eec46b017050623e81aeb099c1351 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 28 Jul 2021 15:55:18 -0400 Subject: [PATCH] [RAC][Metrics UI] Index `reason` field on inventory metric alerts (#106842) (#107061) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: mgiota Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Alejandro Fernández Gómez Co-authored-by: mgiota --- .../alerting/inventory/rule_data_formatters.ts | 14 +++----------- .../inventory_metric_threshold_executor.ts | 10 +++++++--- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/infra/public/alerting/inventory/rule_data_formatters.ts b/x-pack/plugins/infra/public/alerting/inventory/rule_data_formatters.ts index 1d8414d6abd23..30e0de9402191 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/rule_data_formatters.ts +++ b/x-pack/plugins/infra/public/alerting/inventory/rule_data_formatters.ts @@ -5,20 +5,12 @@ * 2.0. */ -import { i18n } from '@kbn/i18n'; -import { ALERT_ID } from '@kbn/rule-data-utils'; +import { ALERT_REASON } from '@kbn/rule-data-utils'; import { ObservabilityRuleTypeFormatter } from '../../../../observability/public'; export const formatReason: ObservabilityRuleTypeFormatter = ({ fields }) => { - const groupName = fields[ALERT_ID]; - const reason = i18n.translate('xpack.infra.metrics.alerting.inventory.alertReasonDescription', { - defaultMessage: 'Inventory alert for {groupName}.', // TEMP reason message, will be deleted once we index the reason field - values: { - groupName, - }, - }); - - const link = '/app/metrics/inventory'; + const reason = fields[ALERT_REASON] ?? '-'; + const link = '/app/metrics/inventory'; // TODO https://github.com/elastic/kibana/issues/106497 return { reason, 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