-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
61 lines (50 loc) · 1.93 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
const redis = require('redis');
const url = require('url');
const Backend = require('./src/backend');
const Config = require('./src/config');
const Instrumenter = require('./src/instrumenter');
const Server = require('./src/server');
const WebServer = require('./src/webserver');
/* eslint-disable no-console */
const configFile = process.argv[2];
if (!configFile) {
console.log('Error: must provide config file as an argument.');
process.exit(1);
}
const config = Config.fromFile(configFile);
const redisPort = parseInt(process.env.REDIS_PORT, 10) || 6379;
const redisHost = process.env.REDIS_HOST || 'localhost';
const redisClient = redis.createClient(redisPort, redisHost);
const backend = new Backend({ redisClient });
const httpServicePort = process.env.HTTP_SERVICE_PORT;
const prometheusMetricsPath = process.env.PROMETHEUS_METRICS_PATH;
const server = new Server({
instrumenter: new Instrumenter({
statsdHost: process.env.STATSD_HOST,
statsdPort: parseInt(process.env.STATSD_PORT, 10),
statsdPrefix: process.env.STATSD_PREFIX || '',
statsdUseTcp: !!process.env.STATSD_USE_TCP,
}),
backend,
config,
port: process.env.PORT,
});
backend.initialize().then(() => {
console.log(`Listening on port TCP port ${server.port}, Redis host ${redisHost}:${redisPort}`);
if (httpServicePort && prometheusMetricsPath) {
WebServer.createAndServe({
port: parseInt(httpServicePort, 10),
metricsPath: prometheusMetricsPath,
});
const metricsLocation = url.format({
protocol: 'http',
hostname: '127.0.0.1',
port: httpServicePort,
pathname: prometheusMetricsPath.startsWith('/') ? prometheusMetricsPath : `/${prometheusMetricsPath}`,
});
console.log(`Serving prometheus metrics at ${metricsLocation}`);
} else if (httpServicePort || prometheusMetricsPath) {
console.warn(`Only found one of HTTP_SERVICE_PORT / PROMETHEUS_METRICS_PATH. Check your environment.`);
}
server.serve();
});