Repository: dbader/node-datadog-metrics
Pull request #15 (2016-03-18 06:14:35)

Support default tags
``` javascript
var metrics = new metrics.BufferedMetricsLogger({
    // ...
    defaultTags: ['one', 'two']
});
metrics.increment('my_counter', 1, ['three']);
// Reports tags: one, two, three
```



Patch:
diff --git a/README.md b/README.md
index a7c0118..5f2b573 100644
--- a/README.md
+++ b/README.md
@@ -89,7 +89,8 @@ var metricsLogger = new metrics.BufferedMetricsLogger({
     apiKey: 'TESTKEY',
     host: 'myhost',
     prefix: 'myapp.',
-    flushIntervalSeconds: 15
+    flushIntervalSeconds: 15,
+    defaultTags: ['env:staging', 'region:us-east-1']
 });
 metricsLogger.gauge('mygauge', 42);
 ```
@@ -116,6 +117,8 @@ Where `options` is an object and can contain the following:
 * `appKey`: Sets the DataDog APP key. (optional)
     * It's usually best to keep this in an environment variable.
       Datadog-metrics looks for the APP key in `DATADOG_APP_KEY` by default.
+* `defaultTags`: Default tags used for all metric reporting. (optional)
+    * Set tags that are common to all metrics.
 
 
 Example:
@@ -202,6 +205,8 @@ npm test
 
 ## Release History
 
+* 0.4.0
+    * ADD: Initialize with a default set of tags
 * 0.3.0
     * FIX: Don't overwrite metrics with the same key but different tags when aggregating them (Thanks @akrylysov and @RavivIsraeli!)
     * ADD: Add success/error callbacks to `metrics.flush()` (Thanks @akrylysov!)
diff --git a/index.js b/index.js
index fdd7020..92f3bd7 100644
--- a/index.js
+++ b/index.js
@@ -10,6 +10,7 @@ var sharedLogger = null;
 //     - appKey: DataDog APP key
 //     - host: Default host for all reported metrics
 //     - prefix: Default key prefix for all metrics
+//     - defaultTags: Common tags for all metrics
 //     - flushIntervalSeconds:
 //
 // You can also use it to override (dependency-inject) the aggregator
diff --git a/lib/aggregators.js b/lib/aggregators.js
index 5fdc0ff..4a5392e 100644
--- a/lib/aggregators.js
+++ b/lib/aggregators.js
@@ -4,8 +4,9 @@
 // --- Aggregator
 //
 
-function Aggregator(opts) {
+function Aggregator(defaultTags) {
     this.buffer = {};
+    this.defaultTags = defaultTags || [];
 }
 
 Aggregator.prototype.makeBufferKey = function(key, tags) {
@@ -30,6 +31,13 @@ Aggregator.prototype.flush = function() {
         }
     }
 
+    // Concat default tags
+    if (this.defaultTags) {
+        for (var i = 0; i < series.length; i++) {
+            series[i].tags = this.defaultTags.concat(series[i].tags);
+        }
+    }
+
     this.buffer = {};
 
     return series;
diff --git a/lib/loggers.js b/lib/loggers.js
index 1b841bd..8b0db2b 100644
--- a/lib/loggers.js
+++ b/lib/loggers.js
@@ -36,7 +36,7 @@ var Histogram = require('./metrics').Histogram;
 //     - reporter: a Reporter instance
 //
 function BufferedMetricsLogger(opts) {
-    this.aggregator = opts.aggregator || new Aggregator();
+    this.aggregator = opts.aggregator || new Aggregator(opts.defaultTags);
     this.reporter = opts.reporter || new DataDogReporter(opts.apiKey, opts.appKey);
     this.host = opts.host;
     this.prefix = opts.prefix || '';
diff --git a/package.json b/package.json
index 235633c..cfd738a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "datadog-metrics",
-  "version": "0.3.0",
+  "version": "0.4.0",
   "description": "Buffered metrics reporting via the DataDog HTTP API",
   "main": "index.js",
   "repository": {


Test patch:
diff --git a/test/aggregators_tests.js b/test/aggregators_tests.js
index 5f4be97..832685b 100644
--- a/test/aggregators_tests.js
+++ b/test/aggregators_tests.js
@@ -73,5 +73,16 @@ describe('Aggregator', function() {
         f.should.have.length(1);
         f[0].should.have.deep.property('points[0][1]', 2);
     });
+
+    it('should report default tags', function() {
+        var defaultTags = ['one', 'two'];
+        var agg = new aggregators.Aggregator(defaultTags);
+        agg.addPoint(metrics.Counter, 'test.mykey', 2, ['mytag1'], 'myhost');
+        agg.addPoint(metrics.Counter, 'test.mykey', 3, ['mytag2'], 'myhost');
+        var f = agg.flush();
+        f.should.have.length(2);
+        f[0].should.have.deep.property('tags[0]', 'one');
+        f[1].should.have.deep.property('tags[1]', 'two');
+    });
 });
 
diff --git a/test/loggers_tests.js b/test/loggers_tests.js
index 2ac7493..45b88ef 100644
--- a/test/loggers_tests.js
+++ b/test/loggers_tests.js
@@ -77,4 +77,12 @@ describe('BufferedMetricsLogger', function() {
         l.increment('test.counter', 23);
         l.histogram('test.histogram', 23);
     });
+
+    it('should allow setting default tags', function() {
+        var l = new BufferedMetricsLogger({
+            reporter: new reporters.NullReporter(),
+            defaultTags: ['one', 'two']
+        });
+        l.aggregator.defaultTags.should.deep.equal(['one', 'two']);
+    });
 });