Skip to content

Support InfluxDB >= 0.11 with tags #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ You can configure the following settings in your StatsD config file.
backends: [ "./backends/graphite", "statsd-influxdb-backend" ],

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)
ssl: false, // InfluxDB is hosted over SSL. (default false)
database: 'dbname', // InfluxDB database instance. (required)
username: 'user', // InfluxDB database username.
password: 'pass', // InfluxDB database password.
host: '127.0.0.1', // InfluxDB host. (default 127.0.0.1)
port: 8086, // InfluxDB port. (default 8086)
ssl: false, // InfluxDB is hosted over SSL. (default false)
version: 0.8, // InfluxDB version. (default 0.8)
database: 'dbname', // InfluxDB database instance. (required)
retentionPolicy: 'default' // InfluxDB retention policy. (default default)
username: 'user', // InfluxDB database username.
password: 'pass', // InfluxDB database password.
flush: {
enable: true // Enable regular flush strategy. (default true)
enable: true // Enable regular flush strategy. (default true)
},
proxy: {
enable: false, // Enable the proxy strategy. (default false)
suffix: 'raw', // Metric name suffix. (default 'raw')
flushInterval: 1000 // Flush interval for the internal buffer.
// (default 1000)
enable: false, // Enable the proxy strategy. (default false)
suffix: 'raw', // Metric name suffix. (default 'raw')
flushInterval: 1000 // Flush interval for the internal buffer. (default 1000)
},
includeStatsdMetrics: false, // Send internal statsd metrics to InfluxDB. (default false)
includeStatsdMetrics: false, // Send internal statsd metrics to InfluxDB. (default false)
includeInfluxdbMetrics: false // Send internal backend metrics to InfluxDB. (default false)
// Requires includeStatsdMetrics to be enabled.
}
Expand Down Expand Up @@ -101,7 +101,7 @@ StatsD package `client_version:1.1|c`, `client_version:1.2|c` as Influx event:
```js
[
{
name: 'visior',
name: 'visitor',
columns: ['value', 'time'],
points: [['1.1', 1384798553000], ['1.2', 1384798553001]]
}
Expand All @@ -111,7 +111,7 @@ StatsD package `client_version:1.1|c`, `client_version:1.2|c` as Influx event:
If you are using Grafana to visualize a Set, then using this query or
something similar

```
```SQL
SELECT version, count(version) FROM client_version GROUP BY version, time(1m)
```

Expand Down Expand Up @@ -262,6 +262,44 @@ StatsD packet `bytes:123|g` as InfluxDB event:
]
```

## InfluxDB >= 0.11

In newer versions of InfluxDB, it's better to differentiate data with tags than with detailed measurement names. Tags are indexed, meaning that queries on tags are more performant than those on fields and can easily be used in `GROUP BY()` clauses. For this reason, with InfluxDB version >= 0.11, StatsD names are parsed such that the first token becomes the measurement name and subsequent tokens become tags.

StatsD packets `appname.datacenter.hostname.requests:1|c` and `appname.datacenter.hostname.bytes.gauge:123|g` as InfluxDB event:

```js
[
{
name: 'appname',
tag1: 'datacenter',
tag2: 'hostname',
tag3: 'requests',
columns: ['value', 'time'],
points: [[802, 1384798553000]]
},
{
name: 'appname',
tag1: 'datacenter',
tag2: 'hostname',
tag3: 'bytes',
tag4: 'gauge',
columns: ['value', 'time'],
points: [[123, 1384798553000]]
}
]
```

In Grafana, you can then graph requests by datacenter with a query like:
```SQL
SELECT mean("value") from 'appname' WHERE "tag3" = 'requests' AND $timeFilter GROUP BY time($interval), "tag1"
```

Or, you could graph bytes transferred by host with a query like:
```SQL
SELECT mean("value") from 'appname' WHERE "tag3" = 'bytes' AND $timeFilter GROUP BY time($interval), "tag2"
```

## Proxy Strategy Notes

### Event Buffering
Expand Down
112 changes: 99 additions & 13 deletions lib/influxdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
* 'influxdb' hash defined in the main statsd config file:
*
* influxdb: {
* host: '127.0.0.1', // InfluxDB host. (default 127.0.0.1)
* port: 8086, // InfluxDB port. (default 8086)
* ssl: false, // InfluxDB is hosted over SSL. (default false)
* database: 'dbname', // InfluxDB database instance. (required)
* username: 'user', // InfluxDB database username.
* password: 'pass', // InfluxDB database password.
* host: '127.0.0.1', // InfluxDB host. (default 127.0.0.1)
* port: 8086, // InfluxDB port. (default 8086)
* ssl: false, // InfluxDB is hosted over SSL. (default false)
* version: 0.8, // InfluxDB version. (default 0.8)
* database: 'dbname', // InfluxDB database instance. (required)
* retentionPolicy: 'default' // InfluxDB retention policy. (default default)
* username: 'user', // InfluxDB database username.
* password: 'pass', // InfluxDB database password.
* flush: {
* enable: true // Enable regular flush strategy. (default true)
* enable: true // Enable regular flush strategy. (default true)
* },
* proxy: {
* enable: false, // Enable the proxy strategy. (default false)
* suffix: 'raw', // Metric name suffix. (default 'raw')
* flushInterval: 1000 // Flush interval for the internal buffer.
* // (default 1000)
* enable: false, // Enable the proxy strategy. (default false)
* suffix: 'raw', // Metric name suffix. (default 'raw')
* flushInterval: 1000 // Flush interval for the internal buffer. (default 1000)
* },
* includeStatsdMetrics: false, // Send internal statsd metrics to InfluxDB. (default false)
* includeStatsdMetrics: false, // Send internal statsd metrics to InfluxDB. (default false)
* includeInfluxdbMetrics: false // Send internal backend metrics to InfluxDB. (default false)
* // Requires includeStatsdMetrics to be enabled.
* }
Expand All @@ -50,6 +51,7 @@ function InfluxdbBackend(startupTime, config, events) {
self.defaultProxyEnable = false;
self.defaultProxySuffix = 'raw';
self.defaultProxyFlushInterval = 1000;
self.defaultRetentionPolicy = 'default';

self.host = self.defaultHost;
self.port = self.defaultPort;
Expand All @@ -72,6 +74,7 @@ function InfluxdbBackend(startupTime, config, events) {
self.user = config.influxdb.username;
self.pass = config.influxdb.password;
self.database = config.influxdb.database;
self.retentionPolicy = config.influxdb.retentionPolicy || self.defaultRetentionPolicy;
self.includeStatsdMetrics = config.influxdb.includeStatsdMetrics;
self.includeInfluxdbMetrics = config.influxdb.includeInfluxdbMetrics;

Expand All @@ -90,7 +93,10 @@ function InfluxdbBackend(startupTime, config, events) {
}
}

if (self.version >= 0.9) {
if (self.version >= 0.11) {
self.assembleEvent = self.assembleEvent_v11;
self.httpPOST = self.httpPOST_v11;
} else if (self.version >= 0.9) {
self.assembleEvent = self.assembleEvent_v09;
self.httpPOST = self.httpPOST_v09;
} else {
Expand Down Expand Up @@ -402,6 +408,19 @@ InfluxdbBackend.prototype.assembleEvent_v09 = function (name, events) {
return payload;
}

InfluxdbBackend.prototype.assembleEvent_v11 = function (name, events) {
var self = this;

var name_parts = name.split('.');
var payload = name_parts[0];
for (var t=1; t<name_parts.length; t++) {
payload = payload + ",tag" + t + "=" + name_parts[t];
}
payload = payload + " value=" + events[0]['value'] + " " + events[0]['time']*1000000;

return payload;
}

InfluxdbBackend.prototype.httpPOST_v08 = function (points) {
/* Do not send if there are no points. */
if (!points.length) { return; }
Expand Down Expand Up @@ -516,6 +535,73 @@ InfluxdbBackend.prototype.httpPOST_v09 = function (points) {
req.end();
}

InfluxdbBackend.prototype.httpPOST_v11 = function (points) {
/* Do not send it there are no points. */
if (!points.length) { return; }

var self = this,
query = {
db: self.database,
rp: self.retentionPolicy,
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',
headers: {
'content-type': 'x-www-form-urlencoded'
},
agent: false
};

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[0];
for (var t=1; t<points.length; t++) {
payload = payload + "\n" + points[t];
}

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;
Expand Down