Skip to content

add experimental flag on plugins to disable them by default #5985

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions packages/datadog-instrumentations/src/helpers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const log = require('../../../dd-trace/src/log')
const checkRequireCache = require('./check-require-cache')
const telemetry = require('../../../dd-trace/src/guardrails/telemetry')
const { isInServerlessEnvironment } = require('../../../dd-trace/src/serverless')
const { isFalse, isTrue, normalizePluginEnvName } = require('../../../dd-trace/src/util')
const { isFalse } = require('../../../dd-trace/src/util')
const { getEnvironmentVariables } = require('../../../dd-trace/src/config-helper')

const envs = getEnvironmentVariables()
Expand All @@ -25,20 +25,15 @@ const names = Object.keys(hooks)
const pathSepExpr = new RegExp(`\\${path.sep}`, 'g')

const disabledInstrumentations = new Set(
DD_TRACE_DISABLED_INSTRUMENTATIONS?.split(',').map(name => normalizePluginEnvName(name, true)) ?? []
DD_TRACE_DISABLED_INSTRUMENTATIONS?.split(',') ?? []
)
const reenabledInstrumentations = new Set()

// Check for DD_TRACE_<INTEGRATION>_ENABLED environment variables
for (const [key, value] of Object.entries(envs)) {
const match = key.match(/^DD_TRACE_(.+)_ENABLED$/)
if (match && value) {
const integration = normalizePluginEnvName(match[1], true)
if (isFalse(value)) {
disabledInstrumentations.add(integration)
} else if (isTrue(value)) {
reenabledInstrumentations.add(integration)
}
if (match && isFalse(value)) {
const integration = match[1].toLowerCase()
disabledInstrumentations.add(integration)
}
}

Expand All @@ -65,8 +60,7 @@ const allInstrumentations = {}

// TODO: make this more efficient
for (const packageName of names) {
const normalizedPackageName = normalizePluginEnvName(packageName, true)
if (disabledInstrumentations.has(normalizedPackageName)) continue
if (disabledInstrumentations.has(packageName)) continue

const hookOptions = {}

Expand All @@ -75,10 +69,6 @@ for (const packageName of names) {
if (hook !== null && typeof hook === 'object') {
if (hook.serverless === false && isInServerlessEnvironment()) continue

// some integrations are disabled by default, but can be enabled by setting
// the DD_TRACE_<INTEGRATION>_ENABLED environment variable to true
if (hook.disabled && !reenabledInstrumentations.has(normalizedPackageName)) continue

hookOptions.internals = hook.esmFirst
hook = hook.fn
}
Expand Down
49 changes: 0 additions & 49 deletions packages/datadog-instrumentations/test/helpers/register.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('register', () => {

hooksMock = {
'@confluentinc/kafka-javascript': {
disabled: true,
fn: sinon.stub().returns('hooked')
},
'mongodb-core': {
Expand Down Expand Up @@ -72,54 +71,6 @@ describe('register', () => {
}
}

it('should skip hooks that are disabled by default and process enabled hooks', () => {
loadRegisterWithEnv()

expect(HookMock.callCount).to.equal(1)
expect(HookMock.args[0]).to.deep.include(['mongodb-core'], { internals: undefined })

runHookCallbacks(HookMock)

expect(hooksMock['@confluentinc/kafka-javascript'].fn).to.not.have.been.called
expect(hooksMock['mongodb-core'].fn).to.have.been.called
})

it('should enable disabled hooks when DD_TRACE_[pkg]_ENABLED is true', () => {
loadRegisterWithEnv({ DD_TRACE_CONFLUENTINC_KAFKA_JAVASCRIPT_ENABLED: 'true' })

expect(HookMock.callCount).to.equal(2)
expect(HookMock.args[0]).to.deep.include(['@confluentinc/kafka-javascript'], { internals: undefined })
expect(HookMock.args[1]).to.deep.include(['mongodb-core'], { internals: undefined })

runHookCallbacks(HookMock)

expect(hooksMock['@confluentinc/kafka-javascript'].fn).to.have.been.called
expect(hooksMock['mongodb-core'].fn).to.have.been.called
})

it('should not enable disabled hooks when DD_TRACE_[pkg]_ENABLED is false', () => {
loadRegisterWithEnv({ DD_TRACE_CONFLUENTINC_KAFKA_JAVASCRIPT_ENABLED: 'false' })

expect(HookMock.callCount).to.equal(1)
expect(HookMock.args[0]).to.deep.include(['mongodb-core'], { internals: undefined })

runHookCallbacks(HookMock)

expect(hooksMock['@confluentinc/kafka-javascript'].fn).to.not.have.been.called
expect(hooksMock['mongodb-core'].fn).to.have.been.called
})

it('should disable hooks that are disabled by DD_TRACE_[pkg]_ENABLED=false', () => {
loadRegisterWithEnv({ DD_TRACE_MONGODB_CORE_ENABLED: 'false' })

expect(HookMock.callCount).to.equal(0)

runHookCallbacks(HookMock)

expect(hooksMock['@confluentinc/kafka-javascript'].fn).to.not.have.been.called
expect(hooksMock['mongodb-core'].fn).to.not.have.been.called
})

it('should disable hooks that are disabled by DD_TRACE_DISABLED_INSTRUMENTATIONS', () => {
loadRegisterWithEnv({ DD_TRACE_DISABLED_INSTRUMENTATIONS: 'mongodb-core,@confluentinc/kafka-javascript' })

Expand Down
12 changes: 8 additions & 4 deletions packages/dd-trace/src/plugin_manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { channel } = require('dc-polyfill')
const { isFalse, normalizePluginEnvName } = require('./util')
const { isFalse, isTrue, normalizePluginEnvName } = require('./util')
const plugins = require('./plugins')
const log = require('./log')
const { getEnvironmentVariable } = require('../../dd-trace/src/config-helper')
Expand Down Expand Up @@ -32,8 +32,7 @@ loadChannel.subscribe(({ name }) => {
function maybeEnable (Plugin) {
if (!Plugin || typeof Plugin !== 'function') return
if (!pluginClasses[Plugin.id]) {
const envName = `DD_TRACE_${Plugin.id.toUpperCase()}_ENABLED`
const enabled = getEnvironmentVariable(normalizePluginEnvName(envName))
const enabled = getEnvEnabled(Plugin)

// TODO: remove the need to load the plugin class in order to disable the plugin
if (isFalse(enabled) || disabledPlugins.has(Plugin.id)) {
Expand All @@ -46,6 +45,11 @@ function maybeEnable (Plugin) {
}
}

function getEnvEnabled (Plugin) {
const envName = `DD_TRACE_${Plugin.id.toUpperCase()}_ENABLED`
return getEnvironmentVariable(normalizePluginEnvName(envName))
}

// TODO this must always be a singleton.
module.exports = class PluginManager {
constructor (tracer) {
Expand Down Expand Up @@ -74,7 +78,7 @@ module.exports = class PluginManager {
this._pluginsByName[name] = new Plugin(this._tracer, this._tracerConfig)
}
const pluginConfig = this._configsByName[name] || {
enabled: this._tracerConfig.plugins !== false
enabled: this._tracerConfig.plugins !== false && (!Plugin.experimental || isTrue(getEnvEnabled(Plugin)))
}

// extracts predetermined configuration from tracer and combines it with plugin-specific config
Expand Down
33 changes: 32 additions & 1 deletion packages/dd-trace/test/plugin_manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Plugin Manager', () => {
let Four
let Five
let Six
let Eight
let pm

beforeEach(() => {
Expand Down Expand Up @@ -53,7 +54,11 @@ describe('Plugin Manager', () => {
return 'six'
}
},
seven: {}
seven: {},
eight: class Eight extends FakePlugin {
static experimental = true
static id = 'eight'
}
}

Two = plugins.two
Expand All @@ -67,6 +72,9 @@ describe('Plugin Manager', () => {
Six = plugins.six
Six.prototype.configure = sinon.spy()

Eight = plugins.eight
Eight.prototype.configure = sinon.spy()

process.env.DD_TRACE_DISABLED_PLUGINS = 'five,six,seven'

PluginManager = proxyquire.noPreserveCache()('../src/plugin_manager', {
Expand All @@ -83,6 +91,7 @@ describe('Plugin Manager', () => {

afterEach(() => {
delete process.env.DD_TRACE_DISABLED_PLUGINS
delete process.env.DD_TRACE_EIGHT_ENABLED
pm.destroy()
})

Expand Down Expand Up @@ -273,6 +282,28 @@ describe('Plugin Manager', () => {
})
})

describe('with an experimental plugin', () => {
it('should disable the plugin by default', () => {
pm.configure()
loadChannel.publish({ name: 'eight' })
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: false })
})

it('should enable the plugin when configured programmatically', () => {
pm.configure()
pm.configurePlugin('eight')
loadChannel.publish({ name: 'eight' })
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: true })
})

it('should enable the plugin when configured with an environment variable', () => {
process.env.DD_TRACE_EIGHT_ENABLED = 'true'
pm.configure()
loadChannel.publish({ name: 'eight' })
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: true })
})
})

it('instantiates plugin classes', () => {
pm.configure()
loadChannel.publish({ name: 'two' })
Expand Down
Loading