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