Decaf cleanup: move functions to top level

This commit is contained in:
Eric Mc Sween 2020-09-11 15:43:08 -04:00
parent afecfd5212
commit 6f69fb4869

View file

@ -1,4 +1,3 @@
let Metrics
console.log('using prometheus')
const ExpressCompression = require('compression')
@ -13,12 +12,9 @@ const destructors = []
require('./uv_threadpool_size')
module.exports = Metrics = {
register: prom.registry,
initialize(_name, opts = {}) {
function initialize(_name, opts = {}) {
appname = _name
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey() })
collectDefaultMetrics({ timeout: 5000, prefix: buildPromKey() })
if (opts.ttlInMinutes) {
prom.ttlInMinutes = opts.ttlInMinutes
}
@ -45,9 +41,7 @@ module.exports = Metrics = {
})
}
console.log(
`ENABLE_PROFILE_AGENT set to ${process.env.ENABLE_PROFILE_AGENT}`
)
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')
@ -59,14 +53,14 @@ module.exports = Metrics = {
})
}
Metrics.inc('process_startup')
},
inc('process_startup')
}
registerDestructor(func) {
function registerDestructor(func) {
destructors.push(func)
},
}
injectMetricsRoute(app) {
function injectMetricsRoute(app) {
app.get(
'/metrics',
ExpressCompression({
@ -77,64 +71,64 @@ module.exports = Metrics = {
res.end(prom.registry.metrics())
}
)
},
}
buildPromKey(key = '') {
function buildPromKey(key = '') {
return key.replace(/[^a-zA-Z0-9]/g, '_')
},
}
sanitizeValue(value) {
function sanitizeValue(value) {
return parseFloat(value)
},
}
set(key, value, sampleRate = 1) {
function set(key, value, sampleRate = 1) {
console.log('counts are not currently supported')
},
}
inc(key, sampleRate = 1, opts = {}) {
key = Metrics.buildPromKey(key)
function inc(key, sampleRate = 1, opts = {}) {
key = buildPromKey(key)
opts.app = appname
opts.host = hostname
prom.metric('counter', key).inc(opts)
if (process.env.DEBUG_METRICS) {
console.log('doing inc', key, opts)
}
},
}
count(key, count, sampleRate = 1, opts = {}) {
key = Metrics.buildPromKey(key)
function count(key, count, sampleRate = 1, opts = {}) {
key = buildPromKey(key)
opts.app = appname
opts.host = hostname
prom.metric('counter', key).inc(opts, count)
if (process.env.DEBUG_METRICS) {
console.log('doing count/inc', key, opts)
}
},
}
summary(key, value, opts = {}) {
key = Metrics.buildPromKey(key)
function summary(key, value, opts = {}) {
key = buildPromKey(key)
opts.app = appname
opts.host = hostname
prom.metric('summary', key).observe(opts, value)
if (process.env.DEBUG_METRICS) {
console.log('doing summary', key, value, opts)
}
},
}
timing(key, timeSpan, sampleRate, opts = {}) {
key = Metrics.buildPromKey('timer_' + key)
function timing(key, timeSpan, sampleRate, opts = {}) {
key = buildPromKey('timer_' + key)
opts.app = appname
opts.host = hostname
prom.metric('summary', key).observe(opts, timeSpan)
if (process.env.DEBUG_METRICS) {
console.log('doing timing', key, opts)
}
},
}
Timer: class {
class Timer {
constructor(key, sampleRate = 1, opts = {}) {
this.start = new Date()
key = Metrics.buildPromKey(key)
key = buildPromKey(key)
this.key = key
this.sampleRate = sampleRate
this.opts = opts
@ -142,13 +136,13 @@ module.exports = Metrics = {
done() {
const timeSpan = new Date() - this.start
Metrics.timing(this.key, timeSpan, this.sampleRate, this.opts)
timing(this.key, timeSpan, this.sampleRate, this.opts)
return timeSpan
}
},
}
gauge(key, value, sampleRate = 1, opts = {}) {
key = Metrics.buildPromKey(key)
function gauge(key, value, sampleRate = 1, opts = {}) {
key = buildPromKey(key)
prom.metric('gauge', key).set(
{
app: appname,
@ -160,26 +154,43 @@ module.exports = Metrics = {
if (process.env.DEBUG_METRICS) {
console.log('doing gauge', key, opts)
}
},
}
globalGauge(key, value, sampleRate = 1, opts = {}) {
key = Metrics.buildPromKey(key)
function globalGauge(key, value, sampleRate = 1, opts = {}) {
key = buildPromKey(key)
prom
.metric('gauge', key)
.set({ app: appname, status: opts.status }, this.sanitizeValue(value))
},
}
function close() {
for (const func of destructors) {
func()
}
}
module.exports = {
initialize,
registerDestructor,
injectMetricsRoute,
buildPromKey,
sanitizeValue,
set,
inc,
count,
summary,
timing,
Timer,
gauge,
globalGauge,
close,
register: prom.registry,
mongodb: require('./mongodb'),
http: require('./http'),
open_sockets: require('./open_sockets'),
event_loop: require('./event_loop'),
memory: require('./memory'),
timeAsyncMethod: require('./timeAsyncMethod'),
close() {
for (const func of destructors) {
func()
}
}
timeAsyncMethod: require('./timeAsyncMethod')
}