Skip to content
Permalink
Newer
Older
100644 56 lines (47 sloc) 1.29 KB
April 11, 2021 19:30
1
// This is the original implementation of statsd-filter-proxy. It is a very
2
// tiny nodejs program with decent performance characteristics. This version
3
// is used as the performance baseline. If the Rust version is slower than
4
// Nodejs, then we are probably doing it wrong.
5
6
const udp = require('dgram');
7
const server = udp.createSocket('udp4');
8
const client = udp.createSocket('udp4');
9
10
const config = {
11
listenPort: 8125,
12
forward: {
13
host: '127.0.0.1',
14
port: 8126,
15
},
16
metricBlocklist: [
17
"foo"
18
]
19
}
20
21
function blacklistMetric(metric) {
22
for (const substring of config.metricBlocklist) {
23
if (metric.includes(substring)) {
24
return true;
25
}
26
}
27
return false;
28
}
29
30
server.on('message', (msg) => {
31
if (blacklistMetric(msg)) {
32
return;
33
}
34
35
client.send(msg, config.forward.port, config.forward.host, (error) => {
36
if (error) {
37
console.log(`Unable to forward datagram to ${config.forward}, ${error}`);
38
process.exit(-1);
39
}
40
});
41
});
42
43
server.on('listening', () => {
44
console.log(`Listening at ${server.address().address}:${server.address().port}`);
45
});
46
47
server.on('close', () => {
48
console.log('UDP server socket is closed');
49
});
50
51
server.on('error', (error) => {
52
console.warn(`UDP server Error: ${error}`);
53
server.close();
54
});
55
56
server.bind(config.listenPort);