@@ -4,6 +4,12 @@ const { performance, constants, PerformanceObserver } = require('perf_hooks')
4
4
const { END_TIMESTAMP_LABEL , SPAN_ID_LABEL , LOCAL_ROOT_SPAN_ID_LABEL , encodeProfileAsync } = require ( './shared' )
5
5
const { Function, Label, Line, Location, Profile, Sample, StringTable, ValueType } = require ( 'pprof-format' )
6
6
const { availableParallelism, effectiveLibuvThreadCount } = require ( '../libuv-size' )
7
+ const { getEnvironmentVariables } = require ( '../../config-helper' )
8
+ const dc = require ( 'dc-polyfill' )
9
+
10
+ const testEventChannel = [ 'true' , '1' ] . includes ( getEnvironmentVariables ( 'TESTING_PROFILING_EVENTS' ) )
11
+ ? dc . channel ( 'dd-trace:profiling:events-test' )
12
+ : undefined
7
13
8
14
// perf_hooks uses millis, with fractional part representing nanos. We emit nanos into the pprof file.
9
15
const MS_TO_NS = 1_000_000
@@ -363,6 +369,27 @@ class DatadogInstrumentationEventSource {
363
369
}
364
370
}
365
371
372
+ class TestEventSource {
373
+ constructor ( eventHandler ) {
374
+ this . eventHandler = eventHandler
375
+ this . started = false
376
+ }
377
+
378
+ start ( ) {
379
+ if ( ! this . started ) {
380
+ testEventChannel . subscribe ( this . eventHandler )
381
+ this . started = true
382
+ }
383
+ }
384
+
385
+ stop ( ) {
386
+ if ( this . started ) {
387
+ testEventChannel . unsubscribe ( this . eventHandler )
388
+ this . started = false
389
+ }
390
+ }
391
+ }
392
+
366
393
class CompositeEventSource {
367
394
constructor ( sources ) {
368
395
this . sources = sources
@@ -430,15 +457,21 @@ class EventsProfiler {
430
457
}
431
458
}
432
459
433
- this . eventSource = options . codeHotspotsEnabled
460
+ const eventSources = options . codeHotspotsEnabled
434
461
// Use Datadog instrumentation to collect events with span IDs. Still use
435
462
// Node API for GC events.
436
- ? new CompositeEventSource ( [
437
- new DatadogInstrumentationEventSource ( eventHandler , eventFilter ) ,
438
- new NodeApiEventSource ( filteringEventHandler , [ 'gc' ] )
439
- ] )
463
+ ? [
464
+ new DatadogInstrumentationEventSource ( eventHandler , eventFilter ) ,
465
+ new NodeApiEventSource ( filteringEventHandler , [ 'gc' ] ) ,
466
+ ]
440
467
// Use Node API instrumentation to collect events without span IDs
441
- : new NodeApiEventSource ( filteringEventHandler )
468
+ : [
469
+ new NodeApiEventSource ( filteringEventHandler )
470
+ ]
471
+ if ( testEventChannel !== undefined ) {
472
+ eventSources . push ( new TestEventSource ( filteringEventHandler ) )
473
+ }
474
+ this . eventSource = new CompositeEventSource ( eventSources )
442
475
}
443
476
444
477
start ( ) {
@@ -461,6 +494,12 @@ class EventsProfiler {
461
494
encode ( profile ) {
462
495
return encodeProfileAsync ( profile ( ) )
463
496
}
497
+
498
+ static emitTestEvent ( event ) {
499
+ if ( testEventChannel !== undefined ) {
500
+ testEventChannel . publish ( event )
501
+ }
502
+ }
464
503
}
465
504
466
505
module . exports = EventsProfiler
0 commit comments