mongodb.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. module.exports = {
  8. monitor(mongodbRequirePath, logger) {
  9. let mongodb, mongodbCore
  10. try {
  11. // for the v1 driver the methods to wrap are in the mongodb
  12. // module in lib/mongodb/db.js
  13. mongodb = require(mongodbRequirePath)
  14. } catch (error) {}
  15. try {
  16. // for the v2 driver the relevant methods are in the mongodb-core
  17. // module in lib/topologies/{server,replset,mongos}.js
  18. const v2Path = mongodbRequirePath.replace(/\/mongodb$/, '/mongodb-core')
  19. mongodbCore = require(v2Path)
  20. } catch (error1) {}
  21. const Metrics = require('./index')
  22. const monitorMethod = function(base, method, type) {
  23. let _method
  24. if (base == null) {
  25. return
  26. }
  27. if ((_method = base[method]) == null) {
  28. return
  29. }
  30. const arglen = _method.length
  31. const mongoDriverV1Wrapper = function(dbCommand, options, callback) {
  32. let query
  33. if (typeof callback === 'undefined') {
  34. callback = options
  35. options = {}
  36. }
  37. const collection = dbCommand.collectionName
  38. if (collection.match(/\$cmd$/)) {
  39. // Ignore noisy command methods like authenticating, ismaster and ping
  40. return _method.call(this, dbCommand, options, callback)
  41. }
  42. if (dbCommand.query != null) {
  43. query = Object.keys(dbCommand.query)
  44. .sort()
  45. .join('_')
  46. }
  47. const timer = new Metrics.Timer('mongo', { collection, query })
  48. const start = new Date()
  49. return _method.call(this, dbCommand, options, function() {
  50. timer.done()
  51. logger.log(
  52. {
  53. query: dbCommand.query,
  54. query_type: type,
  55. collection,
  56. 'response-time': new Date() - start
  57. },
  58. 'mongo request'
  59. )
  60. return callback.apply(this, arguments)
  61. })
  62. }
  63. const mongoDriverV2Wrapper = function(ns, ops, options, callback) {
  64. let query
  65. if (typeof callback === 'undefined') {
  66. callback = options
  67. options = {}
  68. }
  69. if (ns.match(/\$cmd$/)) {
  70. // Ignore noisy command methods like authenticating, ismaster and ping
  71. return _method.call(this, ns, ops, options, callback)
  72. }
  73. let key = `mongo-requests.${ns}.${type}`
  74. if (ops[0].q != null) {
  75. // ops[0].q
  76. query = Object.keys(ops[0].q)
  77. .sort()
  78. .join('_')
  79. key += '.' + query
  80. }
  81. const timer = new Metrics.Timer(key)
  82. const start = new Date()
  83. return _method.call(this, ns, ops, options, function() {
  84. timer.done()
  85. logger.log(
  86. {
  87. query: ops[0].q,
  88. query_type: type,
  89. collection: ns,
  90. 'response-time': new Date() - start
  91. },
  92. 'mongo request'
  93. )
  94. return callback.apply(this, arguments)
  95. })
  96. }
  97. if (arglen === 3) {
  98. return (base[method] = mongoDriverV1Wrapper)
  99. } else if (arglen === 4) {
  100. return (base[method] = mongoDriverV2Wrapper)
  101. }
  102. }
  103. monitorMethod(
  104. mongodb != null ? mongodb.Db.prototype : undefined,
  105. '_executeQueryCommand',
  106. 'query'
  107. )
  108. monitorMethod(
  109. mongodb != null ? mongodb.Db.prototype : undefined,
  110. '_executeRemoveCommand',
  111. 'remove'
  112. )
  113. monitorMethod(
  114. mongodb != null ? mongodb.Db.prototype : undefined,
  115. '_executeInsertCommand',
  116. 'insert'
  117. )
  118. monitorMethod(
  119. mongodb != null ? mongodb.Db.prototype : undefined,
  120. '_executeUpdateCommand',
  121. 'update'
  122. )
  123. monitorMethod(
  124. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  125. 'command',
  126. 'command'
  127. )
  128. monitorMethod(
  129. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  130. 'remove',
  131. 'remove'
  132. )
  133. monitorMethod(
  134. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  135. 'insert',
  136. 'insert'
  137. )
  138. monitorMethod(
  139. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  140. 'update',
  141. 'update'
  142. )
  143. monitorMethod(
  144. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  145. 'command',
  146. 'command'
  147. )
  148. monitorMethod(
  149. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  150. 'remove',
  151. 'remove'
  152. )
  153. monitorMethod(
  154. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  155. 'insert',
  156. 'insert'
  157. )
  158. monitorMethod(
  159. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  160. 'update',
  161. 'update'
  162. )
  163. monitorMethod(
  164. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  165. 'command',
  166. 'command'
  167. )
  168. monitorMethod(
  169. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  170. 'remove',
  171. 'remove'
  172. )
  173. monitorMethod(
  174. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  175. 'insert',
  176. 'insert'
  177. )
  178. return monitorMethod(
  179. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  180. 'update',
  181. 'update'
  182. )
  183. }
  184. }