2014-05-06 11:52:03 -04:00
|
|
|
StatsD = require('lynx')
|
2018-07-06 08:57:52 -04:00
|
|
|
statsd = new StatsD(process.env["STATSD_HOST"] or "localhost", 8125, {on_error:->})
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2018-11-21 05:30:18 -05:00
|
|
|
traceAgent = require('@google-cloud/trace-agent')
|
|
|
|
debugAgent = require('@google-cloud/debug-agent')
|
|
|
|
|
2018-11-06 06:14:26 -05:00
|
|
|
prom = require('prom-client')
|
|
|
|
Register = require('prom-client').register
|
|
|
|
collectDefaultMetrics = prom.collectDefaultMetrics
|
|
|
|
|
2014-05-06 11:52:03 -04:00
|
|
|
name = "unknown"
|
2014-05-07 05:58:52 -04:00
|
|
|
hostname = require('os').hostname()
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2014-05-07 05:58:52 -04:00
|
|
|
buildKey = (key)-> "#{name}.#{hostname}.#{key}"
|
2018-05-18 10:09:11 -04:00
|
|
|
buildGlobalKey = (key)-> "#{name}.global.#{key}"
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2018-11-21 05:01:03 -05:00
|
|
|
|
|
|
|
|
2018-11-20 12:50:54 -05:00
|
|
|
promMetrics = {}
|
2018-11-06 09:22:03 -05: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 =
|
2014-05-06 12:33:09 -04:00
|
|
|
initialize: (_name) ->
|
2018-11-20 11:28:36 -05:00
|
|
|
name = _name
|
2018-11-21 05:01:03 -05:00
|
|
|
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()})
|
2018-10-16 12:19:21 -04:00
|
|
|
traceAgent.start()
|
|
|
|
debugAgent.start({
|
|
|
|
serviceContext: {
|
|
|
|
allowExpressions: true,
|
|
|
|
service: name,
|
|
|
|
version: '0.0.1'
|
|
|
|
}
|
|
|
|
})
|
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) ->
|
|
|
|
app.get('/metrics', (req, res) ->
|
|
|
|
res.set('Content-Type', Register.contentType)
|
|
|
|
res.end(Register.metrics())
|
|
|
|
)
|
|
|
|
|
2018-11-21 05:01:03 -05:00
|
|
|
buildPromKey: (key = "")->
|
|
|
|
Metrics.sanitizeKey "#{name}_#{key}"
|
|
|
|
|
2018-11-07 07:44:10 -05:00
|
|
|
sanitizeKey: (key) ->
|
|
|
|
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)->
|
|
|
|
statsd.set buildKey(key), value, sampleRate
|
|
|
|
|
|
|
|
inc : (key, sampleRate = 1)->
|
|
|
|
statsd.increment buildKey(key), sampleRate
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2018-11-20 12:50:54 -05:00
|
|
|
if !promMetrics[key]?
|
|
|
|
promMetrics[key] = new prom.Counter({
|
2018-11-21 05:01:03 -05:00
|
|
|
name: key,
|
2018-11-06 11:15:41 -05:00
|
|
|
help: key,
|
2018-11-06 09:22:03 -05:00
|
|
|
labelNames: ['name','host']
|
|
|
|
})
|
2018-11-20 12:50:54 -05:00
|
|
|
promMetrics[key].inc({name: name, host: hostname})
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2016-03-15 09:52:32 -04:00
|
|
|
count : (key, count, sampleRate = 1)->
|
|
|
|
statsd.count buildKey(key), count, sampleRate
|
2018-11-21 05:28:32 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
|
|
|
if !promMetrics[key]?
|
|
|
|
promMetrics[key] = new prom.Counter({
|
|
|
|
name: key,
|
|
|
|
help: key,
|
|
|
|
labelNames: ['name','host']
|
|
|
|
})
|
|
|
|
promMetrics[key].inc({name: name, host: hostname}, count)
|
2016-03-15 09:52:32 -04:00
|
|
|
|
2018-11-23 11:24:31 -05:00
|
|
|
timing: (key, timeSpan, sampleRate, opts = {})->
|
2014-05-07 06:08:46 -04:00
|
|
|
statsd.timing(buildKey(key), timeSpan, sampleRate)
|
2018-11-23 11:24:31 -05:00
|
|
|
key = Metrics.sanitizeKey("timer_" + key)
|
2018-11-21 05:20:33 -05:00
|
|
|
if !promMetrics[key]?
|
2018-11-21 03:50:33 -05:00
|
|
|
promMetrics[key] = new prom.Summary({
|
2018-11-21 05:01:03 -05:00
|
|
|
name: key,
|
2018-11-20 16:42:34 -05:00
|
|
|
help: key,
|
|
|
|
maxAgeSeconds: 600,
|
2018-11-23 11:24:31 -05:00
|
|
|
ageBuckets: 10,
|
2018-11-26 06:08:29 -05:00
|
|
|
labelNames: ['app', 'path', 'status_code', 'method', 'collection', 'query']
|
2018-11-20 16:42:34 -05:00
|
|
|
})
|
2018-11-23 11:24:31 -05:00
|
|
|
opts.app = name
|
|
|
|
promMetrics[key].observe(opts, timeSpan)
|
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-11-21 05:01:03 -05:00
|
|
|
key = Metrics.sanitizeKey(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
|
|
|
|
|
|
|
gauge : (key, value, sampleRate = 1)->
|
2014-05-09 08:54:33 -04:00
|
|
|
statsd.gauge buildKey(key), value, sampleRate
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2018-11-21 05:20:33 -05:00
|
|
|
if !promMetrics[key]?
|
2018-11-20 12:50:54 -05:00
|
|
|
promMetrics[key] = new prom.Gauge({
|
2018-11-21 05:01:03 -05:00
|
|
|
name: key,
|
2018-11-06 11:15:41 -05:00
|
|
|
help: key,
|
2018-11-23 11:24:31 -05:00
|
|
|
labelNames: ['app','host']
|
2018-11-06 11:15:41 -05:00
|
|
|
})
|
2018-11-23 11:24:31 -05:00
|
|
|
promMetrics[key].set({app: name, host: hostname}, this.sanitizeValue(value))
|
2014-05-06 11:52:03 -04:00
|
|
|
|
2018-05-18 10:09:11 -04:00
|
|
|
globalGauge: (key, value, sampleRate = 1)->
|
|
|
|
statsd.gauge buildGlobalKey(key), value, sampleRate
|
2018-11-21 05:01:03 -05:00
|
|
|
key = Metrics.buildPromKey(key)
|
2018-11-21 05:20:33 -05:00
|
|
|
if !promMetrics[key]?
|
2018-11-20 12:50:54 -05:00
|
|
|
promMetrics[key] = new prom.Gauge({
|
2018-11-21 05:01:03 -05:00
|
|
|
name: key,
|
2018-11-06 11:15:41 -05:00
|
|
|
help: key,
|
2018-11-23 11:24:31 -05:00
|
|
|
labelNames: ['app','host']
|
2018-11-06 11:15:41 -05:00
|
|
|
})
|
2018-11-23 11:24:31 -05:00
|
|
|
promMetrics[key].set({app: name},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()
|
|
|
|
statsd.close()
|