From 8b72f3515efdf9a47b3957e4273d8d8c266f8dff Mon Sep 17 00:00:00 2001 From: Srini Maddineni Date: Sat, 11 Mar 2023 13:44:51 -0500 Subject: [PATCH] let user function invoke before event skip due to not matching content filters --- src/filters/index.js | 8 +++++++- test/unit/filters/index.test.js | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/filters/index.js b/src/filters/index.js index 9d6b05cd..8f3bea89 100644 --- a/src/filters/index.js +++ b/src/filters/index.js @@ -22,7 +22,13 @@ export const prefilterOnEventTypes = (rules) => export const filterOnContent = (rule, uow) => { /* istanbul ignore else */ if (rule.filters) { - return rule.filters.reduce((a, c) => a && c(uow, rule), true); + const isSuccess = rule.filters.reduce((a, c) => a && c(uow, rule), true); + // Invoke custom function incase of filter conditions are not statisfied + // For example to create cloudwatch metric in case of failure + if (!isSuccess && rule.errorFilter) { + rule.errorFilter(uow); + } + return isSuccess; } else { return true; } diff --git a/test/unit/filters/index.test.js b/test/unit/filters/index.test.js index 2dd0eb40..cc93ce06 100644 --- a/test/unit/filters/index.test.js +++ b/test/unit/filters/index.test.js @@ -64,16 +64,20 @@ describe('filters/index.js', () => { }); it('should filter on compound content', () => { + const errorFilter = (uow) => { uow.isSkipped = true; }; const rule = { filters: [ (uow, r) => uow.event.tags.f1 === r.value, (uow, r) => uow.event.tags.f2 === r.value, ], + errorFilter, value: 'v1', }; expect(filterOnContent(rule, { event: { tags: { f1: 'v1', f2: 'v1' } } })).to.be.true; - expect(filterOnContent(rule, { event: { tags: { f1: 'v2', f2: 'v1' } } })).to.be.false; + const event = { event: { tags: { f1: 'v2', f2: 'v1' } } }; + expect(filterOnContent(rule, event)).to.be.false; + expect(event.isSkipped).to.be.true; expect(filterOnContent(rule, { event: { tags: { f1: 'v1', f2: 'v2' } } })).to.be.false; }); });