mongodb.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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).sort().join('_')
  44. }
  45. const timer = new Metrics.Timer('mongo', { collection, query })
  46. const start = new Date()
  47. return _method.call(this, dbCommand, options, function () {
  48. timer.done()
  49. logger.debug(
  50. {
  51. query: dbCommand.query,
  52. query_type: type,
  53. collection,
  54. 'response-time': new Date() - start,
  55. },
  56. 'mongo request'
  57. )
  58. return callback.apply(this, arguments)
  59. })
  60. }
  61. const mongoDriverV2Wrapper = function (ns, ops, options, callback) {
  62. let query
  63. if (typeof callback === 'undefined') {
  64. callback = options
  65. options = {}
  66. }
  67. if (ns.match(/\$cmd$/)) {
  68. // Ignore noisy command methods like authenticating, ismaster and ping
  69. return _method.call(this, ns, ops, options, callback)
  70. }
  71. let key = `mongo-requests.${ns}.${type}`
  72. if (ops[0].q != null) {
  73. // ops[0].q
  74. query = Object.keys(ops[0].q).sort().join('_')
  75. key += '.' + query
  76. }
  77. const timer = new Metrics.Timer(key)
  78. const start = new Date()
  79. return _method.call(this, ns, ops, options, function () {
  80. timer.done()
  81. logger.debug(
  82. {
  83. query: ops[0].q,
  84. query_type: type,
  85. collection: ns,
  86. 'response-time': new Date() - start,
  87. },
  88. 'mongo request'
  89. )
  90. return callback.apply(this, arguments)
  91. })
  92. }
  93. if (arglen === 3) {
  94. return (base[method] = mongoDriverV1Wrapper)
  95. } else if (arglen === 4) {
  96. return (base[method] = mongoDriverV2Wrapper)
  97. }
  98. }
  99. monitorMethod(
  100. mongodb != null ? mongodb.Db.prototype : undefined,
  101. '_executeQueryCommand',
  102. 'query'
  103. )
  104. monitorMethod(
  105. mongodb != null ? mongodb.Db.prototype : undefined,
  106. '_executeRemoveCommand',
  107. 'remove'
  108. )
  109. monitorMethod(
  110. mongodb != null ? mongodb.Db.prototype : undefined,
  111. '_executeInsertCommand',
  112. 'insert'
  113. )
  114. monitorMethod(
  115. mongodb != null ? mongodb.Db.prototype : undefined,
  116. '_executeUpdateCommand',
  117. 'update'
  118. )
  119. monitorMethod(
  120. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  121. 'command',
  122. 'command'
  123. )
  124. monitorMethod(
  125. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  126. 'remove',
  127. 'remove'
  128. )
  129. monitorMethod(
  130. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  131. 'insert',
  132. 'insert'
  133. )
  134. monitorMethod(
  135. mongodbCore != null ? mongodbCore.Server.prototype : undefined,
  136. 'update',
  137. 'update'
  138. )
  139. monitorMethod(
  140. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  141. 'command',
  142. 'command'
  143. )
  144. monitorMethod(
  145. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  146. 'remove',
  147. 'remove'
  148. )
  149. monitorMethod(
  150. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  151. 'insert',
  152. 'insert'
  153. )
  154. monitorMethod(
  155. mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
  156. 'update',
  157. 'update'
  158. )
  159. monitorMethod(
  160. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  161. 'command',
  162. 'command'
  163. )
  164. monitorMethod(
  165. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  166. 'remove',
  167. 'remove'
  168. )
  169. monitorMethod(
  170. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  171. 'insert',
  172. 'insert'
  173. )
  174. return monitorMethod(
  175. mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
  176. 'update',
  177. 'update'
  178. )
  179. },
  180. }