Skip to content

Commit 7707fc2

Browse files
committed
fix support for environment variable
1 parent d391a45 commit 7707fc2

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/dd-trace/src/plugin_manager.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

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

48+
function getEnvEnabled (Plugin) {
49+
const envName = `DD_TRACE_${Plugin.id.toUpperCase()}_ENABLED`
50+
return getEnvironmentVariable(normalizePluginEnvName(envName))
51+
}
52+
4953
// TODO this must always be a singleton.
5054
module.exports = class PluginManager {
5155
constructor (tracer) {
@@ -74,7 +78,7 @@ module.exports = class PluginManager {
7478
this._pluginsByName[name] = new Plugin(this._tracer, this._tracerConfig)
7579
}
7680
const pluginConfig = this._configsByName[name] || {
77-
enabled: this._tracerConfig.plugins !== false && !Plugin.experimental
81+
enabled: isTrue(getEnvEnabled(Plugin)) || (this._tracerConfig.plugins !== false && !Plugin.experimental)
7882
}
7983

8084
// extracts predetermined configuration from tracer and combines it with plugin-specific config

packages/dd-trace/test/plugin_manager.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe('Plugin Manager', () => {
9191

9292
afterEach(() => {
9393
delete process.env.DD_TRACE_DISABLED_PLUGINS
94+
delete process.env.DD_TRACE_EIGHT_ENABLED
9495
pm.destroy()
9596
})
9697

@@ -287,6 +288,20 @@ describe('Plugin Manager', () => {
287288
loadChannel.publish({ name: 'eight' })
288289
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: false })
289290
})
291+
292+
it('should enable the plugin when configured programmatically', () => {
293+
pm.configure()
294+
pm.configurePlugin('eight')
295+
loadChannel.publish({ name: 'eight' })
296+
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: true })
297+
})
298+
299+
it('should enable the plugin when configured with an environment variable', () => {
300+
process.env.DD_TRACE_EIGHT_ENABLED = 'true'
301+
pm.configure()
302+
loadChannel.publish({ name: 'eight' })
303+
expect(Eight.prototype.configure).to.have.been.calledWithMatch({ enabled: true })
304+
})
290305
})
291306

292307
it('instantiates plugin classes', () => {

0 commit comments

Comments
 (0)