2021-10-27 12:06:50 +00:00
|
|
|
/* eslint-disable no-console */
|
2023-10-18 08:32:14 +00:00
|
|
|
|
2020-09-11 18:18:22 +00:00
|
|
|
const ExpressCompression = require('compression')
|
2020-10-29 13:49:55 +00:00
|
|
|
const promClient = require('prom-client')
|
|
|
|
const promWrapper = require('./prom_wrapper')
|
2020-09-11 18:18:22 +00:00
|
|
|
|
|
|
|
const destructors = []
|
|
|
|
|
|
|
|
require('./uv_threadpool_size')
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
function registerDestructor(func) {
|
|
|
|
destructors.push(func)
|
|
|
|
}
|
2020-09-11 18:18:22 +00:00
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
function injectMetricsRoute(app) {
|
|
|
|
app.get(
|
|
|
|
'/metrics',
|
|
|
|
ExpressCompression({
|
2021-12-16 09:04:32 +00:00
|
|
|
level: parseInt(process.env.METRICS_COMPRESSION_LEVEL || '1', 10),
|
2020-09-11 19:43:08 +00:00
|
|
|
}),
|
2023-02-09 12:24:35 +00:00
|
|
|
function (req, res, next) {
|
2020-10-29 13:49:55 +00:00
|
|
|
res.set('Content-Type', promWrapper.registry.contentType)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.registry
|
|
|
|
.metrics()
|
|
|
|
.then(metrics => {
|
|
|
|
res.end(metrics)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
next(err)
|
|
|
|
})
|
2020-09-11 18:18:22 +00:00
|
|
|
}
|
2020-09-11 19:43:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:54:58 +00:00
|
|
|
function buildPromKey(key) {
|
2020-09-11 19:43:08 +00:00
|
|
|
return key.replace(/[^a-zA-Z0-9]/g, '_')
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeValue(value) {
|
|
|
|
return parseFloat(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
function set(key, value, sampleRate = 1) {
|
|
|
|
console.log('counts are not currently supported')
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function inc(key, sampleRate = 1, labels = {}) {
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 2 && typeof sampleRate === 'object') {
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('counter', key, labels).inc(labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing inc', key, labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function count(key, count, sampleRate = 1, labels = {}) {
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('counter', key, labels).inc(labels, count)
|
2020-09-11 19:43:08 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing count/inc', key, labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 18:18:22 +00:00
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function summary(key, value, labels = {}) {
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('summary', key, labels).observe(labels, value)
|
2020-09-11 19:43:08 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing summary', key, value, labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function timing(key, timeSpan, sampleRate = 1, labels = {}) {
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey('timer_' + key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('summary', key, labels).observe(labels, timeSpan)
|
2020-09-11 19:43:08 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing timing', key, labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function histogram(key, value, buckets, labels = {}) {
|
2022-03-01 15:09:55 +00:00
|
|
|
key = buildPromKey('histogram_' + key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('histogram', key, labels, buckets).observe(labels, value)
|
2022-03-01 15:09:55 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing histogram', key, buckets, labels)
|
2022-03-01 15:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
class Timer {
|
2023-02-09 12:24:35 +00:00
|
|
|
constructor(key, sampleRate = 1, labels = {}, buckets) {
|
2022-10-27 13:53:58 +00:00
|
|
|
if (typeof sampleRate === 'object') {
|
2023-02-09 12:24:35 +00:00
|
|
|
// called with (key, labels, buckets)
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 3) {
|
2023-02-09 12:24:35 +00:00
|
|
|
buckets = labels
|
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
// called with (key, labels)
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 2) {
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sampleRate = 1 // default value to pass to timing function
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
this.start = new Date()
|
|
|
|
key = buildPromKey(key)
|
|
|
|
this.key = key
|
|
|
|
this.sampleRate = sampleRate
|
2023-02-09 12:24:35 +00:00
|
|
|
this.labels = labels
|
2022-03-01 15:09:55 +00:00
|
|
|
this.buckets = buckets
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
|
|
|
const timeSpan = new Date() - this.start
|
2022-03-01 15:09:55 +00:00
|
|
|
if (this.buckets) {
|
2023-02-09 12:24:35 +00:00
|
|
|
histogram(this.key, timeSpan, this.buckets, this.labels)
|
2022-03-01 15:09:55 +00:00
|
|
|
} else {
|
2023-02-09 12:24:35 +00:00
|
|
|
timing(this.key, timeSpan, this.sampleRate, this.labels)
|
2022-03-01 15:09:55 +00:00
|
|
|
}
|
2020-09-11 19:43:08 +00:00
|
|
|
return timeSpan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function gauge(key, value, sampleRate = 1, labels = {}) {
|
2022-10-27 13:53:58 +00:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = sampleRate
|
2022-10-27 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 12:24:35 +00:00
|
|
|
promWrapper.metric('gauge', key, labels).set(labels, sanitizeValue(value))
|
2020-09-11 19:43:08 +00:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 12:24:35 +00:00
|
|
|
console.log('doing gauge', key, labels)
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:35 +00:00
|
|
|
function globalGauge(key, value, sampleRate = 1, labels = {}) {
|
2020-09-11 19:43:08 +00:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 12:24:35 +00:00
|
|
|
labels = { host: 'global', ...labels }
|
|
|
|
promWrapper.metric('gauge', key, labels).set(labels, sanitizeValue(value))
|
2020-09-11 19:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
for (const func of destructors) {
|
|
|
|
func()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:06:50 +00:00
|
|
|
module.exports.registerDestructor = registerDestructor
|
|
|
|
module.exports.injectMetricsRoute = injectMetricsRoute
|
|
|
|
module.exports.buildPromKey = buildPromKey
|
|
|
|
module.exports.sanitizeValue = sanitizeValue
|
|
|
|
module.exports.set = set
|
|
|
|
module.exports.inc = inc
|
|
|
|
module.exports.count = count
|
|
|
|
module.exports.summary = summary
|
|
|
|
module.exports.timing = timing
|
2022-03-01 15:09:55 +00:00
|
|
|
module.exports.histogram = histogram
|
2021-10-27 12:06:50 +00:00
|
|
|
module.exports.Timer = Timer
|
|
|
|
module.exports.gauge = gauge
|
|
|
|
module.exports.globalGauge = globalGauge
|
|
|
|
module.exports.close = close
|
|
|
|
module.exports.prom = promClient
|
|
|
|
module.exports.register = promWrapper.registry
|
|
|
|
|
|
|
|
module.exports.http = require('./http')
|
|
|
|
module.exports.open_sockets = require('./open_sockets')
|
2023-05-23 08:10:44 +00:00
|
|
|
module.exports.leaked_sockets = require('./leaked_sockets')
|
2021-10-27 12:06:50 +00:00
|
|
|
module.exports.event_loop = require('./event_loop')
|
|
|
|
module.exports.memory = require('./memory')
|
2023-02-09 12:24:44 +00:00
|
|
|
module.exports.mongodb = require('./mongodb')
|
2021-10-27 12:06:50 +00:00
|
|
|
module.exports.timeAsyncMethod = require('./timeAsyncMethod')
|