-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathindex.js
137 lines (116 loc) · 4.07 KB
/
index.js
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
'use strict'
const { join } = require('path')
const { Worker, threadId: parentThreadId } = require('worker_threads')
const { randomUUID } = require('crypto')
const log = require('../../log')
const probeIdToResolveBreakpointSet = new Map()
const probeIdToResolveBreakpointRemove = new Map()
class TestVisDynamicInstrumentation {
constructor () {
this.worker = null
this._readyPromise = new Promise(resolve => {
this._onReady = resolve
})
this.breakpointSetChannel = new MessageChannel()
this.breakpointHitChannel = new MessageChannel()
this.breakpointRemoveChannel = new MessageChannel()
this.onHitBreakpointByProbeId = new Map()
}
removeProbe (probeId) {
return new Promise(resolve => {
this.breakpointRemoveChannel.port2.postMessage(probeId)
probeIdToResolveBreakpointRemove.set(probeId, resolve)
})
}
// Return 2 elements:
// 1. Probe ID
// 2. Promise that's resolved when the breakpoint is set
addLineProbe ({ file, line }, onHitBreakpoint) {
const probeId = randomUUID()
this.breakpointSetChannel.port2.postMessage(
{ id: probeId, file, line }
)
this.onHitBreakpointByProbeId.set(probeId, onHitBreakpoint)
return [
probeId,
new Promise(resolve => {
probeIdToResolveBreakpointSet.set(probeId, resolve)
})
]
}
isReady () {
return this._readyPromise
}
start (config) {
if (this.worker) return
log.debug('Starting Test Visibility - Dynamic Instrumentation client...')
const rcChannel = new MessageChannel() // mock channel
const configChannel = new MessageChannel() // mock channel
this.worker = new Worker(
join(__dirname, 'worker', 'index.js'),
{
execArgv: [],
// Not passing `NODE_OPTIONS` results in issues with yarn, which relies on NODE_OPTIONS
// for PnP support, hence why we deviate from the DI pattern here.
// To avoid infinite initialization loops, we're disabling DI and tracing in the worker.
env: {
...process.env,
DD_TRACE_ENABLED: 0,
DD_TEST_DYNAMIC_INSTRUMENTATION_ENABLED: 0
},
workerData: {
config: config.serialize(),
parentThreadId,
rcPort: rcChannel.port1,
configPort: configChannel.port1,
breakpointSetChannel: this.breakpointSetChannel.port1,
breakpointHitChannel: this.breakpointHitChannel.port1,
breakpointRemoveChannel: this.breakpointRemoveChannel.port1
},
transferList: [
rcChannel.port1,
configChannel.port1,
this.breakpointSetChannel.port1,
this.breakpointHitChannel.port1,
this.breakpointRemoveChannel.port1
]
}
)
this.worker.on('online', () => {
log.debug('Test Visibility - Dynamic Instrumentation client is ready')
this._onReady()
})
this.worker.on('error', (err) => {
log.error('Test Visibility - Dynamic Instrumentation worker error', err)
})
this.worker.on('messageerror', (err) => {
log.error('Test Visibility - Dynamic Instrumentation worker messageerror', err)
})
// Allow the parent to exit even if the worker is still running
this.worker.unref()
this.breakpointSetChannel.port2.on('message', (probeId) => {
const resolve = probeIdToResolveBreakpointSet.get(probeId)
if (resolve) {
resolve()
probeIdToResolveBreakpointSet.delete(probeId)
}
}).unref()
this.breakpointHitChannel.port2.on('message', ({ snapshot }) => {
const { probe: { id: probeId } } = snapshot
const onHit = this.onHitBreakpointByProbeId.get(probeId)
if (onHit) {
onHit({ snapshot })
} else {
log.warn('Received a breakpoint hit for an unknown probe')
}
}).unref()
this.breakpointRemoveChannel.port2.on('message', (probeId) => {
const resolve = probeIdToResolveBreakpointRemove.get(probeId)
if (resolve) {
resolve()
probeIdToResolveBreakpointRemove.delete(probeId)
}
}).unref()
}
}
module.exports = new TestVisDynamicInstrumentation()