2020-07-17 10:36:37 -04:00
|
|
|
console.log("using prometheus")
|
2019-01-30 05:29:12 -05:00
|
|
|
|
2019-10-23 12:07:23 -04:00
|
|
|
prom = require('./prom_wrapper')
|
2019-01-15 10:36:35 -05:00
|
|
|
|
2018-11-06 06:14:26 -05:00
|
|
|
collectDefaultMetrics = prom.collectDefaultMetrics
|
|
|
|
|
2018-11-27 05:01:33 -05:00
|
|
|
appname = "unknown"
|
2014-05-07 05:58:52 -04:00
|
|
|
hostname = require('os').hostname()
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2015-01-05 11:45:32 -05:00
|
|
|
destructors = []
|
|
|
|
|
2016-10-24 05:50:44 -04:00
|
|
|
require "./uv_threadpool_size"
|
|
|
|
|
2017-03-15 11:06:54 -04:00
|
|
|
module.exports = Metrics =
|
2019-10-23 12:07:23 -04:00
|
|
|
register: prom.registry
|
|
|
|
|
|
|
|
initialize: (_name, opts = {}) ->
|
2018-11-27 05:01:33 -05:00
|
|
|
appname = _name
|
2018-11-21 05:01:03 -05:00
|
|
|
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()})
|
2019-10-23 12:07:23 -04:00
|
|
|
if opts.ttlInMinutes
|
|
|
|
prom.ttlInMinutes = opts.ttlInMinutes
|
2018-12-11 11:07:34 -05:00
|
|
|
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("ENABLE_TRACE_AGENT set to #{process.env['ENABLE_TRACE_AGENT']}")
|
2018-12-05 08:57:53 -05:00
|
|
|
if process.env['ENABLE_TRACE_AGENT'] == "true"
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("starting google trace agent")
|
2018-12-11 10:46:29 -05:00
|
|
|
traceAgent = require('@google-cloud/trace-agent')
|
|
|
|
|
2018-12-10 17:02:25 -05:00
|
|
|
traceOpts =
|
2020-07-17 10:36:37 -04:00
|
|
|
ignoreUrls: [/^\/status/, /^\/health_check/]
|
2018-12-10 17:02:25 -05:00
|
|
|
traceAgent.start(traceOpts)
|
2018-12-11 10:46:29 -05:00
|
|
|
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("ENABLE_DEBUG_AGENT set to #{process.env['ENABLE_DEBUG_AGENT']}")
|
2018-12-11 10:46:29 -05:00
|
|
|
if process.env['ENABLE_DEBUG_AGENT'] == "true"
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("starting google debug agent")
|
2018-12-11 10:46:29 -05:00
|
|
|
debugAgent = require('@google-cloud/debug-agent')
|
|
|
|
debugAgent.start({
|
2018-12-12 15:11:40 -05:00
|
|
|
allowExpressions: true,
|
|
|
|
serviceContext: {
|
|
|
|
service: appname,
|
|
|
|
version: process.env['BUILD_VERSION']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("ENABLE_PROFILE_AGENT set to #{process.env['ENABLE_PROFILE_AGENT']}")
|
2018-12-12 16:17:12 -05:00
|
|
|
if process.env['ENABLE_PROFILE_AGENT'] == "true"
|
2019-04-12 07:34:17 -04:00
|
|
|
console.log("starting google profile agent")
|
2018-12-12 15:11:40 -05:00
|
|
|
profiler = require('@google-cloud/profiler')
|
|
|
|
profiler.start({
|
2018-12-11 10:46:29 -05:00
|
|
|
serviceContext: {
|
|
|
|
service: appname,
|
|
|
|
version: process.env['BUILD_VERSION']
|
|
|
|
}
|
|
|
|
})
|
2018-12-12 16:05:33 -05:00
|
|
|
|
2018-12-04 10:57:19 -05:00
|
|
|
Metrics.inc("process_startup")
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2015-01-05 11:45:32 -05:00
|
|
|
registerDestructor: (func) ->
|
|
|
|
destructors.push func
|
|
|
|
|
2018-11-06 06:14:26 -05:00
|
|
|
injectMetricsRoute: (app) ->
|
2020-07-17 10:36:37 -04:00
|
|
|
app.get('/metrics', (req, res) ->
|
2019-10-23 12:07:23 -04:00
|
|
|
res.set('Content-Type', prom.registry.contentType)
|
|
|
|
res.end(prom.registry.metrics())
|
2018-11-06 06:14:26 -05:00
|
|
|
)
|
|
|
|
|
2018-11-21 05:01:03 -05:00
|
|
|
buildPromKey: (key = "")->
|
2018-11-07 07:44:10 -05:00
|
|
|
key.replace /[^a-zA-Z0-9]/g, "_"
|
|
|
|
|
2018-11-07 11:08:31 -05:00
|
|
|
sanitizeValue: (value) ->
|
|
|
|
parseFloat(value)
|
|
|
|
|
2014-05-06 11:52:03 -04:00
|
|
|
set : (key, value, sampleRate = 1)->
|
2018-12-05 06:03:40 -05:00
|
|
|
console.log("counts are not currently supported")
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2018-11-27 11:12:12 -05:00
|
|
|
inc : (key, sampleRate = 1, opts = {})->
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2018-11-27 06:33:45 -05:00
|
|
|
opts.app = appname
|
|
|
|
opts.host = hostname
|
2019-10-23 12:07:23 -04:00
|
|
|
prom.metric('counter', key).inc(opts)
|
2018-12-04 11:20:52 -05:00
|
|
|
if process.env['DEBUG_METRICS']
|
|
|
|
console.log("doing inc", key, opts)
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2019-12-16 05:18:30 -05:00
|
|
|
count : (key, count, sampleRate = 1, opts = {})->
|
2018-11-21 05:28:32 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2019-12-16 05:18:30 -05:00
|
|
|
opts.app = appname
|
|
|
|
opts.host = hostname
|
|
|
|
prom.metric('counter', key).inc(opts, count)
|
2018-12-04 11:20:52 -05:00
|
|
|
if process.env['DEBUG_METRICS']
|
|
|
|
console.log("doing count/inc", key, opts)
|
2016-03-15 09:52:32 -04:00
|
|
|
|
2020-03-11 16:25:33 -04:00
|
|
|
summary : (key, value, opts = {})->
|
|
|
|
key = Metrics.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)
|
|
|
|
|
2018-11-23 11:24:31 -05:00
|
|
|
timing: (key, timeSpan, sampleRate, opts = {})->
|
2018-12-05 06:03:40 -05:00
|
|
|
key = Metrics.buildPromKey("timer_" + key)
|
2018-11-27 05:01:33 -05:00
|
|
|
opts.app = appname
|
2018-12-11 07:01:22 -05:00
|
|
|
opts.host = hostname
|
2019-10-23 12:07:23 -04:00
|
|
|
prom.metric('summary', key).observe(opts, timeSpan)
|
2018-12-04 11:20:52 -05:00
|
|
|
if process.env['DEBUG_METRICS']
|
|
|
|
console.log("doing timing", key, opts)
|
2014-05-06 11:52:03 -04:00
|
|
|
|
|
|
|
Timer : class
|
2018-11-26 06:08:29 -05:00
|
|
|
constructor :(key, sampleRate = 1, opts)->
|
2014-05-06 11:52:03 -04:00
|
|
|
this.start = new Date()
|
2018-12-05 06:03:40 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2014-05-07 06:08:46 -04:00
|
|
|
this.key = key
|
2014-05-06 11:52:03 -04:00
|
|
|
this.sampleRate = sampleRate
|
2018-11-26 06:08:29 -05:00
|
|
|
this.opts = opts
|
2018-11-20 12:20:49 -05:00
|
|
|
|
2014-05-06 11:52:03 -04:00
|
|
|
done:->
|
|
|
|
timeSpan = new Date - this.start
|
2018-11-26 06:08:29 -05:00
|
|
|
Metrics.timing(this.key, timeSpan, this.sampleRate, this.opts)
|
2016-03-15 09:52:40 -04:00
|
|
|
return timeSpan
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2019-06-05 11:38:25 -04:00
|
|
|
gauge : (key, value, sampleRate = 1, opts)->
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2019-10-28 08:34:04 -04:00
|
|
|
prom.metric('gauge', key).set({app: appname, host: hostname, status: opts?.status}, this.sanitizeValue(value))
|
2018-12-04 11:20:52 -05:00
|
|
|
if process.env['DEBUG_METRICS']
|
|
|
|
console.log("doing gauge", key, opts)
|
2020-07-17 10:36:37 -04:00
|
|
|
|
2019-06-05 11:38:25 -04:00
|
|
|
globalGauge: (key, value, sampleRate = 1, opts)->
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2019-10-28 08:34:04 -04:00
|
|
|
prom.metric('gauge', key).set({app: appname, status: opts?.status},this.sanitizeValue(value))
|
2018-05-18 10:09:11 -04:00
|
|
|
|
2014-05-06 11:52:03 -04:00
|
|
|
mongodb: require "./mongodb"
|
2014-05-07 05:58:52 -04:00
|
|
|
http: require "./http"
|
2014-05-09 08:30:12 -04:00
|
|
|
open_sockets: require "./open_sockets"
|
2015-06-23 05:51:48 -04:00
|
|
|
event_loop: require "./event_loop"
|
2015-08-13 11:13:09 -04:00
|
|
|
memory: require "./memory"
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2017-03-15 11:06:54 -04:00
|
|
|
timeAsyncMethod: require('./timeAsyncMethod')
|
|
|
|
|
2015-01-05 11:45:32 -05:00
|
|
|
close: () ->
|
|
|
|
for func in destructors
|
|
|
|
func()
|