spm-metrics-js
Advanced tools
Comparing version 1.0.4 to 1.0.5
var Measured = require('measured') | ||
var util = require ('util') | ||
var delegate = require ('delegate') | ||
var extend = require ('extend') | ||
var delegate = require('delegate') | ||
var extend = require('extend') | ||
@@ -18,4 +17,4 @@ /** | ||
this.stats = new Measured.Gauge(function () { return this.value}) | ||
if (! this.options.aggregationMap) { | ||
this.options.aggregationMap = {default: 'avg', count: 'sum', mean: 'avg', median: 'avg', currentRate: 'avg', '1MinuteRate': 'avg'} | ||
if (!this.options.aggregationMap) { | ||
this.options.aggregationMap = {default: 'avg', count: 'sum', mean: 'avg', median: 'avg', currentRate: 'avg', '1MinuteRate': 'avg'} | ||
} | ||
@@ -26,6 +25,8 @@ delegate(this, this.measured) | ||
} | ||
if (this.options.interval>0) | ||
{ | ||
this.tid = setInterval (this.save, this.options.interval) | ||
this.tid.unref() | ||
var self = this | ||
if (this.options.interval > 0) { | ||
this.tid = setInterval(function () { | ||
self.save() | ||
}, Math.max (10000, this.options.interval)) | ||
this.tid.unref() | ||
} | ||
@@ -41,9 +42,7 @@ } | ||
var metricName = this.options.name | ||
if (property) | ||
{ | ||
if (property) { | ||
metricName = metricName + '.' + property | ||
} | ||
console.log (property) | ||
this.spm.add(metricName, | ||
value, this.options.aggregationMap[property||'default'] || 'avg', | ||
value, this.options.aggregationMap[property || 'default'] || 'avg', | ||
this.options.filter1 || 'filter1', | ||
@@ -55,28 +54,23 @@ this.options.filter2 || 'filter2') | ||
var m = 0 | ||
if (this.stats.toJSON) | ||
m = this.stats.toJSON() | ||
else | ||
console.error ('Object has no method toJSON()') | ||
if (this.stats && this.stats.toJSON) { | ||
m = this.stats.toJSON | ||
} else { | ||
throw new Error('Object has no method toJSON()') | ||
} | ||
var values = {} | ||
if (! (m instanceof Number)) | ||
{ | ||
if (m.meter && m.histogram) | ||
{ | ||
if (!(m instanceof Number)) { | ||
if (m.hasOwnProperty('meter') && m.hasOwnProperty('histogram')) { | ||
values = extend(values, m.histogram) | ||
values = extend(values, m.meter) | ||
for (var key in values) | ||
{ | ||
if (this.options.valueFilter instanceof Array) | ||
{ | ||
if (this.options.valueFilter.indexOf(key) >= 0) | ||
this.set (values[key], key) | ||
} else { | ||
this.set (values[key], key) | ||
} | ||
} | ||
} else { | ||
values = m | ||
} | ||
if (this.stats.reset) { | ||
this.stats.reset() | ||
for (var key in values) { | ||
if (this.options.valueFilter instanceof Array) { | ||
if (this.options.valueFilter.indexOf(key) >= 0) { | ||
this.set(values[key], key) | ||
} | ||
} else { | ||
this.set(values[key], key) | ||
} | ||
} | ||
@@ -86,11 +80,15 @@ } else { | ||
} | ||
if (this.stats.reset) { | ||
this.stats.reset() | ||
} | ||
return values | ||
} | ||
CustomMetric.prototype.use = function (measuredType, initValues) { | ||
CustomMetric.prototype.use = function (measuredType, initValues) { | ||
this.usedMeasure = measuredType | ||
this.stats = measuredType.call (this.collection, this.options.name, initValues) | ||
this.stats = measuredType.call(this.collection, this.options.name, initValues) | ||
delegate(this, this.stats) | ||
if (this.stats.unref) | ||
if (this.stats.unref) { | ||
this.stats.unref() | ||
} | ||
return this.stats | ||
@@ -115,8 +113,8 @@ } | ||
CustomMetric.prototype.cancelInterval = function () { | ||
if (this.tid) { | ||
cancelInterval(tid) | ||
} | ||
CustomMetric.prototype.clearInterval = function () { | ||
if (this.tid) { | ||
clearInterval(this.tid) | ||
} | ||
} | ||
module.exports = CustomMetric |
@@ -24,8 +24,8 @@ 'use strict' | ||
var self = this | ||
if (metricsInterval > 0) | ||
if (metricsInterval > 0) { | ||
setInterval(function () { | ||
self.send() | ||
}, metricsInterval) | ||
}, Math.min(metricsInterval, 30000)) | ||
} | ||
} | ||
util.inherits(SpmCustomMetrics, events.EventEmitter) | ||
@@ -55,4 +55,5 @@ | ||
this.emit('add', dp) | ||
if (this.datapoints.length === MAX_DP) | ||
if (this.datapoints.length === MAX_DP) { | ||
this.send() | ||
} | ||
} | ||
@@ -71,3 +72,3 @@ | ||
'Origin': 'https://spm-receiver.sematext.com/' | ||
// 'Keep-Alive': false | ||
// 'Keep-Alive': false | ||
}, | ||
@@ -80,6 +81,7 @@ body: JSON.stringify({ datapoints: this.datapoints }), | ||
request.post(options, function (err, res) { | ||
if (err) | ||
if (err) { | ||
self.emit('error', { source: 'send', err: err }) | ||
else | ||
} else { | ||
self.emit('send metrics', { source: 'send', err: err }) | ||
} | ||
}) | ||
@@ -103,3 +105,3 @@ } | ||
'Content-Type': 'application/json' | ||
// 'Keep-Alive': false | ||
// 'Keep-Alive': false | ||
}, | ||
@@ -112,8 +114,10 @@ body: JSON.stringify(event), | ||
request.post(options, function (err, res) { | ||
if (err) | ||
if (err) { | ||
self.emit('error', { source: 'sendEvent', err: err }) | ||
else | ||
} else { | ||
self.emit('send event', { source: 'sendEvent', event: event, err: err }) | ||
if (callback) | ||
} | ||
if (callback) { | ||
callback(err, res) | ||
} | ||
}) | ||
@@ -120,0 +124,0 @@ } |
{ | ||
"name": "spm-metrics-js", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Node.js client for SPM - Custom Metrics and Event API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
var SPM = require('../lib/index.js') | ||
var token = process.env.SPM_TOKEN | ||
var util = require ('util') | ||
describe('spm custom metrics ', function() { | ||
it('should pass', function(done) { | ||
describe('spm custom metrics ', function () { | ||
it('set metric', function (done) { | ||
try { | ||
@@ -23,3 +22,2 @@ var spmcm = new SPM(token, 0) | ||
}) | ||
it('log events', function (done) { | ||
@@ -35,5 +33,8 @@ this.timeout(10000) | ||
data: 'String or Base64 coded content' }) | ||
eventLogger.log('user1 logged in', function(err, res) { | ||
//console.log(res.body) | ||
done() | ||
eventLogger.log('user1 logged in', function (err, res) { | ||
if (!err) { | ||
done() | ||
} else { | ||
done() | ||
} | ||
}) | ||
@@ -44,3 +45,25 @@ } catch (err) { | ||
}) | ||
it('timer', function (done) { | ||
this.timeout(30000) | ||
try { | ||
var spmcm = new SPM(token, 0) | ||
var value = {} | ||
spmcm.once('send metrics', function (event) { | ||
if (value.min > 200) { | ||
done() | ||
} else { | ||
done(new Error('value : ' + value.min)) | ||
} | ||
}) | ||
var testMetric = spmcm.getCustomMetric({ name: 'timer', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2' }) | ||
var stopwatch = testMetric.timer().start() | ||
setTimeout(function () { | ||
stopwatch.end() | ||
value = testMetric.save() | ||
spmcm.send() | ||
}, 200) | ||
} catch (err) { | ||
done(err) | ||
} | ||
}) | ||
it('meter', function (done) { | ||
@@ -52,8 +75,11 @@ this.timeout(30000) | ||
spmcm.once('send metrics', function (event) { | ||
if (value.count>0) | ||
done() | ||
if (value.count > 0) { | ||
done() | ||
} | ||
}) | ||
var testMetric = spmcm.getCustomMetric({ name: 'test.meter', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2', valueFilter: ['count','mean'] }) | ||
var testMetric = spmcm.getCustomMetric({ name: 'meter', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2' }) | ||
testMetric.meter() | ||
testMetric.mark() | ||
for (var i = 0; i < Math.round(Math.random()*100+1); i++) { | ||
testMetric.mark() | ||
} | ||
value = testMetric.save() | ||
@@ -65,3 +91,2 @@ spmcm.send() | ||
}) | ||
it('counter', function (done) { | ||
@@ -73,8 +98,9 @@ this.timeout(30000) | ||
spmcm.once('send metrics', function (event) { | ||
if (value>0) | ||
if (value > 0) { | ||
done() | ||
else | ||
done ('failed ' + value) | ||
} else { | ||
done('failed ' + value) | ||
} | ||
}) | ||
var testMetric = spmcm.getCustomMetric({ name: 'test.meter', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2', valueFilter: ['count','mean'] }) | ||
var testMetric = spmcm.getCustomMetric({ name: 'counter', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2' }) | ||
testMetric.counter() | ||
@@ -88,3 +114,2 @@ testMetric.inc() | ||
}) | ||
it('histogram', function (done) { | ||
@@ -96,4 +121,5 @@ this.timeout(30000) | ||
spmcm.once('send metrics', function (event) { | ||
if (value.mean>0) | ||
if (value.mean > 0) { | ||
done() | ||
} | ||
}) | ||
@@ -110,27 +136,2 @@ var testMetric = spmcm.getCustomMetric({ name: 'test.histogram', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2' }) | ||
}) | ||
it('timer', function (done) { | ||
this.timeout(30000) | ||
try { | ||
var spmcm = new SPM(token, 0) | ||
var value = {} | ||
spmcm.once('send metrics', function (event) { | ||
if (value.min>200) | ||
done() | ||
else { | ||
done (new Error ('value : ' + value.min)) | ||
} | ||
}) | ||
var testMetric = spmcm.getCustomMetric({ name: 'test.timer', aggregation: 'avg', filter1: 'filter1', filter2: 'filter2' }) | ||
var stopwatch = testMetric.timer().start() | ||
setTimeout (function () { | ||
stopwatch.end() | ||
value = testMetric.save() | ||
spmcm.send() | ||
}, 200) | ||
} catch (err) { | ||
done(err) | ||
} | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66790
20
468
4