diff --git a/README.md b/README.md index 048a258..b0b21ea 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ You can configure the following settings in your StatsD config file. influxdb: { host: '127.0.0.1', // InfluxDB host. (default 127.0.0.1) port: 8086, // InfluxDB port. (default 8086) - version: 0.8, // InfluxDB version. (default 0.8) + version: 0.8, // InfluxDB version. (default 0.8, can be 1.0) ssl: false, // InfluxDB is hosted over SSL. (default false) database: 'dbname', // InfluxDB database instance. (required) username: 'user', // InfluxDB database username. diff --git a/lib/influxdb.js b/lib/influxdb.js index f7a8913..862b0b2 100644 --- a/lib/influxdb.js +++ b/lib/influxdb.js @@ -90,7 +90,10 @@ function InfluxdbBackend(startupTime, config, events) { } } - if (self.version >= 0.9) { + if (self.version > 1) { + self.assembleEvent = self.assembleEvent_v1; + self.httpPOST = self.httpPOST_v1; + } else if (self.version >= 0.9) { self.assembleEvent = self.assembleEvent_v09; self.httpPOST = self.httpPOST_v09; } else { @@ -402,6 +405,13 @@ InfluxdbBackend.prototype.assembleEvent_v09 = function (name, events) { return payload; } +InfluxdbBackend.prototype.assembleEvent_v1 = function (name, events) { + var self = this; + var payload = name + " " + "value=" + events[0]['value'] + + return payload; +} + InfluxdbBackend.prototype.httpPOST_v08 = function (points) { /* Do not send if there are no points. */ if (!points.length) { return; } @@ -516,6 +526,61 @@ InfluxdbBackend.prototype.httpPOST_v09 = function (points) { req.end(); } +InfluxdbBackend.prototype.httpPOST_v1 = function (points) { + /* Do not send if there are no points. */ + if (!points.length) { return; } + + var self = this, + query = {db: self.database, u: self.user, p: self.pass}, + protocolName = self.protocol == http ? 'HTTP' : 'HTTPS', + startTime; + + self.logDebug(function () { + return 'Sending ' + points.length + ' different points via ' + protocolName; + }); + + self.influxdbStats.numStats = points.length; + + var options = { + hostname: self.host, + port: self.port, + path: '/write?' + querystring.stringify(query), + method: 'POST', + agent: false // Is it okay to use "undefined" here? (keep-alive) + }; + + var req = self.protocol.request(options); + + req.on('socket', function (res) { + startTime = process.hrtime(); + }); + + req.on('response', function (res) { + var status = res.statusCode; + + self.influxdbStats.httpResponseTime = millisecondsSince(startTime); + + if (status >= 400) { + self.log(protocolName + ' Error: ' + status); + } + }); + + req.on('error', function (e, i) { + self.log(e); + }); + + var payload = points.join('\n') + + self.influxdbStats.payloadSize = Buffer.byteLength(payload); + + self.logDebug(function () { + var size = (self.influxdbStats.payloadSize / 1024).toFixed(2); + return 'Payload size ' + size + ' KB'; + }); + + req.write(payload); + req.end(); +} InfluxdbBackend.prototype.configCheck = function () { var self = this, success = true;