You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1002 B
32 lines
1002 B
2 months ago
|
'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;
|