apm.js 1002 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const EventEmitter = require('events').EventEmitter;
  3. class Instrumentation extends EventEmitter {
  4. constructor() {
  5. super();
  6. }
  7. instrument(MongoClient, callback) {
  8. // store a reference to the original functions
  9. this.$MongoClient = MongoClient;
  10. const $prototypeConnect = (this.$prototypeConnect = MongoClient.prototype.connect);
  11. const instrumentation = this;
  12. MongoClient.prototype.connect = function(callback) {
  13. this.s.options.monitorCommands = true;
  14. this.on('commandStarted', event => instrumentation.emit('started', event));
  15. this.on('commandSucceeded', event => instrumentation.emit('succeeded', event));
  16. this.on('commandFailed', event => instrumentation.emit('failed', event));
  17. return $prototypeConnect.call(this, callback);
  18. };
  19. if (typeof callback === 'function') callback(null, this);
  20. }
  21. uninstrument() {
  22. this.$MongoClient.prototype.connect = this.$prototypeConnect;
  23. }
  24. }
  25. module.exports = Instrumentation;