From 4dbd257cdf6457902e310cbc386be6152e1f29fc Mon Sep 17 00:00:00 2001 From: AniaMakes Date: Tue, 26 Oct 2021 10:15:58 +0100 Subject: [PATCH] added handling of summarize(sumSeries) healthchecks --- src/checks/graphiteThreshold.check.js | 13 ++++- test/graphiteThreshold.check.spec.js | 81 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/checks/graphiteThreshold.check.js b/src/checks/graphiteThreshold.check.js index 5382898..23d4966 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 0fc656d..dd5830c 100644 --- a/test/graphiteThreshold.check.spec.js +++ b/test/graphiteThreshold.check.spec.js @@ -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){