[Metric threshold rule] Adds new context variable for group by keys (#145654)

## Summary

Closes https://github.com/elastic/kibana/issues/141496

Adds new context variable called `groupByKeys` which holds object of
group by keys for Metric threshold rule.

### Manual Testing

1. Create Metric threshold rule with group by e.g. `host.name`,
`host.network.name`
2. Define action template with `context.groupByKeys`
3. Wait for alerts to be generated
4. Notice the group by object to be included in the alert notification

<img width="883" alt="Screenshot 2022-11-21 at 12 27 20"
src="https://user-images.githubusercontent.com/69037875/203039799-1465a8dc-cdf6-4adb-8fe2-9ced5833c3be.png">

### Checklist
- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Bena Kansara 2022-11-24 22:37:44 +01:00 committed by GitHub
parent bb91749f0c
commit 746774fff9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View file

@ -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',
{

View file

@ -294,3 +294,23 @@ export const flattenObject = (
return acc;
}, {});
export const getGroupByObject = (
groupBy: string | string[] | undefined,
resultGroupSet: Set<string>
): Record<string, object> => {
const groupByKeysObjectMapping: Record<string, object> = {};
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;
};

View file

@ -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<MissingGroupsRecord>();
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(

View file

@ -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 }]
: []),