Skip to content

Commit

Permalink
feat: Provided ability to disable instrumentation for a 3rd party pac…
Browse files Browse the repository at this point in the history
…kage
  • Loading branch information
bizob2828 committed Sep 5, 2024
1 parent ebfa2e9 commit 4ccc73f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/config/build-instrumentation-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const { boolean } = require('./formatters')
const instrumentedLibraries = require('../instrumentations')()
const pkgNames = Object.keys(instrumentedLibraries)

/**
* Builds the stanza for config.instrumentation.*
* It defaults every library to true and assigns a boolean
* formatter for the environment variable conversion of the values
*/
module.exports = pkgNames.reduce((config, pkg) => {
config[pkg] = { enabled: { formatter: boolean, default: true } }
return config
}, {})
8 changes: 7 additions & 1 deletion lib/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const defaultConfig = module.exports
const { array, int, float, boolean, object, objectList, allowList, regex } = require('./formatters')
const pkgInstrumentation = require('./build-instrumentation-config')

/**
* A function that returns the definition of the agent configuration
Expand Down Expand Up @@ -1382,7 +1383,12 @@ defaultConfig.definition = () => ({
default: true
}
}
}
},
/**
* Stanza that contains all keys to disable 3rd party package instrumentation(i.e. mongodb, pg, redis, etc)
*
*/
instrumentation: pkgInstrumentation
})

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/shimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ const shimmer = (module.exports = {
*/
registerThirdPartyInstrumentation(agent) {
for (const [moduleName, instrInfo] of Object.entries(INSTRUMENTATIONS)) {
if (instrInfo.module) {
if (agent.config.instrumentation?.[moduleName]?.enabled === false) {
logger.warn(
`Instrumentation for ${moduleName} has been disabled via 'config.instrumentation.${moduleName}.enabled. Not instrumenting package`
)
return
} else if (instrInfo.module) {
// Because external instrumentations can change independent of
// the agent core, we don't want breakages in them to entirely
// disable the agent.
Expand Down
20 changes: 20 additions & 0 deletions test/unit/config/build-instrumentation-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2022 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const test = require('node:test')
const assert = require('node:assert')

test('should default the instrumentation stanza', () => {
const { boolean } = require('../../../lib/config/formatters')
const pkgs = require('../../../lib/config/build-instrumentation-config')
const instrumentation = require('../../../lib/instrumentations')()
const pkgNames = Object.keys(instrumentation)

pkgNames.forEach((pkg) => {
assert.deepEqual(pkgs[pkg], { enabled: { formatter: boolean, default: true } })
})
})
6 changes: 6 additions & 0 deletions test/unit/config/config-defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,10 @@ test('with default properties', async (t) => {
assert.equal(configuration.ai_monitoring.enabled, false)
assert.equal(configuration.ai_monitoring.streaming.enabled, true)
})

await t.test('instrumentation defaults', () => {
assert.equal(configuration.instrumentation.express.enabled, true)
assert.equal(configuration.instrumentation['@prisma/client'].enabled, true)
assert.equal(configuration.instrumentation.npmlog.enabled, true)
})
})
14 changes: 14 additions & 0 deletions test/unit/config/config-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,5 +768,19 @@ test('when overriding configuration values via environment variables', async (t)
end()
})
})

await t.test('should convert NEW_RELIC_INSTRUMENTATION* accordingly', (t, end) => {
const env = {
NEW_RELIC_INSTRUMENTATION_IOREDIS_ENABLED: 'false',
['NEW_RELIC_INSTRUMENTATION_@GRPC/GRPC-JS_ENABLED']: 'false',
NEW_RELIC_INSTRUMENTATION_KNEX_ENABLED: 'false'
}
idempotentEnv(env, (config) => {
assert.equal(config.instrumentation.ioredis.enabled, false)
assert.equal(config.instrumentation['@grpc/grpc-js'].enabled, false)
assert.equal(config.instrumentation.knex.enabled, false)
end()
})
})
}
})

0 comments on commit 4ccc73f

Please sign in to comment.