Skip to content

Commit

Permalink
fix i18n & check errors and move group to the end of the reason text
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiota committed Oct 18, 2021
1 parent 88fc6ee commit 065de0a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
23 changes: 11 additions & 12 deletions x-pack/plugins/infra/server/lib/alerting/common/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ const thresholdToI18n = ([a, b]: Array<number | string>) => {
};

export const buildFiredAlertReason: (alertResult: {
groupName: string;
group: string;
metric: string;
comparator: Comparator;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ groupName, metric, comparator, threshold, currentValue }) =>
}) => string = ({ group, metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.firedAlertReason', {
defaultMessage:
'{groupName}: {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: {
groupName,
group,
metric,
comparator: comparatorToI18n(comparator, threshold.map(toNumber), toNumber(currentValue)),
threshold: thresholdToI18n(threshold),
Expand All @@ -128,17 +128,17 @@ export const buildFiredAlertReason: (alertResult: {
});

export const buildRecoveredAlertReason: (alertResult: {
groupName: string;
group: string;
metric: string;
comparator: Comparator;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ groupName, metric, comparator, threshold, currentValue }) =>
}) => string = ({ group, metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.recoveredAlertReason', {
defaultMessage:
'{groupName}: {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: {
groupName,
group,
metric,
comparator: recoveredComparatorToI18n(
comparator,
Expand All @@ -151,15 +151,14 @@ export const buildRecoveredAlertReason: (alertResult: {
});

export const buildNoDataAlertReason: (alertResult: {
groupName: string;
group: string;
metric: string;
timeSize: number;
timeUnit: string;
}) => string = ({ groupName, metric, timeSize, timeUnit }) =>
}) => string = ({ group, metric, timeSize, timeUnit }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.noDataAlertReason', {
defaultMessage: '{groupName}: {metric} has reported no data over the past {interval}',
defaultMessage: '{metric} has reported no data over the past {interval} for {group}',
values: {
groupName,
metric,
interval: `${timeSize}${timeUnit}`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,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 @@ -138,8 +138,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
reason = results
.map((result) =>
buildReasonWithVerboseMetricName(
item,
result[item],
group,
result[group],
buildFiredAlertReason,
nextState === AlertStates.WARNING
)
Expand All @@ -152,22 +152,22 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
*/
// } else if (nextState === AlertStates.OK && prevState?.alertState === AlertStates.ALERT) {
// reason = results
// .map((result) => buildReasonWithVerboseMetricName(item, 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)
.filter((result) => result[group].isNoData)
.map((result) =>
buildReasonWithVerboseMetricName(item, result[item], buildNoDataAlertReason)
buildReasonWithVerboseMetricName(group, result[group], buildNoDataAlertReason)
)
.join('\n');
} else if (nextState === AlertStates.ERROR) {
reason = results
.filter((result) => result[item].isError)
.filter((result) => result[group].isError)
.map((result) =>
buildReasonWithVerboseMetricName(item, result[item], buildErrorAlertReason)
buildReasonWithVerboseMetricName(group, result[group], buildErrorAlertReason)
)
.join('\n');
}
Expand All @@ -180,20 +180,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 @@ -204,15 +204,15 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
});

const buildReasonWithVerboseMetricName = (
groupName: string,
group: string,
resultItem: any,
buildReason: (r: any) => string,
useWarningThreshold?: boolean
) => {
if (!resultItem) return '';
const resultWithVerboseMetricName = {
...resultItem,
groupName,
group,
metric:
toMetricOpt(resultItem.metric)?.text ||
(resultItem.metric === 'custom'
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
6 changes: 3 additions & 3 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -13196,15 +13196,15 @@
"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": "{groupName}: {metric}は{comparator} {threshold}のしきい値です(現在の値は{currentValue})",
"xpack.infra.metrics.alerting.threshold.firedAlertReason": "{metric}は{comparator} {threshold}のしきい値です(現在の値は{currentValue})for {group}",
"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.noDataAlertReason": "{metric}は過去{interval}にデータを報告していません for {group}",
"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.recoveredAlertReason": "{metric}は{comparator} {threshold}のしきい値です(現在の値は{currentValue})for {group}",
"xpack.infra.metrics.alerting.threshold.thresholdRange": "{a}と{b}",
"xpack.infra.metrics.alerting.threshold.warning": "警告",
"xpack.infra.metrics.alerting.threshold.warningState": "警告",
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -13383,15 +13383,15 @@
"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": "{groupName}: {metric} {comparator}阈值 {threshold}(当前值为 {currentValue})",
"xpack.infra.metrics.alerting.threshold.firedAlertReason": "{metric} {comparator}阈值 {threshold}(当前值为 {currentValue})for {group}",
"xpack.infra.metrics.alerting.threshold.gtComparator": "大于",
"xpack.infra.metrics.alerting.threshold.ltComparator": "小于",
"xpack.infra.metrics.alerting.threshold.noDataAlertReason": "{groupName}: {metric} 在过去 {interval}中未报告数据",
"xpack.infra.metrics.alerting.threshold.noDataAlertReason": "{metric} 在过去 {interval}中未报告数据 for {group}",
"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": "{groupName}: {metric} 现在{comparator}阈值 {threshold}(当前值为 {currentValue})",
"xpack.infra.metrics.alerting.threshold.recoveredAlertReason": "{metric} 现在{comparator}阈值 {threshold}(当前值为 {currentValue})for {group}",
"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 065de0a

Please sign in to comment.