Skip to content

Commit 2f92e5a

Browse files
committed
Fixes for APM for MongoDB 2.4.x or earlier
1 parent 0a618c3 commit 2f92e5a

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

lib/apm.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var Instrumentation = function(core, options, callback) {
8484
// Reference
8585
var self = this;
8686
// Names of methods we need to wrap
87-
var methods = ['command'];
87+
var methods = ['command', 'insert', 'update', 'remove'];
8888
// Prototype
8989
var proto = core.Server.prototype;
9090
// Core server method we are going to wrap
@@ -105,6 +105,18 @@ var Instrumentation = function(core, options, callback) {
105105
var commandName = keys[0];
106106
var db = ns.split('.')[0];
107107

108+
// Do we have a legacy insert/update/remove command
109+
if(x == 'insert' && !this.lastIsMaster().maxWireVersion) {
110+
commandName = 'insert';
111+
} else if(x == 'update' && !this.lastIsMaster().maxWireVersion) {
112+
commandName = 'update';
113+
} else if(x == 'remove' && !this.lastIsMaster().maxWireVersion) {
114+
commandName = 'delete';
115+
} else if(x == 'insert' || x == 'update' || x == 'remove' && this.lastIsMaster().maxWireVersion >= 2) {
116+
// Skip the insert/update/remove commands as they are executed as actual write commands in 2.6 or higher
117+
return func.apply(this, args);
118+
}
119+
108120
// Get the callback
109121
var callback = args.pop();
110122
// Set current callback operation id from the current context or create
@@ -151,8 +163,10 @@ var Instrumentation = function(core, options, callback) {
151163
};
152164

153165
// If we have an error
154-
if(err || (r && r.result && r.result.writeErrors)) {
155-
command.failure = err || r.result.writeErrors;
166+
if(err
167+
|| (r && r.result && r.result.writeErrors)
168+
|| (r && r.result && r.result.errmsg)) {
169+
command.failure = err || r.result.writeErrors || r.result;
156170
self.emit('failed', command);
157171
} else {
158172
command.reply = r;
@@ -376,6 +390,7 @@ var Instrumentation = function(core, options, callback) {
376390

377391
// Add our callback handler
378392
args.push(function(err, r) {
393+
379394
if(err) {
380395
// Command
381396
var command = {

lib/cursor.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ var toArray = function(self, callback) {
816816
self.rewind();
817817
self.s.state = Cursor.INIT;
818818

819-
820819
// Fetch all the documents
821820
var fetchDocs = function() {
822821
self._next(function(err, doc) {

test/functional/apm_tests.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ exports['Correctly receive the APM events for an insert'] = {
2929

3030
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
3131
db.open(function(err, db) {
32+
test.equal(null, err);
33+
3234
db.collection('apm_test').insertOne({a:1}).then(function(r) {
3335
test.equal(1, r.insertedCount);
3436
test.equal(1, started.length);
@@ -223,7 +225,6 @@ var executeOperation = function(assert, client, listener, scenario, test, callba
223225
});
224226
} else {
225227
params.push(function(err, result) {
226-
227228
// Validate the expectations
228229
test.expectations.forEach(function(x, index) {
229230
validateExpecations(assert, x, {
@@ -351,11 +352,10 @@ exports['Correctly receive the APM events for a find with getmore and killcursor
351352
db.collection('apm_test_2').drop(function(err, r) {
352353

353354
// Insert test documents
354-
db.collection('apm_test_2').insertMany([{a:1}, {a:1}, {a:1}, {a:1}, {a:1}, {a:1}]).then(function(r) {
355+
db.collection('apm_test_2').insertMany([{a:1}, {a:1}, {a:1}, {a:1}, {a:1}, {a:1}], {w:1}).then(function(r) {
355356
test.equal(6, r.insertedCount);
356357

357358
db.collection('apm_test_2').find({a:1})
358-
.sort({a:1})
359359
.project({_id: 1, a:1})
360360
.hint({'_id':1})
361361
.skip(1)
@@ -399,7 +399,7 @@ exports['Correctly receive the APM events for a find with getmore and killcursor
399399
}
400400

401401
exports['Correctly receive the APM failure event for find'] = {
402-
metadata: { requires: { topology: ['single'] } },
402+
metadata: { requires: { topology: ['single'], mongodb: ">=2.6.0" } },
403403

404404
// The actual test we wish to run
405405
test: function(configuration, test) {
@@ -805,7 +805,6 @@ exports['Correctly receive the APM events for a bulk operation'] = {
805805

806806
var listener = require('../..').instrument();
807807
listener.on('started', function(event) {
808-
// console.log(JSON.stringify(event, null, 2))
809808
if(event.commandName == 'insert' || event.commandName == 'update' || event.commandName == 'delete')
810809
started.push(event);
811810
});

0 commit comments

Comments
 (0)