Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metrics Alerts] Fix action variables, default message, and EUI casing #62061

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const AlertDropdown = () => {
'kibana#/management/kibana/triggersActions/alerts'
)}
>
<FormattedMessage id="xpack.infra.alerting.manageAlerts" defaultMessage="Manage Alerts" />
<FormattedMessage id="xpack.infra.alerting.manageAlerts" defaultMessage="Manage alerts" />
</EuiContextMenuItem>,
];
}, [kibana.services]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ export function getAlertType(): AlertTypeModel {
return {
id: METRIC_THRESHOLD_ALERT_TYPE_ID,
name: i18n.translate('xpack.infra.metrics.alertFlyout.alertName', {
defaultMessage: 'Alert Trigger',
defaultMessage: 'Metric Threshold',
}),
iconClass: 'bell',
alertParamsExpression: Expressions,
validate: validateMetricThreshold,
defaultActionMessage: i18n.translate(
'xpack.infra.metrics.alerting.threshold.defaultActionMessage',
{
defaultMessage: `\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\}

\\{\\{context.metricOf.condition0\\}\\} has crossed a threshold of \\{\\{context.thresholdOf.condition0\\}\\}
Current value is \\{\\{context.valueOf.condition0\\}\\}
`,
}
),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ describe('The metric threshold alert type', () => {
expect(alertInstances.get(instanceID).mostRecentAction).toBe(undefined);
expect(alertInstances.get(instanceID).state.alertState).toBe(AlertStates.OK);
});
test('reports expected values to the action context', async () => {
await execute(Comparator.GT, [0.75]);
const mostRecentAction = alertInstances.get(instanceID).mostRecentAction;
expect(mostRecentAction.action.group).toBe('*');
expect(mostRecentAction.action.valueOf.condition0).toBe(1);
expect(mostRecentAction.action.thresholdOf.condition0).toStrictEqual([0.75]);
expect(mostRecentAction.action.metricOf.condition0).toBe('test.metric.1');
});
});

describe('querying with a groupBy parameter', () => {
Expand Down Expand Up @@ -166,6 +174,11 @@ describe('The metric threshold alert type', () => {
expect(alertInstances.get(instanceIdB).mostRecentAction).toBe(undefined);
expect(alertInstances.get(instanceIdB).state.alertState).toBe(AlertStates.OK);
});
test('reports group values to the action context', async () => {
await execute(Comparator.GT, [0.75]);
expect(alertInstances.get(instanceIdA).mostRecentAction.action.group).toBe('a');
expect(alertInstances.get(instanceIdB).mostRecentAction.action.group).toBe('b');
});
});

describe('querying with multiple criteria', () => {
Expand Down Expand Up @@ -215,6 +228,17 @@ describe('The metric threshold alert type', () => {
expect(alertInstances.get(instanceIdB).mostRecentAction).toBe(undefined);
expect(alertInstances.get(instanceIdB).state.alertState).toBe(AlertStates.OK);
});
test('sends all criteria to the action context', async () => {
const instanceID = 'test-*';
await execute(Comparator.GT_OR_EQ, [1.0], [3.0]);
const mostRecentAction = alertInstances.get(instanceID).mostRecentAction;
expect(mostRecentAction.action.valueOf.condition0).toBe(1);
expect(mostRecentAction.action.valueOf.condition1).toBe(3.5);
expect(mostRecentAction.action.thresholdOf.condition0).toStrictEqual([1.0]);
expect(mostRecentAction.action.thresholdOf.condition1).toStrictEqual([3.0]);
expect(mostRecentAction.action.metricOf.condition0).toBe('test.metric.1');
expect(mostRecentAction.action.metricOf.condition1).toBe('test.metric.2');
});
});
describe('querying with the count aggregator', () => {
const instanceID = 'test-*';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ const comparatorMap = {
[Comparator.LT_OR_EQ]: (a: number, [b]: number[]) => a <= b,
};

const mapToConditionsLookup = (
list: any[],
mapFn: (value: any, index: number, array: any[]) => unknown
) =>
list
.map(mapFn)
.reduce(
(result: Record<string, any>, value, i) => ({ ...result, [`condition${i}`]: value }),
{}
);

export const createMetricThresholdExecutor = (alertUUID: string) =>
async function({ services, params }: AlertExecutorOptions) {
const { criteria, groupBy, filterQuery } = params as {
Expand Down Expand Up @@ -261,7 +272,9 @@ export const createMetricThresholdExecutor = (alertUUID: string) =>
if (shouldAlertFire) {
alertInstance.scheduleActions(FIRED_ACTIONS.id, {
group,
value: alertResults.map(result => result[group].currentValue),
valueOf: mapToConditionsLookup(alertResults, result => result[group].currentValue),
thresholdOf: mapToConditionsLookup(criteria, criterion => criterion.threshold),
metricOf: mapToConditionsLookup(criteria, criterion => criterion.metric),
});
}
// Future use: ability to fetch display current alert state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { i18n } from '@kbn/i18n';
import uuid from 'uuid';
import { schema } from '@kbn/config-schema';
import { PluginSetupContract } from '../../../../../alerting/server';
Expand Down Expand Up @@ -49,6 +50,37 @@ export async function registerMetricThresholdAlertType(alertingPlugin: PluginSet
metric: schema.never(),
});

const groupActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription',
{
defaultMessage: 'Name of the group reporting data',
}
);

const valueOfActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.valueOfActionVariableDescription',
{
defaultMessage:
'Record of the current value of the watched metric; grouped by condition, i.e valueOf.condition0, valueOf.condition1, etc.',
}
);

const thresholdOfActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription',
{
defaultMessage:
'Record of the alerting threshold; grouped by condition, i.e thresholdOf.condition0, thresholdOf.condition1, etc.',
}
);

const metricOfActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription',
{
defaultMessage:
'Record of the watched metric; grouped by condition, i.e metricOf.condition0, metricOf.condition1, etc.',
}
);

alertingPlugin.registerType({
id: METRIC_THRESHOLD_ALERT_TYPE_ID,
name: 'Metric Alert - Threshold',
Expand All @@ -62,5 +94,13 @@ export async function registerMetricThresholdAlertType(alertingPlugin: PluginSet
defaultActionGroupId: FIRED_ACTIONS.id,
actionGroups: [FIRED_ACTIONS],
executor: createMetricThresholdExecutor(alertUUID),
actionVariables: {
context: [
{ name: 'group', description: groupActionVariableDescription },
{ name: 'valueOf', description: valueOfActionVariableDescription },
{ name: 'thresholdOf', description: thresholdOfActionVariableDescription },
{ name: 'metricOf', description: metricOfActionVariableDescription },
],
},
});
}