-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (59 loc) · 2.27 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
const debug = require('debug')
const error = debug('prometheus-middleware:error')
const client = require('prom-client')
const http = require('http')
const HTTPHook = require('./src/http_hook')
let METRICS_ROUTE = '/metrics'
client.register.setContentType(
client.Registry.OPENMETRICS_CONTENT_TYPE
)
const requestListener = async (req, res) => {
if (req.url === METRICS_ROUTE) {
try {
const data = await client.register.metrics()
res.writeHead(200, { 'Content-Type': client.register.contentType })
return res.end(data)
} catch (ex) {
error(ex)
res.writeHead(500, { 'Content-Type': 'text/html' })
return res.end('Error')
}
}
res.writeHead(404, { 'Content-Type': 'text/html' })
res.end('404 not found')
}
class APM {
constructor (config = {}) {
this.config = config
this.client = client
this.server = null
if (!Object.prototype.hasOwnProperty.call(config, 'HTTPHook')) {
config.HTTPHook = true
}
if (!Object.prototype.hasOwnProperty.call(config, 'enableExemplars')) {
config.enableExemplars = true
}
}
init () {
METRICS_ROUTE = this.config.METRICS_ROUTE || METRICS_ROUTE
// --------------------------------------------------------------
// Create HTTP hook
// --------------------------------------------------------------
if (this.config.HTTPHook) HTTPHook.init(this.client, this.config)
// --------------------------------------------------------------
// prometheus stuff
// --------------------------------------------------------------
this.config.NORMALIZE_ENDPOINT = this.config.NORMALIZE_ENDPOINT === undefined ? true : this.config.NORMALIZE_ENDPOINT
if ([1, true, 'true', 'on', 'yes', undefined].indexOf(this.config.COLLECT_DEFAULT_METRICS) >= 0) {
const collectDefaultMetrics = this.client.collectDefaultMetrics
collectDefaultMetrics(this.config.PROM_CLIENT_CONF)
}
this.server = http.createServer(requestListener)
this.server.listen(this.config.PORT || 9350)
}
destroy () {
this.server.close()
this.client.register.clear()
}
}
module.exports = APM