-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (51 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Module dependencies
*/
var riemann = require("riemann")
, domain = require("domain").create();
/**
* Cache the clients
* @api private
*/
var CACHE = {};
/**
* Simple Riemann Client
*
* @param {Object|Function} options
* @param {Function} cb
* @return {RiemannClient}
*/
module.exports = function(options, cb) {
if (typeof options === "function") {
cb = options;
options = {};
};
// Defaults
options = options || {};
options.host = options.host || process.env.RIEMANN_HOST || "127.0.0.1";
options.port = options.port || process.env.RIEMANN_PORT || 5555;
var cacheKey = options.host+":"+options.port;
// Noop
cb = cb || function(err){
console.error("Error connecting to riemann at "+cacheKey+":", err.stack || err, "\n Try setting RIEMANN_HOST and RIEMANN_PORT");
};
if (CACHE[cacheKey]) return CACHE[cacheKey];
domain.on("error", cb);
var client;
domain.run(function() {
client = riemann.createClient(options);
domain.removeAllListeners('error');
domain.on("error", function(err) {
delete CACHE[cacheKey];
cb(err);
});
var _disconnect = client.disconnect;
client.disconnect = function() {
delete CACHE[cacheKey];
_disconnect();
domain.dispose();
};
});
if(client) CACHE[cacheKey] = client;
return client;
};