Skip to content

Commit fce2549

Browse files
committed
InfluxDB version 0.9 API support
Signed-off-by: imre Fitos <imre@act.md>
1 parent 0c3d2fb commit fce2549

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ You can configure the following settings in your StatsD config file.
4747
influxdb: {
4848
host: '127.0.0.1', // InfluxDB host. (default 127.0.0.1)
4949
port: 8086, // InfluxDB port. (default 8086)
50+
version: 0.8, // InfluxDB port. (default 0.8)
5051
ssl: false, // InfluxDB is hosted over SSL. (default false)
5152
database: 'dbname', // InfluxDB database instance. (required)
5253
username: 'user', // InfluxDB database username. (required)

lib/influxdb.js

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ function InfluxdbBackend(startupTime, config, events) {
4545

4646
self.defaultHost = '127.0.0.1';
4747
self.defaultPort = 8086;
48+
self.defaultVersion = 0.8;
4849
self.defaultFlushEnable = true;
4950
self.defaultProxyEnable = false;
5051
self.defaultProxySuffix = 'raw';
5152
self.defaultProxyFlushInterval = 1000;
5253

5354
self.host = self.defaultHost;
5455
self.port = self.defaultPort;
56+
self.version = self.defaultVersion;
5557
self.protocol = http;
5658
self.flushEnable = self.defaultFlushEnable;
5759
self.proxyEnable = self.defaultProxyEnable;
@@ -66,6 +68,7 @@ function InfluxdbBackend(startupTime, config, events) {
6668
if (config.influxdb) {
6769
self.host = config.influxdb.host || self.defaultHost;
6870
self.port = config.influxdb.port || self.defaultPort;
71+
self.version = config.influxdb.version || self.defaultVersion;
6972
self.user = config.influxdb.username;
7073
self.pass = config.influxdb.password;
7174
self.database = config.influxdb.database;
@@ -87,6 +90,14 @@ function InfluxdbBackend(startupTime, config, events) {
8790
}
8891
}
8992

93+
if (self.version >= 0.9) {
94+
self.assembleEvent = self.assembleEvent_v09;
95+
self.httpPOST = self.httpPOST_v09;
96+
} else {
97+
self.assembleEvent = self.assembleEvent_v08;
98+
self.httpPOST = self.httpPOST_v08;
99+
}
100+
90101
if (self.proxyEnable) {
91102
self.log('Starting the buffer flush interval. (every ' + self.proxyFlushInterval + 'ms)');
92103
setInterval(function () {
@@ -355,7 +366,7 @@ InfluxdbBackend.prototype.clearRegistry = function () {
355366
return registry;
356367
}
357368

358-
InfluxdbBackend.prototype.assembleEvent = function (name, events) {
369+
InfluxdbBackend.prototype.assembleEvent_v08 = function (name, events) {
359370
var self = this;
360371

361372
var payload = {
@@ -380,7 +391,19 @@ InfluxdbBackend.prototype.assembleEvent = function (name, events) {
380391
return payload;
381392
}
382393

383-
InfluxdbBackend.prototype.httpPOST = function (points) {
394+
InfluxdbBackend.prototype.assembleEvent_v09 = function (name, events) {
395+
var self = this;
396+
397+
var payload = {
398+
name: name,
399+
timestamp: events[0]['time'],
400+
fields: { value: events[0]['value'] }
401+
}
402+
403+
return payload;
404+
}
405+
406+
InfluxdbBackend.prototype.httpPOST_v08 = function (points) {
384407
/* Do not send if there are no points. */
385408
if (!points.length) { return; }
386409

@@ -392,7 +415,7 @@ InfluxdbBackend.prototype.httpPOST = function (points) {
392415
self.logDebug(function () {
393416
return 'Sending ' + points.length + ' different points via ' + protocolName;
394417
});
395-
418+
396419
self.influxdbStats.numStats = points.length;
397420

398421
var options = {
@@ -435,6 +458,66 @@ InfluxdbBackend.prototype.httpPOST = function (points) {
435458
req.end();
436459
}
437460

461+
InfluxdbBackend.prototype.httpPOST_v09 = function (points) {
462+
/* Do not send if there are no points. */
463+
if (!points.length) { return; }
464+
465+
var self = this,
466+
query = {u: self.user, p: self.pass},
467+
protocolName = self.protocol == http ? 'HTTP' : 'HTTPS',
468+
startTime;
469+
470+
self.logDebug(function () {
471+
return 'Sending ' + points.length + ' different points via ' + protocolName;
472+
});
473+
474+
self.influxdbStats.numStats = points.length;
475+
476+
var options = {
477+
hostname: self.host,
478+
port: self.port,
479+
path: '/write?' + querystring.stringify(query),
480+
method: 'POST',
481+
agent: false // Is it okay to use "undefined" here? (keep-alive)
482+
};
483+
484+
var req = self.protocol.request(options);
485+
486+
req.on('socket', function (res) {
487+
startTime = process.hrtime();
488+
});
489+
490+
req.on('response', function (res) {
491+
var status = res.statusCode;
492+
493+
self.influxdbStats.httpResponseTime = millisecondsSince(startTime);
494+
495+
if (status !== 200) {
496+
self.log(protocolName + ' Error: ' + status);
497+
}
498+
});
499+
500+
req.on('error', function (e, i) {
501+
self.log(e);
502+
});
503+
504+
var payload = JSON.stringify({
505+
database: self.database,
506+
time_precision: 'ms',
507+
points: points
508+
});
509+
510+
self.influxdbStats.payloadSize = Buffer.byteLength(payload);
511+
512+
self.logDebug(function () {
513+
var size = (self.influxdbStats.payloadSize / 1024).toFixed(2);
514+
return 'Payload size ' + size + ' KB';
515+
});
516+
517+
req.write(payload);
518+
req.end();
519+
}
520+
438521
InfluxdbBackend.prototype.configCheck = function () {
439522
var self = this,
440523
success = true;

0 commit comments

Comments
 (0)