overleaf/libraries/metrics/metrics.coffee

76 lines
2 KiB
CoffeeScript
Raw Normal View History

2014-05-06 11:52:03 -04:00
StatsD = require('lynx')
statsd = new StatsD(process.env["STATSD_HOST"] or "localhost", 8125, {on_error:->})
2014-05-06 11:52:03 -04:00
2018-10-16 11:47:12 -04:00
prom = require('prom-client')
collectDefaultMetrics = client.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}"
buildGlobalKey = (key)-> "#{name}.global.#{key}"
2014-05-06 11:52:03 -04:00
destructors = []
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) ->
name = _name
2018-10-16 11:47:12 -04:00
collectDefaultMetrics({ timeout: 5000, prefix: name })
2014-05-06 11:52:03 -04:00
registerDestructor: (func) ->
destructors.push func
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-10-16 11:47:12 -04:00
counter = new prom.Counter({
name: key,
help: key #https://prometheus.io/docs/instrumenting/writing_exporters/#help-strings this is probably wrong
});
counter.inc()
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-10-16 11:47:12 -04:00
counter = new prom.Counter({
name: key,
help: key #https://prometheus.io/docs/instrumenting/writing_exporters/#help-strings this is probably wrong
});
counter.inc(count)
2016-03-15 09:52:32 -04:00
2014-05-06 11:52:03 -04:00
timing: (key, timeSpan, sampleRate)->
2014-05-07 06:08:46 -04:00
statsd.timing(buildKey(key), timeSpan, sampleRate)
2014-05-06 11:52:03 -04:00
Timer : class
constructor :(key, sampleRate = 1)->
this.start = new Date()
2014-05-07 06:08:46 -04:00
this.key = key
2014-05-06 11:52:03 -04:00
this.sampleRate = sampleRate
done:->
timeSpan = new Date - this.start
2014-05-07 06:43:46 -04:00
statsd.timing(buildKey(this.key), timeSpan, this.sampleRate)
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
2014-05-06 11:52:03 -04:00
globalGauge: (key, value, sampleRate = 1)->
statsd.gauge buildGlobalKey(key), value, sampleRate
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"
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')
close: () ->
for func in destructors
func()
statsd.close()