Skip to content

Commit

Permalink
feat!: Updated mongodb instrumentation to drop support for versions…
Browse files Browse the repository at this point in the history
… 2 and 3 (#2398)
  • Loading branch information
bizob2828 authored Jul 24, 2024
1 parent 1870010 commit a0ae32a
Show file tree
Hide file tree
Showing 25 changed files with 249 additions and 1,846 deletions.
14 changes: 1 addition & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,11 @@ services:
ports:
- "11211:11211"

mongodb_3:
container_name: nr_node_mongodb
platform: ${DOCKER_PLATFORM:-linux/amd64}
image: library/mongo:3
ports:
- "27017:27017"
healthcheck:
test: ["CMD", "mongo", "--quiet"]
interval: 1s
timeout: 10s
retries: 30

mongodb_5:
container_name: nr_node_mongodb_5
image: library/mongo:5
ports:
- "27018:27017"
- "27017:27017"
healthcheck:
test: ["CMD", "mongo", "--quiet"]
interval: 1s
Expand Down
19 changes: 9 additions & 10 deletions lib/instrumentation/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
'use strict'

const semver = require('semver')
const instrument = require('./mongodb/v2-mongo')
const instrumentV3 = require('./mongodb/v3-mongo')
const instrumentV4 = require('./mongodb/v4-mongo')

// XXX: When this instrumentation is modularized, update this thread
Expand All @@ -34,14 +32,15 @@ function initialize(agent, mongodb, moduleName, shim) {
return
}

shim.setDatastore(shim.MONGODB)

const mongoVersion = shim.pkgVersion
if (semver.satisfies(mongoVersion, '>=4.0.0')) {
instrumentV4(shim, mongodb)
} else if (semver.satisfies(mongoVersion, '>=3.0.6')) {
instrumentV3(shim, mongodb)
} else {
instrument(shim, mongodb)
if (semver.satisfies(mongoVersion, '<4.0.0')) {
shim.logger.warn(
'New Relic Node.js agent no longer supports mongodb < 4, current version %s. Please downgrade to v11 for support, if needed',
mongoVersion
)
return
}

shim.setDatastore(shim.MONGODB)
instrumentV4(shim, mongodb)
}
118 changes: 0 additions & 118 deletions lib/instrumentation/mongodb/v2-mongo.js

This file was deleted.

85 changes: 0 additions & 85 deletions lib/instrumentation/mongodb/v3-mongo.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/lib/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ module.exports = {
mongodb_host: process.env.NR_NODE_TEST_MONGODB_HOST || 'localhost',
mongodb_port: process.env.NR_NODE_TEST_MONGODB_PORT || 27017,

// mongodb 4.2.0 does not allow mongo server v2.
// There is now a separate container that maps 27018 to mongo:5
mongodb_v4_host: process.env.NR_NODE_TEST_MONGODB_V4_HOST || 'localhost',
mongodb_v4_port: process.env.NR_NODE_TEST_MONGODB_V4_PORT || 27018,

mysql_host: process.env.NR_NODE_TEST_MYSQL_HOST || 'localhost',
mysql_port: process.env.NR_NODE_TEST_MYSQL_PORT || 3306,

Expand Down
54 changes: 54 additions & 0 deletions test/unit/instrumentation/mongodb.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const tap = require('tap')

const helper = require('../../lib/agent_helper')
const proxyquire = require('proxyquire')
const sinon = require('sinon')

tap.beforeEach((t) => {
const sandbox = sinon.createSandbox()
t.context.sandbox = sandbox
t.context.agent = helper.loadMockedAgent()
t.context.initialize = proxyquire('../../../lib/instrumentation/mongodb', {
'./mongodb/v4-mongo': function stub() {}
})
const shim = {
setDatastore: sandbox.stub(),
pkgVersion: '4.0.0',
logger: {
warn: sandbox.stub()
}
}
shim.pkgVersion = '4.0.0'
t.context.shim = shim
})

tap.afterEach((t) => {
helper.unloadAgent(t.context.agent)
t.context.sandbox.restore()
})

tap.test('should not log warning if version is >= 4', function (t) {
const { agent, shim, initialize } = t.context
initialize(agent, {}, 'mongodb', shim)
t.equal(shim.logger.warn.callCount, 0)
t.equal(shim.setDatastore.callCount, 1)
t.end()
})

tap.test('should log warning if using unsupported version of mongo', function (t) {
const { agent, shim, initialize } = t.context
shim.pkgVersion = '2.0.0'
initialize(agent, {}, 'mongodb', shim)
t.same(shim.logger.warn.args[0], [
'New Relic Node.js agent no longer supports mongodb < 4, current version %s. Please downgrade to v11 for support, if needed',
'2.0.0'
])
t.end()
})
Loading

0 comments on commit a0ae32a

Please # to comment.