-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathplugin_manager.js
More file actions
153 lines (116 loc) · 3.85 KB
/
plugin_manager.js
File metadata and controls
153 lines (116 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
const { channel } = require('../../diagnostics_channel')
const { isFalse } = require('./util')
const plugins = require('./plugins')
const log = require('./log')
const loadChannel = channel('dd-trace:instrumentation:load')
// instrument everything that needs Plugin System V2 instrumentation
require('../../datadog-instrumentations')
if (process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined) {
// instrument lambda environment
require('./lambda')
}
const { DD_TRACE_DISABLED_PLUGINS } = process.env
const disabledPlugins = new Set(
DD_TRACE_DISABLED_PLUGINS && DD_TRACE_DISABLED_PLUGINS.split(',').map(plugin => plugin.trim())
)
// TODO actually ... should we be looking at envrionment variables this deep down in the code?
const pluginClasses = {}
loadChannel.subscribe(({ name }) => {
const Plugin = plugins[name]
if (!Plugin || typeof Plugin !== 'function') return
if (!pluginClasses[Plugin.id]) {
const envName = `DD_TRACE_${Plugin.id.toUpperCase()}_ENABLED`
const enabled = process.env[envName.replace(/[^a-z0-9_]/ig, '_')]
// TODO: remove the need to load the plugin class in order to disable the plugin
if (isFalse(enabled) || disabledPlugins.has(Plugin.id)) {
log.debug(`Plugin "${Plugin.id}" was disabled via configuration option.`)
pluginClasses[Plugin.id] = null
} else {
pluginClasses[Plugin.id] = Plugin
}
}
})
// TODO this must always be a singleton.
module.exports = class PluginManager {
constructor (tracer) {
this._tracer = tracer
this._tracerConfig = null
this._pluginsByName = {}
this._configsByName = {}
this._loadedSubscriber = ({ name }) => {
const Plugin = plugins[name]
if (!Plugin || typeof Plugin !== 'function') return
this.loadPlugin(Plugin.id)
}
loadChannel.subscribe(this._loadedSubscriber)
}
loadPlugin (name) {
const Plugin = pluginClasses[name]
if (!Plugin) return
if (!this._pluginsByName[name]) {
this._pluginsByName[name] = new Plugin(this._tracer)
}
if (!this._tracerConfig) return // TODO: don't wait for tracer to be initialized
const pluginConfig = this._configsByName[name] || {
enabled: this._tracerConfig.plugins !== false
}
this._pluginsByName[name].configure({
...this._getSharedConfig(name),
...pluginConfig
})
}
// TODO: merge config instead of replacing
configurePlugin (name, pluginConfig) {
const enabled = this._isEnabled(pluginConfig)
this._configsByName[name] = {
...pluginConfig,
enabled
}
this.loadPlugin(name)
}
// like instrumenter.enable()
configure (config = {}) {
this._tracerConfig = config
for (const name in pluginClasses) {
this.loadPlugin(name)
}
}
// This is basically just for testing. like intrumenter.disable()
destroy () {
for (const name in this._pluginsByName) {
this._pluginsByName[name].configure({ enabled: false })
}
loadChannel.unsubscribe(this._loadedSubscriber)
}
_isEnabled (pluginConfig) {
if (typeof pluginConfig === 'boolean') return pluginConfig
if (!pluginConfig) return true
return pluginConfig.enabled !== false
}
// TODO: figure out a better way to handle this
_getSharedConfig (name) {
const {
logInjection,
serviceMapping,
queryStringObfuscation,
site,
url,
dbmPropagationMode
} = this._tracerConfig
const sharedConfig = {}
if (logInjection !== undefined) {
sharedConfig.logInjection = logInjection
}
if (queryStringObfuscation !== undefined) {
sharedConfig.queryStringObfuscation = queryStringObfuscation
}
sharedConfig.dbmPropagationMode = dbmPropagationMode
if (serviceMapping && serviceMapping[name]) {
sharedConfig.service = serviceMapping[name]
}
sharedConfig.site = site
sharedConfig.url = url
return sharedConfig
}
}