-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathindex.js
289 lines (260 loc) · 9.38 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
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
const CiPlugin = require('../../dd-trace/src/plugins/ci_plugin')
const { storage } = require('../../datadog-core')
const {
TEST_STATUS,
finishAllTraceSpans,
getTestSuitePath,
getTestSuiteCommonTags,
getTestSessionName,
getIsFaultyEarlyFlakeDetection,
TEST_SOURCE_FILE,
TEST_IS_RETRY,
TEST_CODE_COVERAGE_LINES_PCT,
TEST_CODE_OWNERS,
TEST_LEVEL_EVENT_TYPES,
TEST_SESSION_NAME,
TEST_SOURCE_START,
TEST_IS_NEW,
TEST_EARLY_FLAKE_ENABLED,
TEST_EARLY_FLAKE_ABORT_REASON,
TEST_RETRY_REASON
} = require('../../dd-trace/src/plugins/util/test')
const { COMPONENT } = require('../../dd-trace/src/constants')
const {
TELEMETRY_EVENT_CREATED,
TELEMETRY_EVENT_FINISHED,
TELEMETRY_TEST_SESSION
} = require('../../dd-trace/src/ci-visibility/telemetry')
// Milliseconds that we subtract from the error test duration
// so that they do not overlap with the following test
// This is because there's some loss of resolution.
const MILLISECONDS_TO_SUBTRACT_FROM_FAILED_TEST_DURATION = 5
class VitestPlugin extends CiPlugin {
static get id () {
return 'vitest'
}
constructor (...args) {
super(...args)
this.taskToFinishTime = new WeakMap()
this.addSub('ci:vitest:test:is-new', ({ knownTests, testSuiteAbsolutePath, testName, onDone }) => {
const testSuite = getTestSuitePath(testSuiteAbsolutePath, this.repositoryRoot)
const testsForThisTestSuite = knownTests[testSuite] || []
onDone(!testsForThisTestSuite.includes(testName))
})
this.addSub('ci:vitest:is-early-flake-detection-faulty', ({
knownTests,
testFilepaths,
onDone
}) => {
const isFaulty = getIsFaultyEarlyFlakeDetection(
testFilepaths.map(testFilepath => getTestSuitePath(testFilepath, this.repositoryRoot)),
knownTests,
this.libraryConfig.earlyFlakeDetectionFaultyThreshold
)
onDone(isFaulty)
})
this.addSub('ci:vitest:test:start', ({
testName,
testSuiteAbsolutePath,
isRetry,
isNew,
mightHitProbe,
isRetryReasonEfd
}) => {
const testSuite = getTestSuitePath(testSuiteAbsolutePath, this.repositoryRoot)
const store = storage.getStore()
const extraTags = {
[TEST_SOURCE_FILE]: testSuite
}
if (isRetry) {
extraTags[TEST_IS_RETRY] = 'true'
}
if (isNew) {
extraTags[TEST_IS_NEW] = 'true'
}
if (isRetryReasonEfd) {
extraTags[TEST_RETRY_REASON] = 'efd'
}
const span = this.startTestSpan(
testName,
testSuite,
this.testSuiteSpan,
extraTags
)
this.enter(span, store)
// TODO: there might be multiple tests for which mightHitProbe is true, so activeTestSpan
// might be wrongly overwritten.
if (mightHitProbe) {
this.activeTestSpan = span
}
})
this.addSub('ci:vitest:test:finish-time', ({ status, task }) => {
const store = storage.getStore()
const span = store?.span
// we store the finish time to finish at a later hook
// this is because the test might fail at a `afterEach` hook
if (span) {
span.setTag(TEST_STATUS, status)
this.taskToFinishTime.set(task, span._getTime())
}
})
this.addSub('ci:vitest:test:pass', ({ task }) => {
const store = storage.getStore()
const span = store?.span
if (span) {
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
hasCodeowners: !!span.context()._tags[TEST_CODE_OWNERS]
})
span.setTag(TEST_STATUS, 'pass')
span.finish(this.taskToFinishTime.get(task))
finishAllTraceSpans(span)
}
})
this.addSub('ci:vitest:test:error', ({ duration, error, shouldSetProbe, promises }) => {
const store = storage.getStore()
const span = store?.span
if (span) {
if (shouldSetProbe && this.di) {
const probeInformation = this.addDiProbe(error)
if (probeInformation) {
const { file, line, stackIndex, setProbePromise } = probeInformation
this.runningTestProbe = { file, line }
this.testErrorStackIndex = stackIndex
promises.setProbePromise = setProbePromise
}
}
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
hasCodeowners: !!span.context()._tags[TEST_CODE_OWNERS]
})
span.setTag(TEST_STATUS, 'fail')
if (error) {
span.setTag('error', error)
}
if (duration) {
span.finish(span._startTime + duration - MILLISECONDS_TO_SUBTRACT_FROM_FAILED_TEST_DURATION) // milliseconds
} else {
span.finish() // `duration` is empty for retries, so we'll use clock time
}
finishAllTraceSpans(span)
}
})
this.addSub('ci:vitest:test:skip', ({ testName, testSuiteAbsolutePath, isNew }) => {
const testSuite = getTestSuitePath(testSuiteAbsolutePath, this.repositoryRoot)
const testSpan = this.startTestSpan(
testName,
testSuite,
this.testSuiteSpan,
{
[TEST_SOURCE_FILE]: testSuite,
[TEST_SOURCE_START]: 1, // we can't get the proper start line in vitest
[TEST_STATUS]: 'skip',
...(isNew ? { [TEST_IS_NEW]: 'true' } : {})
}
)
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
hasCodeowners: !!testSpan.context()._tags[TEST_CODE_OWNERS]
})
testSpan.finish()
})
this.addSub('ci:vitest:test-suite:start', ({ testSuiteAbsolutePath, frameworkVersion }) => {
this.command = process.env.DD_CIVISIBILITY_TEST_COMMAND
this.frameworkVersion = frameworkVersion
const testSessionSpanContext = this.tracer.extract('text_map', {
'x-datadog-trace-id': process.env.DD_CIVISIBILITY_TEST_SESSION_ID,
'x-datadog-parent-id': process.env.DD_CIVISIBILITY_TEST_MODULE_ID
})
// test suites run in a different process, so they also need to init the metadata dictionary
const testSessionName = getTestSessionName(this.config, this.command, this.testEnvironmentMetadata)
const metadataTags = {}
for (const testLevel of TEST_LEVEL_EVENT_TYPES) {
metadataTags[testLevel] = {
[TEST_SESSION_NAME]: testSessionName
}
}
if (this.tracer._exporter.setMetadataTags) {
this.tracer._exporter.setMetadataTags(metadataTags)
}
const testSuite = getTestSuitePath(testSuiteAbsolutePath, this.repositoryRoot)
const testSuiteMetadata = getTestSuiteCommonTags(
this.command,
this.frameworkVersion,
testSuite,
'vitest'
)
testSuiteMetadata[TEST_SOURCE_FILE] = testSuite
testSuiteMetadata[TEST_SOURCE_START] = 1
const codeOwners = this.getCodeOwners(testSuiteMetadata)
if (codeOwners) {
testSuiteMetadata[TEST_CODE_OWNERS] = codeOwners
}
const testSuiteSpan = this.tracer.startSpan('vitest.test_suite', {
childOf: testSessionSpanContext,
tags: {
[COMPONENT]: this.constructor.id,
...this.testEnvironmentMetadata,
...testSuiteMetadata
}
})
this.telemetry.ciVisEvent(TELEMETRY_EVENT_CREATED, 'suite')
const store = storage.getStore()
this.enter(testSuiteSpan, store)
this.testSuiteSpan = testSuiteSpan
})
this.addSub('ci:vitest:test-suite:finish', ({ status, onFinish }) => {
const store = storage.getStore()
const span = store?.span
if (span) {
span.setTag(TEST_STATUS, status)
span.finish()
finishAllTraceSpans(span)
}
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'suite')
// TODO: too frequent flush - find for method in worker to decrease frequency
this.tracer._exporter.flush(onFinish)
if (this.runningTestProbe) {
this.removeDiProbe(this.runningTestProbe)
}
})
this.addSub('ci:vitest:test-suite:error', ({ error }) => {
const store = storage.getStore()
const span = store?.span
if (span && error) {
span.setTag('error', error)
span.setTag(TEST_STATUS, 'fail')
}
})
this.addSub('ci:vitest:session:finish', ({
status,
error,
testCodeCoverageLinesTotal,
isEarlyFlakeDetectionEnabled,
isEarlyFlakeDetectionFaulty,
onFinish
}) => {
this.testSessionSpan.setTag(TEST_STATUS, status)
this.testModuleSpan.setTag(TEST_STATUS, status)
if (error) {
this.testModuleSpan.setTag('error', error)
this.testSessionSpan.setTag('error', error)
}
if (testCodeCoverageLinesTotal) {
this.testModuleSpan.setTag(TEST_CODE_COVERAGE_LINES_PCT, testCodeCoverageLinesTotal)
this.testSessionSpan.setTag(TEST_CODE_COVERAGE_LINES_PCT, testCodeCoverageLinesTotal)
}
if (isEarlyFlakeDetectionEnabled) {
this.testSessionSpan.setTag(TEST_EARLY_FLAKE_ENABLED, 'true')
}
if (isEarlyFlakeDetectionFaulty) {
this.testSessionSpan.setTag(TEST_EARLY_FLAKE_ABORT_REASON, 'faulty')
}
this.testModuleSpan.finish()
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'module')
this.testSessionSpan.finish()
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'session')
finishAllTraceSpans(this.testSessionSpan)
this.telemetry.count(TELEMETRY_TEST_SESSION, { provider: this.ciProviderName })
this.tracer._exporter.flush(onFinish)
})
}
}
module.exports = VitestPlugin