2021-10-27 08:06:50 -04:00
|
|
|
/* eslint-disable no-console */
|
2023-10-18 04:32:14 -04:00
|
|
|
|
2020-09-11 14:18:22 -04:00
|
|
|
const ExpressCompression = require('compression')
|
2020-10-29 09:49:55 -04:00
|
|
|
const promClient = require('prom-client')
|
|
|
|
const promWrapper = require('./prom_wrapper')
|
2020-09-11 14:18:22 -04:00
|
|
|
|
|
|
|
const destructors = []
|
|
|
|
|
|
|
|
require('./uv_threadpool_size')
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
function registerDestructor(func) {
|
|
|
|
destructors.push(func)
|
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
function injectMetricsRoute(app) {
|
|
|
|
app.get(
|
|
|
|
'/metrics',
|
|
|
|
ExpressCompression({
|
2021-12-16 04:04:32 -05:00
|
|
|
level: parseInt(process.env.METRICS_COMPRESSION_LEVEL || '1', 10),
|
2020-09-11 15:43:08 -04:00
|
|
|
}),
|
2023-02-09 07:24:35 -05:00
|
|
|
function (req, res, next) {
|
2020-10-29 09:49:55 -04:00
|
|
|
res.set('Content-Type', promWrapper.registry.contentType)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.registry
|
|
|
|
.metrics()
|
|
|
|
.then(metrics => {
|
|
|
|
res.end(metrics)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
next(err)
|
|
|
|
})
|
2020-09-11 14:18:22 -04:00
|
|
|
}
|
2020-09-11 15:43:08 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:54:58 -04:00
|
|
|
function buildPromKey(key) {
|
2020-09-11 15:43:08 -04: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 07:24:35 -05:00
|
|
|
function inc(key, sampleRate = 1, labels = {}) {
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 2 && typeof sampleRate === 'object') {
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('counter', key, labels).inc(labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing inc', key, labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function count(key, count, sampleRate = 1, labels = {}) {
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('counter', key, labels).inc(labels, count)
|
2020-09-11 15:43:08 -04:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing count/inc', key, labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function summary(key, value, labels = {}) {
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('summary', key, labels).observe(labels, value)
|
2020-09-11 15:43:08 -04:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing summary', key, value, labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function timing(key, timeSpan, sampleRate = 1, labels = {}) {
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey('timer_' + key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('summary', key, labels).observe(labels, timeSpan)
|
2020-09-11 15:43:08 -04:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing timing', key, labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function histogram(key, value, buckets, labels = {}) {
|
2022-03-01 10:09:55 -05:00
|
|
|
key = buildPromKey('histogram_' + key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('histogram', key, labels, buckets).observe(labels, value)
|
2022-03-01 10:09:55 -05:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing histogram', key, buckets, labels)
|
2022-03-01 10:09:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
class Timer {
|
2024-01-30 10:35:54 -05:00
|
|
|
constructor(key, sampleRate = 1, labels = {}, buckets = undefined) {
|
2022-10-27 09:53:58 -04:00
|
|
|
if (typeof sampleRate === 'object') {
|
2023-02-09 07:24:35 -05:00
|
|
|
// called with (key, labels, buckets)
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 3) {
|
2023-02-09 07:24:35 -05:00
|
|
|
buckets = labels
|
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
// called with (key, labels)
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 2) {
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sampleRate = 1 // default value to pass to timing function
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
this.start = new Date()
|
|
|
|
key = buildPromKey(key)
|
|
|
|
this.key = key
|
|
|
|
this.sampleRate = sampleRate
|
2023-02-09 07:24:35 -05:00
|
|
|
this.labels = labels
|
2022-03-01 10:09:55 -05:00
|
|
|
this.buckets = buckets
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
|
2024-01-17 04:22:46 -05:00
|
|
|
// any labels passed into the done method override labels from constructor
|
|
|
|
done(labels = {}) {
|
2020-09-11 15:43:08 -04:00
|
|
|
const timeSpan = new Date() - this.start
|
2022-03-01 10:09:55 -05:00
|
|
|
if (this.buckets) {
|
2024-01-17 04:22:46 -05:00
|
|
|
histogram(this.key, timeSpan, this.buckets, { ...this.labels, ...labels })
|
2022-03-01 10:09:55 -05:00
|
|
|
} else {
|
2024-01-17 04:22:46 -05:00
|
|
|
timing(this.key, timeSpan, this.sampleRate, { ...this.labels, ...labels })
|
2022-03-01 10:09:55 -05:00
|
|
|
}
|
2020-09-11 15:43:08 -04:00
|
|
|
return timeSpan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function gauge(key, value, sampleRate = 1, labels = {}) {
|
2022-10-27 09:53:58 -04:00
|
|
|
if (arguments.length === 3 && typeof sampleRate === 'object') {
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = sampleRate
|
2022-10-27 09:53:58 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 07:24:35 -05:00
|
|
|
promWrapper.metric('gauge', key, labels).set(labels, sanitizeValue(value))
|
2020-09-11 15:43:08 -04:00
|
|
|
if (process.env.DEBUG_METRICS) {
|
2023-02-09 07:24:35 -05:00
|
|
|
console.log('doing gauge', key, labels)
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
function globalGauge(key, value, sampleRate = 1, labels = {}) {
|
2020-09-11 15:43:08 -04:00
|
|
|
key = buildPromKey(key)
|
2023-02-09 07:24:35 -05:00
|
|
|
labels = { host: 'global', ...labels }
|
|
|
|
promWrapper.metric('gauge', key, labels).set(labels, sanitizeValue(value))
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
for (const func of destructors) {
|
|
|
|
func()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 08:06:50 -04: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 10:09:55 -05:00
|
|
|
module.exports.histogram = histogram
|
2021-10-27 08:06:50 -04: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 04:10:44 -04:00
|
|
|
module.exports.leaked_sockets = require('./leaked_sockets')
|
2021-10-27 08:06:50 -04:00
|
|
|
module.exports.event_loop = require('./event_loop')
|
|
|
|
module.exports.memory = require('./memory')
|
2023-02-09 07:24:44 -05:00
|
|
|
module.exports.mongodb = require('./mongodb')
|