Skip to content

Commit

Permalink
chore: use symbol over verision check
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna committed Aug 24, 2023
1 parent d7a3ee8 commit cda3d6e
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions lib/instrumentation/modules/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const semver = require('semver');

const { getDBDestination } = require('../context');
const shimmer = require('../shimmer');
const kListenersAdded = Symbol('kListenersAdded');

// Match expected `<hostname>:<port>`, e.g. "mongo:27017", "::1:27017",
// "127.0.0.1:27017".
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = (mongodb, agent, { version, enabled }) => {
this.on('commandStarted', onStart);
this.on('commandSucceeded', onEnd);
this.on('commandFailed', onEnd);
this[kListenersAdded] = true;
}
}
Object.defineProperty(mongodb, 'MongoClient', {
Expand All @@ -61,12 +63,7 @@ module.exports = (mongodb, agent, { version, enabled }) => {
},
});

// For versions >=4.11.0 is not necessary to shim the connect method since the connect
// method uses the shimmed client constructor to create the instance
// https://github.com/mongodb/node-mongodb-native/blob/v4.11.0/src/mongo_client.ts#L618
if (semver.satisfies(version, '<4.11.0')) {
shimmer.wrap(mongodb.MongoClient, 'connect', wrapConnect);
}
shimmer.wrap(mongodb.MongoClient, 'connect', wrapConnect);
} else {
agent.logger.warn('could not instrument mongodb@%s', version);
}
Expand All @@ -76,6 +73,14 @@ module.exports = (mongodb, agent, { version, enabled }) => {
// It calls back with `function (err, client)` or returns a Promise that
// resolves to the client.
// https://github.com/mongodb/node-mongodb-native/blob/v4.2.1/src/mongo_client.ts#L503-L511
//
// From versions >=4.11.0 the method uses `new this` to create the client instance. Hence
// our `MongoClientTraced`'s constructor is used and the command listeners are already
// registered. We should check if the client has already added the listeners to avoid handling
// the same events twice
// NOTE: prefering to use a Symbol over version check since internal implementation may
// change in future versions
// https://github.com/mongodb/node-mongodb-native/blob/v4.11.0/src/mongo_client.ts#L618
function wrapConnect(origConnect) {
return function wrappedConnect(url, options, callback) {
if (typeof options === 'function') {
Expand All @@ -95,19 +100,25 @@ module.exports = (mongodb, agent, { version, enabled }) => {
if (err) {
callback(err);
} else {
client.on('commandStarted', onStart);
client.on('commandSucceeded', onEnd);
client.on('commandFailed', onEnd);
if (!client[kListenersAdded]) {
client.on('commandStarted', onStart);
client.on('commandSucceeded', onEnd);
client.on('commandFailed', onEnd);
client[kListenersAdded] = true;
}
callback(err, client);
}
},
);
} else {
const p = origConnect.call(this, url, options, callback);
p.then((client) => {
client.on('commandStarted', onStart);
client.on('commandSucceeded', onEnd);
client.on('commandFailed', onEnd);
if (!client[kListenersAdded]) {
client.on('commandStarted', onStart);
client.on('commandSucceeded', onEnd);
client.on('commandFailed', onEnd);
client[kListenersAdded] = true;
}
});
return p;
}
Expand Down

0 comments on commit cda3d6e

Please sign in to comment.