librato-node
Version:
A node.js client for Librato Metrics (http://metrics.librato.com/)
127 lines (106 loc) • 3.22 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Client, Collector, EventEmitter, Worker, client, collector, config, format_key, librato, middleware, ref, sanitize_name, worker;
EventEmitter = require('events').EventEmitter;
Client = require('./client');
Worker = require('./worker');
Collector = require('./collector');
middleware = require('./middleware');
ref = {}, collector = ref.collector, client = ref.client, worker = ref.worker, config = ref.config;
librato = new EventEmitter();
librato.configure = function(newConfig) {
config = newConfig;
collector = new Collector();
client = new Client(config);
return worker = new Worker({
job: librato.flush,
period: newConfig.period
});
};
librato.increment = function(name, value, opts) {
var ref1;
if (value == null) {
value = 1;
}
if (opts == null) {
opts = {};
}
if (value === Object(value)) {
opts = value;
value = 1;
}
return collector.increment("" + ((ref1 = config.prefix) != null ? ref1 : '') + (format_key(name, opts.source)), value);
};
librato.measure = function(name, value, opts) {
var ref1;
if (opts == null) {
opts = {};
}
return collector.measure("" + ((ref1 = config.prefix) != null ? ref1 : '') + (format_key(name, opts.source)), value);
};
librato.timing = function(name, fn, opts, cb) {
var ref1, ref2;
if (opts == null) {
opts = {};
}
if ((cb == null) && typeof opts === 'function') {
ref1 = [{}, opts], opts = ref1[0], cb = ref1[1];
}
return collector.timing("" + ((ref2 = config.prefix) != null ? ref2 : '') + (format_key(name, opts.source)), fn, cb);
};
librato.start = function() {
return worker.start();
};
librato.stop = function(cb) {
worker.stop();
return librato.flush(cb);
};
librato.flush = function(cb) {
var counters, gauges, i, j, len, len1, measurement;
if (cb == null) {
cb = function() {};
}
counters = [];
gauges = [];
collector.flushTo(counters, gauges);
if (config.source != null) {
for (i = 0, len = counters.length; i < len; i++) {
measurement = counters[i];
if (measurement.source == null) {
measurement.source = config.source;
}
}
for (j = 0, len1 = gauges.length; j < len1; j++) {
measurement = gauges[j];
if (measurement.source == null) {
measurement.source = config.source;
}
}
}
if (!(counters.length || gauges.length)) {
return process.nextTick(cb);
}
return client.send({
counters: counters,
gauges: gauges
}, function(err) {
if (err != null) {
librato.emit('error', err);
}
return cb(err);
});
};
librato.middleware = middleware(librato);
module.exports = librato;
format_key = function(name, source) {
if (source != null) {
return (sanitize_name(name)) + ";" + source;
} else {
return sanitize_name(name);
}
};
sanitize_name = function(name) {
return name.replace(/[^-.:_\w]+/g, '_').substr(0, 255);
};
}).call(this);
//# sourceMappingURL=librato.js.map