This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstatsd.js
110 lines (102 loc) · 2.83 KB
/
statsd.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// @flow
const os = require('os');
const debug = require('debug')('shared:middlewares:statsd');
type Tags = {
[key: string]: string,
};
const stringify = (obj?: { [key: string]: string }): Array<string> => {
if (!obj) return [];
return Object.keys(obj).reduce((arr, key) => {
// $FlowFixMe
return arr.concat([`${key}:${obj[key]}`]);
}, []);
};
const log = (
name: string,
key: string,
value?: number,
tags?: Tags,
timestamp?: number
) => {
let stringTags;
if (!tags && typeof value !== 'number') {
tags = value;
value = undefined;
}
if (tags) {
if (Array.isArray(tags)) {
stringTags = tags.join(',');
} else {
stringTags = stringify(tags).join(',');
}
}
debug(
`${name}: ${key}${value !== undefined ? `:${value}` : ''}${
stringTags ? `#${stringTags}` : ''
}`
);
};
let counts = {};
export let statsd = {
histogram: (key: string, value: number, tags?: Tags, timestamp?: number) => {
log('histogram', key, value, tags, timestamp);
},
timing: (key: string, value: number, tags?: Tags, timestamp?: number) => {
log('timing', key, value, tags, timestamp);
},
increment: (key: string, value?: number, tags?: Tags, timestamp?: number) => {
if (!counts[key]) counts[key] = 0;
counts[key] += typeof value === 'number' ? value : 1;
log(
'increment',
key,
counts[key],
tags || (typeof value !== 'number' && value) || undefined,
timestamp
);
},
gauge: (key: string, value: number, tags?: Tags, timestamp?: number) => {
log('gauge', key, value, tags, timestamp);
},
};
if (
!process.env.DATADOG_API_KEY ||
process.env.DATADOG_API_KEY === 'undefined'
) {
console.warn('No DATADOG_API_KEY provided, not tracking metrics.');
} else {
console.warn('Tracking metrics to DataDog.');
const metrics = require('datadog-metrics');
metrics.init({
defaultTags: [
`server:${process.env.SENTRY_NAME || 'unknown_server'}`,
`hostname: ${os.hostname() || 'unknown_instance_hostname'}`,
],
});
// This is necessary for express-hot-shots to work
const handleObjectTags = method => {
const original = metrics[method];
metrics[method] = (key, val, tags, timestamp) => {
return original.call(
metrics,
key,
val,
Array.isArray(tags) ? tags : stringify(tags),
timestamp
);
};
};
handleObjectTags('histogram');
handleObjectTags('gauge');
handleObjectTags('increment');
metrics.timing = (...args) => metrics.histogram.call(metrics, ...args);
statsd = metrics;
}
function collectMemoryStats() {
var memory = process.memoryUsage();
statsd.gauge('memory.rss', memory.rss);
statsd.gauge('memory.heapTotal', memory.heapTotal);
statsd.gauge('memory.heapUsed', memory.heapUsed);
}
// Report memory usage every second
setInterval(collectMemoryStats, 1000);