Skip to content

Commit 29ee75f

Browse files
committed
Add test, fix no data eval
1 parent e81719c commit 29ee75f

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ export const evaluateAlert = (
6767
currentValue: Array.isArray(points) ? last(points)?.value : NaN,
6868
timestamp: Array.isArray(points) ? last(points)?.key : NaN,
6969
shouldFire: Array.isArray(points)
70-
? points.map((point) => comparisonFunction(point.value, threshold))
70+
? points.map(
71+
(point) =>
72+
typeof point.value === 'number' && comparisonFunction(point.value, threshold)
73+
)
7174
: [false],
72-
isNoData: points === null,
73-
isError: isNaN(points),
75+
isNoData: (Array.isArray(points) ? last(points)?.value : points) === null,
76+
isError: isNaN(Array.isArray(points) ? last(points)?.value : points),
7477
};
7578
});
7679
})

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ describe('The metric threshold alert type', () => {
318318
});
319319
});
320320

321+
describe("querying a rate-aggregated metric that hasn't reported data", () => {
322+
const instanceID = '*';
323+
const execute = () =>
324+
executor({
325+
services,
326+
params: {
327+
criteria: [
328+
{
329+
...baseCriterion,
330+
comparator: Comparator.GT,
331+
threshold: 1,
332+
metric: 'test.metric.3',
333+
aggType: 'rate',
334+
},
335+
],
336+
alertOnNoData: true,
337+
},
338+
});
339+
test('sends a No Data alert', async () => {
340+
await execute();
341+
expect(mostRecentAction(instanceID).id).toBe(FIRED_ACTIONS.id);
342+
expect(getState(instanceID).alertState).toBe(AlertStates.NO_DATA);
343+
});
344+
});
345+
321346
// describe('querying a metric that later recovers', () => {
322347
// const instanceID = '*';
323348
// const execute = (threshold: number[]) =>
@@ -401,7 +426,9 @@ services.callCluster.mockImplementation(async (_: string, { body, index }: any)
401426
if (metric === 'test.metric.2') {
402427
return mocks.alternateMetricResponse;
403428
} else if (metric === 'test.metric.3') {
404-
return mocks.emptyMetricResponse;
429+
return body.aggs.aggregatedIntervals.aggregations.aggregatedValue_max
430+
? mocks.emptyRateResponse
431+
: mocks.emptyMetricResponse;
405432
}
406433
return mocks.basicMetricResponse;
407434
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ export const emptyMetricResponse = {
6262
},
6363
};
6464

65+
export const emptyRateResponse = {
66+
aggregations: {
67+
aggregatedIntervals: {
68+
buckets: [
69+
{
70+
doc_count: 2,
71+
aggregatedValue_max: { value: null },
72+
},
73+
],
74+
},
75+
},
76+
};
77+
6578
export const basicCompositeResponse = {
6679
aggregations: {
6780
groupings: {

0 commit comments

Comments
 (0)