Skip to content

Commit

Permalink
[RAC] [Metrics UI] Include group name in the reason message (#115171)
Browse files Browse the repository at this point in the history
* [RAC] [Metrics UI] Include group name in the reason message

* remove console log

* fix i18n errors

* fix more i18n errors

* fix i18n & check errors and move group to the end of the reason text

* add empty lines at the end of translation files

* fix more i18n tests

* try to remove manually added translations

* Revert "try to remove manually added translations"

This reverts commit 6949af2.

* apply i18n_check fix and reorder values in the formatted reason

* log threshold reformat reason message and move group info at the end
  • Loading branch information
mgiota authored Oct 19, 2021
1 parent b9024c6 commit 5fe9a31
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
18 changes: 12 additions & 6 deletions x-pack/plugins/infra/server/lib/alerting/common/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ const thresholdToI18n = ([a, b]: Array<number | string>) => {
};

export const buildFiredAlertReason: (alertResult: {
group: string;
metric: string;
comparator: Comparator;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ metric, comparator, threshold, currentValue }) =>
}) => string = ({ group, metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.firedAlertReason', {
defaultMessage:
'{metric} is {comparator} a threshold of {threshold} (current value is {currentValue})',
'{metric} is {comparator} a threshold of {threshold} (current value is {currentValue}) for {group}',
values: {
group,
metric,
comparator: comparatorToI18n(comparator, threshold.map(toNumber), toNumber(currentValue)),
threshold: thresholdToI18n(threshold),
Expand All @@ -126,14 +128,15 @@ export const buildFiredAlertReason: (alertResult: {
});

export const buildRecoveredAlertReason: (alertResult: {
group: string;
metric: string;
comparator: Comparator;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ metric, comparator, threshold, currentValue }) =>
}) => string = ({ group, metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.recoveredAlertReason', {
defaultMessage:
'{metric} is now {comparator} a threshold of {threshold} (current value is {currentValue})',
'{metric} is now {comparator} a threshold of {threshold} (current value is {currentValue}) for {group}',
values: {
metric,
comparator: recoveredComparatorToI18n(
Expand All @@ -143,19 +146,22 @@ export const buildRecoveredAlertReason: (alertResult: {
),
threshold: thresholdToI18n(threshold),
currentValue,
group,
},
});

export const buildNoDataAlertReason: (alertResult: {
group: string;
metric: string;
timeSize: number;
timeUnit: string;
}) => string = ({ metric, timeSize, timeUnit }) =>
}) => string = ({ group, metric, timeSize, timeUnit }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.noDataAlertReason', {
defaultMessage: '{metric} has reported no data over the past {interval}',
defaultMessage: '{metric} has reported no data over the past {interval} for {group}',
values: {
metric,
interval: `${timeSize}${timeUnit}`,
group,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
)
);
const inventoryItems = Object.keys(first(results)!);
for (const item of inventoryItems) {
for (const group of inventoryItems) {
// AND logic; all criteria must be across the threshold
const shouldAlertFire = results.every((result) => {
// Grab the result of the most recent bucket
return last(result[item].shouldFire);
return last(result[group].shouldFire);
});
const shouldAlertWarn = results.every((result) => last(result[item].shouldWarn));
const shouldAlertWarn = results.every((result) => last(result[group].shouldWarn));

// AND logic; because we need to evaluate all criteria, if one of them reports no data then the
// whole alert is in a No Data/Error state
const isNoData = results.some((result) => last(result[item].isNoData));
const isError = results.some((result) => result[item].isError);
const isNoData = results.some((result) => last(result[group].isNoData));
const isError = results.some((result) => result[group].isError);

const nextState = isError
? AlertStates.ERROR
Expand All @@ -129,7 +129,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
reason = results
.map((result) =>
buildReasonWithVerboseMetricName(
result[item],
group,
result[group],
buildFiredAlertReason,
nextState === AlertStates.WARNING
)
Expand All @@ -142,19 +143,23 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
*/
// } else if (nextState === AlertStates.OK && prevState?.alertState === AlertStates.ALERT) {
// reason = results
// .map((result) => buildReasonWithVerboseMetricName(result[item], buildRecoveredAlertReason))
// .map((result) => buildReasonWithVerboseMetricName(group, result[group], buildRecoveredAlertReason))
// .join('\n');
}
if (alertOnNoData) {
if (nextState === AlertStates.NO_DATA) {
reason = results
.filter((result) => result[item].isNoData)
.map((result) => buildReasonWithVerboseMetricName(result[item], buildNoDataAlertReason))
.filter((result) => result[group].isNoData)
.map((result) =>
buildReasonWithVerboseMetricName(group, result[group], buildNoDataAlertReason)
)
.join('\n');
} else if (nextState === AlertStates.ERROR) {
reason = results
.filter((result) => result[item].isError)
.map((result) => buildReasonWithVerboseMetricName(result[item], buildErrorAlertReason))
.filter((result) => result[group].isError)
.map((result) =>
buildReasonWithVerboseMetricName(group, result[group], buildErrorAlertReason)
)
.join('\n');
}
}
Expand All @@ -166,20 +171,20 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
? WARNING_ACTIONS.id
: FIRED_ACTIONS.id;

const alertInstance = alertInstanceFactory(`${item}`, reason);
const alertInstance = alertInstanceFactory(`${group}`, reason);
alertInstance.scheduleActions(
/**
* TODO: We're lying to the compiler here as explicitly calling `scheduleActions` on
* the RecoveredActionGroup isn't allowed
*/
actionGroupId as unknown as InventoryMetricThresholdAllowedActionGroups,
{
group: item,
group,
alertState: stateToAlertMessage[nextState],
reason,
timestamp: moment().toISOString(),
value: mapToConditionsLookup(results, (result) =>
formatMetric(result[item].metric, result[item].currentValue)
formatMetric(result[group].metric, result[group].currentValue)
),
threshold: mapToConditionsLookup(criteria, (c) => c.threshold),
metric: mapToConditionsLookup(criteria, (c) => c.metric),
Expand All @@ -190,13 +195,15 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
});

const buildReasonWithVerboseMetricName = (
group: string,
resultItem: any,
buildReason: (r: any) => string,
useWarningThreshold?: boolean
) => {
if (!resultItem) return '';
const resultWithVerboseMetricName = {
...resultItem,
group,
metric:
toMetricOpt(resultItem.metric)?.text ||
(resultItem.metric === 'custom'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getReasonMessageForGroupedCountAlert = (
) =>
i18n.translate('xpack.infra.logs.alerting.threshold.groupedCountAlertReasonDescription', {
defaultMessage:
'{groupName}: {actualCount, plural, one {{actualCount} log entry} other {{actualCount} log entries} } ({translatedComparator} {expectedCount}) match the conditions.',
'{actualCount, plural, one {{actualCount} log entry} other {{actualCount} log entries} } ({translatedComparator} {expectedCount}) match the conditions for {groupName}.',
values: {
actualCount,
expectedCount,
Expand Down Expand Up @@ -66,7 +66,7 @@ export const getReasonMessageForGroupedRatioAlert = (
) =>
i18n.translate('xpack.infra.logs.alerting.threshold.groupedRatioAlertReasonDescription', {
defaultMessage:
'{groupName}: The log entries ratio is {actualRatio} ({translatedComparator} {expectedRatio}).',
'The log entries ratio is {actualRatio} ({translatedComparator} {expectedRatio}) for {groupName}.',
values: {
actualRatio,
expectedRatio,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
if (nextState === AlertStates.ALERT || nextState === AlertStates.WARNING) {
reason = alertResults
.map((result) =>
buildFiredAlertReason(
formatAlertResult(result[group], nextState === AlertStates.WARNING)
)
buildFiredAlertReason({
...formatAlertResult(result[group], nextState === AlertStates.WARNING),
group,
})
)
.join('\n');
/*
Expand Down Expand Up @@ -181,7 +182,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
if (nextState === AlertStates.NO_DATA) {
reason = alertResults
.filter((result) => result[group].isNoData)
.map((result) => buildNoDataAlertReason(result[group]))
.map((result) => buildNoDataAlertReason({ ...result[group], group }))
.join('\n');
} else if (nextState === AlertStates.ERROR) {
reason = alertResults
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -13446,15 +13446,12 @@
"xpack.infra.metrics.alerting.threshold.errorAlertReason": "{metric}のデータのクエリを試行しているときに、Elasticsearchが失敗しました",
"xpack.infra.metrics.alerting.threshold.errorState": "エラー",
"xpack.infra.metrics.alerting.threshold.fired": "アラート",
"xpack.infra.metrics.alerting.threshold.firedAlertReason": "{metric}は{comparator} {threshold}のしきい値です(現在の値は{currentValue})",
"xpack.infra.metrics.alerting.threshold.gtComparator": "より大きい",
"xpack.infra.metrics.alerting.threshold.ltComparator": "より小さい",
"xpack.infra.metrics.alerting.threshold.noDataAlertReason": "{metric}は過去{interval}にデータを報告していません",
"xpack.infra.metrics.alerting.threshold.noDataFormattedValue": "[データなし]",
"xpack.infra.metrics.alerting.threshold.noDataState": "データなし",
"xpack.infra.metrics.alerting.threshold.okState": "OK [回復済み]",
"xpack.infra.metrics.alerting.threshold.outsideRangeComparator": "の間にない",
"xpack.infra.metrics.alerting.threshold.recoveredAlertReason": "{metric}は{comparator} {threshold}のしきい値です(現在の値は{currentValue})",
"xpack.infra.metrics.alerting.threshold.thresholdRange": "{a}と{b}",
"xpack.infra.metrics.alerting.threshold.warning": "警告",
"xpack.infra.metrics.alerting.threshold.warningState": "警告",
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -13634,15 +13634,12 @@
"xpack.infra.metrics.alerting.threshold.errorAlertReason": "Elasticsearch 尝试查询 {metric} 的数据时出现故障",
"xpack.infra.metrics.alerting.threshold.errorState": "错误",
"xpack.infra.metrics.alerting.threshold.fired": "告警",
"xpack.infra.metrics.alerting.threshold.firedAlertReason": "{metric} {comparator}阈值 {threshold}(当前值为 {currentValue})",
"xpack.infra.metrics.alerting.threshold.gtComparator": "大于",
"xpack.infra.metrics.alerting.threshold.ltComparator": "小于",
"xpack.infra.metrics.alerting.threshold.noDataAlertReason": "{metric} 在过去 {interval}中未报告数据",
"xpack.infra.metrics.alerting.threshold.noDataFormattedValue": "[无数据]",
"xpack.infra.metrics.alerting.threshold.noDataState": "无数据",
"xpack.infra.metrics.alerting.threshold.okState": "正常 [已恢复]",
"xpack.infra.metrics.alerting.threshold.outsideRangeComparator": "不介于",
"xpack.infra.metrics.alerting.threshold.recoveredAlertReason": "{metric} 现在{comparator}阈值 {threshold}(当前值为 {currentValue})",
"xpack.infra.metrics.alerting.threshold.thresholdRange": "{a} 和 {b}",
"xpack.infra.metrics.alerting.threshold.warning": "警告",
"xpack.infra.metrics.alerting.threshold.warningState": "警告",
Expand Down

0 comments on commit 5fe9a31

Please sign in to comment.