-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathprofiler.spec.js
More file actions
804 lines (725 loc) · 28.1 KB
/
Copy pathprofiler.spec.js
File metadata and controls
804 lines (725 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
'use strict'
const assert = require('node:assert/strict')
const {
FakeAgent,
sandboxCwd,
useSandbox,
assertObjectContains,
} = require('../helpers')
const childProcess = require('child_process')
const { fork } = childProcess
const path = require('path')
const fs = require('fs/promises')
const fsync = require('fs')
const net = require('net')
const zlib = require('zlib')
const { Profile } = require('../../vendor/dist/pprof-format')
const satisfies = require('semifies')
const DEFAULT_PROFILE_TYPES = ['wall', 'space']
if (process.platform !== 'win32') {
DEFAULT_PROFILE_TYPES.push('events')
}
const TIMEOUT = 30000
function checkProfiles (agent, proc, timeout,
expectedProfileTypes = DEFAULT_PROFILE_TYPES, expectBadExit = false, expectSeq = true
) {
return Promise.all([
processExitPromise(proc, timeout, expectBadExit),
expectProfileMessagePromise(agent, timeout, expectedProfileTypes, expectSeq)
])
}
function expectProfileMessagePromise (agent, timeout,
expectedProfileTypes = DEFAULT_PROFILE_TYPES, expectSeq = true
) {
const fileNames = expectedProfileTypes.map(type => `${type}.pprof`)
return agent.assertMessageReceived(({ headers, _, files }) => {
let event
try {
assertObjectContains(headers, {
host: `127.0.0.1:${agent.port}`
})
assertObjectContains(files[0], {
originalname: 'event.json'
})
event = JSON.parse(files[0].buffer.toString())
assertObjectContains(event, {
family: 'node'
})
assert.strictEqual(typeof event.info.profiler.activation, 'string')
assert.strictEqual(typeof event.info.profiler.ssi.mechanism, 'string')
const attachments = event.attachments
assert.ok(Array.isArray(attachments))
// Profiler encodes the files with Promise.all, so their ordering is not guaranteed
assert.deepStrictEqual(attachments.slice().sort(), fileNames.sort())
for (const [index, fileName] of attachments.entries()) {
assertObjectContains(files[index + 1], {
originalname: fileName
})
}
if (expectSeq) {
assert(event.tags_profiler.indexOf(',profile_seq:') !== -1)
}
} catch (e) {
e.message += ` ${JSON.stringify({ headers, files, event })}`
throw e
}
}, timeout, 1, true)
}
function processExitPromise (proc, timeout, expectBadExit = false) {
return new Promise((resolve, reject) => {
const timeoutObj = setTimeout(() => {
reject(new Error('Process timed out'))
}, timeout)
function checkExitCode (code) {
clearTimeout(timeoutObj)
if ((code !== 0) !== expectBadExit) {
reject(new Error(`Process exited with unexpected status code ${code}.`))
} else {
resolve()
}
}
proc
.on('error', reject)
.on('exit', checkExitCode)
})
}
async function getLatestProfile (cwd, pattern) {
const pprofGzipped = await readLatestFile(cwd, pattern)
const pprofUnzipped = zlib.gunzipSync(pprofGzipped)
return { profile: Profile.decode(pprofUnzipped), encoded: pprofGzipped.toString('base64') }
}
async function readLatestFile (cwd, pattern) {
const dirEntries = await fs.readdir(cwd)
// Get the latest file matching the pattern
const pprofEntries = dirEntries.filter(name => pattern.test(name))
assert.ok(pprofEntries.length > 0, `No file matching pattern ${pattern} found in ${cwd}`)
const pprofEntry = pprofEntries
.map(name => ({ name, modified: fsync.statSync(path.join(cwd, name), { bigint: true }).mtimeNs }))
.reduce((a, b) => a.modified > b.modified ? a : b)
.name
return await fs.readFile(path.join(cwd, pprofEntry))
}
function expectTimeout (messagePromise, allowErrors = false) {
return messagePromise.then(
() => {
throw new Error('Received unexpected message')
}, (e) => {
if (e.message !== 'timeout' && (!allowErrors || !e.message.startsWith('timeout, additionally:'))) {
throw e
}
}
)
}
class TimelineEventProcessor {
constructor (strings, encoded) {
this.strings = strings
this.encoded = encoded
}
}
class NetworkEventProcessor extends TimelineEventProcessor {
constructor (strings, encoded) {
super(strings, encoded)
this.hostKey = strings.dedup('host')
this.addressKey = strings.dedup('address')
this.portKey = strings.dedup('port')
}
processLabel (label, processedLabels) {
switch (label.key) {
case this.hostKey:
processedLabels.host = label.str
return true
case this.addressKey:
processedLabels.address = label.str
return true
case this.portKey:
processedLabels.port = label.num
return true
default:
return false
}
}
decorateEvent (ev, pl) {
// Exactly one of these is defined
assert.ok(!!pl.address !== !!pl.host, this.encoded)
if (pl.address) {
ev.address = this.strings.strings[pl.address]
} else {
ev.host = this.strings.strings[pl.host]
}
if (pl.port) {
ev.port = pl.port
}
}
}
async function gatherNetworkTimelineEvents (cwd, scriptFilePath, agentPort, eventType, args) {
return gatherTimelineEvents(cwd, scriptFilePath, agentPort, eventType, args, NetworkEventProcessor)
}
class FilesystemEventProcessor extends TimelineEventProcessor {
constructor (strings, encoded) {
super(strings, encoded)
this.fdKey = strings.dedup('fd')
this.fileKey = strings.dedup('file')
this.flagKey = strings.dedup('flag')
this.modeKey = strings.dedup('mode')
this.pathKey = strings.dedup('path')
}
processLabel (label, processedLabels) {
switch (label.key) {
case this.fdKey:
processedLabels.fd = label.num
return true
case this.fileKey:
processedLabels.file = label.str
return true
case this.flagKey:
processedLabels.flag = label.str
return true
case this.modeKey:
processedLabels.mode = label.str
return true
case this.pathKey:
processedLabels.path = label.str
return true
default:
return false
}
}
decorateEvent (ev, pl) {
ev.fd = pl.fd
ev.file = this.strings.strings[pl.file]
ev.flag = this.strings.strings[pl.flag]
ev.mode = this.strings.strings[pl.mode]
ev.path = this.strings.strings[pl.path]
for (const [k, v] of Object.entries(ev)) {
if (v === undefined) {
delete ev[k]
}
}
}
}
async function gatherFilesystemTimelineEvents (cwd, scriptFilePath, agentPort) {
return gatherTimelineEvents(cwd, scriptFilePath, agentPort, 'fs', [], FilesystemEventProcessor)
}
async function gatherTimelineEvents (cwd, scriptFilePath, agentPort, eventType, args, Processor) {
const procStart = BigInt(Date.now() * 1000000)
const proc = fork(path.join(cwd, scriptFilePath), args, {
cwd,
env: {
DD_PROFILING_EXPORTERS: 'file',
DD_PROFILING_ENABLED: '1',
DD_INTERNAL_PROFILING_TIMELINE_SAMPLING_ENABLED: '0', // capture all events
DD_TRACE_AGENT_PORT: agentPort
}
})
await processExitPromise(proc, TIMEOUT)
const procEnd = BigInt(Date.now() * 1000000)
const { profile, encoded } = await getLatestProfile(cwd, /^events_.+\.pprof$/)
const strings = profile.stringTable
const tsKey = strings.dedup('end_timestamp_ns')
const eventKey = strings.dedup('event')
const operationKey = strings.dedup('operation')
const spanIdKey = strings.dedup('span id')
const localRootSpanIdKey = strings.dedup('local root span id')
const eventValue = strings.dedup(eventType)
const events = []
const processor = new Processor(strings, encoded)
for (const sample of profile.sample) {
let ts, event, operation, spanId, localRootSpanId
const processedLabels = {}
const unexpectedLabels = []
for (const label of sample.label) {
switch (label.key) {
case tsKey: ts = label.num; break
case operationKey: operation = label.str; break
case eventKey: event = label.str; break
case spanIdKey: spanId = label.str; break
case localRootSpanIdKey: localRootSpanId = label.str; break
default:
if (!processor.processLabel(label, processedLabels)) {
unexpectedLabels.push(label.key)
}
}
}
// Timestamp must be defined and be between process start and end time
assert.notStrictEqual(ts, undefined, encoded)
assert.strictEqual(typeof ts, 'bigint', encoded)
assert.ok(ts <= procEnd, encoded)
assert.ok(ts >= procStart, encoded)
// Gather only tested events
if (event === eventValue) {
if (process.platform !== 'win32') {
assert.notStrictEqual(spanId, undefined, encoded)
assert.notStrictEqual(localRootSpanId, undefined, encoded)
} else {
assert.strictEqual(spanId, undefined, encoded)
assert.strictEqual(localRootSpanId, undefined, encoded)
}
assert.notStrictEqual(operation, undefined, encoded)
if (unexpectedLabels.length > 0) {
const labelsStr = JSON.stringify(unexpectedLabels)
const labelsStrStr = unexpectedLabels.map(k => strings.strings[k]).join(',')
assert.fail(`Unexpected labels: ${labelsStr}\n${labelsStrStr}\n${encoded}`)
}
const ev = { operation: strings.strings[operation] }
processor.decorateEvent(ev, processedLabels)
events.push(ev)
}
}
return events
}
describe('profiler', () => {
let agent
let proc
let cwd
let profilerTestFile
let ssiTestFile
let oomTestFile
let oomEnv
let oomExecArgv
const timeout = TIMEOUT
// Target sample count per span for the code hotspots test
const idealSamplesPerSpan = 10
// Intrinsic invariants for the code hotspots test
const expectedSpans = 9 // codehotspots.js creates 3x3 spans
const profilerSamplingFrequency = 99 // Hz
// Computed values for the code hotspots test. busyCycleTimeNs is adaptively adjusted by the test
// when it needs to be repeated.
const idealSampleCount = idealSamplesPerSpan * expectedSpans // we'd like 10 samples per span, ideally
let busyCycleTimeNs = 1000000000 * idealSamplesPerSpan / profilerSamplingFrequency
const maxBusyCycleTimeNs = (timeout - 1000) * 1000000 / expectedSpans
useSandbox()
before(() => {
cwd = sandboxCwd()
profilerTestFile = path.join(cwd, 'profiler/index.js')
ssiTestFile = path.join(cwd, 'profiler/ssi.js')
oomTestFile = path.join(cwd, 'profiler/oom.js')
oomExecArgv = ['--max-old-space-size=50']
})
beforeEach(async () => {
agent = await new FakeAgent().start()
})
afterEach(async () => {
await agent.stop()
})
describe('on non-Windows platforms', () => {
before(function () {
if (process.platform === 'win32') {
this.skip()
}
})
it('code hotspots and endpoint tracing works', async function () {
// see comment on busyCycleTimeNs recomputation below. Ideally a single retry should be enough
// with recomputed busyCycleTimeNs, but let's give ourselves more leeway.
this.retries(9)
const procStart = BigInt(Date.now() * 1000000)
const env = {
DD_PROFILING_EXPORTERS: 'file',
DD_PROFILING_ENABLED: '1',
BUSY_CYCLE_TIME: (busyCycleTimeNs | 0).toString(),
DD_TRACE_AGENT_PORT: agent.port
}
// With Node 22.9.0 or later, test the profiler with async context frame use.
const execArgv = []
if (satisfies(process.versions.node, '>=22.9.0')) {
env.DD_PROFILING_ASYNC_CONTEXT_FRAME_ENABLED = 1
if (!satisfies(process.versions.node, '>=24.0.0')) {
// For Node 22.9.0+, use the experimental command line flag for Node to enable
// async context frame. Node 24 has it enabled by default.
execArgv.push('--experimental-async-context-frame')
}
}
const proc = fork(path.join(cwd, 'profiler/codehotspots.js'), { cwd, env, execArgv })
await processExitPromise(proc, timeout)
const procEnd = BigInt(Date.now() * 1000000)
// Must've counted the number of times each endpoint was hit
const event = JSON.parse((await readLatestFile(cwd, /^event_.+\.json$/)).toString())
assert.deepStrictEqual(event.endpoint_counts, { 'endpoint-0': 1, 'endpoint-1': 1, 'endpoint-2': 1 })
const { profile, encoded } = await getLatestProfile(cwd, /^wall_.+\.pprof$/)
// Recompute in case we need to retry. It is possible that some of the assertions in the test
// will fail because we gathered a too small number of samples. This can happen if the machine
// is CPU-constrained so the V8 thread that triggers PROF signals gets CPU starved. If we need
// to retry, the busyCycleTime will be prolonged to have the next execution of codehotspots.js
// run for long enough in the current environment for the profiler to capture the ideal number
// of samples.
busyCycleTimeNs = Math.min(maxBusyCycleTimeNs, busyCycleTimeNs * idealSampleCount / profile.sample.length)
// We check the profile for following invariants:
// - every sample needs to have an 'end_timestamp_ns' label that has values (nanos since UNIX
// epoch) between process start and end.
// - it needs to have samples with 9 total different 'span id's, and 3 different
// 'local root span id's
// - samples with spans also must have a 'trace endpoint' label with values 'endpoint-0',
// 'endpoint-1', or 'endpoint-2'
// - every occurrence of a span must have the same root span and endpoint
const rootSpans = new Set()
const endpoints = new Set()
const spans = new Map()
const strings = profile.stringTable
const tsKey = strings.dedup('end_timestamp_ns')
const spanKey = strings.dedup('span id')
const rootSpanKey = strings.dedup('local root span id')
const endpointKey = strings.dedup('trace endpoint')
const threadNameKey = strings.dedup('thread name')
const threadIdKey = strings.dedup('thread id')
const osThreadIdKey = strings.dedup('os thread id')
const threadNameValue = strings.dedup('Main Event Loop')
const nonJSThreadNameValue = strings.dedup('Non-JS threads')
for (const sample of profile.sample) {
let ts, spanId, rootSpanId, endpoint, threadName, threadId, osThreadId
for (const label of sample.label) {
switch (label.key) {
case tsKey: ts = label.num; break
case spanKey: spanId = label.num; break
case rootSpanKey: rootSpanId = label.num; break
case endpointKey: endpoint = label.str; break
case threadNameKey: threadName = label.str; break
case threadIdKey: threadId = label.str; break
case osThreadIdKey: osThreadId = label.str; break
default: assert.fail(`Unexpected label key ${strings.dedup(label.key)} ${encoded}`)
}
}
if (threadName !== nonJSThreadNameValue) {
// Timestamp must be defined and be between process start and end time
assert.notStrictEqual(ts, undefined, encoded)
assert.strictEqual(typeof osThreadId, 'number', encoded)
assert.strictEqual(threadId, strings.dedup('0'), encoded)
assert.ok(ts <= procEnd, encoded)
assert.ok(ts >= procStart, encoded)
// Thread name must be defined and exactly equal "Main Event Loop"
assert.strictEqual(threadName, threadNameValue, encoded)
} else {
assert.strictEqual(threadId, strings.dedup('NA'), encoded)
}
// Either all or none of span-related labels are defined
if (endpoint === undefined) {
// It is possible to catch a sample executing in tracer's startSpan so
// that endpoint is not yet set. We'll ignore those samples.
continue
}
if (spanId || rootSpanId) {
assert.notStrictEqual(spanId, undefined, encoded)
assert.notStrictEqual(rootSpanId, undefined, encoded)
rootSpans.add(rootSpanId)
if (spanId === rootSpanId) {
// It is possible to catch a sample executing in the root span before
// it entered the nested span; we ignore these too, although we'll
// still record the root span ID as we want to assert there'll only be
// 3 of them.
continue
}
const spanData = { rootSpanId, endpoint }
const existingSpanData = spans.get(spanId)
if (existingSpanData) {
// Span's root span and endpoint must be consistent across samples
assert.deepStrictEqual(spanData, existingSpanData, encoded)
} else {
// New span id, store span data
spans.set(spanId, spanData)
// Verify endpoint value
const endpointVal = strings.strings[endpoint]
switch (endpointVal) {
case 'endpoint-0':
case 'endpoint-1':
case 'endpoint-2':
endpoints.add(endpoint)
break
default:
assert.fail(`Unexpected endpoint value ${endpointVal} ${encoded}`)
}
}
}
}
// Need to have a total of 9 different spans, with 3 different root spans
// and 3 different endpoints.
assert.strictEqual(spans.size, 9, encoded)
assert.strictEqual(rootSpans.size, 3, encoded)
assert.strictEqual(endpoints.size, 3, encoded)
})
it('fs timeline events work', async () => {
const fsEvents = await gatherFilesystemTimelineEvents(cwd, 'profiler/fstest.js', agent.port)
assert.strictEqual(fsEvents.length, 6)
const path = fsEvents[0].path
const fd = fsEvents[1].fd
assert(path.endsWith('tempfile.txt'))
assertObjectContains(fsEvents, [
{ flag: 'w', mode: '', operation: 'open', path },
{ fd, operation: 'write' },
{ fd, operation: 'close' },
{ file: path, operation: 'writeFile' },
{ operation: 'readFile', path },
{ operation: 'unlink', path }
])
})
it('dns timeline events work', async () => {
const dnsEvents = await gatherNetworkTimelineEvents(cwd, 'profiler/dnstest.js', agent.port, 'dns')
const compare = (a, b) => {
return a.operation.localeCompare(b.operation) || (a.host?.localeCompare(b.host) ?? 0)
}
assertObjectContains(dnsEvents.sort(compare), [
{ operation: 'lookup', host: 'datadoghq.com' },
{ operation: 'lookup', host: 'example.com' },
{ operation: 'lookup', host: 'example.org' },
{ operation: 'lookupService', address: '13.224.103.60', port: 80 },
{ operation: 'queryA', host: 'datadoghq.com' },
])
})
it('net timeline events work', async () => {
// Simple server that writes a constant message to the socket.
const msg = 'cya later!\n'
function createServer () {
const server = net.createServer((socket) => {
socket.end(msg, 'utf8')
}).on('error', (err) => {
throw err
})
return server
}
// Create two instances of the server
const server1 = createServer()
try {
const server2 = createServer()
try {
// Have the servers listen on ephemeral ports
const p = new Promise(resolve => {
server1.listen(0, () => {
server2.listen(0, async () => {
resolve([server1.address().port, server2.address().port])
})
})
})
const [port1, port2] = await p
const args = [String(port1), String(port2), msg]
// Invoke the profiled program, passing it the ports of the servers and
// the expected message.
const events = await gatherNetworkTimelineEvents(cwd, 'profiler/nettest.js', agent.port, 'net', args)
// The profiled program should have two TCP connection events to the two
// servers.
assertObjectContains(events, [
{ operation: 'connect', host: '127.0.0.1', port: port1 },
{ operation: 'connect', host: '127.0.0.1', port: port2 }
])
} finally {
server2.close()
}
} finally {
server1.close()
}
})
})
context('shutdown', () => {
beforeEach(() => {
oomEnv = {
DD_TRACE_AGENT_PORT: agent.port,
DD_PROFILING_ENABLED: '1',
DD_TRACE_DEBUG: '1',
DD_TRACE_LOG_LEVEL: 'warn'
}
})
afterEach(() => {
proc.kill()
})
it('records profile on process exit', async () => {
proc = fork(profilerTestFile, {
cwd,
env: {
DD_TRACE_AGENT_PORT: agent.port,
DD_PROFILING_ENABLED: '1'
}
})
const checkTelemetry = agent.assertTelemetryReceived('generate-metrics', 1000)
// SSI telemetry is not supposed to have been emitted when DD_INJECTION_ENABLED is absent,
// so expect telemetry callback to time out
await Promise.all([checkProfiles(agent, proc, timeout), expectTimeout(checkTelemetry)])
})
describe('on non-Windows platform', () => {
before(function () {
if (process.platform === 'win32') {
this.skip()
}
})
it('sends a heap profile on OOM with external process', () => {
proc = fork(oomTestFile, {
cwd,
execArgv: oomExecArgv,
env: oomEnv
})
return checkProfiles(agent, proc, timeout, ['space'], true, false)
})
it('sends a heap profile on OOM in worker thread and exits successfully', () => {
proc = fork(oomTestFile, [1, 50], {
cwd,
env: { ...oomEnv, DD_PROFILING_WALLTIME_ENABLED: '0' }
})
return checkProfiles(agent, proc, timeout, ['space'], false)
})
// Following tests are flaky because they use unreliable strategies to export profiles
// (or check that the process can recover from OOM, which is also unreliable).
// We retry them 3 times to decrease flakiness.
it('sends a heap profile on OOM with external process and exits successfully', () => {
proc = fork(oomTestFile, {
cwd,
execArgv: oomExecArgv,
env: {
...oomEnv,
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE: '15000000',
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT: '3'
}
})
return checkProfiles(agent, proc, timeout, ['space'], false, false)
}).retries(3)
it('sends a heap profile on OOM with async callback', () => {
proc = fork(oomTestFile, {
cwd,
execArgv: oomExecArgv,
env: {
...oomEnv,
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE: '10000000',
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT: '1',
DD_PROFILING_EXPERIMENTAL_OOM_EXPORT_STRATEGIES: 'async'
}
})
return checkProfiles(agent, proc, timeout, ['space'], true)
}).retries(3)
it('sends heap profiles on OOM with multiple strategies', () => {
proc = fork(oomTestFile, {
cwd,
execArgv: oomExecArgv,
env: {
...oomEnv,
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE: '10000000',
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT: '1',
DD_PROFILING_EXPERIMENTAL_OOM_EXPORT_STRATEGIES: 'async,process'
}
})
return checkProfiles(agent, proc, timeout, ['space'], true)
}).retries(3)
})
})
context('SSI heuristics', () => {
afterEach(() => {
proc.kill()
})
describe('does not trigger for', () => {
it('a short-lived app that creates no spans', () => {
return heuristicsDoesNotTriggerFor([], false)
})
it('a short-lived app that creates a span', () => {
return heuristicsDoesNotTriggerFor(['create-span'], true)
})
it('a long-lived app that creates no spans', () => {
return heuristicsDoesNotTriggerFor(['long-lived'], false)
})
})
it('triggers for long-lived span-creating app', () => {
return heuristicsTrigger()
})
})
context('Profiler telemetry', () => {
beforeEach(async () => {
agent = await new FakeAgent().start()
})
afterEach(async () => {
proc.kill()
await agent.stop()
})
it('sends profiler API telemetry', async () => {
proc = fork(profilerTestFile, {
cwd,
env: {
DD_TRACE_AGENT_PORT: agent.port,
DD_PROFILING_ENABLED: '1',
DD_PROFILING_UPLOAD_PERIOD: '1',
TEST_DURATION_MS: 2500
}
})
let requestCount = 0
let pointsCount = 0
const checkMetrics = agent.assertTelemetryReceived(({ _, payload }) => {
const pp = payload.payload
assert.strictEqual(pp.namespace, 'profilers')
const series = pp.series
const requests = series.find(s => s.metric === 'profile_api.requests')
assert.strictEqual(requests.type, 'count')
// There's a race between metrics and on-shutdown profile, so metric
// value will be between 1 and 3
requestCount = requests.points[0][1]
assert.ok(requestCount >= 1)
assert.ok(requestCount <= 3)
const responses = series.find(s => s.metric === 'profile_api.responses')
assert.strictEqual(responses.type, 'count')
assert.deepStrictEqual(responses.tags, ['status_code:200'])
// Same number of requests and responses
assert.strictEqual(responses.points[0][1], requestCount)
}, 'generate-metrics', timeout)
const checkDistributions = agent.assertTelemetryReceived(({ _, payload }) => {
const pp = payload.payload
assert.strictEqual(pp.namespace, 'profilers')
const series = pp.series
assert.strictEqual(series.length, 2)
assert.strictEqual(series[0].metric, 'profile_api.bytes')
assert.strictEqual(series[1].metric, 'profile_api.ms')
// Same number of points
pointsCount = series[0].points.length
assert.strictEqual(pointsCount, series[1].points.length)
}, 'distributions', timeout)
await Promise.all([checkProfiles(agent, proc, timeout), checkMetrics, checkDistributions])
// Same number of requests and points
assert.strictEqual(requestCount, pointsCount)
})
it('sends wall profiler sample context telemetry', async function () {
if (satisfies(process.versions.node, '<24.0.0')) {
this.skip() // Wall profiler context count telemetry is not supported in Node < 24
}
if (process.platform === 'win32') {
this.skip() // Wall profiler context count telemetry is not supported on Windows
}
if (process.platform === 'darwin') {
this.skip() // Test is flaky on macOS
}
proc = fork(profilerTestFile, {
cwd,
env: {
DD_TRACE_AGENT_PORT: agent.port,
DD_PROFILING_ENABLED: '1',
DD_PROFILING_UPLOAD_PERIOD: '1',
DD_PROFILING_ASYNC_CONTEXT_FRAME_ENABLED: '1',
DD_TELEMETRY_HEARTBEAT_INTERVAL: '1', // every second
TEST_DURATION_MS: 1500
}
})
const checkMetrics = agent.assertTelemetryReceived(({ _, payload }) => {
const pp = payload.payload
assert.strictEqual(pp.namespace, 'profilers');
['live', 'used'].forEach(metricName => {
const sampleContexts = pp.series.find(s => s.metric === `wall.async_contexts_${metricName}`)
assert.notStrictEqual(sampleContexts, undefined)
assert.strictEqual(sampleContexts.type, 'gauge')
assert.ok(sampleContexts.points[0][1] >= 1)
})
}, 'generate-metrics', timeout)
await Promise.all([checkProfiles(agent, proc, timeout), checkMetrics])
})
})
function forkSsi (args) {
return fork(ssiTestFile, args, {
cwd,
env: {
DD_TRACE_AGENT_PORT: agent.port,
DD_INTERNAL_PROFILING_LONG_LIVED_THRESHOLD: '1300',
DD_PROFILING_ENABLED: 'auto'
}
})
}
function heuristicsTrigger () {
return checkProfiles(agent,
forkSsi(['create-span', 'long-lived']),
timeout,
DEFAULT_PROFILE_TYPES,
false)
}
function heuristicsDoesNotTriggerFor (args, allowTraceMessage) {
return Promise.all([
processExitPromise(forkSsi(args), timeout, false),
expectTimeout(expectProfileMessagePromise(agent, 1500), allowTraceMessage)
])
}
})