Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mongodb async context fix #3657

Closed
wants to merge 14 commits into from
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 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
Loading