2021-10-27 08:06:50 -04:00
|
|
|
/* eslint-disable no-console */
|
2020-10-29 09:49:55 -04:00
|
|
|
const os = require('os')
|
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')
|
2022-10-31 10:37:49 -04:00
|
|
|
const tracing = require('./tracing')
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-10-29 09:49:55 -04:00
|
|
|
const DEFAULT_APP_NAME = 'unknown'
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-10-29 09:49:55 -04:00
|
|
|
const { collectDefaultMetrics } = promWrapper
|
2020-09-11 14:18:22 -04:00
|
|
|
const destructors = []
|
|
|
|
|
|
|
|
require('./uv_threadpool_size')
|
|
|
|
|
2020-09-11 15:54:58 -04:00
|
|
|
/**
|
|
|
|
* Configure the metrics module
|
|
|
|
*/
|
|
|
|
function configure(opts = {}) {
|
2020-10-29 09:49:55 -04:00
|
|
|
const appName = opts.appName || DEFAULT_APP_NAME
|
|
|
|
const hostname = os.hostname()
|
|
|
|
promClient.register.setDefaultLabels({ app: appName, host: hostname })
|
2020-09-11 15:43:08 -04:00
|
|
|
if (opts.ttlInMinutes) {
|
2020-10-29 09:49:55 -04:00
|
|
|
promWrapper.ttlInMinutes = opts.ttlInMinutes
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
2020-09-11 15:54:58 -04:00
|
|
|
}
|
|
|
|
|
2023-02-09 07:24:35 -05:00
|
|
|
let initialized = false
|
|
|
|
|
2020-09-11 15:54:58 -04:00
|
|
|
/**
|
|
|
|
* Configure the metrics module and start the default metrics collectors and
|
|
|
|
* profiling agents.
|
|
|
|
*/
|
2020-10-29 09:49:55 -04:00
|
|
|
function initialize(appName, opts = {}) {
|
2023-02-09 07:24:35 -05:00
|
|
|
if (initialized) {
|
|
|
|
return
|
|
|
|
}
|
2020-10-29 09:49:55 -04:00
|
|
|
appName = appName || DEFAULT_APP_NAME
|
2022-10-31 10:37:49 -04:00
|
|
|
if (tracing.tracingEnabled()) {
|
|
|
|
tracing.initialize(appName)
|
|
|
|
}
|
2020-10-29 09:49:55 -04:00
|
|
|
configure({ ...opts, appName })
|
2020-09-16 16:45:56 -04:00
|
|
|
collectDefaultMetrics({ timeout: 5000, prefix: '' })
|
2020-10-03 13:40:46 -04:00
|
|
|
promWrapper.setupSweeping()
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
console.log(`ENABLE_TRACE_AGENT set to ${process.env.ENABLE_TRACE_AGENT}`)
|
|
|
|
if (process.env.ENABLE_TRACE_AGENT === 'true') {
|
|
|
|
console.log('starting google trace agent')
|
|
|
|
const traceAgent = require('@google-cloud/trace-agent')
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
const traceOpts = { ignoreUrls: [/^\/status/, /^\/health_check/] }
|
|
|
|
traceAgent.start(traceOpts)
|
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
console.log(`ENABLE_DEBUG_AGENT set to ${process.env.ENABLE_DEBUG_AGENT}`)
|
|
|
|
if (process.env.ENABLE_DEBUG_AGENT === 'true') {
|
|
|
|
console.log('starting google debug agent')
|
|
|
|
const debugAgent = require('@google-cloud/debug-agent')
|
|
|
|
debugAgent.start({
|
|
|
|
allowExpressions: true,
|
|
|
|
serviceContext: {
|
2020-10-29 09:49:55 -04:00
|
|
|
service: appName,
|
2021-12-16 04:04:32 -05:00
|
|
|
version: process.env.BUILD_VERSION,
|
|
|
|
},
|
2020-09-11 15:43:08 -04:00
|
|
|
})
|
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
console.log(`ENABLE_PROFILE_AGENT set to ${process.env.ENABLE_PROFILE_AGENT}`)
|
|
|
|
if (process.env.ENABLE_PROFILE_AGENT === 'true') {
|
|
|
|
console.log('starting google profile agent')
|
|
|
|
const profiler = require('@google-cloud/profiler')
|
|
|
|
profiler.start({
|
|
|
|
serviceContext: {
|
2020-10-29 09:49:55 -04:00
|
|
|
service: appName,
|
2021-12-16 04:04:32 -05:00
|
|
|
version: process.env.BUILD_VERSION,
|
|
|
|
},
|
2020-09-11 15:43:08 -04:00
|
|
|
})
|
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2020-09-11 15:43:08 -04:00
|
|
|
inc('process_startup')
|
2023-02-09 07:24:35 -05:00
|
|
|
initialized = true
|
2020-09-11 15:43:08 -04:00
|
|
|
}
|
2020-09-11 14:18:22 -04:00
|
|
|
|
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 {
|
2023-02-09 07:24:35 -05:00
|
|
|
constructor(key, sampleRate = 1, labels = {}, buckets) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
|
|
|
const timeSpan = new Date() - this.start
|
2022-03-01 10:09:55 -05:00
|
|
|
if (this.buckets) {
|
2023-02-09 07:24:35 -05:00
|
|
|
histogram(this.key, timeSpan, this.buckets, this.labels)
|
2022-03-01 10:09:55 -05:00
|
|
|
} else {
|
2023-02-09 07:24:35 -05:00
|
|
|
timing(this.key, timeSpan, this.sampleRate, this.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.configure = configure
|
|
|
|
module.exports.initialize = initialize
|
|
|
|
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')
|
2021-10-27 08:06:50 -04:00
|
|
|
module.exports.timeAsyncMethod = require('./timeAsyncMethod')
|