From c524a51d0c8ddeea588b8a3ed89ab27f6b5cf707 Mon Sep 17 00:00:00 2001 From: Alexandre Strzelewicz Date: Tue, 30 Jan 2018 18:21:25 +0100 Subject: [PATCH] #109 done - #107 partial (metric only) --- lib/Probe.js | 59 +++++++++++-------- .../{ => probe.mocha}/histogram.fixture.js | 3 +- test/fixtures/probe.mocha/metric.fixture.js | 26 ++++++++ .../probe.mocha/never_called.fixture.js | 22 +++++++ .../{ => probe.mocha}/probe.fixture.js | 2 +- test/probe.mocha.js | 50 ++++++++++++++-- 6 files changed, 133 insertions(+), 29 deletions(-) rename test/fixtures/{ => probe.mocha}/histogram.fixture.js (92%) create mode 100644 test/fixtures/probe.mocha/metric.fixture.js create mode 100644 test/fixtures/probe.mocha/never_called.fixture.js rename test/fixtures/{ => probe.mocha}/probe.fixture.js (98%) diff --git a/lib/Probe.js b/lib/Probe.js index 07711b5..fa7effa 100644 --- a/lib/Probe.js +++ b/lib/Probe.js @@ -28,7 +28,8 @@ Probe.AVAILABLE_MEASUREMENTS = [ 'p99', 'p999' ]; -Probe.default_aggregation = 'avg'; +Probe.default_aggregation = 'avg'; +Probe.default_type = 'user'; function getValue(value) { if (typeof(value) == 'function') @@ -44,6 +45,9 @@ function cookData(data) { Object.keys(data).forEach(function(probe_name) { + if (typeof(data[probe_name].value) == 'undefined') + return false; + cooked_data[probe_name] = { value: getValue(data[probe_name].value) }; @@ -55,6 +59,9 @@ function cookData(data) { data[probe_name].agg_type != 'none') cooked_data[probe_name].agg_type = data[probe_name].agg_type; + cooked_data[probe_name].historic = data[probe_name].historic; + cooked_data[probe_name].type = data[probe_name].type; + /** * Attach Alert configuration */ @@ -78,6 +85,14 @@ function checkIssues(data) { }); }; +function historicEnabled(historic) { + if (historic === false) + return false; + if (typeof(historic) === 'undefined') + return true; + return true; +} + function attachAlert(opts, conf) { /** * pm2 set module-name:probes:probe_name:value 20 @@ -154,16 +169,14 @@ Probe.probe = function() { }; }, metric : function(opts) { - var agg_type = opts.agg_type || Probe.default_aggregation; - if (!opts.name) return console.error('[Probe][Metric] Name not defined'); - if (Probe.AVAILABLE_AGG_TYPES.indexOf(agg_type) == -1) - return console.error("[Probe][Metric] Unknown agg_type: %s", agg_type); Probe._var[opts.name] = { - value : opts.value || 0, - agg_type: agg_type + value : opts.value, + type : opts.type || opts.name, + historic: historicEnabled(opts.historic), + agg_type: opts.agg_type || Probe.default_aggregation }; /** @@ -189,20 +202,22 @@ Probe.probe = function() { histogram : function(opts) { if (!opts.name) return console.error('[Probe][Histogram] Name not defined'); + opts.measurement = opts.measurement || 'mean'; opts.unit = opts.unit || ''; - var agg_type = opts.agg_type || Probe.default_aggregation; if (Probe.AVAILABLE_MEASUREMENTS.indexOf(opts.measurement) == -1) return console.error('[Probe][Histogram] Measure type %s does not exists', opts.measurement); - if (Probe.AVAILABLE_AGG_TYPES.indexOf(agg_type) == -1) - return console.error("[Probe][Metric] Unknown agg_type: %s", agg_type); var histogram = new Histogram(opts); Probe._var[opts.name] = { - value: function() { return (Math.round(histogram.val() * 100) / 100) + '' + opts.unit }, - agg_type: agg_type + value: function() { + return (Math.round(histogram.val() * 100) / 100) + '' + opts.unit + }, + type : opts.type || opts.name, + historic: historicEnabled(opts.historic), + agg_type: opts.agg_type || Probe.default_aggregation }; /** @@ -214,20 +229,20 @@ Probe.probe = function() { return histogram; }, meter : function(opts) { - var agg_type = opts.agg_type || Probe.default_aggregation; - if (!opts.name) return console.error('[Probe][Meter] Name not defined'); - if (Probe.AVAILABLE_AGG_TYPES.indexOf(agg_type) == -1) - return console.error("[Probe][Metric] Unknown agg_type: %s", agg_type); opts.unit = opts.unit || ''; var meter = new Meter(opts); Probe._var[opts.name] = { - value: function() { return meter.val() + '' + opts.unit }, - agg_type: agg_type + value: function() { + return meter.val() + '' + opts.unit + }, + type : opts.type || opts.name, + historic: historicEnabled(opts.historic), + agg_type: opts.agg_type || Probe.default_aggregation }; /** @@ -239,18 +254,16 @@ Probe.probe = function() { return meter; }, counter : function(opts) { - var agg_type = opts.agg_type || Probe.default_aggregation; - if (!opts.name) return console.error('[Probe][Counter] Name not defined'); - if (Probe.AVAILABLE_AGG_TYPES.indexOf(agg_type) == -1) - return console.error("[Probe][Metric] Unknown agg_type: %s", agg_type); var counter = new Counter(); Probe._var[opts.name] = { value: function() { return counter.val() }, - agg_type: agg_type + type : opts.type || opts.name, + historic: historicEnabled(opts.historic), + agg_type: opts.agg_type || Probe.default_aggregation }; /** diff --git a/test/fixtures/histogram.fixture.js b/test/fixtures/probe.mocha/histogram.fixture.js similarity index 92% rename from test/fixtures/histogram.fixture.js rename to test/fixtures/probe.mocha/histogram.fixture.js index f8a2776..73cea59 100644 --- a/test/fixtures/histogram.fixture.js +++ b/test/fixtures/probe.mocha/histogram.fixture.js @@ -1,6 +1,6 @@ -var axm = require('../..'); +var axm = require('../../..'); var probe = axm.probe(); @@ -33,6 +33,7 @@ setInterval(function() { var h3 = probe.histogram({ name : 'min', + historic : true, measurement : 'min', agg_type: 'min' }); diff --git a/test/fixtures/probe.mocha/metric.fixture.js b/test/fixtures/probe.mocha/metric.fixture.js new file mode 100644 index 0000000..8cee4b9 --- /dev/null +++ b/test/fixtures/probe.mocha/metric.fixture.js @@ -0,0 +1,26 @@ + + +var axm = require('../../..'); + +var probe = axm.probe(); + +var users = { + 'alex' : 'ok', + 'musta' : 'fa' +}; + +/** + * Monitor synchronous return of functions + */ +var rt_users = probe.metric({ + name : 'Realtime user', + historic : false, + type : 'v8/smthing', + value : function() { + return Object.keys(users).length; + } +}); + +setInterval(function() { + // keep event loop active +}, 1000); diff --git a/test/fixtures/probe.mocha/never_called.fixture.js b/test/fixtures/probe.mocha/never_called.fixture.js new file mode 100644 index 0000000..f2bf63b --- /dev/null +++ b/test/fixtures/probe.mocha/never_called.fixture.js @@ -0,0 +1,22 @@ +var pmx = require('../../..'); +var probe = pmx.probe(); + +probe.histogram({ + name : 'test' +}); + +probe.metric({ + name : 'metric-test' +}); + +probe.counter({ + name : 'counter-test' +}); + +probe.meter({ + name : 'meter-test' +}); + +setInterval(function() { + // keep event loop active +}, 1000); diff --git a/test/fixtures/probe.fixture.js b/test/fixtures/probe.mocha/probe.fixture.js similarity index 98% rename from test/fixtures/probe.fixture.js rename to test/fixtures/probe.mocha/probe.fixture.js index 89e30c2..05d5e13 100644 --- a/test/fixtures/probe.fixture.js +++ b/test/fixtures/probe.mocha/probe.fixture.js @@ -1,5 +1,5 @@ -var axm = require('../..'); +var axm = require('../../..'); var probe = axm.probe(); diff --git a/test/probe.mocha.js b/test/probe.mocha.js index 1b70545..9c47b7b 100644 --- a/test/probe.mocha.js +++ b/test/probe.mocha.js @@ -1,17 +1,27 @@ var axm = require('..'); +var should = require('should'); function fork(script) { - var app = require('child_process').fork(__dirname + (script || '/fixtures/probe.fixture.js'), []); + var app = require('child_process').fork(__dirname + (script || '/fixtures/probe.mocha/probe.fixture.js'), []); return app; } function forkHistogram() { - var app = require('child_process').fork(__dirname + '/fixtures/histogram.fixture.js', []); + var app = require('child_process').fork(__dirname + '/fixtures/probe.mocha/histogram.fixture.js', []); return app; } + +function forkMetric() { + return require('child_process').fork(__dirname + '/fixtures/probe.mocha/metric.fixture.js', []); +} + +function forkNonUserProbes() { + return require('child_process').fork(__dirname + '/fixtures/probe.mocha/never_called.fixture.js', []); +} describe('Probe', function() { + it('should have the right properties', function(done) { axm.should.have.property('probe'); @@ -24,6 +34,31 @@ describe('Probe', function() { done(); }); + it('should instanciate metric sample app', function(done) { + var app = forkMetric(); + + app.on('message', function(pck) { + // Will iterate two times, metric change the value to false + should(pck.data['Realtime user'].value).eql(2); + should(pck.data['Realtime user'].historic).eql(false); + should(pck.data['Realtime user'].type).eql('v8/smthing'); + app.kill() + done() + }) + }); + + // Metric is the only value that can be detected + // as a non used metrics + it.skip('should not retrieve non used metrics', function(done) { + var app = forkNonUserProbes() + + app.on('message', function(pck) { + console.log(pck) + //app.kill() + }) + }); + + it('should fork app and receive data from probes', function(done) { var app = fork(); @@ -36,6 +71,14 @@ describe('Probe', function() { 'random', 'Cheerio'); + should(pck.data['Realtime user'].historic).eql(true); + should(pck.data['random'].historic).eql(true); + should(pck.data['Cheerio'].historic).eql(true); + should(pck.data['req/min'].historic).eql(true); + should(pck.data['Realtime user'].type).eql('Realtime user'); + should(pck.data['random'].type).eql('random'); + should(pck.data['req/min'].type).eql('req/min'); + if (pck.data.random.value && pck.data.random.agg_type == 'sum' && pck.data.Cheerio.value.yes == true && pck.data.Cheerio.agg_type == 'avg' && pck.data.Downloads.value > 1 && pck.data.Downloads.agg_type == 'max') { @@ -55,7 +98,6 @@ describe('Probe', function() { pck.data.should.have.properties('style_2_docker_config', 'style_1_docker_config'); - if (pck.data.style_1_docker_config.value.val == 'new value' && pck.data.style_2_docker_config.value.val == 'new value') { app.kill(); @@ -68,7 +110,7 @@ describe('Probe', function() { var app = forkHistogram(); app.on('message', function(pck) { - pck.type.should.eql('axm:monitor'); + should(pck.data['min'].historic).eql(true); if (pck.data.mean && pck.data.mean.agg_type == 'avg' && pck.data.min && pck.data.min.agg_type == 'min' &&