Skip to content

Commit 31cdd5d

Browse files
authored
fix tracer metadata when optional dependencies are missing (#7143)
1 parent 0e7563d commit 31cdd5d

3 files changed

Lines changed: 69 additions & 44 deletions

File tree

integration-tests/helpers/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ async function createSandbox (
309309
addFlags.push('--prefer-offline')
310310
}
311311

312+
if (process.env.OMIT) {
313+
addFlags.push(...process.env.OMIT.split(',').map(omit => `--omit=${omit}`))
314+
}
315+
312316
if (DEBUG !== 'true') {
313317
addFlags.push('--silent')
314318
}

integration-tests/startup.spec.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,33 @@ const execArgvs = [
2525
{
2626
execArgv: ['--loader', 'dd-trace/loader-hook.mjs'],
2727
skip: semver.satisfies(process.versions.node, '>=20.6')
28+
},
29+
{
30+
execArgv: [],
31+
optional: false
2832
}
2933
]
3034

31-
execArgvs.forEach(({ execArgv, skip }) => {
35+
execArgvs.forEach(({ execArgv, skip, optional = true }) => {
3236
const describe = skip ? globalThis.describe.skip : globalThis.describe
3337

34-
describe(`startup ${execArgv.join(' ')}`, () => {
38+
describe(`startup ${execArgv.join(' ').concat(`(optional: ${optional})`)}`, () => {
3539
let agent
3640
let proc
3741
let cwd
3842
let startupTestFile
3943
let unsupportedTestFile
4044

45+
before(() => {
46+
if (optional === false) {
47+
process.env.OMIT = 'optional'
48+
}
49+
})
50+
51+
after(() => {
52+
delete process.env.OMIT
53+
})
54+
4155
useSandbox(['d3-format@3.1.0'])
4256

4357
before(() => {
@@ -74,38 +88,41 @@ execArgvs.forEach(({ execArgv, skip }) => {
7488
})
7589
})
7690

77-
it('saves tracer configuration on disk', async () => {
78-
if (process.platform !== 'linux') {
79-
return
80-
}
81-
82-
proc = await spawnProc(startupTestFile, {
83-
cwd,
84-
execArgv,
85-
env: {
86-
AGENT_PORT: agent.port
91+
// This feature requires libdatadog which is an optional dependency.
92+
if (optional) {
93+
it('saves tracer configuration on disk', async () => {
94+
if (process.platform !== 'linux') {
95+
return
8796
}
88-
})
8997

90-
const containsDatadogMemfd = (fds) => {
91-
for (const fd of fds) {
92-
try {
93-
const fdName = fs.readlinkSync(`/proc/${proc.pid}/fd/${fd}`)
94-
if (fdName.includes('datadog-tracer-info-')) {
95-
return true
96-
}
97-
} catch {}
98+
proc = await spawnProc(startupTestFile, {
99+
cwd,
100+
execArgv,
101+
env: {
102+
AGENT_PORT: agent.port
103+
}
104+
})
105+
106+
const containsDatadogMemfd = (fds) => {
107+
for (const fd of fds) {
108+
try {
109+
const fdName = fs.readlinkSync(`/proc/${proc.pid}/fd/${fd}`)
110+
if (fdName.includes('datadog-tracer-info-')) {
111+
return true
112+
}
113+
} catch {}
114+
}
115+
return false
98116
}
99-
return false
100-
}
101117

102-
const fds = fs.readdirSync(`/proc/${proc.pid}/fd`)
118+
const fds = fs.readdirSync(`/proc/${proc.pid}/fd`)
103119

104-
assert(
105-
containsDatadogMemfd(fds),
106-
`FDs ${inspect(fds)} of PID ${proc.pid} did not contain the datadog tracer configuration in memfd`
107-
)
108-
})
120+
assert(
121+
containsDatadogMemfd(fds),
122+
`FDs ${inspect(fds)} of PID ${proc.pid} did not contain the datadog tracer configuration in memfd`
123+
)
124+
})
125+
}
109126

110127
it('works for options.url', async () => {
111128
proc = await spawnProc(startupTestFile, {
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
'use strict'
22

3-
// Load binding first to not import other modules if it throws
4-
const libdatadog = require('@datadog/libdatadog')
53
const tracerVersion = require('../../../version').VERSION
64

75
function storeConfig (config) {
8-
const processDiscovery = libdatadog.maybeLoad('process-discovery')
9-
if (processDiscovery === undefined) {
10-
return
11-
}
6+
try {
7+
// Load binding first to not import other modules if it throws
8+
const libdatadog = require('@datadog/libdatadog')
9+
const processDiscovery = libdatadog.maybeLoad('process-discovery')
10+
if (processDiscovery === undefined) {
11+
return
12+
}
1213

13-
const metadata = new processDiscovery.TracerMetadata(
14-
config.tags['runtime-id'],
15-
tracerVersion,
16-
config.hostname,
17-
config.service || null,
18-
config.env || null,
19-
config.version || null
20-
)
14+
const metadata = new processDiscovery.TracerMetadata(
15+
config.tags['runtime-id'],
16+
tracerVersion,
17+
config.hostname,
18+
config.service || null,
19+
config.env || null,
20+
config.version || null
21+
)
2122

22-
return processDiscovery.storeMetadata(metadata)
23+
return processDiscovery.storeMetadata(metadata)
24+
} catch {
25+
// Either libdatadog or process-discovery is unavailable.
26+
}
2327
}
2428

2529
module.exports = storeConfig

0 commit comments

Comments
 (0)