metrics.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. console.log("using prometheus")
  2. prom = require('./prom_wrapper')
  3. collectDefaultMetrics = prom.collectDefaultMetrics
  4. appname = "unknown"
  5. hostname = require('os').hostname()
  6. destructors = []
  7. require "./uv_threadpool_size"
  8. module.exports = Metrics =
  9. register: prom.registry
  10. initialize: (_name, opts = {}) ->
  11. appname = _name
  12. collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()})
  13. if opts.ttlInMinutes
  14. prom.ttlInMinutes = opts.ttlInMinutes
  15. console.log("ENABLE_TRACE_AGENT set to #{process.env['ENABLE_TRACE_AGENT']}")
  16. if process.env['ENABLE_TRACE_AGENT'] == "true"
  17. console.log("starting google trace agent")
  18. traceAgent = require('@google-cloud/trace-agent')
  19. traceOpts =
  20. ignoreUrls: [/^\/status/, /^\/health_check/]
  21. traceAgent.start(traceOpts)
  22. console.log("ENABLE_DEBUG_AGENT set to #{process.env['ENABLE_DEBUG_AGENT']}")
  23. if process.env['ENABLE_DEBUG_AGENT'] == "true"
  24. console.log("starting google debug agent")
  25. debugAgent = require('@google-cloud/debug-agent')
  26. debugAgent.start({
  27. allowExpressions: true,
  28. serviceContext: {
  29. service: appname,
  30. version: process.env['BUILD_VERSION']
  31. }
  32. })
  33. console.log("ENABLE_PROFILE_AGENT set to #{process.env['ENABLE_PROFILE_AGENT']}")
  34. if process.env['ENABLE_PROFILE_AGENT'] == "true"
  35. console.log("starting google profile agent")
  36. profiler = require('@google-cloud/profiler')
  37. profiler.start({
  38. serviceContext: {
  39. service: appname,
  40. version: process.env['BUILD_VERSION']
  41. }
  42. })
  43. Metrics.inc("process_startup")
  44. registerDestructor: (func) ->
  45. destructors.push func
  46. injectMetricsRoute: (app) ->
  47. app.get('/metrics', (req, res) ->
  48. res.set('Content-Type', prom.registry.contentType)
  49. res.end(prom.registry.metrics())
  50. )
  51. buildPromKey: (key = "")->
  52. key.replace /[^a-zA-Z0-9]/g, "_"
  53. sanitizeValue: (value) ->
  54. parseFloat(value)
  55. set : (key, value, sampleRate = 1)->
  56. console.log("counts are not currently supported")
  57. inc : (key, sampleRate = 1, opts = {})->
  58. key = Metrics.buildPromKey(key)
  59. opts.app = appname
  60. opts.host = hostname
  61. prom.metric('counter', key).inc(opts)
  62. if process.env['DEBUG_METRICS']
  63. console.log("doing inc", key, opts)
  64. count : (key, count, sampleRate = 1, opts = {})->
  65. key = Metrics.buildPromKey(key)
  66. opts.app = appname
  67. opts.host = hostname
  68. prom.metric('counter', key).inc(opts, count)
  69. if process.env['DEBUG_METRICS']
  70. console.log("doing count/inc", key, opts)
  71. summary : (key, value, opts = {})->
  72. key = Metrics.buildPromKey(key)
  73. opts.app = appname
  74. opts.host = hostname
  75. prom.metric('summary', key).observe(opts, value)
  76. if process.env['DEBUG_METRICS']
  77. console.log("doing summary", key, value, opts)
  78. timing: (key, timeSpan, sampleRate, opts = {})->
  79. key = Metrics.buildPromKey("timer_" + key)
  80. opts.app = appname
  81. opts.host = hostname
  82. prom.metric('summary', key).observe(opts, timeSpan)
  83. if process.env['DEBUG_METRICS']
  84. console.log("doing timing", key, opts)
  85. Timer : class
  86. constructor :(key, sampleRate = 1, opts)->
  87. this.start = new Date()
  88. key = Metrics.buildPromKey(key)
  89. this.key = key
  90. this.sampleRate = sampleRate
  91. this.opts = opts
  92. done:->
  93. timeSpan = new Date - this.start
  94. Metrics.timing(this.key, timeSpan, this.sampleRate, this.opts)
  95. return timeSpan
  96. gauge : (key, value, sampleRate = 1, opts)->
  97. key = Metrics.buildPromKey(key)
  98. prom.metric('gauge', key).set({app: appname, host: hostname, status: opts?.status}, this.sanitizeValue(value))
  99. if process.env['DEBUG_METRICS']
  100. console.log("doing gauge", key, opts)
  101. globalGauge: (key, value, sampleRate = 1, opts)->
  102. key = Metrics.buildPromKey(key)
  103. prom.metric('gauge', key).set({app: appname, status: opts?.status},this.sanitizeValue(value))
  104. mongodb: require "./mongodb"
  105. http: require "./http"
  106. open_sockets: require "./open_sockets"
  107. event_loop: require "./event_loop"
  108. memory: require "./memory"
  109. timeAsyncMethod: require('./timeAsyncMethod')
  110. close: () ->
  111. for func in destructors
  112. func()