|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const childProcess = require('node:child_process') |
| 5 | +const { fork } = childProcess |
| 6 | +const path = require('node:path') |
| 7 | + |
| 8 | +const satisfies = require('semifies') |
| 9 | + |
| 10 | +const { Profile } = require('../../vendor/dist/pprof-format') |
| 11 | +const { |
| 12 | + FakeAgent, |
| 13 | + sandboxCwd, |
| 14 | + stopProc, |
| 15 | + useSandbox, |
| 16 | +} = require('../helpers') |
| 17 | +const { processExitPromise } = require('./helpers') |
| 18 | + |
| 19 | +const TIMEOUT = 30000 |
| 20 | +const isAtLeast26 = satisfies(process.versions.node, '>=26.0.0') |
| 21 | + |
| 22 | +function getString (strings, value) { |
| 23 | + const index = typeof value?.toNumber === 'function' ? value.toNumber() : Number(value) |
| 24 | + return strings[index] |
| 25 | +} |
| 26 | + |
| 27 | +function getSampleTypeNames (profile) { |
| 28 | + const strings = profile.stringTable.strings |
| 29 | + return profile.sampleType.map(sampleType => getString(strings, sampleType.type)) |
| 30 | +} |
| 31 | + |
| 32 | +function findFile (files, originalname) { |
| 33 | + const file = files.find(file => file.originalname === originalname) |
| 34 | + assert.ok(file, `Expected ${originalname} attachment`) |
| 35 | + return file |
| 36 | +} |
| 37 | + |
| 38 | +function expectProfileUpload (agent) { |
| 39 | + let upload |
| 40 | + |
| 41 | + const messagePromise = agent.assertMessageReceived(({ files }) => { |
| 42 | + assert.ok(files, 'Expected profiling upload') |
| 43 | + |
| 44 | + upload = { |
| 45 | + event: JSON.parse(findFile(files, 'event.json').buffer.toString()), |
| 46 | + spaceProfile: Profile.decode(findFile(files, 'space.pprof').buffer), |
| 47 | + } |
| 48 | + }, TIMEOUT) |
| 49 | + |
| 50 | + return messagePromise.then(() => upload) |
| 51 | +} |
| 52 | + |
| 53 | +describe('allocation profiler', () => { |
| 54 | + let agent |
| 55 | + let cwd |
| 56 | + let proc |
| 57 | + let profilerTestFile |
| 58 | + |
| 59 | + useSandbox() |
| 60 | + |
| 61 | + before(() => { |
| 62 | + cwd = sandboxCwd() |
| 63 | + profilerTestFile = path.join(cwd, 'profiler/allocation.js') |
| 64 | + }) |
| 65 | + |
| 66 | + beforeEach(async () => { |
| 67 | + agent = await new FakeAgent().start() |
| 68 | + }) |
| 69 | + |
| 70 | + afterEach(async () => { |
| 71 | + await stopProc(proc) |
| 72 | + await agent.stop() |
| 73 | + }) |
| 74 | + |
| 75 | + it('sends heap profiles with the expected sample types on Node.js 26+', async function () { |
| 76 | + if (!isAtLeast26) { |
| 77 | + this.skip() |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + const cases = [ |
| 82 | + { |
| 83 | + allocationProfilingEnabled: false, |
| 84 | + sampleTypes: ['objects', 'space'], |
| 85 | + }, |
| 86 | + { |
| 87 | + allocationProfilingEnabled: true, |
| 88 | + sampleTypes: ['inuse_objects', 'alloc_objects', 'inuse_space', 'alloc_space'], |
| 89 | + }, |
| 90 | + ] |
| 91 | + |
| 92 | + for (const { allocationProfilingEnabled, sampleTypes } of cases) { |
| 93 | + proc = fork(profilerTestFile, { |
| 94 | + cwd, |
| 95 | + env: { |
| 96 | + DD_TRACE_AGENT_PORT: agent.port, |
| 97 | + DD_PROFILING_ALLOCATION_ENABLED: allocationProfilingEnabled ? '1' : '0', |
| 98 | + DD_PROFILING_DEBUG_UPLOAD_COMPRESSION: 'off', |
| 99 | + DD_PROFILING_EXPORTERS: 'agent', |
| 100 | + DD_PROFILING_PROFILERS: 'space', |
| 101 | + DD_PROFILING_SOURCE_MAP: '0', |
| 102 | + DD_PROFILING_UPLOAD_PERIOD: '1', |
| 103 | + TEST_DURATION_MS: '5000', |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + const [ |
| 108 | + { event, spaceProfile }, |
| 109 | + ] = await Promise.all([ |
| 110 | + expectProfileUpload(agent), |
| 111 | + processExitPromise(proc, TIMEOUT), |
| 112 | + ]) |
| 113 | + |
| 114 | + assert.deepStrictEqual(event.attachments, ['space.pprof']) |
| 115 | + assert.strictEqual(event.info.profiler.settings.allocationProfilingEnabled, allocationProfilingEnabled) |
| 116 | + assert.deepStrictEqual(getSampleTypeNames(spaceProfile), sampleTypes) |
| 117 | + |
| 118 | + await stopProc(proc) |
| 119 | + proc = undefined |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + it('does not crash when allocation profiling is requested on unsupported Node.js versions', async function () { |
| 124 | + if (isAtLeast26) { |
| 125 | + this.skip() |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + proc = fork(profilerTestFile, { |
| 130 | + cwd, |
| 131 | + env: { |
| 132 | + DD_PROFILING_ALLOCATION_ENABLED: '1', |
| 133 | + DD_PROFILING_DEBUG_UPLOAD_COMPRESSION: 'off', |
| 134 | + DD_PROFILING_EXPORTERS: 'file', |
| 135 | + DD_PROFILING_PROFILERS: 'space', |
| 136 | + DD_PROFILING_SOURCE_MAP: '0', |
| 137 | + DD_PROFILING_UPLOAD_PERIOD: '1', |
| 138 | + TEST_DURATION_MS: '5000', |
| 139 | + }, |
| 140 | + }) |
| 141 | + |
| 142 | + await processExitPromise(proc, TIMEOUT) |
| 143 | + }) |
| 144 | +}) |
0 commit comments