|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { EOL } = require('node:os') |
| 4 | +const { spawn } = require('node:child_process') |
| 5 | +const { randomUUID } = require('node:crypto') |
| 6 | + |
| 7 | +const { assert } = require('chai') |
| 8 | +const getPort = require('get-port') |
| 9 | +const Axios = require('axios') |
| 10 | + |
| 11 | +const { createSandbox, FakeAgent, assertObjectContains } = require('../helpers') |
| 12 | +const { generateProbeConfig } = require('../../packages/dd-trace/test/debugger/devtools_client/utils') |
| 13 | + |
| 14 | +// A race condition exists where the tracer receives a probe via RC, before Node.js has had a chance to load all the JS |
| 15 | +// files from disk. If this race condition is triggered, it results in the tracer either not being able to find any |
| 16 | +// script to attach the probe to, or, if the probe filename is a bit generic, it finds an incorrect match. |
| 17 | +// |
| 18 | +// If it can't find any script matching the expected filename, instead of emitting an `INSTALLED` event, it emits an |
| 19 | +// `ERROR` event with the message: `No loaded script found for <file>`. |
| 20 | +// |
| 21 | +// If it finds any incorrect match, where it's possible to attach a breakpoint to the line requested, the race |
| 22 | +// condition is a little less obvious, as the tracer just attaches to this wrong script. In this case, the test |
| 23 | +// behavior depends on where the breakpoint ends up being attached. In most cases, the test just times out, as the |
| 24 | +// breakpoint is never exercised during the test, and the tracer therefore never emits the `EMITTING` event. |
| 25 | +// |
| 26 | +// This is only really an issue if Node.js is using the ESM loader, as this is really slow. If the application is |
| 27 | +// purely a CommonJS application, this race condtion will probably never be triggered. |
| 28 | +// |
| 29 | +// This test tries to trigger the race condition. However, it doesn't always happen, so it runs multiple times. |
| 30 | +describe('Dynamic Instrumentation Probe Re-Evaluation', function () { |
| 31 | + let sandbox |
| 32 | + |
| 33 | + before(async function () { |
| 34 | + sandbox = await createSandbox( |
| 35 | + undefined, |
| 36 | + undefined, |
| 37 | + // Ensure the test scripts live in the root of the sandbox so they are always the shortest path when |
| 38 | + // `findScriptFromPartialPath` is called |
| 39 | + ['./integration-tests/debugger/target-app/re-evaluation/*'] |
| 40 | + ) |
| 41 | + }) |
| 42 | + |
| 43 | + after(async function () { |
| 44 | + await sandbox?.remove() |
| 45 | + }) |
| 46 | + |
| 47 | + describe('Could not find source file', genTestsForSourceFile('unique-filename.js')) |
| 48 | + |
| 49 | + describe('Initially finds the wrong source file', genTestsForSourceFile('index.js')) |
| 50 | + |
| 51 | + function genTestsForSourceFile (sourceFile) { |
| 52 | + return function () { |
| 53 | + let rcConfig, appPort, agent, proc, axios |
| 54 | + |
| 55 | + beforeEach(async function () { |
| 56 | + rcConfig = { |
| 57 | + product: 'LIVE_DEBUGGING', |
| 58 | + id: `logProbe_${randomUUID()}`, |
| 59 | + config: generateProbeConfig({ sourceFile, line: 4 }) |
| 60 | + } |
| 61 | + appPort = await getPort() |
| 62 | + agent = await new FakeAgent().start() |
| 63 | + proc = spawn( |
| 64 | + process.execPath, |
| 65 | + ['--import', 'dd-trace/initialize.mjs', sourceFile], |
| 66 | + { |
| 67 | + cwd: sandbox.folder, |
| 68 | + env: { |
| 69 | + APP_PORT: appPort, |
| 70 | + DD_DYNAMIC_INSTRUMENTATION_ENABLED: true, |
| 71 | + DD_TRACE_AGENT_PORT: agent.port, |
| 72 | + DD_TRACE_DEBUG: process.env.DD_TRACE_DEBUG // inherit to make debugging the sandbox easier |
| 73 | + } |
| 74 | + } |
| 75 | + ) |
| 76 | + proc |
| 77 | + .on('exit', (code) => { |
| 78 | + if (code !== 0) { |
| 79 | + throw new Error(`Child process exited with code ${code}`) |
| 80 | + } |
| 81 | + }) |
| 82 | + .on('error', (error) => { |
| 83 | + throw error |
| 84 | + }) |
| 85 | + proc.stdout.on('data', log.bind(null, '[child process stdout]')) |
| 86 | + proc.stderr.on('data', log.bind(null, '[child process stderr]')) |
| 87 | + axios = Axios.create({ |
| 88 | + baseURL: `http://localhost:${appPort}` |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + afterEach(async function () { |
| 93 | + proc?.kill(0) |
| 94 | + await agent?.stop() |
| 95 | + }) |
| 96 | + |
| 97 | + for (let attempt = 1; attempt <= 5; attempt++) { |
| 98 | + const testName = 'should attach probe to the right script, ' + |
| 99 | + 'even if it is not loaded when the probe is received ' + |
| 100 | + `(attempt ${attempt})` |
| 101 | + |
| 102 | + it(testName, function (done) { |
| 103 | + this.timeout(5000) |
| 104 | + |
| 105 | + const probeId = rcConfig.config.id |
| 106 | + const expectedPayloads = [{ |
| 107 | + ddsource: 'dd_debugger', |
| 108 | + service: 're-evaluation-test', |
| 109 | + debugger: { diagnostics: { probeId, probeVersion: 0, status: 'RECEIVED' } } |
| 110 | + }, { |
| 111 | + ddsource: 'dd_debugger', |
| 112 | + service: 're-evaluation-test', |
| 113 | + debugger: { diagnostics: { probeId, probeVersion: 0, status: 'INSTALLED' } } |
| 114 | + }, { |
| 115 | + ddsource: 'dd_debugger', |
| 116 | + service: 're-evaluation-test', |
| 117 | + debugger: { diagnostics: { probeId, probeVersion: 0, status: 'EMITTING' } } |
| 118 | + }] |
| 119 | + |
| 120 | + agent.on('debugger-diagnostics', async ({ payload }) => { |
| 121 | + await Promise.all(payload.map(async (event) => { |
| 122 | + if (event.debugger.diagnostics.status === 'ERROR') { |
| 123 | + // shortcut to fail with a more relevent error message in case the target script could not be found, |
| 124 | + // instead of asserting the entire expected event. |
| 125 | + assert.fail(event.debugger.diagnostics.exception.message) |
| 126 | + } |
| 127 | + |
| 128 | + const expected = expectedPayloads.shift() |
| 129 | + assertObjectContains(event, expected) |
| 130 | + |
| 131 | + if (event.debugger.diagnostics.status === 'INSTALLED') { |
| 132 | + const response = await axios.get('/') |
| 133 | + assert.strictEqual(response.status, 200) |
| 134 | + } |
| 135 | + })) |
| 136 | + |
| 137 | + if (expectedPayloads.length === 0) done() |
| 138 | + }) |
| 139 | + |
| 140 | + agent.addRemoteConfig(rcConfig) |
| 141 | + }) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +}) |
| 146 | + |
| 147 | +function log (prefix, data) { |
| 148 | + const msg = data.toString().trim().split(EOL).map((line) => `${prefix} ${line}`).join(EOL) |
| 149 | + console.log(msg) // eslint-disable-line no-console |
| 150 | +} |
0 commit comments