|
3 | 3 | const assert = require('node:assert') |
4 | 4 | const { setup } = require('./utils') |
5 | 5 |
|
6 | | -const tolerance = 5 |
| 6 | +const DEFAULT_MAX_COLLECTION_SIZE = 100 |
| 7 | +const COLLECTION_SIZE_THRESHOLD = 500 |
7 | 8 |
|
8 | 9 | describe('Dynamic Instrumentation', function () { |
9 | 10 | describe('input messages', function () { |
10 | 11 | describe('with snapshot under tight time budget', function () { |
11 | 12 | context('1ms time budget', function () { |
12 | 13 | // Force a very small time budget in ms to trigger partial snapshots |
13 | 14 | const target = 1 |
| 15 | + const tolerance = 5 |
14 | 16 | const t = setup({ |
15 | 17 | dependencies: ['fastify'], |
16 | 18 | env: { DD_DYNAMIC_INSTRUMENTATION_CAPTURE_TIMEOUT_MS: String(target) } |
17 | 19 | }) |
18 | 20 |
|
19 | 21 | it( |
20 | 22 | 'should include partial snapshot marked with notCapturedReason: timeout', |
21 | | - test({ t, maxPausedTime: target + tolerance, breakpointIndex: 0, maxReferenceDepth: 5 }) |
| 23 | + test({ t, maxPausedTime: target + tolerance, breakpointIndex: 0, maxReferenceDepth: 5 }, (locals) => { |
| 24 | + assert.strictEqual( |
| 25 | + containsTimeBudget(locals), |
| 26 | + true, |
| 27 | + 'expected at least one field/element to be marked with notCapturedReason: "timeout"' |
| 28 | + ) |
| 29 | + }) |
22 | 30 | ) |
23 | 31 | }) |
24 | 32 |
|
25 | 33 | context('default time budget', function () { |
26 | 34 | const target = 10 // default time budget in ms |
| 35 | + const tolerance = 5 |
27 | 36 | const t = setup({ dependencies: ['fastify'] }) |
28 | 37 |
|
29 | | - // TODO: Make this pass |
30 | | - it.skip( |
31 | | - 'should keep budget when state includes an object with 1 million properties', |
32 | | - test({ t, maxPausedTime: target + tolerance, breakpointIndex: 1, maxReferenceDepth: 1 }) |
| 38 | + it( |
| 39 | + 'should timeout first, then disable subsequent snapshots and emit error diagnostics', |
| 40 | + async function () { |
| 41 | + const breakpoint = t.breakpoints[1] |
| 42 | + |
| 43 | + // Listen for the first snapshot payload (should contain notCapturedReason: "timeout") |
| 44 | + const firstPayloadReceived = new Promise((resolve) => { |
| 45 | + t.agent.once('debugger-input', ({ payload: [{ debugger: { snapshot: { captures } } }] }) => { |
| 46 | + const { locals } = captures.lines[breakpoint.line] |
| 47 | + resolve(locals) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + // Prepare to assert that an ERROR diagnostics event with exception details is emitted |
| 52 | + const errorDiagnosticsReceived = new Promise((/** @type {(value?: unknown) => void} */ resolve, reject) => { |
| 53 | + const handler = ({ payload }) => { |
| 54 | + payload.forEach(({ debugger: { diagnostics } }) => { |
| 55 | + if (diagnostics.status !== 'ERROR') return |
| 56 | + try { |
| 57 | + assert.strictEqual( |
| 58 | + diagnostics.exception.message, |
| 59 | + 'An object with more than 500 properties was detected while collecting a snapshot. Future ' + |
| 60 | + 'snapshots for exising probes in this location will be skipped until the Node.js process is ' + |
| 61 | + 'restarted' |
| 62 | + ) |
| 63 | + resolve() |
| 64 | + } catch (e) { |
| 65 | + reject(e) |
| 66 | + } finally { |
| 67 | + t.agent.off('debugger-diagnostics', handler) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | + t.agent.on('debugger-diagnostics', handler) |
| 72 | + }) |
| 73 | + |
| 74 | + // Install probe with snapshot capture enabled |
| 75 | + t.agent.addRemoteConfig(breakpoint.generateRemoteConfig({ |
| 76 | + captureSnapshot: true, |
| 77 | + capture: { maxReferenceDepth: 1 } |
| 78 | + })) |
| 79 | + |
| 80 | + // Trigger once; this run is expected to be slow and mark fields with "timeout" |
| 81 | + const result1 = await breakpoint.triggerBreakpoint() |
| 82 | + assert.ok( |
| 83 | + result1.data.paused >= 1_000, |
| 84 | + `expected thread to be paused for at least 1 second, but was paused for ~${result1.data.paused}ms` |
| 85 | + ) |
| 86 | + const locals = await firstPayloadReceived |
| 87 | + assert.strictEqual( |
| 88 | + containsTimeBudget(locals), |
| 89 | + true, |
| 90 | + 'expected at least one field/element to be marked with notCapturedReason: "timeout"' |
| 91 | + ) |
| 92 | + await errorDiagnosticsReceived |
| 93 | + |
| 94 | + // Prepare to assert that no snapshot is produced on a subsequent trigger |
| 95 | + const noSnapshotAfterSecondTrigger = new Promise((/** @type {(value?: unknown) => void} */ resolve) => { |
| 96 | + t.agent.once('debugger-input', ({ payload: [{ debugger: { snapshot: { captures } } }] }) => { |
| 97 | + assert.strictEqual(captures, undefined) |
| 98 | + resolve() |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + // Trigger the same breakpoint again directly |
| 103 | + const result2 = await t.axios.get(breakpoint.url) |
| 104 | + assert.ok( |
| 105 | + result2.data.paused <= 5, |
| 106 | + `expected thread to be paused <=5ms, but was paused for ~${result2.data.paused}ms` |
| 107 | + ) |
| 108 | + |
| 109 | + await noSnapshotAfterSecondTrigger |
| 110 | + } |
33 | 111 | ) |
34 | 112 |
|
35 | | - // TODO: Make this pass |
36 | | - it.skip( |
37 | | - 'should keep budget when state includes an array of 1 million primitives', |
38 | | - test({ t, maxPausedTime: target + tolerance, breakpointIndex: 2, maxReferenceDepth: 1 }) |
| 113 | + it( |
| 114 | + 'should keep budget when state includes collections with 1 million elements', |
| 115 | + test({ t, maxPausedTime: target + tolerance, breakpointIndex: 2, maxReferenceDepth: 1 }, (locals) => { |
| 116 | + assert.strictEqual(locals.arrOfPrimitives.notCapturedReason, 'collectionSize') |
| 117 | + assert.strictEqual(locals.arrOfPrimitives.size, 1_000_000) |
| 118 | + assert.strictEqual(locals.arrOfPrimitives.elements.length, 0) |
| 119 | + assert.strictEqual(locals.arrOfObjects.notCapturedReason, 'collectionSize') |
| 120 | + assert.strictEqual(locals.arrOfObjects.size, 1_000_000) |
| 121 | + assert.strictEqual(locals.arrOfObjects.elements.length, 0) |
| 122 | + assert.strictEqual(locals.map.notCapturedReason, 'collectionSize') |
| 123 | + assert.strictEqual(locals.map.size, 1_000_000) |
| 124 | + assert.strictEqual(locals.map.entries.length, 0) |
| 125 | + assert.strictEqual(locals.set.notCapturedReason, 'collectionSize') |
| 126 | + assert.strictEqual(locals.set.size, 1_000_000) |
| 127 | + assert.strictEqual(locals.set.elements.length, 0) |
| 128 | + }) |
39 | 129 | ) |
40 | 130 |
|
41 | | - // TODO: Make this pass |
42 | | - it.skip( |
43 | | - 'should keep budget when state includes an array of 1 million objects', |
44 | | - test({ t, maxPausedTime: target + tolerance, breakpointIndex: 3, maxReferenceDepth: 1 }) |
| 131 | + it( |
| 132 | + 'should keep budget when state includes collections with less than the size threshold', |
| 133 | + // Use a slightly larger tolerance to avoid flakiness |
| 134 | + test({ t, maxPausedTime: target + tolerance * 3, breakpointIndex: 3, maxReferenceDepth: 1 }, (locals) => { |
| 135 | + assert.strictEqual(locals.arrOfPrimitives.notCapturedReason, 'collectionSize') |
| 136 | + assert.strictEqual(locals.arrOfPrimitives.size, COLLECTION_SIZE_THRESHOLD - 1) |
| 137 | + assert.strictEqual(locals.arrOfPrimitives.elements.length, DEFAULT_MAX_COLLECTION_SIZE) |
| 138 | + assert.strictEqual(locals.arrOfObjects.notCapturedReason, 'collectionSize') |
| 139 | + assert.strictEqual(locals.arrOfObjects.size, COLLECTION_SIZE_THRESHOLD - 1) |
| 140 | + assert.strictEqual(locals.arrOfObjects.elements.length, DEFAULT_MAX_COLLECTION_SIZE) |
| 141 | + assert.strictEqual(locals.map.notCapturedReason, 'collectionSize') |
| 142 | + assert.strictEqual(locals.map.size, COLLECTION_SIZE_THRESHOLD - 1) |
| 143 | + assert.strictEqual(locals.map.entries.length, DEFAULT_MAX_COLLECTION_SIZE) |
| 144 | + assert.strictEqual(locals.set.notCapturedReason, 'collectionSize') |
| 145 | + assert.strictEqual(locals.set.size, COLLECTION_SIZE_THRESHOLD - 1) |
| 146 | + assert.strictEqual(locals.set.elements.length, DEFAULT_MAX_COLLECTION_SIZE) |
| 147 | + }) |
45 | 148 | ) |
46 | 149 | }) |
47 | 150 | }) |
48 | 151 | }) |
49 | 152 | }) |
50 | 153 |
|
51 | | -function test ({ t, maxPausedTime, breakpointIndex, maxReferenceDepth }) { |
| 154 | +function test ({ t, maxPausedTime = 0, breakpointIndex, maxReferenceDepth }, assertFn) { |
52 | 155 | const breakpoint = t.breakpoints[breakpointIndex] |
53 | 156 |
|
54 | 157 | return async function () { |
55 | 158 | const payloadReceived = new Promise((/** @type {(value?: unknown) => void} */ resolve) => { |
56 | 159 | t.agent.on('debugger-input', ({ payload: [{ debugger: { snapshot: { captures } } }] }) => { |
57 | 160 | const { locals } = captures.lines[breakpoint.line] |
58 | | - assert.strictEqual( |
59 | | - containsTimeBudget(locals), |
60 | | - true, |
61 | | - 'expected at least one field/element to be marked with notCapturedReason: "timeout"' |
62 | | - ) |
| 161 | + assertFn?.(locals) |
63 | 162 | resolve() |
64 | 163 | }) |
65 | 164 | }) |
|
0 commit comments