Skip to content

Commit 5620b0b

Browse files
authored
fix: fix esm instrument express-session regression (#6741)
1 parent 43efd4d commit 5620b0b

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

.github/workflows/apm-integrations.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ jobs:
384384
- uses: ./.github/actions/install
385385
- run: yarn test:plugins:ci
386386

387+
cookie-session:
388+
runs-on: ubuntu-latest
389+
env:
390+
PLUGINS: express-session
391+
steps:
392+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
393+
- uses: ./.github/actions/plugins/test
394+
with:
395+
dd_api_key: ${{ secrets.DD_API_KEY }}
396+
387397
fastify:
388398
runs-on: ubuntu-latest
389399
env:

integration-tests/helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ async function createSandbox (
368368
*/
369369
function varySandbox (sandbox, filename, variants, namedVariant, packageName = variants) {
370370
if (typeof variants === 'string') {
371-
const bindingName = variants
371+
const bindingName = namedVariant || variants
372372
variants = {
373373
default: `import ${bindingName} from '${packageName}'`,
374374
star: namedVariant

packages/datadog-instrumentations/src/express-session.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ addHook({
3737
name: 'express-session',
3838
versions: ['>=1.5.0']
3939
}, session => {
40+
if (session.default) return session
4041
return shimmer.wrapFunction(session, wrapSession)
4142
})

packages/datadog-plugin-cookie-parser/test/integration-test/client.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ withVersions('cookie-parser', 'cookie-parser', version => {
1515
this.timeout(50000)
1616
sandbox = await createSandbox([`'cookie-parser@${version}'`, 'express'], false,
1717
['./packages/datadog-plugin-cookie-parser/test/integration-test/*'])
18-
variants = varySandbox(sandbox, 'server.mjs', 'cookie-parser', 'cookieParser')
18+
variants = varySandbox(sandbox, 'server.mjs', 'cookieParser', undefined, 'cookie-parser')
1919
})
2020

2121
after(async function () {
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('express-session', 'express-session', version => {
11+
describe('ESM', () => {
12+
let sandbox, variants, proc, agent
13+
14+
before(async function () {
15+
this.timeout(50000)
16+
sandbox = await createSandbox([`'express-session@${version}'`, 'express'], false,
17+
['./packages/datadog-plugin-express-session/test/integration-test/*'])
18+
variants = varySandbox(sandbox, 'server.mjs', 'expressSession', undefined, 'express-session')
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'dd-trace/init.js'
2+
import express from 'express'
3+
import expressSession from 'express-session'
4+
import dc from 'dc-polyfill'
5+
6+
const sessionMiddlewareCh = dc.channel('datadog:express-session:middleware:finish')
7+
let counter = 0
8+
sessionMiddlewareCh.subscribe(() => {
9+
counter += 1
10+
})
11+
12+
const app = express()
13+
14+
app.use(expressSession({
15+
secret: 'secret',
16+
resave: false,
17+
rolling: true,
18+
saveUninitialized: true,
19+
genid: () => 'sid_123'
20+
}))
21+
22+
app.get('/', (req, res) => {
23+
res.setHeader('X-Counter', counter)
24+
res.send('OK')
25+
})
26+
27+
const server = app.listen(0, () => {
28+
const port = server.address().port
29+
process.send({ port })
30+
})

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ 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
2121
'datadog-plugin-mongoose', // mongoose tracing is done through mongodb-core instrumentation
22-
'datadog-plugin-cookie-parser' // cookie-parser does not produce spans
22+
'datadog-plugin-cookie-parser', // cookie-parser does not produce spans
23+
'datadog-plugin-express-session' // express-session does not produce spans
2324
]
2425

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

0 commit comments

Comments
 (0)