Skip to content

Commit

Permalink
feat(mongodb): add support for mongodb v4, v5 & mongoose v6 & v7
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 authored and Bastian Krol committed May 9, 2023
1 parent d550505 commit 4e80a26
Show file tree
Hide file tree
Showing 12 changed files with 39,757 additions and 34,725 deletions.
73,213 changes: 39,001 additions & 34,212 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@
"mocha-junit-reporter": "^2.0.2",
"mocha-multi-reporters": "^1.5.1",
"mock-require": "3.0.3",
"mongodb": "^3.6.9",
"mongodb": "^5.4.0",
"mongodb-v3": "npm:mongodb@^3.7.3",
"mongodb-v4": "npm:mongodb@^4.16.0",
"mongoose": "^7.1.0",
"mongoose-v5": "npm:mongoose@5.13.17",
"mongoose-v6": "npm:mongoose@6.11.1",
"morgan": "^1.10.0",
"mssql": "^9.0.1",
"mssql-v8": "npm:mssql@^8.1.4",
Expand Down
43 changes: 40 additions & 3 deletions packages/collector/test/tracing/database/mongodb/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

'use strict';

require('./mockVersion');
const isLatest = process.env.MONGODB_VERSION === 'latest';

const agentPort = process.env.INSTANA_AGENT_PORT;

require('../../../..')({
Expand Down Expand Up @@ -65,8 +68,17 @@ if (process.env.USE_LEGACY_3_X_CONNECTION_MECHANISM) {
collection = db.collection('mydocs');
log('Connected to MongoDB');
});
} else if (isLatest) {
(async () => {
const client = new MongoClient(connectString);
await client.connect();
db = client.db('myproject');
collection = db.collection('mydocs');

log('Connected to MongoDB');
})();
} else {
// mongodb versions >= 3.x, newer "unified" topology
// mongodb versions >= 3.x, newer "unified" topology, < 5
MongoClient.connect(connectString, { useUnifiedTopology: true }, (err, client) => {
assert.equal(null, err);
db = client.db();
Expand All @@ -82,7 +94,13 @@ app.get('/', (req, res) => {
}
});

app.post('/count', (req, res) => {
app.post('/count', async (req, res) => {
if (isLatest) {
const mongoResponse = await collection.count(req.body);
res.json(mongoResponse);
return;
}

collection.count(req.body, (err, mongoResponse) => {
if (err) {
log('Failed to count', err);
Expand Down Expand Up @@ -224,11 +242,30 @@ app.post('/long-find', (req, res) => {
});
});

app.get('/findall', (req, res) => {
app.get('/findall', async (req, res) => {
const filter = {};

if (isLatest) {
const findOpts = {};
findOpts.batchSize = 2;
findOpts.limit = 10;

// NOTE: filter by property "unique"
if (req.query && req.query.unique) {
filter.unique = req.query.unique;
}

const resp = await collection.find(filter, findOpts).toArray();
await request(`http://127.0.0.1:${agentPort}`);
res.json(resp);
return;
}

// NOTE: filter by property "unique"
if (req.query && req.query.unique) {
filter.unique = req.query.unique;
}

collection
.find(filter)
.batchSize(2)
Expand Down
36 changes: 34 additions & 2 deletions packages/collector/test/tracing/database/mongodb/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

const isLatest = process.env.MONGODB_VERSION === 'latest';
const agentPort = process.env.INSTANA_AGENT_PORT;

process.env.INSTANA_LOG_LEVEL = 'warn';
Expand Down Expand Up @@ -62,6 +63,15 @@ if (process.env.USE_LEGACY_3_X_CONNECTION_MECHANISM) {
collection = db.collection('mydocs');
log('Connected to MongoDB');
});
} else if (isLatest) {
(async () => {
const client = new MongoClient.MongoClient(connectString);
await client.connect();
db = client.db('myproject');
collection = db.collection('mydocs');

log('Connected to MongoDB');
})();
} else {
// mongodb versions >= 3.x, newer "unified" topology
MongoClient.connect(connectString, { useUnifiedTopology: true }, (err, client) => {
Expand All @@ -79,7 +89,13 @@ app.get('/', (req, res) => {
}
});

app.post('/count', (req, res) => {
app.post('/count', async (req, res) => {
if (isLatest) {
const mongoResponse = await collection.count(req.body);
res.json(mongoResponse);
return;
}

collection.count(req.body, (err, mongoResponse) => {
if (err) {
log('Failed to count', err);
Expand Down Expand Up @@ -221,8 +237,24 @@ app.post('/long-find', (req, res) => {
});
});

app.get('/findall', (req, res) => {
app.get('/findall', async (req, res) => {
const filter = {};

if (isLatest) {
const findOpts = {};
findOpts.batchSize = 2;
findOpts.limit = 10;

if (req.query && req.query.unique) {
filter.unique = req.query.unique;
}

const resp = await collection.find(filter, findOpts).toArray();
await request(`http://127.0.0.1:${agentPort}`);
res.json(resp);
return;
}

if (req.query && req.query.unique) {
filter.unique = req.query.unique;
}
Expand Down
30 changes: 30 additions & 0 deletions packages/collector/test/tracing/database/mongodb/mockVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* (c) Copyright IBM Corp. 2023
*/

'use strict';

const mock = require('mock-require');
const requireHook = require('../../../../../core/src/util/requireHook');

const MONGODB_VERSION = process.env.MONGODB_VERSION;
const MONGODB_REQUIRE = process.env.MONGODB_VERSION === 'latest' ? 'mongodb' : `mongodb-${MONGODB_VERSION}`;

if (MONGODB_REQUIRE !== 'mongodb') {
mock('mongodb', MONGODB_REQUIRE);
}

const originalOnFileLoad = requireHook.onFileLoad;
requireHook.onFileLoad = function onFileLoad() {
if (
arguments[0].source === '\\/mongodb\\/lib\\/cmap\\/connection\\.js' ||
arguments[0].source === '\\/mongodb\\/lib\\/core\\/connection\\/pool\\.js'
) {
const str = arguments[0].source.replace('mongodb', MONGODB_REQUIRE);
const reg = new RegExp(str, '');
arguments[0] = reg;
return originalOnFileLoad.apply(this, arguments);
}

return originalOnFileLoad.apply(this, arguments);
};
Loading

0 comments on commit 4e80a26

Please sign in to comment.