Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Where `options` is an object and can contain the following:
* `defaultTags`: Default tags used for all metric reporting. (optional)
* Set tags that are common to all metrics.
* `agent`: custom https agent (optional)
* `apiHost`: Sets the hostname of the target API endpoint. (optional)
* Defaults to `api.datadoghq.com`


Example:
Expand Down
17 changes: 13 additions & 4 deletions lib/loggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ var Histogram = require('./metrics').Histogram;
// - apiKey: DataDog API key
// - appKey: DataDog APP key
// - host: Default host for all reported metrics
// - apiHost: Hostname dogapi uses to send metrics to
// - prefix: Default key prefix for all metrics
// - agent: a https agent, passed as dogapi's proxy_agent
// - intervalHandler: alternative interval handler vs. default setTimeout
// - flushIntervalSeconds:
//
// You can also use it to override (dependency-inject) the aggregator
Expand All @@ -38,10 +40,13 @@ var Histogram = require('./metrics').Histogram;
//
function BufferedMetricsLogger(opts) {
this.aggregator = opts.aggregator || new Aggregator(opts.defaultTags);
this.reporter = opts.reporter || new DataDogReporter(opts.apiKey, opts.appKey, opts.agent);
this.reporter = opts.reporter || new DataDogReporter(opts.apiKey, opts.appKey, opts.agent, opts.apiHost);
this.host = opts.host;
this.prefix = opts.prefix || '';
this.flushIntervalSeconds = opts.flushIntervalSeconds;
this.intervalHandler = opts.intervalHandler || function(callback, delay) {
return setTimeout(callback, delay);
};

if (this.flushIntervalSeconds) {
debug('Auto-flushing every %d seconds', this.flushIntervalSeconds);
Expand All @@ -54,9 +59,13 @@ function BufferedMetricsLogger(opts) {
self.flush();
if (self.flushIntervalSeconds) {
var interval = self.flushIntervalSeconds * 1000;
var tid = setTimeout(autoFlushCallback, interval);
// Let the event loop exit if this is the only active timer.
tid.unref();
var tid = self.intervalHandler(autoFlushCallback, interval);
// `.unref()` is not available on browser `setTimeout()`
if (typeof tid.unref === 'function') {
// Node provides `.unref()` to let the event loop exit
// if this is the only active timer left.
tid.unref();
}
}
};

Expand Down
10 changes: 6 additions & 4 deletions lib/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ function NullReporter(apiKey, appKey) {

NullReporter.prototype.report = function(series, onSuccess, onError) {
// Do nothing.
onSuccess();
if (typeof onSuccess === 'function') {
onSuccess();
}
};


//
// DataDogReporter
//

function DataDogReporter(apiKey, appKey, agent) {
function DataDogReporter(apiKey, appKey, agent, apiHost) {
apiKey = apiKey || process.env.DATADOG_API_KEY;
appKey = appKey || process.env.DATADOG_APP_KEY;
apiHost = apiHost || process.env.DATADOG_API_HOST || 'api.datadoghq.com';

if (!apiKey) {
throw new Error('DATADOG_API_KEY environment variable not set');
}

// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
dogapi.initialize({api_key: apiKey, app_key: appKey, proxy_agent: agent });
dogapi.initialize({ api_key: apiKey, app_key: appKey, api_host: apiHost, proxy_agent: agent });
// jscs:enable requireCamelCaseOrUpperCaseIdentifiers
}

Expand Down
Loading