Skip to content

Commit

Permalink
[test optimization] Fix active span being null in cypress (#4863)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored and rochdev committed Nov 14, 2024
1 parent 36d2505 commit d84a489
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions packages/datadog-plugin-cypress/src/cypress-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,55 +654,57 @@ class CypressPlugin {
return this.activeTestSpan ? { traceId: this.activeTestSpan.context().toTraceId() } : {}
},
'dd:afterEach': ({ test, coverage }) => {
if (!this.activeTestSpan) {
log.warn('There is no active test span in dd:afterEach handler')
return null
}
const { state, error, isRUMActive, testSourceLine, testSuite, testName, isNew, isEfdRetry } = test
if (this.activeTestSpan) {
if (coverage && this.isCodeCoverageEnabled && this.tracer._tracer._exporter?.exportCoverage) {
const coverageFiles = getCoveredFilenamesFromCoverage(coverage)
const relativeCoverageFiles = coverageFiles.map(file => getTestSuitePath(file, this.rootDir))
if (!relativeCoverageFiles.length) {
incrementCountMetric(TELEMETRY_CODE_COVERAGE_EMPTY)
}
distributionMetric(TELEMETRY_CODE_COVERAGE_NUM_FILES, {}, relativeCoverageFiles.length)
const { _traceId, _spanId } = this.testSuiteSpan.context()
const formattedCoverage = {
sessionId: _traceId,
suiteId: _spanId,
testId: this.activeTestSpan.context()._spanId,
files: relativeCoverageFiles
}
this.tracer._tracer._exporter.exportCoverage(formattedCoverage)
}
const testStatus = CYPRESS_STATUS_TO_TEST_STATUS[state]
this.activeTestSpan.setTag(TEST_STATUS, testStatus)

if (error) {
this.activeTestSpan.setTag('error', error)
}
if (isRUMActive) {
this.activeTestSpan.setTag(TEST_IS_RUM_ACTIVE, 'true')
}
if (testSourceLine) {
this.activeTestSpan.setTag(TEST_SOURCE_START, testSourceLine)
}
if (isNew) {
this.activeTestSpan.setTag(TEST_IS_NEW, 'true')
if (isEfdRetry) {
this.activeTestSpan.setTag(TEST_IS_RETRY, 'true')
}
if (coverage && this.isCodeCoverageEnabled && this.tracer._tracer._exporter?.exportCoverage) {
const coverageFiles = getCoveredFilenamesFromCoverage(coverage)
const relativeCoverageFiles = coverageFiles.map(file => getTestSuitePath(file, this.rootDir))
if (!relativeCoverageFiles.length) {
incrementCountMetric(TELEMETRY_CODE_COVERAGE_EMPTY)
}
const finishedTest = {
testName,
testStatus,
finishTime: this.activeTestSpan._getTime(), // we store the finish time here
testSpan: this.activeTestSpan
distributionMetric(TELEMETRY_CODE_COVERAGE_NUM_FILES, {}, relativeCoverageFiles.length)
const { _traceId, _spanId } = this.testSuiteSpan.context()
const formattedCoverage = {
sessionId: _traceId,
suiteId: _spanId,
testId: this.activeTestSpan.context()._spanId,
files: relativeCoverageFiles
}
if (this.finishedTestsByFile[testSuite]) {
this.finishedTestsByFile[testSuite].push(finishedTest)
} else {
this.finishedTestsByFile[testSuite] = [finishedTest]
this.tracer._tracer._exporter.exportCoverage(formattedCoverage)
}
const testStatus = CYPRESS_STATUS_TO_TEST_STATUS[state]
this.activeTestSpan.setTag(TEST_STATUS, testStatus)

if (error) {
this.activeTestSpan.setTag('error', error)
}
if (isRUMActive) {
this.activeTestSpan.setTag(TEST_IS_RUM_ACTIVE, 'true')
}
if (testSourceLine) {
this.activeTestSpan.setTag(TEST_SOURCE_START, testSourceLine)
}
if (isNew) {
this.activeTestSpan.setTag(TEST_IS_NEW, 'true')
if (isEfdRetry) {
this.activeTestSpan.setTag(TEST_IS_RETRY, 'true')
}
// test spans are finished at after:spec
}
const finishedTest = {
testName,
testStatus,
finishTime: this.activeTestSpan._getTime(), // we store the finish time here
testSpan: this.activeTestSpan
}
if (this.finishedTestsByFile[testSuite]) {
this.finishedTestsByFile[testSuite].push(finishedTest)
} else {
this.finishedTestsByFile[testSuite] = [finishedTest]
}
// test spans are finished at after:spec
this.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
hasCodeOwners: !!this.activeTestSpan.context()._tags[TEST_CODE_OWNERS],
isNew,
Expand Down

0 comments on commit d84a489

Please sign in to comment.