Comparing version 2.0.1 to 2.0.2
@@ -81,8 +81,5 @@ /* | ||
metric.tags['nodejs.process.ppid'] = process.ppid | ||
if (!metric.ts) { | ||
metric.ts = new Date().getTime() | ||
if (!metric.timestamp) { | ||
metric.timestamp = new Date() | ||
} | ||
if (!metric.type) { | ||
metric.type = this.defaultType | ||
} | ||
this.emit('metric', metric) | ||
@@ -89,0 +86,0 @@ if (metric.measurement) { |
@@ -65,3 +65,3 @@ /* | ||
counters.error += 1 | ||
logger.error('Error in spm-sender ' + JSON.stringify(e)) | ||
logger.error('Error in influx-sender ' + JSON.stringify(e)) | ||
}) | ||
@@ -68,0 +68,0 @@ this.spmSender.on('retransmit', function () { |
@@ -22,2 +22,3 @@ /* | ||
const defaultSpmSenderUrl = 'https://spm-receiver.sematext.com:443' | ||
// ring buffer size for non-shipped metrics | ||
const maxBufferSize = 10000 | ||
@@ -54,3 +55,3 @@ | ||
this.tid.unref() | ||
process.on('beforeExit', function () { | ||
process.once('beforeExit', function () { | ||
// TODO: flush metrics | ||
@@ -62,13 +63,14 @@ self.sendMetrics() | ||
}, 10) | ||
process.on('SIGINT', function () { | ||
process.once('SIGINT', function () { | ||
self.sendMetrics() | ||
self.sendClientInfo('Stop', 'SIGINT') | ||
}) | ||
process.on('SIGTERM', function () { | ||
process.once('SIGTERM', function () { | ||
self.sendMetrics() | ||
self.sendClientInfo('Stop', 'SIGTERM') | ||
}) | ||
process.on('SIGQUIT', function () { | ||
process.once('SIGQUIT', function () { | ||
self.sendMetrics() | ||
self.sendClientInfo('Stop', 'SIGQUIT') | ||
process.removeListener(this) | ||
}) | ||
@@ -96,2 +98,5 @@ } | ||
} | ||
if (!metric.timestamp) { | ||
metric.timestamp = new Date() | ||
} | ||
this.datapoints.push(metric) | ||
@@ -110,3 +115,6 @@ // logger.debug('InfluxSender: add metric ' + JSON.stringify(metric)) | ||
} | ||
self.influx.writePoints(this.datapoints) | ||
var reqSize = Math.min(this.datapoints.length, 100) | ||
var datapointsToShip = this.datapoints.slice(0, reqSize) | ||
this.datapoints = this.datapoints.slice(reqSize, this.datapoints.length) | ||
self.influx.writePoints(datapointsToShip) | ||
.then(() => { | ||
@@ -117,12 +125,29 @@ const msg = 'InfluxSender: ' + self.datapoints.length + ' data points successfully sent to ' + self.influxUrl | ||
msg: msg, | ||
count: self.datapoints.length, | ||
count: datapointsToShip.length, | ||
url: self.influxUrl | ||
}) | ||
self.datapoints = [] | ||
// more metrics to ship? | ||
if (self.datapoints.length > 0) { | ||
setTimeout(self.sendMetrics().bind(this), 100) | ||
} | ||
}) | ||
.catch((err) => { | ||
if (err) { | ||
console.error(err) | ||
logger.error('InfluxSender: error sending metrics ' + err) | ||
self.emit('sendFailed', {msg: err.message}) | ||
// error obj contains: err.req, err.res, err.body | ||
if (err.res && err.res.statusCode && | ||
err.res.statusCode > 399 && | ||
err.res.statusCode < 500) { | ||
// receiver rejected the metrics, so we drop the metrics | ||
logger.warn('InfluxSender: receiver rejected metrics with status code ' + err.res.statusCode) | ||
self.emit('send', {cound: datapointsToShip.length}) | ||
} else { | ||
// add metrics back to ring-buffer, if the buffer will not reach maxBufferSize | ||
if (self.datapoints.length <= (self.maxBufferSize - datapointsToShip.length)) { | ||
self.datapoints = self.datapoints.concat(datapointsToShip) | ||
} | ||
self.emit('sendFailed', {msg: err.message}) | ||
logger.error('InfluxSender: error sending metrics ' + err) | ||
} | ||
} else { | ||
self.emit('sendFailed', {msg: 'unknown error'}) | ||
} | ||
@@ -129,0 +154,0 @@ }) |
{ | ||
"name": "spm-agent", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Node.js agent framework for SPM by Sematext", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -19,3 +19,15 @@ /* | ||
global.spmSenderUrlParameters = '&countainerCount=1' | ||
var http = require('http') | ||
http.createServer(function (req, res) { | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }) | ||
res.end('{"code":"400"}\n') | ||
}).listen(3314, '127.0.0.1') | ||
config.influx = { | ||
dbName: 'metrics', | ||
host: '127.0.0.1', | ||
protocol: 'http', | ||
port: 3314 | ||
} | ||
describe('SPM for NodeJS tests', function () { | ||
@@ -53,5 +65,5 @@ it('SPM Agent Stats', function (done) { | ||
}) | ||
it('Influx Agent Stats', function (done) { | ||
it('Influx Agent emits sender-stats, handling metrics rejection on status 400', function (done) { | ||
try { | ||
this.timeout(50000) | ||
this.timeout(60000) | ||
config.collectionInterval = 1000 | ||
@@ -85,7 +97,8 @@ config.retransmitInterval = 1000 | ||
// testAgent.start() | ||
client.once('metric', function (stats) { | ||
if (stats.measurement === 'myapp.process.memory') { | ||
client.once('stats', function (stats) { | ||
if (stats && stats.send >= 1) { | ||
console.log(stats) | ||
done() | ||
} else { | ||
throw new Error('metric has no measurement') | ||
throw new Error('Agent does not emit stats object') | ||
} | ||
@@ -92,0 +105,0 @@ }) |
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
Network access
Supply chain riskThis module accesses the network.
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
70249
1359
1