index.js 5.3 KB

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