Skip to content

Commit b12f8fe

Browse files
pabloerhardjuan-fernandez
authored andcommitted
fix(esm): do not wrap .default when instrumenting cookie-parser (#6708)
* Fix to cookie-parser failing to instrument * Added cookie-parser test and excluded it from plugin tests
1 parent 2679556 commit b12f8fe

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

.github/workflows/apm-integrations.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ jobs:
249249
api_key: ${{ secrets.DD_API_KEY }}
250250
service: dd-trace-js-tests
251251

252+
cookie-parser:
253+
runs-on: ubuntu-latest
254+
env:
255+
PLUGINS: cookie-parser
256+
steps:
257+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
258+
- uses: ./.github/actions/plugins/test
259+
with:
260+
dd_api_key: ${{ secrets.DD_API_KEY }}
261+
252262
couchbase:
253263
strategy:
254264
matrix:

packages/datadog-instrumentations/src/cookie-parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ addHook({
2525
name: 'cookie-parser',
2626
versions: ['>=1.0.0']
2727
}, cookieParser => {
28+
// This prevents the non default export from entering the wrapping process
29+
if (cookieParser.default) return cookieParser
2830
return shimmer.wrapFunction(cookieParser, cookieParser => function () {
2931
const cookieMiddleware = cookieParser.apply(this, arguments)
3032

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const {
4+
createSandbox, varySandbox, curl,
5+
FakeAgent, spawnPluginIntegrationTestProc
6+
} = require('../../../../integration-tests/helpers')
7+
const { assert } = require('chai')
8+
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
9+
10+
withVersions('cookie-parser', 'cookie-parser', version => {
11+
describe('ESM', () => {
12+
let sandbox, variants, proc, agent
13+
14+
before(async function () {
15+
this.timeout(50000)
16+
sandbox = await createSandbox([`'cookie-parser@${version}'`, 'express'], false,
17+
['./packages/datadog-plugin-cookie-parser/test/integration-test/*'])
18+
variants = varySandbox(sandbox, 'server.mjs', 'cookie-parser', 'cookieParser')
19+
})
20+
21+
after(async function () {
22+
this.timeout(50000)
23+
await sandbox.remove()
24+
})
25+
26+
beforeEach(async () => {
27+
agent = await new FakeAgent().start()
28+
})
29+
30+
afterEach(async () => {
31+
proc?.kill()
32+
await agent.stop()
33+
})
34+
35+
for (const variant of varySandbox.VARIANTS) {
36+
it(`is instrumented loaded with ${variant}`, async () => {
37+
proc = await spawnPluginIntegrationTestProc(sandbox.folder, variants[variant], agent.port)
38+
const response = await curl(proc)
39+
assert.equal(response.headers['x-counter'], '1')
40+
})
41+
}
42+
})
43+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dd-trace/init.js'
2+
import express from 'express'
3+
import cookieParser from 'cookie-parser'
4+
import dc from 'dc-polyfill'
5+
const cookieParserReadCh = dc.channel('datadog:cookie-parser:read:finish')
6+
let counter = 0
7+
cookieParserReadCh.subscribe(() => {
8+
counter += 1
9+
})
10+
const app = express()
11+
12+
app.use(cookieParser())
13+
app.use((req, res) => {
14+
res.setHeader('X-Counter', counter)
15+
res.end('hello, world\n')
16+
})
17+
18+
const server = app.listen(0, () => {
19+
const port = server.address().port
20+
process.send({ port })
21+
})

packages/dd-trace/test/plugins/plugin-structure.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const abstractPlugins = [
1818
const missingPlugins = [
1919
'datadog-plugin-axios', // we test axios to ensure our functionality works with axios, see: https://github.com/DataDog/dd-trace-js/pull/1469
2020
'datadog-plugin-limitd-client', // limitd-client instrumentation handles trace context propagation, no tracing is done
21-
'datadog-plugin-mongoose' // mongoose tracing is done through mongodb-core instrumentation
21+
'datadog-plugin-mongoose', // mongoose tracing is done through mongodb-core instrumentation
22+
'datadog-plugin-cookie-parser' // cookie-parser does not produce spans
2223
]
2324

2425
// instrumentations that do not have a hook, but are still instrumented

0 commit comments

Comments
 (0)