-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathinstrumenter.js
168 lines (132 loc) · 3.96 KB
/
instrumenter.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict'
const shimmer = require('shimmer')
const log = require('./log')
const platform = require('./platform')
shimmer({ logger: () => {} })
const plugins = platform.plugins
class Instrumenter {
constructor (tracer) {
this._tracer = tracer
this._loader = new platform.Loader(this)
this._enabled = false
this._names = new Set()
this._plugins = new Map()
this._instrumented = new Map()
}
use (name, config) {
if (typeof config === 'boolean') {
config = { enabled: config }
}
config = config || {}
try {
this._set(plugins[name.toLowerCase()], { name, config })
} catch (e) {
log.debug(`Could not find a plugin named "${name}".`)
}
if (this._enabled) {
this._loader.reload(this._plugins)
}
}
enable (config) {
config = config || {}
if (config.plugins !== false) {
Object.keys(plugins)
.filter(name => !this._plugins.has(plugins[name]))
.forEach(name => {
this._set(plugins[name], { name, config: {} })
})
}
this._enabled = true
this._loader.reload(this._plugins)
}
disable () {
for (const instrumentation of this._instrumented.keys()) {
this.unpatch(instrumentation)
}
this._plugins.clear()
this._enabled = false
this._loader.reload(this._plugins)
}
wrap (nodules, names, wrapper) {
nodules = [].concat(nodules)
names = [].concat(names)
nodules.forEach(nodule => {
names.forEach(name => {
if (typeof nodule[name] !== 'function') {
throw new Error(`Expected object ${nodule} to contain method ${name}.`)
}
Object.defineProperty(nodule[name], '_datadog_patched', {
value: true,
configurable: true
})
})
})
shimmer.massWrap.call(this, nodules, names, wrapper)
}
unwrap (nodules, names, wrapper) {
nodules = [].concat(nodules)
names = [].concat(names)
shimmer.massUnwrap.call(this, nodules, names, wrapper)
nodules.forEach(nodule => {
names.forEach(name => {
nodule[name] && delete nodule[name]._datadog_patched
})
})
}
load (plugin, meta) {
if (!this._enabled) return
const instrumentations = [].concat(plugin)
const enabled = meta.config.enabled !== false
platform.metrics().boolean(`datadog.tracer.node.plugin.enabled.by.name`, enabled, `name:${meta.name}`)
try {
instrumentations
.forEach(instrumentation => {
this._loader.load(instrumentation, meta.config)
})
} catch (e) {
log.error(e)
this.unload(plugin)
log.debug(`Error while trying to patch ${meta.name}. The plugin has been disabled.`)
platform.metrics().increment(`datadog.tracer.node.plugin.errors`, true)
}
}
unload (plugin) {
[].concat(plugin)
.forEach(instrumentation => {
this.unpatch(instrumentation)
this._instrumented.delete(instrumentation)
})
const meta = this._plugins.get(plugin)
if (meta) {
this._plugins.delete(plugin)
platform.metrics().boolean(`datadog.tracer.node.plugin.enabled.by.name`, false, `name:${meta.name}`)
}
}
patch (instrumentation, moduleExports, config) {
let instrumented = this._instrumented.get(instrumentation)
if (!instrumented) {
this._instrumented.set(instrumentation, instrumented = new Set())
}
if (!instrumented.has(moduleExports)) {
instrumented.add(moduleExports)
return instrumentation.patch.call(this, moduleExports, this._tracer._tracer, config)
}
}
unpatch (instrumentation) {
const instrumented = this._instrumented.get(instrumentation)
if (instrumented) {
instrumented.forEach(moduleExports => {
try {
instrumentation.unpatch.call(this, moduleExports, this._tracer)
} catch (e) {
log.error(e)
}
})
}
}
_set (plugin, meta) {
this._plugins.set(plugin, meta)
this.load(plugin, meta)
}
}
module.exports = Instrumenter