diff --git a/x-pack/plugins/infra/server/lib/alerting/common/messages.ts b/x-pack/plugins/infra/server/lib/alerting/common/messages.ts index 644c31813deae..d35651d66c95a 100644 --- a/x-pack/plugins/infra/server/lib/alerting/common/messages.ts +++ b/x-pack/plugins/infra/server/lib/alerting/common/messages.ts @@ -162,6 +162,13 @@ export const groupActionVariableDescription = i18n.translate( } ); +export const groupByKeysActionVariableDescription = i18n.translate( + 'xpack.infra.metrics.alerting.groupByKeysActionVariableDescription', + { + defaultMessage: 'The object containing groups that are reporting data', + } +); + export const alertStateActionVariableDescription = i18n.translate( 'xpack.infra.metrics.alerting.alertStateActionVariableDescription', { diff --git a/x-pack/plugins/infra/server/lib/alerting/common/utils.ts b/x-pack/plugins/infra/server/lib/alerting/common/utils.ts index ce0f0970f37bf..72974f645e4ee 100644 --- a/x-pack/plugins/infra/server/lib/alerting/common/utils.ts +++ b/x-pack/plugins/infra/server/lib/alerting/common/utils.ts @@ -294,3 +294,23 @@ export const flattenObject = ( return acc; }, {}); + +export const getGroupByObject = ( + groupBy: string | string[] | undefined, + resultGroupSet: Set +): Record => { + const groupByKeysObjectMapping: Record = {}; + if (groupBy) { + resultGroupSet.forEach((groupSet) => { + const groupSetKeys = groupSet.split(','); + groupByKeysObjectMapping[groupSet] = unflattenObject( + Array.isArray(groupBy) + ? groupBy.reduce((result, group, index) => { + return { ...result, [group]: groupSetKeys[index] }; + }, {}) + : { [groupBy]: groupSet } + ); + }); + } + return groupByKeysObjectMapping; +}; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts index 09eced776d1a9..7fc1a80812a6f 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts @@ -36,6 +36,7 @@ import { hasAdditionalContext, validGroupByForContext, flattenAdditionalContext, + getGroupByObject, } from '../common/utils'; import { EvaluatedRuleParams, evaluateRule } from './lib/evaluate_rule'; @@ -187,6 +188,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => } } + const groupByKeysObjectMapping = getGroupByObject(params.groupBy, resultGroupSet); const groups = [...resultGroupSet]; const nextMissingGroups = new Set(); const hasGroups = !isEqual(groups, [UNGROUPED_FACTORY_KEY]); @@ -277,6 +279,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => alertDetailsUrl: getAlertDetailsUrl(libs.basePath, spaceId, alertUuid), alertState: stateToAlertMessage[nextState], group, + groupByKeys: groupByKeysObjectMapping[group], metric: mapToConditionsLookup(criteria, (c) => c.metric), reason, threshold: mapToConditionsLookup( diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts index cb98a6a23d4f0..55e2379bcf19a 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts @@ -18,6 +18,7 @@ import { cloudActionVariableDescription, containerActionVariableDescription, groupActionVariableDescription, + groupByKeysActionVariableDescription, hostActionVariableDescription, labelsActionVariableDescription, metricActionVariableDescription, @@ -106,6 +107,7 @@ export async function registerMetricThresholdRuleType( actionVariables: { context: [ { name: 'group', description: groupActionVariableDescription }, + { name: 'groupByKeys', description: groupByKeysActionVariableDescription }, ...(getAlertDetailsPageEnabledForApp(config, 'metrics') ? [{ name: 'alertDetailsUrl', description: alertDetailUrlActionVariableDescription }] : []),