-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·85 lines (70 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
require('dotenv').config()
const http = require('http')
const Prometheus = require('prom-client')
const { argv } = require('./lib/args')
const scraper = require('./lib/scraper')
const logger = require('./lib/logger')
Prometheus.collectDefaultMetrics()
const server = http.createServer((req, res) => {
switch (req.url) {
case '/metrics':
res.writeHead(200, { 'Content-Type': Prometheus.register.contentType })
Prometheus.register.metrics().then(str => {
res.write(str)
res.end()
})
break
case '/health':
res.writeHead(200)
res.write('OK')
res.end()
break
case '/':
res.writeHead(302, { Location: '/metrics' })
res.write('OK')
res.end()
break
default:
res.writeHead(404).end()
break
}
})
server.listen(argv.port, argv.host, (err) => {
if (err) {
logger.error(`Unable to listen on ${argv.host}:${argv.port}`, err)
logger.verbose(err)
return
}
logger.info(`Listen on ${argv.host}:${argv.port}`)
})
scraper.initScrapeGlobal(argv.interval * 1000, argv.spread)
argv.organization.forEach((organization) => {
scraper.initScrapeOrganization(organization, argv.interval * 1000, argv.spread)
})
argv.user.forEach((username) => {
scraper.initScrapeUser(username, argv.interval * 1000, argv.spread)
})
if (argv.repository.length !== 0) {
scraper.initScrapeRepositories(argv.repository, argv.interval * 1000, argv.spread)
}
// Graceful shutdown
process.on('SIGTERM', () => {
server.close((err) => {
if (err) {
logger.error(`Failed to stop server: ${err.message}`)
logger.verbose(err)
process.exit(1)
}
process.exit(0)
})
})
const originalEmit = process.emit
process.emit = function (name, data, ...args) {
if (
name === 'warning' &&
typeof data === 'object' &&
data.name === 'ExperimentalWarning'
) { return false }
return originalEmit.apply(process, arguments)
}