librato-node
Version:
A node.js client for Librato Metrics (http://metrics.librato.com/)
74 lines (61 loc) • 2.22 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Client, _, packageJson, request, util;
request = require('requestretry');
util = require('util');
packageJson = require('../package.json');
_ = require('lodash');
Client = (function() {
Client.prototype.endpoint = 'https://metrics-api.librato.com/v1';
function Client(arg) {
var email, requestOptions, simulate, token;
email = arg.email, token = arg.token, simulate = arg.simulate, requestOptions = arg.requestOptions;
if (!email || !token) {
if (!simulate) {
console.warn("librato-node metrics disabled: no email or token provided.");
}
} else {
this._requestOptions = _.defaults(requestOptions || {}, {
method: 'POST',
uri: this.endpoint + "/metrics",
headers: {},
maxAttempts: 3,
retryDelay: 100,
delayStrategy: function() {
return Math.pow(2, this.attempts) * (this.retryDelay / 2);
}
});
this._requestOptions.headers = _.defaults(this._requestOptions.headers, {
authorization: 'Basic ' + new Buffer(email + ":" + token).toString('base64'),
'user-agent': "librato-node/" + packageJson.version
});
}
}
Client.prototype.send = function(json, cb) {
var requestOptions;
if (this._requestOptions == null) {
return process.nextTick(cb);
}
requestOptions = _.extend({}, this._requestOptions, {
json: json
});
return request(requestOptions, function(err, res, body) {
if ((res != null ? res.attempts : void 0) > 1) {
console.warn("librato API required " + res.attempts + " retries");
}
if (err != null) {
return cb(err);
}
if (res.statusCode > 399 || ((body != null ? body.errors : void 0) != null)) {
return cb(new Error("Error sending to Librato: " + (util.inspect(body, {
depth: null
})) + " (statusCode: " + res.statusCode + ")"));
}
return cb(null, body);
});
};
return Client;
})();
module.exports = Client;
}).call(this);
//# sourceMappingURL=client.js.map