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') console.log('using prometheus')
const ExpressCompression = require('compression') const ExpressCompression = require('compression')
@ -13,173 +12,185 @@ const destructors = []
require('./uv_threadpool_size') require('./uv_threadpool_size')
module.exports = Metrics = { function initialize(_name, opts = {}) {
register: prom.registry, appname = _name
collectDefaultMetrics({ timeout: 5000, prefix: buildPromKey() })
if (opts.ttlInMinutes) {
prom.ttlInMinutes = opts.ttlInMinutes
}
initialize(_name, opts = {}) { console.log(`ENABLE_TRACE_AGENT set to ${process.env.ENABLE_TRACE_AGENT}`)
appname = _name if (process.env.ENABLE_TRACE_AGENT === 'true') {
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey() }) console.log('starting google trace agent')
if (opts.ttlInMinutes) { const traceAgent = require('@google-cloud/trace-agent')
prom.ttlInMinutes = opts.ttlInMinutes
}
console.log(`ENABLE_TRACE_AGENT set to ${process.env.ENABLE_TRACE_AGENT}`) const traceOpts = { ignoreUrls: [/^\/status/, /^\/health_check/] }
if (process.env.ENABLE_TRACE_AGENT === 'true') { traceAgent.start(traceOpts)
console.log('starting google trace agent') }
const traceAgent = require('@google-cloud/trace-agent')
const traceOpts = { ignoreUrls: [/^\/status/, /^\/health_check/] } console.log(`ENABLE_DEBUG_AGENT set to ${process.env.ENABLE_DEBUG_AGENT}`)
traceAgent.start(traceOpts) if (process.env.ENABLE_DEBUG_AGENT === 'true') {
} console.log('starting google debug agent')
const debugAgent = require('@google-cloud/debug-agent')
console.log(`ENABLE_DEBUG_AGENT set to ${process.env.ENABLE_DEBUG_AGENT}`) debugAgent.start({
if (process.env.ENABLE_DEBUG_AGENT === 'true') { allowExpressions: true,
console.log('starting google debug agent') serviceContext: {
const debugAgent = require('@google-cloud/debug-agent') service: appname,
debugAgent.start({ version: process.env.BUILD_VERSION
allowExpressions: true,
serviceContext: {
service: appname,
version: process.env.BUILD_VERSION
}
})
}
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: {
service: appname,
version: process.env.BUILD_VERSION
}
})
}
Metrics.inc('process_startup')
},
registerDestructor(func) {
destructors.push(func)
},
injectMetricsRoute(app) {
app.get(
'/metrics',
ExpressCompression({
level: parseInt(process.env.METRICS_COMPRESSION_LEVEL || '1', 10)
}),
function(req, res) {
res.set('Content-Type', prom.registry.contentType)
res.end(prom.registry.metrics())
} }
) })
}, }
buildPromKey(key = '') { console.log(`ENABLE_PROFILE_AGENT set to ${process.env.ENABLE_PROFILE_AGENT}`)
return key.replace(/[^a-zA-Z0-9]/g, '_') if (process.env.ENABLE_PROFILE_AGENT === 'true') {
}, console.log('starting google profile agent')
const profiler = require('@google-cloud/profiler')
profiler.start({
serviceContext: {
service: appname,
version: process.env.BUILD_VERSION
}
})
}
sanitizeValue(value) { inc('process_startup')
return parseFloat(value) }
},
set(key, value, sampleRate = 1) { function registerDestructor(func) {
console.log('counts are not currently supported') destructors.push(func)
}, }
inc(key, sampleRate = 1, opts = {}) { function injectMetricsRoute(app) {
key = Metrics.buildPromKey(key) app.get(
opts.app = appname '/metrics',
opts.host = hostname ExpressCompression({
prom.metric('counter', key).inc(opts) level: parseInt(process.env.METRICS_COMPRESSION_LEVEL || '1', 10)
if (process.env.DEBUG_METRICS) { }),
console.log('doing inc', key, opts) function(req, res) {
res.set('Content-Type', prom.registry.contentType)
res.end(prom.registry.metrics())
} }
}, )
}
count(key, count, sampleRate = 1, opts = {}) { function buildPromKey(key = '') {
key = Metrics.buildPromKey(key) return key.replace(/[^a-zA-Z0-9]/g, '_')
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 = {}) { function sanitizeValue(value) {
key = Metrics.buildPromKey(key) return parseFloat(value)
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 = {}) { function set(key, value, sampleRate = 1) {
key = Metrics.buildPromKey('timer_' + key) console.log('counts are not currently supported')
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 { function inc(key, sampleRate = 1, opts = {}) {
constructor(key, sampleRate = 1, opts = {}) { key = buildPromKey(key)
this.start = new Date() opts.app = appname
key = Metrics.buildPromKey(key) opts.host = hostname
this.key = key prom.metric('counter', key).inc(opts)
this.sampleRate = sampleRate if (process.env.DEBUG_METRICS) {
this.opts = opts console.log('doing inc', key, opts)
} }
}
done() { function count(key, count, sampleRate = 1, opts = {}) {
const timeSpan = new Date() - this.start key = buildPromKey(key)
Metrics.timing(this.key, timeSpan, this.sampleRate, this.opts) opts.app = appname
return timeSpan opts.host = hostname
} prom.metric('counter', key).inc(opts, count)
}, if (process.env.DEBUG_METRICS) {
console.log('doing count/inc', key, opts)
}
}
gauge(key, value, sampleRate = 1, opts = {}) { function summary(key, value, opts = {}) {
key = Metrics.buildPromKey(key) key = buildPromKey(key)
prom.metric('gauge', key).set( opts.app = appname
{ opts.host = hostname
app: appname, prom.metric('summary', key).observe(opts, value)
host: hostname, if (process.env.DEBUG_METRICS) {
status: opts.status console.log('doing summary', key, value, opts)
}, }
this.sanitizeValue(value) }
)
if (process.env.DEBUG_METRICS) {
console.log('doing gauge', key, opts)
}
},
globalGauge(key, value, sampleRate = 1, opts = {}) { function timing(key, timeSpan, sampleRate, opts = {}) {
key = Metrics.buildPromKey(key) key = buildPromKey('timer_' + key)
prom opts.app = appname
.metric('gauge', key) opts.host = hostname
.set({ app: appname, status: opts.status }, this.sanitizeValue(value)) prom.metric('summary', key).observe(opts, timeSpan)
}, if (process.env.DEBUG_METRICS) {
console.log('doing timing', key, opts)
}
}
class Timer {
constructor(key, sampleRate = 1, opts = {}) {
this.start = new Date()
key = buildPromKey(key)
this.key = key
this.sampleRate = sampleRate
this.opts = opts
}
done() {
const timeSpan = new Date() - this.start
timing(this.key, timeSpan, this.sampleRate, this.opts)
return timeSpan
}
}
function gauge(key, value, sampleRate = 1, opts = {}) {
key = buildPromKey(key)
prom.metric('gauge', key).set(
{
app: appname,
host: hostname,
status: opts.status
},
this.sanitizeValue(value)
)
if (process.env.DEBUG_METRICS) {
console.log('doing gauge', key, opts)
}
}
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'), mongodb: require('./mongodb'),
http: require('./http'), http: require('./http'),
open_sockets: require('./open_sockets'), open_sockets: require('./open_sockets'),
event_loop: require('./event_loop'), event_loop: require('./event_loop'),
memory: require('./memory'), memory: require('./memory'),
timeAsyncMethod: require('./timeAsyncMethod')
timeAsyncMethod: require('./timeAsyncMethod'),
close() {
for (const func of destructors) {
func()
}
}
} }