diff --git a/README.md b/README.md index be9ee66..8d32bb8 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,16 @@ client.write(metrics, timestamp, function(err) { }); ``` +In Graphite [1.1.1 (21.12.17)](http://graphite.readthedocs.io/en/latest/releases/1_1_1.html), [tagging](http://graphite.readthedocs.io/en/latest/tags.html) becomes available. You can send tagged metrics as follows: + +```js +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 diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index 30bf610..961f125 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; +}; + /** * Writes the given metrics to the underlying plaintext socket to Graphite * @@ -52,6 +68,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 = GraphiteClient.appendTags(GraphiteClient.flatten(metrics), tags); + this.write(taggedMetrics, timestamp, cb); +} + GraphiteClient.prototype.end = function() { this._carbon.end(); };