diff --git a/src/checks/graphiteThreshold.check.js b/src/checks/graphiteThreshold.check.js index 5382898c..23d4966e 100644 --- a/src/checks/graphiteThreshold.check.js +++ b/src/checks/graphiteThreshold.check.js @@ -44,8 +44,19 @@ class GraphiteThresholdCheck extends Check { const results = await fetch(this.sampleUrl, { headers: { key: this.ftGraphiteKey } }).then(fetchres.json); - + const simplifiedResults = results.map(result => { + + if(result.target && result.target.includes('summarize(sumSeries')){ + const fetchCountPerTimeUnit = result.datapoints.map(item => Number(item[0])); + const sumUp = (previousValue, currentValue) => previousValue + currentValue; + const talliedUp = fetchCountPerTimeUnit.reduce(sumUp); + const isFailing = this.direction === 'above' ? + Number(talliedUp) > this.threshold : + Number(talliedUp) < this.threshold; + return { target: result.target, isFailing }; + } + const isFailing = result.datapoints.some(value => { if (value[0] === null) { // metric data is unavailable, we don't fail this threshold check if metric data is unavailable diff --git a/test/graphiteThreshold.check.spec.js b/test/graphiteThreshold.check.spec.js index 0fc656d0..4e2855ef 100644 --- a/test/graphiteThreshold.check.spec.js +++ b/test/graphiteThreshold.check.spec.js @@ -34,7 +34,7 @@ function mockGraphite (results) { }); } -describe('Graphite Threshold Check', function(){ +describe.only('Graphite Threshold Check', function(){ let check; @@ -169,6 +169,87 @@ describe('Graphite Threshold Check', function(){ }); }); + }); + + context('It handles summarize(sumSeries)', function () { + + it('Should be healthy if sum above lower threshold', function (done) { + mockGraphite([ + { + datapoints: [ + [ null, 1635125640],[null,1635129240],[1,1635132840],[1,1635136440],[null,1635140040],[3,1635143640]], + target: 'summarize(sumSeries)' + } + ]); + check = new Check(getCheckConfig({ + threshold: 1, + direction: 'below' + })); + check.start(); + setTimeout(() => { + expect(check.getStatus().ok).to.be.true; + done(); + }); + }); + + it('Should be healthy if sum below upper threshold', function (done) { + mockGraphite([ + { datapoints: [ + [ null, 1635125640],[null,1635129240],[1,1635132840],[1,1635136440],[null,1635140040],[3,1635143640]], + target: 'summarize(sumSeries)' + } + ]); + check = new Check(getCheckConfig({ + threshold: 8 + })); + check.start(); + setTimeout(() => { + expect(check.getStatus().ok).to.be.true; + done(); + }); + }); + + it('Should be unhealthy if sum above lower threshold', function (done) { + mockGraphite([ + { datapoints: [ + [ null, 1635125640],[null,1635129240],[1,1635132840],[1,1635136440],[null,1635140040],[3,1635143640]], + target: 'summarize(sumSeries)' + + } + ]); + check = new Check(getCheckConfig({ + threshold: 6, + direction: 'below' + })); + check.start(); + setTimeout(() => { + expect(check.getStatus().ok).to.be.false; + done(); + }); + }); + + it('Should be unhealthy if sum below upper threshold', function (done) { + mockGraphite([ + { datapoints: [ + [ null, 1635125640],[null,1635129240],[1,1635132840],[1,1635136440],[null,1635140040],[3,1635143640]], + target: 'summarize(sumSeries)' + + }, + + ]); + check = new Check(getCheckConfig({ + threshold: 4 + })); + check.start(); + setTimeout(() => { + expect(check.getStatus().ok).to.be.false; + done(); + }); + }); + + + + }); it('Should be possible to configure sample period', function(done){