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