mongodb.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. _ = require("underscore")
  2. module.exports =
  3. monitor: (mongodb_require_path, logger) ->
  4. Db = require("#{mongodb_require_path}/lib/mongodb/db").Db
  5. Metrics = require("./metrics")
  6. monitorMethod = (base, method, type) ->
  7. _method = base[method]
  8. base[method] = (db_command, options, callback) ->
  9. if (typeof callback == 'undefined')
  10. callback = options
  11. options = {}
  12. callback = () ->
  13. collection = db_command.collectionName
  14. if collection.match(/\$cmd$/)
  15. # Ignore noisy command methods like authenticating, ismaster and ping
  16. return _method.call this, db_command, options, callback
  17. key = "mongo-requests.#{collection}.#{type}"
  18. if db_command.query?
  19. query = Object.keys(db_command.query).sort().join("_")
  20. key += "." + query
  21. timer = new Metrics.Timer(key)
  22. start = new Date()
  23. _method.call this, db_command, options, () ->
  24. timer.done()
  25. time = new Date() - start
  26. logger.log
  27. query: db_command.query
  28. query_type: type
  29. collection: collection
  30. "response-time": new Date() - start
  31. "mongo request"
  32. callback.apply this, arguments
  33. monitorMethod(Db.prototype, "_executeQueryCommand", "query")
  34. monitorMethod(Db.prototype, "_executeRemoveCommand", "remove")
  35. monitorMethod(Db.prototype, "_executeInsertCommand", "insert")
  36. monitorMethod(Db.prototype, "_executeUpdateCommand", "update")