mongodb.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module.exports =
  2. monitor: (mongodb_require_path, logger) ->
  3. try
  4. # for the v1 driver the methods to wrap are in the mongodb
  5. # module in lib/mongodb/db.js
  6. mongodb = require("#{mongodb_require_path}")
  7. try
  8. # for the v2 driver the relevant methods are in the mongodb-core
  9. # module in lib/topologies/{server,replset,mongos}.js
  10. v2_path = mongodb_require_path.replace(/\/mongodb$/, '/mongodb-core')
  11. mongodbCore = require(v2_path)
  12. Metrics = require("./metrics")
  13. monitorMethod = (base, method, type) ->
  14. return unless base?
  15. return unless (_method = base[method])?
  16. arglen = _method.length
  17. mongo_driver_v1_wrapper = (db_command, options, callback) ->
  18. if (typeof callback == 'undefined')
  19. callback = options
  20. options = {}
  21. callback = () ->
  22. collection = db_command.collectionName
  23. if collection.match(/\$cmd$/)
  24. # Ignore noisy command methods like authenticating, ismaster and ping
  25. return _method.call this, db_command, options, callback
  26. key = "mongo-requests.#{collection}.#{type}"
  27. if db_command.query?
  28. query = Object.keys(db_command.query).sort().join("_")
  29. key += "." + query
  30. timer = new Metrics.Timer(key)
  31. start = new Date()
  32. _method.call this, db_command, options, () ->
  33. timer.done()
  34. time = new Date() - start
  35. logger.log
  36. query: db_command.query
  37. query_type: type
  38. collection: collection
  39. "response-time": new Date() - start
  40. "mongo request"
  41. callback.apply this, arguments
  42. mongo_driver_v2_wrapper = (ns, ops, options, callback) ->
  43. if (typeof callback == 'undefined')
  44. callback = options
  45. options = {}
  46. callback = () ->
  47. if ns.match(/\$cmd$/)
  48. # Ignore noisy command methods like authenticating, ismaster and ping
  49. return _method.call this, ns, ops, options, callback
  50. key = "mongo-requests.#{ns}.#{type}"
  51. if ops[0].q? # ops[0].q
  52. query = Object.keys(ops[0].q).sort().join("_")
  53. key += "." + query
  54. timer = new Metrics.Timer(key)
  55. start = new Date()
  56. _method.call this, ns, ops, options, () ->
  57. timer.done()
  58. time = new Date() - start
  59. logger.log
  60. query: ops[0].q
  61. query_type: type
  62. collection: ns
  63. "response-time": new Date() - start
  64. "mongo request"
  65. callback.apply this, arguments
  66. if arglen == 3
  67. base[method] = mongo_driver_v1_wrapper
  68. else if arglen == 4
  69. base[method] = mongo_driver_v2_wrapper
  70. monitorMethod(mongodb?.Db.prototype, "_executeQueryCommand", "query")
  71. monitorMethod(mongodb?.Db.prototype, "_executeRemoveCommand", "remove")
  72. monitorMethod(mongodb?.Db.prototype, "_executeInsertCommand", "insert")
  73. monitorMethod(mongodb?.Db.prototype, "_executeUpdateCommand", "update")
  74. monitorMethod(mongodbCore?.Server.prototype, "command", "command")
  75. monitorMethod(mongodbCore?.Server.prototype, "remove", "remove")
  76. monitorMethod(mongodbCore?.Server.prototype, "insert", "insert")
  77. monitorMethod(mongodbCore?.Server.prototype, "update", "update")
  78. monitorMethod(mongodbCore?.ReplSet.prototype, "command", "command")
  79. monitorMethod(mongodbCore?.ReplSet.prototype, "remove", "remove")
  80. monitorMethod(mongodbCore?.ReplSet.prototype, "insert", "insert")
  81. monitorMethod(mongodbCore?.ReplSet.prototype, "update", "update")
  82. monitorMethod(mongodbCore?.Mongos.prototype, "command", "command")
  83. monitorMethod(mongodbCore?.Mongos.prototype, "remove", "remove")
  84. monitorMethod(mongodbCore?.Mongos.prototype, "insert", "insert")
  85. monitorMethod(mongodbCore?.Mongos.prototype, "update", "update")