Skip to content

Commit

Permalink
#109 done - #107 partial (metric only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Jan 30, 2018
1 parent 215bb94 commit c524a51
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 29 deletions.
59 changes: 36 additions & 23 deletions lib/Probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
};
Expand All @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
};

/**
Expand All @@ -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
};

/**
Expand All @@ -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
};

/**
Expand All @@ -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
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


var axm = require('../..');
var axm = require('../../..');

var probe = axm.probe();

Expand Down Expand Up @@ -33,6 +33,7 @@ setInterval(function() {

var h3 = probe.histogram({
name : 'min',
historic : true,
measurement : 'min',
agg_type: 'min'
});
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/probe.mocha/metric.fixture.js
Original file line number Diff line number Diff line change
@@ -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);
22 changes: 22 additions & 0 deletions test/fixtures/probe.mocha/never_called.fixture.js
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var axm = require('../..');
var axm = require('../../..');

var probe = axm.probe();

Expand Down
50 changes: 46 additions & 4 deletions test/probe.mocha.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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();

Expand All @@ -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') {
Expand All @@ -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();
Expand All @@ -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' &&
Expand Down

0 comments on commit c524a51

Please sign in to comment.