mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
big refactor remoing statsd and converting keys to prom keys
This commit is contained in:
parent
b9f3a3f987
commit
fdd4db25a3
2 changed files with 23 additions and 23 deletions
|
@ -1,6 +1,3 @@
|
||||||
StatsD = require('lynx')
|
|
||||||
statsd = new StatsD(process.env["STATSD_HOST"] or "localhost", 8125, {on_error:->})
|
|
||||||
|
|
||||||
traceAgent = require('@google-cloud/trace-agent')
|
traceAgent = require('@google-cloud/trace-agent')
|
||||||
debugAgent = require('@google-cloud/debug-agent')
|
debugAgent = require('@google-cloud/debug-agent')
|
||||||
|
|
||||||
|
@ -8,7 +5,7 @@ prom = require('prom-client')
|
||||||
Register = require('prom-client').register
|
Register = require('prom-client').register
|
||||||
collectDefaultMetrics = prom.collectDefaultMetrics
|
collectDefaultMetrics = prom.collectDefaultMetrics
|
||||||
|
|
||||||
name = "unknown"
|
appname = "unknown"
|
||||||
hostname = require('os').hostname()
|
hostname = require('os').hostname()
|
||||||
|
|
||||||
buildKey = (key)-> "#{name}.#{hostname}.#{key}"
|
buildKey = (key)-> "#{name}.#{hostname}.#{key}"
|
||||||
|
@ -24,13 +21,13 @@ require "./uv_threadpool_size"
|
||||||
|
|
||||||
module.exports = Metrics =
|
module.exports = Metrics =
|
||||||
initialize: (_name) ->
|
initialize: (_name) ->
|
||||||
name = _name
|
appname = _name
|
||||||
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()})
|
collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()})
|
||||||
traceAgent.start()
|
traceAgent.start()
|
||||||
debugAgent.start({
|
debugAgent.start({
|
||||||
serviceContext: {
|
serviceContext: {
|
||||||
allowExpressions: true,
|
allowExpressions: true,
|
||||||
service: name,
|
service: appname,
|
||||||
version: '0.0.1'
|
version: '0.0.1'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -45,7 +42,7 @@ module.exports = Metrics =
|
||||||
)
|
)
|
||||||
|
|
||||||
buildPromKey: (key = "")->
|
buildPromKey: (key = "")->
|
||||||
Metrics.sanitizeKey "#{name}_#{key}"
|
Metrics.sanitizeKey key
|
||||||
|
|
||||||
sanitizeKey: (key) ->
|
sanitizeKey: (key) ->
|
||||||
key.replace /[^a-zA-Z0-9]/g, "_"
|
key.replace /[^a-zA-Z0-9]/g, "_"
|
||||||
|
@ -54,32 +51,29 @@ module.exports = Metrics =
|
||||||
parseFloat(value)
|
parseFloat(value)
|
||||||
|
|
||||||
set : (key, value, sampleRate = 1)->
|
set : (key, value, sampleRate = 1)->
|
||||||
statsd.set buildKey(key), value, sampleRate
|
|
||||||
|
|
||||||
inc : (key, sampleRate = 1)->
|
inc : (key, sampleRate = 1)->
|
||||||
statsd.increment buildKey(key), sampleRate
|
|
||||||
key = Metrics.buildPromKey(key)
|
key = Metrics.buildPromKey(key)
|
||||||
if !promMetrics[key]?
|
if !promMetrics[key]?
|
||||||
promMetrics[key] = new prom.Counter({
|
promMetrics[key] = new prom.Counter({
|
||||||
name: key,
|
name: key,
|
||||||
help: key,
|
help: key,
|
||||||
labelNames: ['name','host']
|
labelNames: ['app','host']
|
||||||
})
|
})
|
||||||
promMetrics[key].inc({name: name, host: hostname})
|
console.log("doing inc", key, appname)
|
||||||
|
promMetrics[key].inc({app: appname, host: hostname})
|
||||||
|
|
||||||
count : (key, count, sampleRate = 1)->
|
count : (key, count, sampleRate = 1)->
|
||||||
statsd.count buildKey(key), count, sampleRate
|
|
||||||
key = Metrics.buildPromKey(key)
|
key = Metrics.buildPromKey(key)
|
||||||
if !promMetrics[key]?
|
if !promMetrics[key]?
|
||||||
promMetrics[key] = new prom.Counter({
|
promMetrics[key] = new prom.Counter({
|
||||||
name: key,
|
name: key,
|
||||||
help: key,
|
help: key,
|
||||||
labelNames: ['name','host']
|
labelNames: ['app','host']
|
||||||
})
|
})
|
||||||
promMetrics[key].inc({name: name, host: hostname}, count)
|
promMetrics[key].inc({app: appname, host: hostname}, count)
|
||||||
|
|
||||||
timing: (key, timeSpan, sampleRate, opts = {})->
|
timing: (key, timeSpan, sampleRate, opts = {})->
|
||||||
statsd.timing(buildKey(key), timeSpan, sampleRate)
|
|
||||||
key = Metrics.sanitizeKey("timer_" + key)
|
key = Metrics.sanitizeKey("timer_" + key)
|
||||||
if !promMetrics[key]?
|
if !promMetrics[key]?
|
||||||
promMetrics[key] = new prom.Summary({
|
promMetrics[key] = new prom.Summary({
|
||||||
|
@ -89,12 +83,14 @@ module.exports = Metrics =
|
||||||
ageBuckets: 10,
|
ageBuckets: 10,
|
||||||
labelNames: ['app', 'path', 'status_code', 'method', 'collection', 'query']
|
labelNames: ['app', 'path', 'status_code', 'method', 'collection', 'query']
|
||||||
})
|
})
|
||||||
opts.app = name
|
opts.app = appname
|
||||||
|
console.log("doing timing", key, opts)
|
||||||
promMetrics[key].observe(opts, timeSpan)
|
promMetrics[key].observe(opts, timeSpan)
|
||||||
|
|
||||||
Timer : class
|
Timer : class
|
||||||
constructor :(key, sampleRate = 1, opts)->
|
constructor :(key, sampleRate = 1, opts)->
|
||||||
this.start = new Date()
|
this.start = new Date()
|
||||||
|
console.log("creating new timer", key)
|
||||||
key = Metrics.sanitizeKey(key)
|
key = Metrics.sanitizeKey(key)
|
||||||
this.key = key
|
this.key = key
|
||||||
this.sampleRate = sampleRate
|
this.sampleRate = sampleRate
|
||||||
|
@ -106,7 +102,6 @@ module.exports = Metrics =
|
||||||
return timeSpan
|
return timeSpan
|
||||||
|
|
||||||
gauge : (key, value, sampleRate = 1)->
|
gauge : (key, value, sampleRate = 1)->
|
||||||
statsd.gauge buildKey(key), value, sampleRate
|
|
||||||
key = Metrics.buildPromKey(key)
|
key = Metrics.buildPromKey(key)
|
||||||
if !promMetrics[key]?
|
if !promMetrics[key]?
|
||||||
promMetrics[key] = new prom.Gauge({
|
promMetrics[key] = new prom.Gauge({
|
||||||
|
@ -114,10 +109,9 @@ module.exports = Metrics =
|
||||||
help: key,
|
help: key,
|
||||||
labelNames: ['app','host']
|
labelNames: ['app','host']
|
||||||
})
|
})
|
||||||
promMetrics[key].set({app: name, host: hostname}, this.sanitizeValue(value))
|
promMetrics[key].set({app: appname, host: hostname}, this.sanitizeValue(value))
|
||||||
|
|
||||||
globalGauge: (key, value, sampleRate = 1)->
|
globalGauge: (key, value, sampleRate = 1)->
|
||||||
statsd.gauge buildGlobalKey(key), value, sampleRate
|
|
||||||
key = Metrics.buildPromKey(key)
|
key = Metrics.buildPromKey(key)
|
||||||
if !promMetrics[key]?
|
if !promMetrics[key]?
|
||||||
promMetrics[key] = new prom.Gauge({
|
promMetrics[key] = new prom.Gauge({
|
||||||
|
@ -125,7 +119,7 @@ module.exports = Metrics =
|
||||||
help: key,
|
help: key,
|
||||||
labelNames: ['app','host']
|
labelNames: ['app','host']
|
||||||
})
|
})
|
||||||
promMetrics[key].set({app: name},this.sanitizeValue(value))
|
promMetrics[key].set({app: appname},this.sanitizeValue(value))
|
||||||
|
|
||||||
mongodb: require "./mongodb"
|
mongodb: require "./mongodb"
|
||||||
http: require "./http"
|
http: require "./http"
|
||||||
|
@ -138,4 +132,3 @@ module.exports = Metrics =
|
||||||
close: () ->
|
close: () ->
|
||||||
for func in destructors
|
for func in destructors
|
||||||
func()
|
func()
|
||||||
statsd.close()
|
|
||||||
|
|
|
@ -6,8 +6,14 @@ module.exports = (obj, methodName, prefix, logger) ->
|
||||||
throw new Error("[Metrics] expected object property '#{methodName}' to be a function")
|
throw new Error("[Metrics] expected object property '#{methodName}' to be a function")
|
||||||
|
|
||||||
realMethod = obj[methodName]
|
realMethod = obj[methodName]
|
||||||
key = "#{prefix}.#{methodName}"
|
keys = prefix.split(".")
|
||||||
|
key = keys[0].toLowerCase()
|
||||||
|
|
||||||
|
|
||||||
|
if keys[1]?
|
||||||
|
methodName = "#{keys[1]}_#{methodName}"
|
||||||
|
|
||||||
|
console.log "Async method", keys, methodName
|
||||||
obj[methodName] = (originalArgs...) ->
|
obj[methodName] = (originalArgs...) ->
|
||||||
|
|
||||||
[firstArgs..., callback] = originalArgs
|
[firstArgs..., callback] = originalArgs
|
||||||
|
@ -17,7 +23,8 @@ module.exports = (obj, methodName, prefix, logger) ->
|
||||||
logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
|
logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
|
||||||
return realMethod.apply this, originalArgs
|
return realMethod.apply this, originalArgs
|
||||||
|
|
||||||
timer = new metrics.Timer(prefix, null, {method: methodName})
|
console.log("creating timer for async method")
|
||||||
|
timer = new metrics.Timer(key, null, {method: methodName})
|
||||||
|
|
||||||
realMethod.call this, firstArgs..., (callbackArgs...) ->
|
realMethod.call this, firstArgs..., (callbackArgs...) ->
|
||||||
elapsedTime = timer.done()
|
elapsedTime = timer.done()
|
||||||
|
|
Loading…
Reference in a new issue