Skip to content

Commit 996f7ea

Browse files
committed
Add support for multiple alert expressions
1 parent 390165b commit 996f7ea

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import uuid from 'uuid';
77
import { i18n } from '@kbn/i18n';
88
import { schema } from '@kbn/config-schema';
99
import {
10-
MetricThresholdAlertTypeParams,
10+
MetricExpressionParams,
1111
Comparator,
1212
AlertStates,
1313
METRIC_THRESHOLD_ALERT_TYPE_ID,
@@ -23,7 +23,7 @@ const FIRED_ACTIONS = {
2323

2424
async function getMetric(
2525
{ callCluster }: AlertServices,
26-
{ metric, aggType, timeUnit, timeSize, indexPattern }: MetricThresholdAlertTypeParams
26+
{ metric, aggType, timeUnit, timeSize, indexPattern }: MetricExpressionParams
2727
) {
2828
const interval = `${timeSize}${timeUnit}`;
2929
const searchBody = {
@@ -95,37 +95,49 @@ export async function registerMetricThresholdAlertType(alertingPlugin: PluginSet
9595
name: 'Metric Alert - Threshold',
9696
validate: {
9797
params: schema.object({
98-
threshold: schema.arrayOf(schema.number()),
99-
comparator: schema.string(),
100-
aggType: schema.string(),
101-
metric: schema.string(),
102-
timeUnit: schema.string(),
103-
timeSize: schema.number(),
104-
indexPattern: schema.string(),
98+
expressions: schema.arrayOf(
99+
schema.object({
100+
threshold: schema.arrayOf(schema.number()),
101+
comparator: schema.string(),
102+
aggType: schema.string(),
103+
metric: schema.string(),
104+
timeUnit: schema.string(),
105+
timeSize: schema.number(),
106+
indexPattern: schema.string(),
107+
})
108+
),
105109
}),
106110
},
107111
defaultActionGroupId: FIRED_ACTIONS.id,
108112
actionGroups: [FIRED_ACTIONS],
109113
async executor({ services, params }) {
110-
const { threshold, comparator } = params as MetricThresholdAlertTypeParams;
114+
const { expressions } = params as { expressions: MetricExpressionParams[] };
111115
const alertInstance = services.alertInstanceFactory(alertUUID);
112-
const currentValue = await getMetric(services, params as MetricThresholdAlertTypeParams);
113-
if (typeof currentValue === 'undefined')
114-
throw new Error('Could not get current value of metric');
115116

116-
const comparisonFunction = comparatorMap[comparator];
117+
const alertResults = await Promise.all(
118+
expressions.map(({ threshold, comparator }) =>
119+
(async () => {
120+
const currentValue = await getMetric(services, params as MetricExpressionParams);
121+
if (typeof currentValue === 'undefined')
122+
throw new Error('Could not get current value of metric');
117123

118-
const isValueInAlertState = comparisonFunction(currentValue, threshold);
124+
const comparisonFunction = comparatorMap[comparator];
125+
return { shouldFire: comparisonFunction(currentValue, threshold), currentValue };
126+
})()
127+
)
128+
);
119129

120-
if (isValueInAlertState) {
130+
const shouldAlertFire = alertResults.every(({ shouldFire }) => shouldFire);
131+
132+
if (shouldAlertFire) {
121133
alertInstance.scheduleActions(FIRED_ACTIONS.id, {
122-
value: currentValue,
134+
value: alertResults.map(({ currentValue }) => currentValue),
123135
});
124136
}
125137

126138
// Future use: ability to fetch display current alert state
127139
alertInstance.replaceState({
128-
alertState: isValueInAlertState ? AlertStates.ALERT : AlertStates.OK,
140+
alertState: shouldAlertFire ? AlertStates.ALERT : AlertStates.OK,
129141
});
130142
},
131143
});

x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export enum AlertStates {
2323

2424
export type TimeUnit = 's' | 'm' | 'h' | 'd';
2525

26-
export interface MetricThresholdAlertTypeParams {
26+
export interface MetricExpressionParams {
2727
aggType: MetricsExplorerAggregation;
2828
metric: string;
2929
timeSize: number;

0 commit comments

Comments
 (0)