Repository: dbader/node-datadog-metrics
Pull request #90 (2022-09-21 22:46:31)

Add missing release notes
This adds release notes for the last several versions, which were missing from the changelog in the README. The first commit should be pretty straightforward, and if this formatting looks good, I’ll also add tags for these in git.

The second commit is more speculative, and adds a log of changes that will be in the next release. I think it helps to keep track of those in the source somewhere, but can pull that commit out if we’d rather keep the README to what’s actually been published so far.

Mostly covers #79, although it doesn’t attempt to script anything about releases.


Patch:
diff --git a/README.md b/README.md
index 0e91fff..b9c263b 100644
--- a/README.md
+++ b/README.md
@@ -86,7 +86,7 @@ If you need even more control you can create one or more `BufferedMetricsLogger`
 ```js
 var metrics = require('datadog-metrics');
 var metricsLogger = new metrics.BufferedMetricsLogger({
-    apiHost: 'app.datadoghq.eu',
+    apiHost: 'datadoghq.eu',
     apiKey: 'TESTKEY',
     host: 'myhost',
     prefix: 'myapp.',
@@ -112,8 +112,11 @@ Where `options` is an object and can contain the following:
 * `flushIntervalSeconds`: How often to send metrics to Datadog. (optional)
     * This defaults to 15 seconds. Set it to 0 to disable auto-flushing which
       means you must call `flush()` manually.
-* `apiHost`: Sets the Datadog API host. (optional)
-    * Defaults to `app.datadoghq.com`.
+* `apiHost`: Sets the Datadog API host (also called "site" in Datadog docs).
+    (optional)
+    * Defaults to `datadoghq.com`.
+    * See more details on setting your site at:
+        https://docs.datadoghq.com/getting_started/site/#access-the-datadog-site
 * `apiKey`: Sets the Datadog API key. (optional)
     * It's usually best to keep this in an environment variable.
       Datadog-metrics looks for the API key in `DATADOG_API_KEY` by default.
@@ -262,6 +265,101 @@ npm test
 
 ## Release History
 
+* (In development)
+    * Support distribution metrics. You can now send distributions to Datadog by doing:
+
+        ```js
+        const metrics = require('datadog-metrics');
+        metrics.distribution('my.metric.name', 3.8, ['tags:here']);
+        ```
+
+        Distributions are similar to histograms (they create several metrics for count, average, percentiles, etc.), but they are calculated server-side on DataDog’s systems. For more details and guidance on when to use them, see:
+        * The documentation in this project’s README
+        * DataDog’s documentation at https://docs.datadoghq.com/metrics/distributions/
+
+        (Thanks to @Mr0grog.)
+
+    * Don’t use `unref()` on timers in non-Node.js environments. This is a step towards browser compatibility, although we are not testing browser-based usage yet. (Thanks to @Mr0grog.)
+    * Expose built-in reporter classes for public use. If you need to disable the metrics library for some reason, you can now do so with:
+
+        ```js
+        const metrics = require('datadog-metrics');
+        metrics.init({
+            reporter: new metrics.reporters.NullReporter((flushedMetrics) => {
+                // Optional callback to be notified when metrics are flushed.
+            }),
+        });
+        ```
+
+        (Thanks to @Mr0grog.)
+
+    * Add `.median` aggregation for histograms. When you log a histogram metric, it ultimately creates several metrics that track the minimum value, average value, maximum value, etc. There is now one that tracks the median value. StatsD creates the same metric from histograms, so you may find this useful if transitioning from StatsD. (Thanks to @Mr0grog.)
+
+    * This package no longer locks specific versions of its dependencies (instead, your package manager can choose any version that is compatible). This may help when deduplicating packages for faster installs or smaller bundles. (Thanks to @Mr0grog.)
+
+    * INTERNAL: Renamed the default branch in this repo to `main`. (Thanks to @dbader.)
+    * INTERNAL: Use GitHub actions for continuous integration. (Thanks to @Mr0grog.)
+    * INTERNAL: Code style cleanup. (Thanks to @Mr0grog.)
+    * INTERNAL: When flushing, send each metric with its own list of tags. This helps mitigate subtle errors where a change to one metric’s tags may affect others. (Thanks to @Mr0grog.)
+
+* 0.10.1 (2022-09-11)
+    * FIX: bug in 0.10.0 where `@datadog/datadog-api-client` was not used correctly. (Thanks to @gquinteros93)
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/4c29447bbde00565d5258e722b147601f3cc014c...023acfa3a2c5d8dd3f5bbb48c8c02467b2519559)
+
+* 0.10.0 (2022-09-08)
+    * **Breaking change:** we now use Datadog’s official `@datadog/datadog-api-client` package to send metrics to Datadog. This makes `datadog-metrics` usable with Webpack, but removes the `agent` option. If you were using this option and the new library does not provide a way to meet your needs, please let us know by filing an issue! (Thanks to @thatguychrisw)
+    * You can now customize what metrics are generated by a histogram. When logging a histogram metric, the 5th argument is an optional object with information about which aggregations and percentiles to create metrics for:
+
+        ```js
+        const metrics = require('datadog-metrics');
+        metrics.histogram('my.metric.name', 3.8, [], Date.now(), {
+            // Aggregates can include 'max', 'min', 'sum', 'avg', or 'count'.
+            aggregations: ['max', 'min', 'sum', 'avg', 'count'],
+            // Percentiles can include any decimal between 0 and 1.
+            percentiles: [0.75, 0.85, 0.95, 0.99]
+        });
+        ```
+
+        (Thanks to @gquinteros93.)
+    
+    * INTERNAL: Clean up continuous integration on TravisCI. (Thanks to @ErikBoesen.)
+
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/99bdc97cead1d3fabe12bf594e945c64cba5d643...4c29447bbde00565d5258e722b147601f3cc014c)
+
+* 0.9.3 (2021-03-22)
+    * INTERNAL: Update `dogapi` and `jshint` to their latest versions. (Thanks to @ErikBoesen.)
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/6f27d31c7931d3d68cd888a0237c87e3ed4feea1...99bdc97cead1d3fabe12bf594e945c64cba5d643)
+
+* 0.9.2 (2021-03-14)
+    * Expose new `apiHost` option on `init()` and `BufferedMetricsLogger` constructor. This makes it possible to actually configure the Datadog site to submit metrics to. For example, you can now submit metrics to Datadog’s Europe servers with:
+
+        ```js
+        const metrics = require('datadog-metrics');
+        metrics.init({
+            apiHost: 'datadoghq.eu'
+        });
+        ```
+
+        (Thanks to @ErikBoesen.)
+
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/8adaeb9f7bff15f620337a0fb882921925d63116...6f27d31c7931d3d68cd888a0237c87e3ed4feea1)
+
+* 0.9.1 (2021-02-19)
+    * FIX: Add default Datadog site. (Thanks to @ErikBoesen.)
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/e58b13055b803a9c4f4c7b2426e3784b8fd4e0ae...8adaeb9f7bff15f620337a0fb882921925d63116)
+
+* 0.9.0 (2021-02-10)
+    * Clean up continuous integration tooling on TravisCI. (Thanks to @rpelliard.)
+    * Correct “DataDog” to “Datadog” throughout the documentation. It turns out there’s not supposed to be a captial D in the middle. (Thanks to @dbenamy.)
+    * INTERNAL: Add internal support for submitting metrics to different Datadog sites (e.g. `datadoghq.eu` for Europe). (Thanks to @fermelone.)
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/ebb4bf701f700841e8b5c5325165f13397249b51...e58b13055b803a9c4f4c7b2426e3784b8fd4e0ae)
+
+* 0.8.2 (2020-11-16)
+    * Added @ErikBoesen as a maintainer!
+    * INTERNAL: Update `dogapi` version.
+    * INTERNAL: Validate the `onSuccess` callback in `NullReporter`. (Thanks to @dkMorlok.)
+    * [View diff](https://github.com/dbader/node-datadog-metrics/compare/294e36e411e805c664a2bf614c083ed862860ce1...ebb4bf701f700841e8b5c5325165f13397249b51)
+
 * 0.8.1
     * FIX: don't increment count when value is 0 (Thanks to @haspriyank)
 * 0.8.0
diff --git a/lib/loggers.js b/lib/loggers.js
index 70afc66..3d0512d 100644
--- a/lib/loggers.js
+++ b/lib/loggers.js
@@ -28,7 +28,7 @@ const Distribution = require('./metrics').Distribution;
 //     - appKey: DataDog APP key
 //     - host: Default host for all reported metrics
 //     - prefix: Default key prefix for all metrics
-//     - agent: a https agent, passed as dogapi's proxy_agent
+//     - apiHost: Datadog API host (also called "site" in Datadog docs)
 //     - flushIntervalSeconds:
 //
 // You can also use it to override (dependency-inject) the aggregator
@@ -39,7 +39,7 @@ const Distribution = require('./metrics').Distribution;
 //
 function BufferedMetricsLogger(opts) {
     this.aggregator = opts.aggregator || new Aggregator(opts.defaultTags);
-    this.reporter = opts.reporter || new DataDogReporter(opts.apiKey, opts.appKey, opts.agent, opts.apiHost);
+    this.reporter = opts.reporter || new DataDogReporter(opts.apiKey, opts.appKey, opts.apiHost);
     this.host = opts.host;
     this.prefix = opts.prefix || '';
     this.flushIntervalSeconds = opts.flushIntervalSeconds;
diff --git a/lib/reporters.js b/lib/reporters.js
index cd15d6f..ebec06a 100644
--- a/lib/reporters.js
+++ b/lib/reporters.js
@@ -22,9 +22,10 @@ NullReporter.prototype.report = function(series, onSuccess) {
 // DataDogReporter
 //
 
-function DataDogReporter(apiKey, appKey) {
+function DataDogReporter(apiKey, appKey, apiHost) {
     apiKey = apiKey || process.env.DATADOG_API_KEY;
     appKey = appKey || process.env.DATADOG_APP_KEY;
+    this.apiHost = apiHost || process.env.DATADOG_API_HOST;
 
     if (!apiKey) {
         throw new Error('DATADOG_API_KEY environment variable not set');
@@ -36,6 +37,15 @@ function DataDogReporter(apiKey, appKey) {
             appKeyAuth: appKey
         }
     });
+    if (this.apiHost) {
+        // Strip leading `app.` from the site in case someone copy/pasted the
+        // URL from their web browser. More details on correct configuration:
+        // https://docs.datadoghq.com/getting_started/site/#access-the-datadog-site
+        this.apiHost = apiHost.replace(/^app\./i, '');
+        datadogApiClient.client.setServerVariables(configuration, {
+            site: this.apiHost
+        });
+    }
     metricsApi = new datadogApiClient.v1.MetricsApi(configuration);
 }
 


Test patch:
diff --git a/test/loggers_tests.js b/test/loggers_tests.js
index 961b16d..4fe2063 100644
--- a/test/loggers_tests.js
+++ b/test/loggers_tests.js
@@ -118,4 +118,20 @@ describe('BufferedMetricsLogger', function() {
         });
         l.aggregator.defaultTags.should.deep.equal(['one', 'two']);
     });
+
+    it('should allow setting apiHost/site', function() {
+        var l = new BufferedMetricsLogger({
+            apiKey: 'abc123',
+            apiHost: 'datadoghq.eu'
+        });
+        l.reporter.should.have.property('apiHost', 'datadoghq.eu');
+    });
+
+    it('should allow setting apiHost/site with "app.*" URLs', function() {
+        var l = new BufferedMetricsLogger({
+            apiKey: 'abc123',
+            apiHost: 'app.datadoghq.eu'
+        });
+        l.reporter.should.have.property('apiHost', 'datadoghq.eu');
+    });
 });