From dd8780d294ad0883afc1a2943663563898d126f3 Mon Sep 17 00:00:00 2001 From: Xerxes Date: Fri, 5 Jan 2018 23:32:09 +0100 Subject: [PATCH 1/3] implement tagging --- lib/GraphiteClient.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index 8ec201c..d1e4b27 100644 --- a/lib/GraphiteClient.js +++ b/lib/GraphiteClient.js @@ -28,6 +28,22 @@ GraphiteClient.flatten = function(obj, flat, prefix) { return flat; }; +GraphiteClient.appendTags = function(flatMetrics, tags) { + tagSuffix = ''; + res = {}; + + flatTags = GraphiteClient.flatten(tags); + for (var key in flatTags) { + tagSuffix += ';' + key + '=' + flatTags[key]; + } + + for (var key in flatMetrics) { + res[key + tagSuffix] = flatMetrics[key]; + } + + return res; +} + GraphiteClient.prototype.write = function(metrics, timestamp, cb) { if (typeof timestamp === 'function') { cb = timestamp; @@ -40,6 +56,11 @@ GraphiteClient.prototype.write = function(metrics, timestamp, cb) { this._carbon.write(GraphiteClient.flatten(metrics), timestamp, cb); }; +GraphiteClient.prototype.writeTagged = function(metrics, tags, timestamp, cb) { + taggedMetrics = appendTags(GraphiteClient.flatten(metrics), tags); + this.write(taggedMetrics, timestamp, cb); +} + GraphiteClient.prototype.end = function() { this._carbon.end(); }; From 17df812511090cf062388bd655666d3a300fe937 Mon Sep 17 00:00:00 2001 From: Serphentas Date: Sat, 6 Jan 2018 00:05:15 +0100 Subject: [PATCH 2/3] update readme for tagged writes --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 0a3a4a6..2bbc8da 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,19 @@ client.write(metrics, function(err) { }); ``` +Sending data with tags: + +```js +var graphite = require('graphite'); +var client = graphite.createClient('plaintext://graphite.example.org:2003/'); + +var metrics = {foo: 23}; +var tags = {'name': 'foo.bar', 'some.fancy.tag': 'somefancyvalue'}; +client.writeTagged(metrics, tags, function(err) { + // if err is null, your data was sent to graphite! +}); +``` + ## Todo * More docs From 54a2b453232eae625c573fb873593e0dac8bb0d4 Mon Sep 17 00:00:00 2001 From: Serphentas Date: Sat, 6 Jan 2018 00:44:37 +0100 Subject: [PATCH 3/3] fix appendTags call --- lib/GraphiteClient.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index d1e4b27..8807825 100644 --- a/lib/GraphiteClient.js +++ b/lib/GraphiteClient.js @@ -42,7 +42,7 @@ GraphiteClient.appendTags = function(flatMetrics, tags) { } return res; -} +}; GraphiteClient.prototype.write = function(metrics, timestamp, cb) { if (typeof timestamp === 'function') { @@ -57,7 +57,7 @@ GraphiteClient.prototype.write = function(metrics, timestamp, cb) { }; GraphiteClient.prototype.writeTagged = function(metrics, tags, timestamp, cb) { - taggedMetrics = appendTags(GraphiteClient.flatten(metrics), tags); + taggedMetrics = GraphiteClient.appendTags(GraphiteClient.flatten(metrics), tags); this.write(taggedMetrics, timestamp, cb); }