Skip to content

Commit

Permalink
Merge pull request #49 from jasonray/next
Browse files Browse the repository at this point in the history
merge 1.4 to master
  • Loading branch information
jasonray authored Apr 6, 2018
2 parents 4320237 + 0e63495 commit b4d8369
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 108 deletions.
15 changes: 8 additions & 7 deletions lib/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('underscore');
const isEmpty = require('lodash.isempty');
const isFunction = require('lodash.isfunction');

//global collection of metrics. Having this global makes it easy for other parts of application to access
var _metrics;
Expand All @@ -9,13 +10,13 @@ function reset() {
}

function register(key, metric) {
if (_.isEmpty(metric)) {
if (isEmpty(metric)) {
metric = key;
key = null;
}

key = determineKey(key, metric);
if (_.isEmpty(key)) {
if (isEmpty(key)) {
throw Error('Cannot register a metric without a key. Key can be specified as a parameter, or supplied in the metric as name, name(), or getName()');
}

Expand All @@ -24,11 +25,11 @@ function register(key, metric) {


function determineKey(key, metric) {
if (_.isEmpty(key)) {
if (_.isFunction(metric.name)) {
if (isEmpty(key)) {
if (isFunction(metric.name)) {
key = metric.name();
}
else if (_.isFunction(metric.getName)) {
else if (isFunction(metric.getName)) {
key = metric.getName();
}
else if (metric.name) {
Expand All @@ -45,7 +46,7 @@ function findOrCreate(metricname, constructor) {
existingMetric = get(metricname);
}

if (_.isEmpty(existingMetric)) {
if (isEmpty(existingMetric)) {
var newMetric = new constructor(metricname);
return register(newMetric);
} else {
Expand Down
10 changes: 4 additions & 6 deletions lib/statman.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var Stopwatch = module.exports.Stopwatch = require('statman-stopwatch');
var Meter = module.exports.Meter = require('statman-meter');
var Gauge = module.exports.Gauge = require('statman-gauge');
const Stopwatch = module.exports.Stopwatch = require('statman-stopwatch');
const Meter = module.exports.Meter = require('statman-meter');
const Gauge = module.exports.Gauge = require('statman-gauge');

var _ = require('underscore');

var _registry = require('./registry');
const _registry = require('./registry');

// var httpFilters = require('./httpFilters');
// exports.httpFilters = httpFilters;
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "statman",
"version": "1.3.0",
"version": "1.4.0",
"description": "Package to assist with collection of metrics, including stopwatch functionality",
"main": "./lib/statman.js",
"scripts": {
Expand All @@ -16,10 +16,12 @@
"url": "https://github.com/jasonray/statman/issues"
},
"dependencies": {
"lodash.isempty": "^4.4.0",
"lodash.isfunction": "^3.0.9",
"lodash.isnull": "^3.0.0",
"statman-gauge": "^1.0.5",
"statman-meter": "^1.0.2",
"statman-stopwatch": "^2.4.0",
"underscore": "^1.8.3"
"statman-stopwatch": "^2.7.0"
},
"devDependencies": {
"should": "^11.1.2",
Expand Down
22 changes: 11 additions & 11 deletions test/gauge-test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
/*jslint node: true */
"use strict";

var mocha = require('mocha');
var assert = require('assert');
var should = require('should');
var statman = require('../lib/statman');
const mocha = require('mocha');
const assert = require('assert');
const should = require('should');
const statman = require('../lib/statman');

describe('gauge (smoke test)', function () {
it('gauagename', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
assert.equal('metric-name', gauge.name());
});

it('increment', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.increment();
assert.equal(1, gauge.value());
});

it('incrementByValue', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.set(10);
gauge.increment(2);
assert.equal(12, gauge.value());
});

it('decrement', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.set(10);
gauge.decrement();
assert.equal(9, gauge.value());
});

it('decrementByValue', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.set(10);
gauge.decrement(2);
assert.equal(8, gauge.value());
});

it('set', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.set(5);
assert.equal(5, gauge.value());
});

it('toString produces something with the value', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
gauge.set(5);
gauge.toString().should.containEql(5);
});
Expand Down
20 changes: 8 additions & 12 deletions test/meter-test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
/*jslint node: true */
"use strict";

var statman = require('../lib/statman');
var should = require('should');
const statman = require('../lib/statman');
const should = require('should');

describe('meter (smoke test)', function () {
this.timeout(5000);

it('init should return an instance of meter', function () {
var meter;
meter = new statman.Meter();
const meter = new statman.Meter();
should.exist(meter);
});

it('record once and get count should return 1', function () {
var meter;
meter = new statman.Meter();
const meter = new statman.Meter();
meter.record();
meter.getCount().should.be.equal(1);
});

it('record and read', function () {
var meter;
meter = new statman.Meter();
const meter = new statman.Meter();
meter.record(1000);
meter.record(2000);
meter.getAverage().should.equal(1500);
meter.getCount().should.equal(2);
});

it('meter.start() provides an event, which can be used to auto record meter', function (done) {
var meter = new statman.Meter();
const meter = new statman.Meter();

var meterEvent = meter.startEvent();
const meterEvent = meter.startEvent();
setTimeout(function () {
meterEvent.stop();

Expand All @@ -44,8 +41,7 @@ describe('meter (smoke test)', function () {
});

it('toString()', function () {
var meter;
meter = new statman.Meter();
const meter = new statman.Meter();
meter.record(2.2);
meter.record(4.4);
meter.toString().should.equal('[count:2; average:3.30]');
Expand Down
62 changes: 31 additions & 31 deletions test/statman-test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var statman = require('../lib/statman');
var assert = require('assert');
var should = require('should');
const statman = require('../lib/statman');
const assert = require('assert');
const should = require('should');

describe('registry', function () {
beforeEach(function () {
statman.reset();
});

it('registry() => returns full list of metrics', function () {
var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
statman.registry().length.should.equal(3);
});

it('registry(name) => returns single item', function () {
var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
Expand All @@ -30,11 +30,11 @@ describe('registry', function () {
});

it('registry can have key passed explicitly', function () {
var metric = {value: 'abc'};
const metric = {value: 'abc'};

var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
Expand All @@ -44,11 +44,11 @@ describe('registry', function () {
});

it('registry can have key discovered from name', function () {
var metric = {name: 'x'};
const metric = {name: 'x'};

var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
Expand All @@ -58,15 +58,15 @@ describe('registry', function () {
});

it('registry can have key discovered from name()', function () {
var metric = {
const metric = {
name: function () {
return 'x'
}
};

var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
Expand All @@ -76,15 +76,15 @@ describe('registry', function () {
});

it('registry can have key discovered from getName()', function () {
var metric = {
const metric = {
getName: function () {
return 'x'
}
};

var metricA = {name: 'metricA'};
var metricB = {name: 'metricB'};
var metricC = {name: 'metric-C'};
const metricA = {name: 'metricA'};
const metricB = {name: 'metricB'};
const metricC = {name: 'metric-C'};
statman.register(metricA);
statman.register(metricB);
statman.register(metricC);
Expand All @@ -94,12 +94,12 @@ describe('registry', function () {
});

it('registering metric with same name overwrites the first', function () {
var metricA = {
const metricA = {
name: function () {
return 'metric-a'
}, value: 1
};
var metricB = {
const metricB = {
name: function () {
return 'metric-a'
}, value: 2
Expand All @@ -126,12 +126,12 @@ describe('registry', function () {

describe('gauge in registry', function () {
it('get new instance of a gauge', function () {
var gauge = statman.gauge('metric-name');
const gauge = statman.gauge('metric-name');
gauge.name().should.equal('metric-name');
});

it('explicit register gauge', function () {
var gauge = new statman.Gauge('metric-name');
const gauge = new statman.Gauge('metric-name');
should.not.exist(statman.registry('metric-name'));
statman.register(gauge);
should.exist(statman.registry('metric-name'));
Expand All @@ -155,12 +155,12 @@ describe('registry', function () {

describe('meter in registry', function () {
it('get new instance of a meter', function () {
var meter = statman.meter('metric-name');
const meter = statman.meter('metric-name');
meter.name().should.equal('metric-name');
});

it('explicit register meter', function () {
var meter = new statman.Meter('metric-name');
const meter = new statman.Meter('metric-name');
should.not.exist(statman.registry('metric-name'));
statman.register(meter);
should.exist(statman.registry('metric-name'));
Expand Down
Loading

0 comments on commit b4d8369

Please sign in to comment.