Skip to content

Commit 3c60245

Browse files
authored
fix(NODE-3513): default command monitoring to off (#2926)
* fix(NODE-3513): default command monitoring to off * fix: tests that expect command monitoring to be on by default * test: setting monitorCommands to true
1 parent 58c1e84 commit 3c60245

7 files changed

+113
-40
lines changed

src/connection_string.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ export const OPTIONS = {
724724
type: 'uint'
725725
},
726726
monitorCommands: {
727-
default: true,
727+
default: false,
728728
type: 'boolean'
729729
},
730730
name: {

test/functional/causal_consistency.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Causal Consistency', function () {
1111

1212
beforeEach(function () {
1313
test.commands = { started: [], succeeded: [] };
14-
test.client = this.configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
14+
test.client = this.configuration.newClient({ w: 1 }, { maxPoolSize: 1, monitorCommands: true });
1515
test.client.on('commandStarted', event => {
1616
if (ignoredCommands.indexOf(event.commandName) === -1) test.commands.started.push(event);
1717
});

test/functional/cursor.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3564,7 +3564,10 @@ describe('Cursor', function () {
35643564
test: function (done) {
35653565
var started = [];
35663566
const configuration = this.configuration;
3567-
const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
3567+
const client = configuration.newClient(configuration.writeConcernMax(), {
3568+
maxPoolSize: 1,
3569+
monitorCommands: true
3570+
});
35683571
client.on('commandStarted', function (event) {
35693572
if (event.commandName === 'count') started.push(event);
35703573
});

test/functional/find_and_modify.test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ describe('Find and Modify', function () {
2121
var succeeded = [];
2222

2323
var configuration = this.configuration;
24-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
24+
var client = configuration.newClient(configuration.writeConcernMax(), {
25+
maxPoolSize: 1,
26+
monitorCommands: true
27+
});
28+
2529
client.on('commandStarted', function (event) {
2630
if (event.commandName === 'findAndModify') started.push(event);
2731
});
@@ -85,7 +89,11 @@ describe('Find and Modify', function () {
8589
var succeeded = [];
8690

8791
var configuration = this.configuration;
88-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
92+
var client = configuration.newClient(configuration.writeConcernMax(), {
93+
maxPoolSize: 1,
94+
monitorCommands: true
95+
});
96+
8997
client.on('commandStarted', function (event) {
9098
if (event.commandName === 'findAndModify') started.push(event);
9199
});
@@ -146,7 +154,8 @@ describe('Find and Modify', function () {
146154
url = url.indexOf('?') !== -1 ? f('%s&%s', url, 'fsync=true') : f('%s?%s', url, 'fsync=true');
147155

148156
// Establish connection to db
149-
const client = configuration.newClient(url, { sslValidate: false });
157+
const client = configuration.newClient(url, { sslValidate: false, monitorCommands: true });
158+
150159
client.on('commandStarted', function (event) {
151160
if (event.commandName === 'findAndModify') started.push(event);
152161
});

test/functional/index.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,10 @@ describe('Indexes', function () {
947947
var configuration = this.configuration;
948948
var started = [];
949949
var succeeded = [];
950-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
950+
var client = configuration.newClient(configuration.writeConcernMax(), {
951+
maxPoolSize: 1,
952+
monitorCommands: true
953+
});
951954

952955
client.on('commandStarted', function (event) {
953956
if (event.commandName === 'createIndexes') started.push(event);

test/functional/insert.test.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,10 @@ describe('Insert', function () {
23772377
var succeeded = [];
23782378

23792379
var configuration = this.configuration;
2380-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
2380+
var client = configuration.newClient(configuration.writeConcernMax(), {
2381+
maxPoolSize: 1,
2382+
monitorCommands: true
2383+
});
23812384
client.on('commandStarted', function (event) {
23822385
if (event.commandName === 'insert') started.push(event);
23832386
});
@@ -2409,39 +2412,10 @@ describe('Insert', function () {
24092412
var succeeded = [];
24102413

24112414
var configuration = this.configuration;
2412-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
2413-
client.on('commandStarted', function (event) {
2414-
if (event.commandName === 'insert') started.push(event);
2415-
});
2416-
2417-
client.on('commandSucceeded', function (event) {
2418-
if (event.commandName === 'insert') succeeded.push(event);
2419-
});
2420-
2421-
client.connect(function (err, client) {
2422-
var db = client.db(configuration.db);
2423-
expect(err).to.not.exist;
2424-
2425-
db.collection('apm_test')
2426-
.insertMany([{ a: 1 }], { forceServerObjectId: true })
2427-
.then(function () {
2428-
expect(started[0].command.documents[0]._id).to.not.exist;
2429-
2430-
client.close(done);
2431-
});
2415+
var client = configuration.newClient(configuration.writeConcernMax(), {
2416+
maxPoolSize: 1,
2417+
monitorCommands: true
24322418
});
2433-
}
2434-
});
2435-
2436-
it('Correctly allow forceServerObjectId for insertMany', {
2437-
metadata: { requires: { topology: ['single'] } },
2438-
2439-
test: function (done) {
2440-
var started = [];
2441-
var succeeded = [];
2442-
2443-
var configuration = this.configuration;
2444-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
24452419
client.on('commandStarted', function (event) {
24462420
if (event.commandName === 'insert') started.push(event);
24472421
});

test/unit/mongo_client_options.test.js

+84
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const os = require('os');
33
const fs = require('fs');
44
const { expect } = require('chai');
5+
const { getSymbolFrom } = require('../tools/utils');
56
const { parseOptions } = require('../../src/connection_string');
67
const { ReadConcern } = require('../../src/read_concern');
78
const { WriteConcern } = require('../../src/write_concern');
@@ -450,4 +451,87 @@ describe('MongoOptions', function () {
450451
);
451452
});
452453
});
454+
455+
describe('default options', () => {
456+
const doNotCheckEq = Symbol('do not check equality');
457+
const KNOWN_DEFAULTS = [
458+
['connecttimeoutms', 30000],
459+
['directconnection', false],
460+
['forceserverobjectid', false],
461+
['heartbeatfrequencyms', 10000],
462+
['keepalive', true],
463+
['keepaliveinitialdelay', 120000],
464+
['localthresholdms', 15],
465+
['logger', doNotCheckEq],
466+
['maxidletimems', 0],
467+
['maxpoolsize', 100],
468+
['minpoolsize', 0],
469+
['minheartbeatfrequencyms', 500],
470+
['monitorcommands', false], // NODE-3513
471+
['nodelay', true],
472+
['raw', false],
473+
['retryreads', true],
474+
['retrywrites', true],
475+
['serverselectiontimeoutms', 30000],
476+
['sockettimeoutms', 0],
477+
['waitqueuetimeoutms', 0],
478+
['zlibcompressionlevel', 0],
479+
480+
// map to objects that are not worth checking deep equality
481+
['compressors', doNotCheckEq],
482+
['readpreference', doNotCheckEq],
483+
['pkfactory', doNotCheckEq]
484+
];
485+
486+
const findMatchingKey = (o, searchKey) => {
487+
return Object.keys(o).filter(key => {
488+
return key.toLowerCase() === searchKey;
489+
})[0];
490+
};
491+
492+
it(`should define known defaults in client.options`, () => {
493+
const client = new MongoClient('mongodb://localhost');
494+
const clientOptions = client.options;
495+
496+
for (const [optionName, value] of KNOWN_DEFAULTS) {
497+
const camelCaseName = findMatchingKey(clientOptions, optionName);
498+
expect(camelCaseName, `did not find a camelcase match for ${optionName}`).to.be.a('string');
499+
500+
expect(clientOptions).to.have.property(camelCaseName);
501+
502+
if (value !== doNotCheckEq) {
503+
expect(clientOptions).to.have.property(camelCaseName).that.deep.equals(value);
504+
}
505+
}
506+
});
507+
508+
it('set monitorCommands to false (NODE-3513)', function () {
509+
const client = new MongoClient('mongodb://localhost');
510+
const clientOptions = client.options;
511+
512+
expect(clientOptions).to.have.property('monitorCommands', false);
513+
expect(client.s.options).to.have.property('monitorCommands', false);
514+
expect(client).to.have.property('monitorCommands', false);
515+
const optionsSym = getSymbolFrom(client, 'options');
516+
expect(client[optionsSym]).to.have.property('monitorCommands', false);
517+
});
518+
519+
it('respects monitorCommands option passed in', function () {
520+
const clientViaOpt = new MongoClient('mongodb://localhost', { monitorCommands: true });
521+
const clientViaUri = new MongoClient('mongodb://localhost?monitorCommands=true');
522+
523+
const testTable = [
524+
[clientViaOpt, clientViaOpt.options],
525+
[clientViaUri, clientViaUri.options]
526+
];
527+
528+
for (const [client, clientOptions] of testTable) {
529+
expect(clientOptions).to.have.property('monitorCommands', true);
530+
expect(client.s.options).to.have.property('monitorCommands', true);
531+
expect(client).to.have.property('monitorCommands', true);
532+
const optionsSym = getSymbolFrom(client, 'options');
533+
expect(client[optionsSym]).to.have.property('monitorCommands', true);
534+
}
535+
});
536+
});
453537
});

0 commit comments

Comments
 (0)