'use strict';
var debug = require('debug')('metrics');
var dogapi = require('dogapi');


//
// NullReporter
//

function NullReporter(apiKey, appKey) {

}

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


//
// DataDogReporter
//

function DataDogReporter(apiKey, appKey, agent) {
    apiKey = apiKey || process.env.DATADOG_API_KEY;
    appKey = appKey || process.env.DATADOG_APP_KEY;

    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 });
    // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
}

DataDogReporter.prototype.report = function(series, onSuccess, onError) {
    var callback = function(err, res, status) {
        if (err === null && status.toString()[0] === '2') {
            debug('add_metrics succeeded (status=%s)', status);
            if (typeof onSuccess === 'function') {
                onSuccess();
            }
        } else {
            debug(
                'ERROR: add_metrics failed: %s (err=%s, status=%s)',
                res, err, status
            );
            if (typeof onError === 'function') {
                onError(err, res, status);
            }
        }
    };

    if (debug.enabled) {
        // Only call stringify when debugging.
        debug('Calling add_metrics with %s', JSON.stringify(series));
    }

    // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
    dogapi.metric.send_all(series, callback);
    // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
};


module.exports = {
    NullReporter: NullReporter,
    DataDogReporter: DataDogReporter
};

@@ -386,16 +386,25 @@
 y, agent
+, apiHost
 ) %7B%0A    
@@ -502,16 +502,71 @@
 APP_KEY;
+%0A    apiHost = apiHost %7C%7C process.env.DATADOG_API_HOST;
 %0A%0A    if
@@ -792,17 +792,35 @@
 t: agent
- 
+, api_host: apiHost
 %7D);%0A    

Extend initialization to support EU realm