From 1a92d7ba4b29d9b2a6d66bc9362db9d6a25af9d2 Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 28 Jul 2020 15:05:05 -0500 Subject: [PATCH 1/3] [Metrics UI] Fix evaluating rate-aggregated alerts when there's no normalized value --- .../server/lib/alerting/metric_threshold/lib/evaluate_alert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index d862f70c47caec..8e4b48953107b9 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -172,7 +172,7 @@ const getValuesFromAggregations = ( } return buckets.map((bucket) => ({ key: bucket.key_as_string, - value: bucket.aggregatedValue.value, + value: bucket.aggregatedValue?.value, })); } catch (e) { return NaN; // Error state From e81719c9ee5faa02776aed7607bf24683ccae58a Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 28 Jul 2020 15:11:13 -0500 Subject: [PATCH 2/3] Force null on undefined aggregatedValue --- .../server/lib/alerting/metric_threshold/lib/evaluate_alert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index 8e4b48953107b9..24d84225b91971 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -172,7 +172,7 @@ const getValuesFromAggregations = ( } return buckets.map((bucket) => ({ key: bucket.key_as_string, - value: bucket.aggregatedValue?.value, + value: bucket.aggregatedValue?.value ?? null, })); } catch (e) { return NaN; // Error state From 29ee75f95611df4d7035ecbc28ff9032c1c267ac Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 28 Jul 2020 15:32:18 -0500 Subject: [PATCH 3/3] Add test, fix no data eval --- .../metric_threshold/lib/evaluate_alert.ts | 9 ++++-- .../metric_threshold_executor.test.ts | 29 ++++++++++++++++++- .../alerting/metric_threshold/test_mocks.ts | 13 +++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index 24d84225b91971..ca46f6cc16547a 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -67,10 +67,13 @@ export const evaluateAlert = ( currentValue: Array.isArray(points) ? last(points)?.value : NaN, timestamp: Array.isArray(points) ? last(points)?.key : NaN, shouldFire: Array.isArray(points) - ? points.map((point) => comparisonFunction(point.value, threshold)) + ? points.map( + (point) => + typeof point.value === 'number' && comparisonFunction(point.value, threshold) + ) : [false], - isNoData: points === null, - isError: isNaN(points), + isNoData: (Array.isArray(points) ? last(points)?.value : points) === null, + isError: isNaN(Array.isArray(points) ? last(points)?.value : points), }; }); }) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts index 9a46925a51762e..fa705798baf7a2 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts @@ -318,6 +318,31 @@ describe('The metric threshold alert type', () => { }); }); + describe("querying a rate-aggregated metric that hasn't reported data", () => { + const instanceID = '*'; + const execute = () => + executor({ + services, + params: { + criteria: [ + { + ...baseCriterion, + comparator: Comparator.GT, + threshold: 1, + metric: 'test.metric.3', + aggType: 'rate', + }, + ], + alertOnNoData: true, + }, + }); + test('sends a No Data alert', async () => { + await execute(); + expect(mostRecentAction(instanceID).id).toBe(FIRED_ACTIONS.id); + expect(getState(instanceID).alertState).toBe(AlertStates.NO_DATA); + }); + }); + // describe('querying a metric that later recovers', () => { // const instanceID = '*'; // const execute = (threshold: number[]) => @@ -401,7 +426,9 @@ services.callCluster.mockImplementation(async (_: string, { body, index }: any) if (metric === 'test.metric.2') { return mocks.alternateMetricResponse; } else if (metric === 'test.metric.3') { - return mocks.emptyMetricResponse; + return body.aggs.aggregatedIntervals.aggregations.aggregatedValue_max + ? mocks.emptyRateResponse + : mocks.emptyMetricResponse; } return mocks.basicMetricResponse; }); diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/test_mocks.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/test_mocks.ts index c7e53eb2008f54..5c2f76cea87c4b 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/test_mocks.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/test_mocks.ts @@ -62,6 +62,19 @@ export const emptyMetricResponse = { }, }; +export const emptyRateResponse = { + aggregations: { + aggregatedIntervals: { + buckets: [ + { + doc_count: 2, + aggregatedValue_max: { value: null }, + }, + ], + }, + }, +}; + export const basicCompositeResponse = { aggregations: { groupings: {