index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const ExpressCompression = require('compression')
  2. const prom = require('./prom_wrapper')
  3. const { collectDefaultMetrics } = prom
  4. let appname = 'unknown'
  5. const hostname = require('os').hostname()
  6. const destructors = []
  7. require('./uv_threadpool_size')
  8. /**
  9. * Configure the metrics module
  10. */
  11. function configure(opts = {}) {
  12. if (opts.appName) {
  13. appname = opts.appName
  14. }
  15. if (opts.ttlInMinutes) {
  16. prom.ttlInMinutes = opts.ttlInMinutes
  17. }
  18. }
  19. /**
  20. * Configure the metrics module and start the default metrics collectors and
  21. * profiling agents.
  22. */
  23. function initialize(_name, opts = {}) {
  24. configure({ ...opts, appName: _name })
  25. collectDefaultMetrics({ timeout: 5000, prefix: '' })
  26. console.log(`ENABLE_TRACE_AGENT set to ${process.env.ENABLE_TRACE_AGENT}`)
  27. if (process.env.ENABLE_TRACE_AGENT === 'true') {
  28. console.log('starting google trace agent')
  29. const traceAgent = require('@google-cloud/trace-agent')
  30. const traceOpts = { ignoreUrls: [/^\/status/, /^\/health_check/] }
  31. traceAgent.start(traceOpts)
  32. }
  33. console.log(`ENABLE_DEBUG_AGENT set to ${process.env.ENABLE_DEBUG_AGENT}`)
  34. if (process.env.ENABLE_DEBUG_AGENT === 'true') {
  35. console.log('starting google debug agent')
  36. const debugAgent = require('@google-cloud/debug-agent')
  37. debugAgent.start({
  38. allowExpressions: true,
  39. serviceContext: {
  40. service: appname,
  41. version: process.env.BUILD_VERSION
  42. }
  43. })
  44. }
  45. console.log(`ENABLE_PROFILE_AGENT set to ${process.env.ENABLE_PROFILE_AGENT}`)
  46. if (process.env.ENABLE_PROFILE_AGENT === 'true') {
  47. console.log('starting google profile agent')
  48. const profiler = require('@google-cloud/profiler')
  49. profiler.start({
  50. serviceContext: {
  51. service: appname,
  52. version: process.env.BUILD_VERSION
  53. }
  54. })
  55. }
  56. inc('process_startup')
  57. }
  58. function registerDestructor(func) {
  59. destructors.push(func)
  60. }
  61. function injectMetricsRoute(app) {
  62. app.get(
  63. '/metrics',
  64. ExpressCompression({
  65. level: parseInt(process.env.METRICS_COMPRESSION_LEVEL || '1', 10)
  66. }),
  67. function(req, res) {
  68. res.set('Content-Type', prom.registry.contentType)
  69. res.end(prom.registry.metrics())
  70. }
  71. )
  72. }
  73. function buildPromKey(key) {
  74. return key.replace(/[^a-zA-Z0-9]/g, '_')
  75. }
  76. function sanitizeValue(value) {
  77. return parseFloat(value)
  78. }
  79. function set(key, value, sampleRate = 1) {
  80. console.log('counts are not currently supported')
  81. }
  82. function inc(key, sampleRate = 1, opts = {}) {
  83. key = buildPromKey(key)
  84. opts.app = appname
  85. opts.host = hostname
  86. prom.metric('counter', key).inc(opts)
  87. if (process.env.DEBUG_METRICS) {
  88. console.log('doing inc', key, opts)
  89. }
  90. }
  91. function count(key, count, sampleRate = 1, opts = {}) {
  92. key = buildPromKey(key)
  93. opts.app = appname
  94. opts.host = hostname
  95. prom.metric('counter', key).inc(opts, count)
  96. if (process.env.DEBUG_METRICS) {
  97. console.log('doing count/inc', key, opts)
  98. }
  99. }
  100. function summary(key, value, opts = {}) {
  101. key = buildPromKey(key)
  102. opts.app = appname
  103. opts.host = hostname
  104. prom.metric('summary', key).observe(opts, value)
  105. if (process.env.DEBUG_METRICS) {
  106. console.log('doing summary', key, value, opts)
  107. }
  108. }
  109. function timing(key, timeSpan, sampleRate = 1, opts = {}) {
  110. key = buildPromKey('timer_' + key)
  111. opts.app = appname
  112. opts.host = hostname
  113. prom.metric('summary', key).observe(opts, timeSpan)
  114. if (process.env.DEBUG_METRICS) {
  115. console.log('doing timing', key, opts)
  116. }
  117. }
  118. class Timer {
  119. constructor(key, sampleRate = 1, opts = {}) {
  120. this.start = new Date()
  121. key = buildPromKey(key)
  122. this.key = key
  123. this.sampleRate = sampleRate
  124. this.opts = opts
  125. }
  126. done() {
  127. const timeSpan = new Date() - this.start
  128. timing(this.key, timeSpan, this.sampleRate, this.opts)
  129. return timeSpan
  130. }
  131. }
  132. function gauge(key, value, sampleRate = 1, opts = {}) {
  133. key = buildPromKey(key)
  134. prom.metric('gauge', key).set(
  135. {
  136. app: appname,
  137. host: hostname,
  138. status: opts.status
  139. },
  140. this.sanitizeValue(value)
  141. )
  142. if (process.env.DEBUG_METRICS) {
  143. console.log('doing gauge', key, opts)
  144. }
  145. }
  146. function globalGauge(key, value, sampleRate = 1, opts = {}) {
  147. key = buildPromKey(key)
  148. prom
  149. .metric('gauge', key)
  150. .set({ app: appname, status: opts.status }, this.sanitizeValue(value))
  151. }
  152. function close() {
  153. for (const func of destructors) {
  154. func()
  155. }
  156. }
  157. module.exports = {
  158. configure,
  159. initialize,
  160. registerDestructor,
  161. injectMetricsRoute,
  162. buildPromKey,
  163. sanitizeValue,
  164. set,
  165. inc,
  166. count,
  167. summary,
  168. timing,
  169. Timer,
  170. gauge,
  171. globalGauge,
  172. close,
  173. register: prom.registry,
  174. mongodb: require('./mongodb'),
  175. http: require('./http'),
  176. open_sockets: require('./open_sockets'),
  177. event_loop: require('./event_loop'),
  178. memory: require('./memory'),
  179. timeAsyncMethod: require('./timeAsyncMethod')
  180. }