Skip to content

Commit 29704cf

Browse files
authored
fix(esbuild): call the hook function properly in the bundler register (#5934)
* fix hook function * add test * call hook function only if it exists
1 parent 205a001 commit 29704cf

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
const fs = require('fs')
4+
const { spawnSync } = require('child_process')
5+
6+
const ddPlugin = require('../../esbuild') // dd-trace/esbuild
7+
const esbuild = require('esbuild')
8+
9+
const SCRIPT = './openai-out.js'
10+
11+
esbuild.build({
12+
entryPoints: ['openai.js'],
13+
bundle: true,
14+
outfile: SCRIPT,
15+
plugins: [ddPlugin],
16+
platform: 'node',
17+
target: ['node18'],
18+
external: []
19+
}).then(() => {
20+
const { status, stdout, stderr } = spawnSync('node', [SCRIPT])
21+
if (stdout.length) {
22+
console.log(stdout.toString())
23+
}
24+
if (stderr.length) {
25+
console.error(stderr.toString())
26+
}
27+
if (status) {
28+
throw new Error('generated script failed to run')
29+
}
30+
console.log('ok')
31+
}).catch((err) => {
32+
console.error(err)
33+
process.exit(1)
34+
}).finally(() => {
35+
fs.rmSync(SCRIPT, { force: true })
36+
})

integration-tests/esbuild/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ esbuildVersions.forEach((version) => {
7777
})
7878
})
7979

80+
it('handles instrumentations where the patching function is a property of the hook', () => {
81+
const command = 'node ./build-and-test-openai.js'
82+
console.log(command)
83+
chproc.execSync(command, {
84+
timeout: 1000 * 30
85+
})
86+
})
87+
8088
describe('ESM', () => {
8189
afterEach(() => {
8290
fs.rmSync('./out.mjs', { force: true })

integration-tests/esbuild/openai.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('../../').init()
2+
require('openai')

integration-tests/esbuild/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"esbuild": "*",
2828
"express": "^4.16.2",
2929
"knex": "*",
30-
"koa": "*"
30+
"koa": "*",
31+
"openai": "*"
3132
}
3233
}

packages/datadog-instrumentations/src/helpers/bundler-register.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,29 @@ if (!dc.unsubscribe) {
2626
}
2727
}
2828

29-
dc.subscribe(CHANNEL, (payload) => {
30-
try {
31-
hooks[payload.package]()
32-
} catch (err) {
29+
function doHook (payload) {
30+
const hook = hooks[payload.package]
31+
if (!hook) {
3332
log.error('esbuild-wrapped %s missing in list of hooks', payload.package)
34-
throw err
33+
return
34+
}
35+
36+
const hookFn = hook.fn ?? hook
37+
if (typeof hookFn !== 'function') {
38+
log.error('esbuild-wrapped hook %s is not a function', payload.package)
39+
return
3540
}
3641

42+
try {
43+
hookFn()
44+
} catch {
45+
log.error('esbuild-wrapped %s hook failed', payload.package)
46+
}
47+
}
48+
49+
dc.subscribe(CHANNEL, (payload) => {
50+
doHook(payload)
51+
3752
if (!instrumentations[payload.package]) {
3853
log.error('esbuild-wrapped %s missing in list of instrumentations', payload.package)
3954
return

0 commit comments

Comments
 (0)