Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
refactor(pool): support command monitoring at pool level
Browse files Browse the repository at this point in the history
NODE-1390
  • Loading branch information
mbroadst committed Apr 9, 2018
1 parent 3dba4ca commit 0e660eb
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var inherits = require('util').inherits,
uncompressibleCommands = require('../wireprotocol/compression').uncompressibleCommands,
resolveClusterTime = require('../topologies/shared').resolveClusterTime;

const apm = require('./apm');

var MongoCR = require('../auth/mongocr'),
X509 = require('../auth/x509'),
Plain = require('../auth/plain'),
Expand Down Expand Up @@ -1227,6 +1229,29 @@ Pool.prototype.write = function(commands, options, cb) {
});
}

// If command monitoring is enabled we need to modify the callback here
if (self.options.enableCommandMonitoring) {
// NOTE: there is only ever a single command, for some legay reason I am unaware of we
// treat this as a potential array of commands
const command = commands[0];
this.emit('commandStarted', new apm.CommandStartedEvent(this, command));

operation.started = Date.now();
operation.cb = (err, reply) => {
if (err) {
self.emit('commandFailed', new apm.CommandFailedEvent(this, command, err, operation.started));
} else {
if (reply.result.ok === 0) {
self.emit('commandFailed', new apm.CommandFailedEvent(this, command, reply.result, operation.started));
} else {
self.emit('commandSucceeded', new apm.CommandSucceededEvent(this, command, reply, operation.started));
}
}

cb(err, reply);
};
}

// Prepare the operation buffer
serializeCommands(self, commands, [], function(err, serializedCommands) {
if (err) throw err;
Expand Down

0 comments on commit 0e660eb

Please sign in to comment.