Skip to content

Commit fac5c7d

Browse files
tlhunterclaude
andcommitted
test(config): add integration tests for pm2_env expansion in init.js
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f7e2538 commit fac5c7d

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

integration-tests/init.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,56 @@ describe('init.js', () => {
272272

273273
testInjectionScenarios('require', 'init.js', false)
274274
testRuntimeVersionChecks('require', 'init.js')
275+
276+
describe('PM2 cluster mode', () => {
277+
useEnv({ NODE_OPTIONS: '--require dd-trace/init' })
278+
279+
afterEach(() => {
280+
delete process.env.pm2_env
281+
})
282+
283+
function checkEnv (expectedValues) {
284+
return testFile('init/pm2-env.js', out => {
285+
const env = JSON.parse(out.trim())
286+
for (const [key, value] of Object.entries(expectedValues)) {
287+
assert.strictEqual(env[key], value, `expected env.${key} to equal ${value}`)
288+
}
289+
}, [], '')
290+
}
291+
292+
it('applies all env vars from pm2_env blob to process.env', () => {
293+
process.env.pm2_env = JSON.stringify({ DD_SERVICE: 'pm2-svc', DD_ENV: 'pm2-env', MY_APP_VAR: 'hello' })
294+
return checkEnv({ DD_SERVICE: 'pm2-svc', DD_ENV: 'pm2-env', MY_APP_VAR: 'hello' })
295+
})
296+
297+
it('coerces non-string values to strings', () => {
298+
process.env.pm2_env = JSON.stringify({ DD_TRACE_SAMPLE_RATE: 0.5 })
299+
return checkEnv({ DD_TRACE_SAMPLE_RATE: '0.5' })
300+
})
301+
302+
it('skips keys with null values', () => {
303+
process.env.pm2_env = JSON.stringify({ DD_SERVICE: null })
304+
return checkEnv({ DD_SERVICE: undefined })
305+
})
306+
307+
it('does not crash on malformed pm2_env JSON', () => {
308+
process.env.pm2_env = 'not-valid-json'
309+
return checkEnv({ DD_SERVICE: undefined })
310+
})
311+
312+
it('does nothing when pm2_env is absent', () => {
313+
return checkEnv({ DD_SERVICE: undefined })
314+
})
315+
316+
describe('when env vars are already set', () => {
317+
useEnv({ DD_SERVICE: 'original-service', MY_APP_VAR: 'original' })
318+
319+
it('overwrites existing env vars with pm2_env values', () => {
320+
process.env.pm2_env = JSON.stringify({ DD_SERVICE: 'pm2-service', MY_APP_VAR: 'pm2-value' })
321+
return checkEnv({ DD_SERVICE: 'pm2-service', MY_APP_VAR: 'pm2-value' })
322+
})
323+
})
324+
})
275325
})
276326

277327
// ESM is not supportable prior to Node.js 14.13.1 on the 14.x line,

integration-tests/init/pm2-env.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
// Fixture for PM2 cluster mode integration tests.
4+
// Prints select process.env values as JSON so tests can assert pm2_env expansion.
5+
// eslint-disable-next-line no-console
6+
console.log(JSON.stringify({
7+
DD_SERVICE: process.env.DD_SERVICE,
8+
DD_ENV: process.env.DD_ENV,
9+
DD_TRACE_SAMPLE_RATE: process.env.DD_TRACE_SAMPLE_RATE,
10+
MY_APP_VAR: process.env.MY_APP_VAR,
11+
}))
12+
process.exit()

0 commit comments

Comments
 (0)