Skip to content

Commit

Permalink
fix: add mongodb connection pool patch (#3665)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna authored Oct 17, 2023
1 parent d968537 commit 517174f
Show file tree
Hide file tree
Showing 10 changed files with 1,046 additions and 376 deletions.
8 changes: 4 additions & 4 deletions .tav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ mongodb-core:
mongodb-3:
name: mongodb
versions: '>=3.3 <4'
commands: node test/instrumentation/modules/mongodb.test.js
commands: node test/instrumentation/modules/mongodb/mongodb.test.js
mongodb-4:
name: mongodb
versions: '>=4 <5'
node: '>=12.9.0'
commands: node test/instrumentation/modules/mongodb.test.js
commands: node test/instrumentation/modules/mongodb/mongodb.test.js
mongodb-5:
name: mongodb
versions: '>=5 <6'
node: '>=14.20.1'
commands: node test/instrumentation/modules/mongodb.test.js
commands: node test/instrumentation/modules/mongodb/mongodb.test.js
mongodb:
name: mongodb
versions: '>=6 <7'
node: '>=15.0.0'
commands: node test/instrumentation/modules/mongodb.test.js
commands: node test/instrumentation/modules/mongodb/mongodb.test.js

# Bluebird is effectively deprecated (https://github.com/petkaantonov/bluebird#%EF%B8%8Fnote%EF%B8%8F).
# Testing the full set of supported bluebird releases (`>=2 <4`) is currently
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ See the <<upgrade-to-v4>> guide.
[float]
===== Bug fixes
* Fix `mongodb` instrumentation to avoid loosing context when multiple cursors
are running concurrently. ({issues}3161[#3161])
[float]
===== Chores
Expand Down
4 changes: 4 additions & 0 deletions lib/instrumentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ var MODULE_PATCHERS = [
{ modPath: 'mimic-response' },
{ modPath: 'mongodb-core' },
{ modPath: 'mongodb' },
{
modPath: 'mongodb/lib/cmap/connection_pool.js',
patcher: './modules/mongodb/lib/cmap/connection_pool.js',
},
{ modPath: 'mysql' },
{ modPath: 'mysql2' },
{ modPath: 'next' },
Expand Down
39 changes: 39 additions & 0 deletions lib/instrumentation/modules/mongodb/lib/cmap/connection_pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/

'use strict';

const { AsyncResource } = require('async_hooks');

const semver = require('semver');

module.exports = (mod, agent, { version, enabled }) => {
if (!enabled) return mod;
if (!semver.satisfies(version, '>=3.3 <7.0')) {
agent.logger.debug(
'mongodb version %s not instrumented (mongodb <3.3 is instrumented via mongodb-core)',
version,
);
return mod;
}

if (mod.ConnectionPool) {
class ConnectionPoolTraced extends mod.ConnectionPool {
checkOut(callback) {
return super.checkOut(AsyncResource.bind(callback));
}
}

Object.defineProperty(mod, 'ConnectionPool', {
enumerable: true,
get: function () {
return ConnectionPoolTraced;
},
});

return mod;
}
};
3 changes: 2 additions & 1 deletion test/_is_mongodb_incompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'use strict';

var semver = require('semver');
const { safeGetPackageVersion } = require('./_utils');

/**
* Return whether the current 'mongodb' version is incompatible with the
Expand All @@ -26,7 +27,7 @@ var semver = require('semver');
*/
function isMongodbIncompat() {
const nodeVer = process.version;
const mongodbVer = require('mongodb/package.json').version;
const mongodbVer = safeGetPackageVersion('mongodb');
const msg = `mongodb@${mongodbVer} is incompatible with node@${nodeVer}`;

if (semver.satisfies(mongodbVer, '4.x')) {
Expand Down
Loading

0 comments on commit 517174f

Please sign in to comment.