From 6b225d5966ccd32f1d944d912678e759e6f4e3ec Mon Sep 17 00:00:00 2001 From: Michael Wehrle Date: Fri, 18 Dec 2015 09:41:07 -0800 Subject: [PATCH 1/2] Make keys work with tags --- lib/influxdb.js | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/influxdb.js b/lib/influxdb.js index f7a8913..687b225 100644 --- a/lib/influxdb.js +++ b/lib/influxdb.js @@ -137,6 +137,18 @@ function millisecondsSince(start) { return diff[0] * 1000 + diff[1] / 1000000; } +// data passed in from statsd +// deploys.display.newstuff,product=Marvellous\ Marbles,product_id=1:2|g +// determines the "real" key, appends type of metric to real key +// returns reassembled data +function influxKey(key, type) { + // find the actual key + var k = key.match(/^(.*?),/i)[1]; + var rest = key.replace(k, ''); + + return k + '.' + type + rest; +} + InfluxdbBackend.prototype.log = function (msg) { util.log('[influxdb] ' + msg); } @@ -187,7 +199,7 @@ InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) { if (!self.includeStatsdMetrics && key.match(statsPrefixRegexp)) { continue; } var value = counters[key], - k = key + '.counter'; + k = influxKey(key, 'counter'); if (value) { points.push(self.assembleEvent(k, [{value: value, time: timestamp}])); @@ -206,7 +218,7 @@ InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) { if (!self.includeStatsdMetrics && key.match(statsPrefixRegexp)) { continue; } var value = gauges[key], - k = key + '.gauge'; + k = influxKey(key, 'gauge'); if (!isNaN(parseFloat(value)) && isFinite(value)) { points.push(self.assembleEvent(k, [{value: value, time: timestamp}])); @@ -223,7 +235,7 @@ InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) { for (histoKey in histoMetrics) { var value = histoMetrics[histoKey], - k = key + '.timer.histogram.' + histoKey; + k = influxKey(key, 'timer.histogram.' + histoKey); points.push(self.assembleEvent(k, [{value: value, time: timestamp}])); } @@ -235,7 +247,7 @@ InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) { // Iterate over normal metrics: for (timerKey in timerMetrics) { var value = timerMetrics[timerKey], - k = key + '.timer' + '.' + timerKey; + k = influxKey(key, 'timer' + '.' + timerKey); points.push(self.assembleEvent(k, [{value: value, time: timestamp}])); } @@ -462,7 +474,7 @@ InfluxdbBackend.prototype.httpPOST_v09 = function (points) { if (!points.length) { return; } var self = this, - query = {u: self.user, p: self.pass}, + query = {u: self.user, p: self.pass, db: self.database}, protocolName = self.protocol == http ? 'HTTP' : 'HTTPS', startTime; @@ -500,10 +512,18 @@ InfluxdbBackend.prototype.httpPOST_v09 = function (points) { self.log(e); }); - var payload = JSON.stringify({ - database: self.database, - points: points - }); + var points_to_payload = function(points) { + // {"database":"statsd","points":[{"measurement":"my.test.cnt,host=server1,name=abc.gauge","fields":{"value":56}}]} + lines = ''; + for (p in points) { + p = points[p]; + var l = p['measurement'] + ' value=' + p['fields']['value']; + lines += l + "\n"; + } + return lines; + }; + + var payload = points_to_payload(points); self.influxdbStats.payloadSize = Buffer.byteLength(payload); @@ -512,6 +532,10 @@ InfluxdbBackend.prototype.httpPOST_v09 = function (points) { return 'Payload size ' + size + ' KB'; }); + self.logDebug(function () { + return "Payload: \n" + payload; + }); + req.write(payload); req.end(); } From 10791685813d1e19dd1e04e58c6da989b0b1bea9 Mon Sep 17 00:00:00 2001 From: Michael Wehrle Date: Fri, 18 Dec 2015 11:06:18 -0800 Subject: [PATCH 2/2] Handle the standard case as well --- lib/influxdb.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/influxdb.js b/lib/influxdb.js index 687b225..2e364b0 100644 --- a/lib/influxdb.js +++ b/lib/influxdb.js @@ -143,7 +143,10 @@ function millisecondsSince(start) { // returns reassembled data function influxKey(key, type) { // find the actual key - var k = key.match(/^(.*?),/i)[1]; + var k = key.substr(0, key.indexOf(',')); + if (!k) { + return key; + } var rest = key.replace(k, ''); return k + '.' + type + rest;