Skip to content

Commit e8ac8bf

Browse files
authored
remove async storage from child_process instrumentation (#5960)
1 parent f97d2cd commit e8ac8bf

File tree

3 files changed

+131
-90
lines changed

3 files changed

+131
-90
lines changed

packages/datadog-instrumentations/src/child_process.js

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const { errorMonitor } = require('events')
44
const util = require('util')
55

66
const {
7-
addHook,
8-
AsyncResource
7+
addHook
98
} = require('./helpers/instrument')
109
const shimmer = require('../../datadog-shimmer')
1110
const dc = require('dc-polyfill')
@@ -80,7 +79,8 @@ function createContextFromChildProcessInfo (childProcessInfo) {
8079
const context = {
8180
command: childProcessInfo.command,
8281
file: childProcessInfo.file,
83-
shell: childProcessInfo.shell
82+
shell: childProcessInfo.shell,
83+
abortController: new AbortController()
8484
}
8585

8686
if (childProcessInfo.fileArgs) {
@@ -98,17 +98,12 @@ function wrapChildProcessSyncMethod (returnError, shell = false) {
9898
}
9999

100100
const childProcessInfo = normalizeArgs(arguments, shell)
101+
const context = createContextFromChildProcessInfo(childProcessInfo)
101102

102-
const innerResource = new AsyncResource('bound-anonymous-fn')
103-
return innerResource.runInAsyncScope(() => {
104-
const context = createContextFromChildProcessInfo(childProcessInfo)
105-
const abortController = new AbortController()
106-
107-
childProcessChannel.start.publish({ ...context, abortController })
108-
103+
return childProcessChannel.start.runStores(context, () => {
109104
try {
110-
if (abortController.signal.aborted) {
111-
const error = abortController.signal.reason || new Error('Aborted')
105+
if (context.abortController.signal.aborted) {
106+
const error = context.abortController.signal.reason || new Error('Aborted')
112107
// expected behaviors on error are different
113108
return returnError(error, context)
114109
}
@@ -141,21 +136,17 @@ function wrapChildProcessCustomPromisifyMethod (customPromisifyMethod, shell) {
141136
const context = createContextFromChildProcessInfo(childProcessInfo)
142137

143138
const { start, end, asyncStart, asyncEnd, error } = childProcessChannel
144-
const abortController = new AbortController()
145-
146-
start.publish({
147-
...context,
148-
abortController
149-
})
139+
start.publish(context)
150140

151141
let result
152-
if (abortController.signal.aborted) {
153-
result = Promise.reject(abortController.signal.reason || new Error('Aborted'))
142+
if (context.abortController.signal.aborted) {
143+
result = Promise.reject(context.abortController.signal.reason || new Error('Aborted'))
154144
} else {
155145
try {
156146
result = customPromisifyMethod.apply(this, arguments)
157147
} catch (error) {
158-
error.publish({ ...context, error })
148+
context.error = error
149+
error.publish(context)
159150
throw error
160151
} finally {
161152
end.publish(context)
@@ -192,26 +183,15 @@ function wrapChildProcessAsyncMethod (ChildProcess, shell = false) {
192183

193184
const childProcessInfo = normalizeArgs(arguments, shell)
194185

195-
const cb = arguments[arguments.length - 1]
196-
if (typeof cb === 'function') {
197-
const callbackResource = new AsyncResource('bound-anonymous-fn')
198-
arguments[arguments.length - 1] = callbackResource.bind(cb)
199-
}
200-
201-
const innerResource = new AsyncResource('bound-anonymous-fn')
202-
return innerResource.runInAsyncScope(() => {
203-
const context = createContextFromChildProcessInfo(childProcessInfo)
204-
const abortController = new AbortController()
205-
206-
childProcessChannel.start.publish({ ...context, abortController })
207-
186+
const context = createContextFromChildProcessInfo(childProcessInfo)
187+
return childProcessChannel.start.runStores(context, () => {
208188
let childProcess
209-
if (abortController.signal.aborted) {
189+
if (context.abortController.signal.aborted) {
210190
childProcess = new ChildProcess()
211191
childProcess.on('error', () => {}) // Original method does not crash when non subscribers
212192

213193
process.nextTick(() => {
214-
const error = abortController.signal.reason || new Error('Aborted')
194+
const error = context.abortController.signal.reason || new Error('Aborted')
215195
childProcess.emit('error', error)
216196

217197
const cb = arguments[arguments.length - 1]
@@ -230,17 +210,16 @@ function wrapChildProcessAsyncMethod (ChildProcess, shell = false) {
230210

231211
childProcess.on(errorMonitor, (e) => {
232212
errorExecuted = true
233-
childProcessChannel.error.publish(e)
213+
context.error = e
214+
childProcessChannel.error.publish(context)
234215
})
235216

236217
childProcess.on('close', (code = 0) => {
237218
if (!errorExecuted && code !== 0) {
238-
childProcessChannel.error.publish()
219+
childProcessChannel.error.publish(context)
239220
}
240-
childProcessChannel.asyncEnd.publish({
241-
...context,
242-
result: code
243-
})
221+
context.result = code
222+
childProcessChannel.asyncEnd.publish(context)
244223
})
245224
}
246225

0 commit comments

Comments
 (0)