-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.01 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
/*
* Initialize a hot-shots client.
*/
const statsdClient = require('hot-shots');
const STATSD_HOST = '127.0.0.1';
const STATSD_PORT = 8125;
const CONTAINER_HOSTNAME = process.env.HOSTNAME;
const { NOMAD_ALLOC_ID } = process.env;
function setupStatsdClient({
svc, env, host = STATSD_HOST, port = STATSD_PORT, log,
}) {
if (!svc) {
throw new Error('Must provide a service name');
}
const globalTags = { env };
// If we don't add these conditionals, these tags will be set
// with an `undefined` value, which is wasteful.
if (CONTAINER_HOSTNAME) {
globalTags.container_hostname = CONTAINER_HOSTNAME;
}
if (NOMAD_ALLOC_ID) {
globalTags.nomad_alloc_id = NOMAD_ALLOC_ID;
}
if (log) {
log.info(`Initializing statsd client for service ${svc} @ ${STATSD_HOST}:${STATSD_PORT}...`);
}
const client = new statsdClient.StatsD(
{
host,
port,
prefix: `${svc}_`,
globalTags,
telegraf: true,
},
);
return client;
}
module.exports = {
setupStatsdClient,
};