From 1b2e712056e6c22be49d85088cc3ecb554893a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Tue, 27 Jul 2021 13:38:56 +0200 Subject: [PATCH 1/4] Pass `reason` from the alert fields --- .../alerting/inventory/rule_data_formatters.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 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..7dd8f119405cb 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'; // FIXME? Not sure if we need to do something with this link return { reason, 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 2/4] 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 From 246d857d0d9b42621da500cf3684b529b1343bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Wed, 28 Jul 2021 10:56:50 +0200 Subject: [PATCH 3/4] Don't leave the reason empty if it's not indexed Co-authored-by: mgiota --- .../infra/public/alerting/inventory/rule_data_formatters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7dd8f119405cb..5b4fd153f3c69 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 @@ -9,7 +9,7 @@ import { ALERT_REASON } from '@kbn/rule-data-utils'; import { ObservabilityRuleTypeFormatter } from '../../../../observability/public'; export const formatReason: ObservabilityRuleTypeFormatter = ({ fields }) => { - const reason = fields[ALERT_REASON] ?? ''; + const reason = fields[ALERT_REASON] ?? '-'; const link = '/app/metrics/inventory'; // FIXME? Not sure if we need to do something with this link return { From 48c33d854a1449e909430693db88ff1ee4795bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Wed, 28 Jul 2021 10:58:42 +0200 Subject: [PATCH 4/4] Link to the righ issue to update the metrics link --- .../infra/public/alerting/inventory/rule_data_formatters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5b4fd153f3c69..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 @@ -10,7 +10,7 @@ import { ObservabilityRuleTypeFormatter } from '../../../../observability/public export const formatReason: ObservabilityRuleTypeFormatter = ({ fields }) => { const reason = fields[ALERT_REASON] ?? '-'; - const link = '/app/metrics/inventory'; // FIXME? Not sure if we need to do something with this link + const link = '/app/metrics/inventory'; // TODO https://github.com/elastic/kibana/issues/106497 return { reason,