Skip to content

Commit 4f23f0a

Browse files
[Uptime] Fix alerting false positives (#75577)
* Add test and modify code to account for complex filter input. * Avoid unnecessary nesting. * Simplify filtering code. * Use `must` instead of `should`. * Fix types. * Refresh snapshots. * Use `filter` instead of `must`. * Refactor to improve readability. * Resolve filtering issue for availability query. * Add test covering filter fix. * Removed unused var. Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 304670c commit 4f23f0a

File tree

4 files changed

+462
-37
lines changed

4 files changed

+462
-37
lines changed

x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts

Lines changed: 145 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,32 @@ describe('monitor availability', () => {
203203
},
204204
},
205205
},
206-
],
207-
"minimum_should_match": 1,
208-
"should": Array [
209206
Object {
210207
"bool": Object {
211208
"minimum_should_match": 1,
212209
"should": Array [
213210
Object {
214-
"match_phrase": Object {
215-
"monitor.id": "apm-dev",
211+
"bool": Object {
212+
"minimum_should_match": 1,
213+
"should": Array [
214+
Object {
215+
"match_phrase": Object {
216+
"monitor.id": "apm-dev",
217+
},
218+
},
219+
],
216220
},
217221
},
218-
],
219-
},
220-
},
221-
Object {
222-
"bool": Object {
223-
"minimum_should_match": 1,
224-
"should": Array [
225222
Object {
226-
"match_phrase": Object {
227-
"monitor.id": "auto-http-0X8D6082B94BBE3B8A",
223+
"bool": Object {
224+
"minimum_should_match": 1,
225+
"should": Array [
226+
Object {
227+
"match_phrase": Object {
228+
"monitor.id": "auto-http-0X8D6082B94BBE3B8A",
229+
},
230+
},
231+
],
228232
},
229233
},
230234
],
@@ -797,6 +801,133 @@ describe('monitor availability', () => {
797801
]
798802
`);
799803
});
804+
805+
it('does not overwrite filters', async () => {
806+
const [callES, esMock] = setupMockEsCompositeQuery<
807+
AvailabilityKey,
808+
GetMonitorAvailabilityResult,
809+
AvailabilityDoc
810+
>(
811+
[
812+
{
813+
bucketCriteria: [],
814+
},
815+
],
816+
genBucketItem
817+
);
818+
await getMonitorAvailability({
819+
callES,
820+
dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS,
821+
range: 3,
822+
rangeUnit: 's',
823+
threshold: '99',
824+
filters: JSON.stringify({ bool: { filter: [{ term: { 'monitor.id': 'foo' } }] } }),
825+
});
826+
const [, params] = esMock.callAsCurrentUser.mock.calls[0];
827+
expect(params).toMatchInlineSnapshot(`
828+
Object {
829+
"body": Object {
830+
"aggs": Object {
831+
"monitors": Object {
832+
"aggs": Object {
833+
"down_sum": Object {
834+
"sum": Object {
835+
"field": "summary.down",
836+
"missing": 0,
837+
},
838+
},
839+
"fields": Object {
840+
"top_hits": Object {
841+
"size": 1,
842+
"sort": Array [
843+
Object {
844+
"@timestamp": Object {
845+
"order": "desc",
846+
},
847+
},
848+
],
849+
},
850+
},
851+
"filtered": Object {
852+
"bucket_selector": Object {
853+
"buckets_path": Object {
854+
"threshold": "ratio.value",
855+
},
856+
"script": "params.threshold < 0.99",
857+
},
858+
},
859+
"ratio": Object {
860+
"bucket_script": Object {
861+
"buckets_path": Object {
862+
"downTotal": "down_sum",
863+
"upTotal": "up_sum",
864+
},
865+
"script": "
866+
if (params.upTotal + params.downTotal > 0) {
867+
return params.upTotal / (params.upTotal + params.downTotal);
868+
} return null;",
869+
},
870+
},
871+
"up_sum": Object {
872+
"sum": Object {
873+
"field": "summary.up",
874+
"missing": 0,
875+
},
876+
},
877+
},
878+
"composite": Object {
879+
"size": 2000,
880+
"sources": Array [
881+
Object {
882+
"monitorId": Object {
883+
"terms": Object {
884+
"field": "monitor.id",
885+
},
886+
},
887+
},
888+
Object {
889+
"location": Object {
890+
"terms": Object {
891+
"field": "observer.geo.name",
892+
"missing_bucket": true,
893+
},
894+
},
895+
},
896+
],
897+
},
898+
},
899+
},
900+
"query": Object {
901+
"bool": Object {
902+
"filter": Array [
903+
Object {
904+
"range": Object {
905+
"@timestamp": Object {
906+
"gte": "now-3s",
907+
"lte": "now",
908+
},
909+
},
910+
},
911+
Object {
912+
"bool": Object {
913+
"filter": Array [
914+
Object {
915+
"term": Object {
916+
"monitor.id": "foo",
917+
},
918+
},
919+
],
920+
},
921+
},
922+
],
923+
},
924+
},
925+
"size": 0,
926+
},
927+
"index": "heartbeat-8*",
928+
}
929+
`);
930+
});
800931
});
801932

802933
describe('formatBuckets', () => {

0 commit comments

Comments
 (0)