Skip to content

Commit

Permalink
fix stack index
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Jan 9, 2025
1 parent f55d1e4 commit 0f248df
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
5 changes: 2 additions & 3 deletions packages/datadog-instrumentations/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const originalTestFns = new WeakMap()
const retriedTestsToNumAttempts = new Map()
const newTestsTestStatuses = new Map()

const BREAKPOINT_HIT_GRACE_PERIOD_MS = 500
const BREAKPOINT_HIT_GRACE_PERIOD_MS = 200

// based on https://github.com/facebook/jest/blob/main/packages/jest-circus/src/formatNodeAssertErrors.ts#L41
function formatJestError (errors) {
Expand Down Expand Up @@ -316,8 +316,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
}

// After finishing it might take a bit for the snapshot to be handled.
// We'll give 500ms for the snapshot to be handled.
// This means that tests retried with DI are 500ms slower at least.
// This means that tests retried with DI are BREAKPOINT_HIT_GRACE_PERIOD_MS slower at least.
if (mightHitBreakpoint) {
await new Promise(resolve => {
setTimeout(() => {
Expand Down
43 changes: 28 additions & 15 deletions packages/datadog-plugin-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const {
TEST_IS_RUM_ACTIVE,
TEST_BROWSER_DRIVER,
DI_ERROR_DEBUG_INFO_CAPTURED,
DI_DEBUG_ERROR_SNAPSHOT_ID,
DI_DEBUG_ERROR_FILE,
DI_DEBUG_ERROR_LINE,
DI_DEBUG_ERROR_PREFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX,
getFormattedError
} = require('../../dd-trace/src/plugins/util/test')
const { COMPONENT } = require('../../dd-trace/src/constants')
Expand All @@ -42,6 +43,7 @@ const {
TELEMETRY_CODE_COVERAGE_NUM_FILES,
TELEMETRY_TEST_SESSION
} = require('../../dd-trace/src/ci-visibility/telemetry')
const log = require('../../dd-trace/src/log')

const isJestWorker = !!process.env.JEST_WORKER_ID

Expand Down Expand Up @@ -342,7 +344,7 @@ class JestPlugin extends CiPlugin {
span.finish()
finishAllTraceSpans(span)
this.activeTestSpan = null
if (shouldRemoveProbe) {
if (shouldRemoveProbe && this.runningTestProbeId) {
promises.isProbeRemoved = withTimeout(this.removeDiProbe(this.runningTestProbeId), 2000)
this.runningTestProbeId = null
}
Expand All @@ -356,9 +358,13 @@ class JestPlugin extends CiPlugin {
span.setTag(TEST_STATUS, 'fail')
span.setTag('error', getFormattedError(error, this.repositoryRoot))
if (shouldSetProbe) {
const [probeId, setProbePromise] = this.addDiProbe(error, this.onDiBreakpointHit.bind(this))
this.runningTestProbeId = probeId
promises.isProbeReady = withTimeout(setProbePromise, 2000)
const probeInformation = this.addDiProbe(error, this.onDiBreakpointHit.bind(this))
if (probeInformation) {
const { probeId, setProbePromise, stackIndex } = probeInformation
this.runningTestProbeId = probeId
this.testErrorStackIndex = stackIndex
promises.isProbeReady = withTimeout(setProbePromise, 2000)
}
}
}
}
Expand All @@ -373,19 +379,26 @@ class JestPlugin extends CiPlugin {

onDiBreakpointHit ({ snapshot }) {
if (!this.activeTestSpan || this.activeTestSpan.context()._isFinished) {
console.log('active span is null or finished', this.activeTestSpan)
// this is unexpected
// This is unexpected and is caused by a race condition.
log.warn('Breakpoint snapshot could not be attached to the active test span')
return
}

// TODO: WHERE TO GET THE STACK INDEX FROM????
const stackIndex = 3
const stackIndex = this.testErrorStackIndex

this.activeTestSpan.setTag(DI_ERROR_DEBUG_INFO_CAPTURED, 'true')
// TODO: replace DI_DEBUG_ERROR tags by just their prefix
this.activeTestSpan.setTag(DI_DEBUG_ERROR_SNAPSHOT_ID.replace('0', stackIndex), snapshot.id)
this.activeTestSpan.setTag(DI_DEBUG_ERROR_FILE.replace('0', stackIndex), snapshot.probe.location.file)
this.activeTestSpan.setTag(DI_DEBUG_ERROR_LINE.replace('0', stackIndex), snapshot.probe.location.lines[0])
this.activeTestSpan.setTag(
`${DI_DEBUG_ERROR_PREFIX}.${stackIndex}.${DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX}`,
snapshot.id
)
this.activeTestSpan.setTag(
`${DI_DEBUG_ERROR_PREFIX}.${stackIndex}.${DI_DEBUG_ERROR_FILE_SUFFIX}`,
snapshot.probe.location.file
)
this.activeTestSpan.setTag(
`${DI_DEBUG_ERROR_PREFIX}.${stackIndex}.${DI_DEBUG_ERROR_LINE_SUFFIX}`,
snapshot.probe.location.lines[0]
)

const activeTestSpanContext = this.activeTestSpan.context()
this.tracer._exporter.exportDiLogs(this.testEnvironmentMetadata, {
Expand Down
10 changes: 9 additions & 1 deletion packages/dd-trace/src/plugins/ci_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ module.exports = class CiPlugin extends Plugin {
return
}

return this.di.addLineProbe({ file, line }, onHitBreakpoint)
const [probeId, setProbePromise] = this.di.addLineProbe({ file, line }, onHitBreakpoint)

return {
probeId,
setProbePromise,
stackIndex,
file,
line
}
}
}
15 changes: 8 additions & 7 deletions packages/dd-trace/src/plugins/util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ const TEST_LEVEL_EVENT_TYPES = [

// Dynamic instrumentation - Test optimization integration tags
const DI_ERROR_DEBUG_INFO_CAPTURED = 'error.debug_info_captured'
// TODO: for the moment we'll only use a single snapshot id, so `0` is hardcoded
const DI_DEBUG_ERROR_SNAPSHOT_ID = '_dd.debug.error.0.snapshot_id'
const DI_DEBUG_ERROR_FILE = '_dd.debug.error.0.file'
const DI_DEBUG_ERROR_LINE = '_dd.debug.error.0.line'
const DI_DEBUG_ERROR_PREFIX = '_dd.debug.error'
const DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX = 'snapshot_id'
const DI_DEBUG_ERROR_FILE_SUFFIX = 'file'
const DI_DEBUG_ERROR_LINE_SUFFIX = 'line'

module.exports = {
TEST_CODE_OWNERS,
Expand Down Expand Up @@ -191,9 +191,10 @@ module.exports = {
getNumFromKnownTests,
getFileAndLineNumberFromError,
DI_ERROR_DEBUG_INFO_CAPTURED,
DI_DEBUG_ERROR_SNAPSHOT_ID,
DI_DEBUG_ERROR_FILE,
DI_DEBUG_ERROR_LINE,
DI_DEBUG_ERROR_PREFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX,
getFormattedError
}

Expand Down

0 comments on commit 0f248df

Please sign in to comment.