-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstatsd-filter-proxy.js
56 lines (47 loc) · 1.29 KB
/
statsd-filter-proxy.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
// This is the original implementation of statsd-filter-proxy. It is a very
// tiny nodejs program with decent performance characteristics. This version
// is used as the performance baseline. If the Rust version is slower than
// Nodejs, then we are probably doing it wrong.
const udp = require('dgram');
const server = udp.createSocket('udp4');
const client = udp.createSocket('udp4');
const config = {
listenPort: 8125,
forward: {
host: '127.0.0.1',
port: 8126,
},
metricBlocklist: [
"foo"
]
}
function blacklistMetric(metric) {
for (const substring of config.metricBlocklist) {
if (metric.includes(substring)) {
return true;
}
}
return false;
}
server.on('message', (msg) => {
if (blacklistMetric(msg)) {
return;
}
client.send(msg, config.forward.port, config.forward.host, (error) => {
if (error) {
console.log(`Unable to forward datagram to ${config.forward}, ${error}`);
process.exit(-1);
}
});
});
server.on('listening', () => {
console.log(`Listening at ${server.address().address}:${server.address().port}`);
});
server.on('close', () => {
console.log('UDP server socket is closed');
});
server.on('error', (error) => {
console.warn(`UDP server Error: ${error}`);
server.close();
});
server.bind(config.listenPort);