dd-trace 5.93.0 → 5.94.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,6 +17,36 @@ const {
17
17
 
18
18
  const { getNumFromKnownTests } = require('../../plugins/util/test')
19
19
 
20
+ const MAX_KNOWN_TESTS_PAGES = 10_000
21
+
22
+ /**
23
+ * Deep-merges page tests into aggregate.
24
+ * Structure: { module: { suite: [testName, ...] } }
25
+ */
26
+ function mergeKnownTests (aggregate, page) {
27
+ if (!page) return aggregate
28
+ if (!aggregate) return page
29
+
30
+ for (const [moduleName, suites] of Object.entries(page)) {
31
+ if (!suites) continue
32
+
33
+ if (!aggregate[moduleName]) {
34
+ aggregate[moduleName] = suites
35
+ continue
36
+ }
37
+
38
+ for (const [suiteName, tests] of Object.entries(suites)) {
39
+ if (!tests || tests.length === 0) continue
40
+
41
+ aggregate[moduleName][suiteName] = aggregate[moduleName][suiteName]
42
+ ? [...aggregate[moduleName][suiteName], ...tests]
43
+ : tests
44
+ }
45
+ }
46
+
47
+ return aggregate
48
+ }
49
+
20
50
  function getKnownTests ({
21
51
  url,
22
52
  isEvpProxy,
@@ -59,53 +89,94 @@ function getKnownTests ({
59
89
  options.headers['dd-api-key'] = apiKey
60
90
  }
61
91
 
62
- const data = JSON.stringify({
63
- data: {
64
- id: id().toString(10),
65
- type: 'ci_app_libraries_tests_request',
66
- attributes: {
67
- configurations: {
68
- 'os.platform': osPlatform,
69
- 'os.version': osVersion,
70
- 'os.architecture': osArchitecture,
71
- 'runtime.name': runtimeName,
72
- 'runtime.version': runtimeVersion,
73
- custom,
74
- },
75
- service,
76
- env,
77
- repository_url: repositoryUrl,
78
- sha,
79
- },
80
- },
81
- })
92
+ const configurations = {
93
+ 'os.platform': osPlatform,
94
+ 'os.version': osVersion,
95
+ 'os.architecture': osArchitecture,
96
+ 'runtime.name': runtimeName,
97
+ 'runtime.version': runtimeVersion,
98
+ custom,
99
+ }
82
100
 
83
101
  incrementCountMetric(TELEMETRY_KNOWN_TESTS)
84
102
 
85
103
  const startTime = Date.now()
104
+ let aggregateTests = null
105
+ let totalResponseBytes = 0
106
+ let pageNumber = 0
107
+
108
+ function fetchPage (pageState) {
109
+ pageNumber++
110
+
111
+ if (pageNumber > MAX_KNOWN_TESTS_PAGES) {
112
+ log.error('Known tests pagination exceeded maximum of %d pages. Aborting.', MAX_KNOWN_TESTS_PAGES)
113
+ distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
114
+ return done(new Error(`Known tests pagination exceeded maximum of ${MAX_KNOWN_TESTS_PAGES} pages`))
115
+ }
116
+
117
+ const pageInfo = pageState ? { page_state: pageState } : {}
118
+
119
+ const data = JSON.stringify({
120
+ data: {
121
+ id: id().toString(10),
122
+ type: 'ci_app_libraries_tests_request',
123
+ attributes: {
124
+ configurations,
125
+ service,
126
+ env,
127
+ repository_url: repositoryUrl,
128
+ sha,
129
+ page_info: pageInfo,
130
+ },
131
+ },
132
+ })
133
+
134
+ request(data, options, (err, res, statusCode) => {
135
+ if (err) {
136
+ distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
137
+ incrementCountMetric(TELEMETRY_KNOWN_TESTS_ERRORS, { statusCode })
138
+ return done(err)
139
+ }
86
140
 
87
- request(data, options, (err, res, statusCode) => {
88
- distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
89
- if (err) {
90
- incrementCountMetric(TELEMETRY_KNOWN_TESTS_ERRORS, { statusCode })
91
- done(err)
92
- } else {
93
141
  try {
94
- const { data: { attributes: { tests: knownTests } } } = JSON.parse(res)
142
+ totalResponseBytes += res.length
143
+
144
+ const { data: { attributes } } = JSON.parse(res)
145
+ const { tests: pageTests, page_info: responsePageInfo } = attributes
95
146
 
96
- const numTests = getNumFromKnownTests(knownTests)
147
+ aggregateTests = mergeKnownTests(aggregateTests, pageTests)
148
+
149
+ // Check if there are more pages
150
+ if (responsePageInfo && responsePageInfo.has_next) {
151
+ if (!responsePageInfo.cursor) {
152
+ log.error(
153
+ 'Known tests response has has_next=true but no cursor on page %d. Aborting pagination.', pageNumber
154
+ )
155
+ distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
156
+ return done(new Error('Known tests pagination: has_next=true but no cursor'))
157
+ }
158
+ return fetchPage(responsePageInfo.cursor)
159
+ }
160
+
161
+ // Done — no more pages
162
+ distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
163
+
164
+ const numTests = getNumFromKnownTests(aggregateTests)
97
165
 
98
166
  distributionMetric(TELEMETRY_KNOWN_TESTS_RESPONSE_TESTS, {}, numTests)
99
- distributionMetric(TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES, {}, res.length)
167
+ distributionMetric(TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES, {}, totalResponseBytes)
100
168
 
101
169
  log.debug('Number of received known tests:', numTests)
102
170
 
103
- done(null, knownTests)
171
+ done(null, aggregateTests)
104
172
  } catch (err) {
173
+ distributionMetric(TELEMETRY_KNOWN_TESTS_MS, {}, Date.now() - startTime)
105
174
  done(err)
106
175
  }
107
- }
108
- })
176
+ })
177
+ }
178
+
179
+ fetchPage(null)
109
180
  }
110
181
 
111
182
  module.exports = { getKnownTests }
@@ -2,10 +2,6 @@
2
2
  const { JSONEncoder } = require('../../encode/json-encoder')
3
3
  const { getEnvironmentVariable } = require('../../../config/helper')
4
4
  const log = require('../../../log')
5
- const {
6
- VITEST_WORKER_TRACE_PAYLOAD_CODE,
7
- VITEST_WORKER_LOGS_PAYLOAD_CODE,
8
- } = require('../../../plugins/util/test')
9
5
 
10
6
  class Writer {
11
7
  constructor (interprocessCode) {
@@ -29,12 +25,6 @@ class Writer {
29
25
  }
30
26
 
31
27
  _sendPayload (data, onDone = () => {}) {
32
- // ## Jest
33
- // Only available when `child_process` is used for the jest worker.
34
- // If worker_threads is used, this will not work
35
- // TODO: make `jest` instrumentation compatible with worker_threads
36
- // https://github.com/facebook/jest/blob/bb39cb2c617a3334bf18daeca66bd87b7ccab28b/packages/jest-worker/README.md#experimental-worker
37
-
38
28
  // ## Cucumber
39
29
  // This reports to the test's main process the same way test data is reported by Cucumber
40
30
  // See cucumber code:
@@ -47,19 +37,17 @@ class Writer {
47
37
  ? { __tinypool_worker_message__: true, interprocessCode: this._interprocessCode, data }
48
38
  : [this._interprocessCode, data]
49
39
 
50
- const isVitestTestWorker =
51
- this._interprocessCode === VITEST_WORKER_TRACE_PAYLOAD_CODE ||
52
- this._interprocessCode === VITEST_WORKER_LOGS_PAYLOAD_CODE
53
-
40
+ // child_process workers (jest default, cucumber)
54
41
  if (process.send) {
55
42
  process.send(payload, () => {
56
43
  onDone()
57
44
  })
58
- } else if (isVitestTestWorker) { // TODO: worker_threads are only supported in vitest right now
59
- const { isMainThread, parentPort } = require('worker_threads')
60
- if (isMainThread) {
61
- return onDone()
62
- }
45
+ return
46
+ }
47
+
48
+ // worker_threads (jest --workerThreads, vitest)
49
+ const { isMainThread, parentPort } = require('node:worker_threads')
50
+ if (!isMainThread && parentPort) {
63
51
  try {
64
52
  parentPort.postMessage(payload)
65
53
  } catch (error) {
@@ -67,9 +55,10 @@ class Writer {
67
55
  } finally {
68
56
  onDone()
69
57
  }
70
- } else {
71
- onDone()
58
+ return
72
59
  }
60
+
61
+ onDone()
73
62
  }
74
63
  }
75
64
 
@@ -35,6 +35,7 @@ const integrationCounters = {
35
35
 
36
36
  const startCh = channel('dd-trace:span:start')
37
37
  const finishCh = channel('dd-trace:span:finish')
38
+ const tagsUpdateCh = channel('dd-trace:span:tags:update')
38
39
 
39
40
  function getIntegrationCounter (event, integration) {
40
41
  const counters = integrationCounters[event]
@@ -399,6 +400,10 @@ class DatadogSpan {
399
400
  tagger.add(this._spanContext._tags, keyValuePairs)
400
401
 
401
402
  this._prioritySampler.sample(this, false)
403
+
404
+ if (tagsUpdateCh.hasSubscribers) {
405
+ tagsUpdateCh.publish(this)
406
+ }
402
407
  }
403
408
  }
404
409
 
@@ -18,13 +18,6 @@ const TEST_OPTIMIZATION_PLUGINS = new Set([
18
18
 
19
19
  const loadChannel = channel('dd-trace:instrumentation:load')
20
20
 
21
- // instrument everything that needs Plugin System V2 instrumentation
22
- require('../../datadog-instrumentations')
23
- if (getEnvironmentVariable('AWS_LAMBDA_FUNCTION_NAME') !== undefined) {
24
- // instrument lambda environment
25
- require('./lambda')
26
- }
27
-
28
21
  const DD_TRACE_DISABLED_PLUGINS = getValueFromEnvSources('DD_TRACE_DISABLED_PLUGINS')
29
22
 
30
23
  const disabledPlugins = new Set(
@@ -35,10 +28,20 @@ const disabledPlugins = new Set(
35
28
 
36
29
  const pluginClasses = {}
37
30
 
31
+ // Subscribe before requiring instrumentations so that loadChannel events fired
32
+ // during instrumentation initialization (e.g. re-requires in bundler contexts)
33
+ // are captured and populate pluginClasses correctly.
38
34
  loadChannel.subscribe(({ name }) => {
39
35
  maybeEnable(plugins[name])
40
36
  })
41
37
 
38
+ // instrument everything that needs Plugin System V2 instrumentation
39
+ require('../../datadog-instrumentations')
40
+ if (getEnvironmentVariable('AWS_LAMBDA_FUNCTION_NAME') !== undefined) {
41
+ // instrument lambda environment
42
+ require('./lambda')
43
+ }
44
+
42
45
  function maybeEnable (Plugin) {
43
46
  if (!Plugin || typeof Plugin !== 'function') return
44
47
  if (!pluginClasses[Plugin.id]) {
@@ -88,8 +88,24 @@ const TEST_EARLY_FLAKE_ABORT_REASON = 'test.early_flake.abort_reason'
88
88
  const TEST_RETRY_REASON = 'test.retry_reason'
89
89
  const TEST_HAS_FAILED_ALL_RETRIES = 'test.has_failed_all_retries'
90
90
  const TEST_IS_MODIFIED = 'test.is_modified'
91
+ const TEST_HAS_DYNAMIC_NAME = '_dd.has_dynamic_name'
91
92
  const CI_APP_ORIGIN = 'ciapp-test'
92
93
 
94
+ // Matches patterns that are almost certainly runtime-generated values in test names:
95
+ // - Unix timestamps in ms (13 digits, years ~2020-2090) or s (10 digits)
96
+ // - UUIDs (8-4-4-4-12 hex)
97
+ // - ISO 8601 dates (2024-03-23) or date-times (2024-03-23T14:30)
98
+ // - Random ports on localhost, 127.0.0.1, or 0.0.0.0
99
+ // - Math.random() float values (10+ decimal digits after 0.)
100
+ const DYNAMIC_NAME_RE = new RegExp(
101
+ String.raw`\b1[6-9]\d{8,11}\b|` +
102
+ String.raw`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|` +
103
+ String.raw`\b\d{4}-\d{2}-\d{2}|` +
104
+ String.raw`(?:localhost|127\.0\.0\.1|0\.0\.0\.0):\d{4,5}\b|` +
105
+ String.raw`\b0\.\d{10,}`,
106
+ 'i'
107
+ )
108
+
93
109
  const JEST_TEST_RUNNER = 'test.jest.test_runner'
94
110
  const JEST_DISPLAY_NAME = 'test.jest.display_name'
95
111
 
@@ -260,6 +276,7 @@ module.exports = {
260
276
  TEST_RETRY_REASON,
261
277
  TEST_HAS_FAILED_ALL_RETRIES,
262
278
  TEST_IS_MODIFIED,
279
+ TEST_HAS_DYNAMIC_NAME,
263
280
  getTestEnvironmentMetadata,
264
281
  getTestParametersString,
265
282
  finishAllTraceSpans,
@@ -337,6 +354,9 @@ module.exports = {
337
354
  POSSIBLE_BASE_BRANCHES,
338
355
  GIT_COMMIT_SHA,
339
356
  GIT_REPOSITORY_URL,
357
+ DYNAMIC_NAME_RE,
358
+ collectDynamicNamesFromTraces,
359
+ logDynamicNamesWarning,
340
360
  }
341
361
 
342
362
  // Returns pkg manager and its version, separated by '-', e.g. npm-8.15.0 or yarn-1.22.19
@@ -1178,6 +1198,62 @@ function getModifiedFilesFromDiff (diff) {
1178
1198
  return result
1179
1199
  }
1180
1200
 
1201
+ /**
1202
+ * Scans serialized worker trace payloads for tests tagged with TEST_HAS_DYNAMIC_NAME
1203
+ * and populates the provided Set. Silently ignores parse errors.
1204
+ *
1205
+ * @param {string} data - JSON-serialized traces from a worker
1206
+ * @param {Set<string>} newTestsWithDynamicNames - Set to populate with "suite › name" strings
1207
+ */
1208
+ function collectDynamicNamesFromTraces (data, newTestsWithDynamicNames) {
1209
+ try {
1210
+ const traces = JSON.parse(data)
1211
+ for (const trace of traces) {
1212
+ for (const span of trace) {
1213
+ if (span.meta?.[TEST_HAS_DYNAMIC_NAME] === 'true') {
1214
+ const suite = span.meta[TEST_SUITE]
1215
+ const name = span.meta[TEST_NAME]
1216
+ if (suite && name) {
1217
+ newTestsWithDynamicNames.add(`${suite} › ${name}`)
1218
+ }
1219
+ }
1220
+ }
1221
+ }
1222
+ } catch {
1223
+ // ignore parse errors
1224
+ }
1225
+ }
1226
+
1227
+ /**
1228
+ * Logs a "Datadog Test Optimization" warning about new tests with dynamic names.
1229
+ * Clears the Set after logging. No-op if the Set is empty.
1230
+ *
1231
+ * @param {Set<string>} newTestsWithDynamicNames
1232
+ */
1233
+ function logDynamicNamesWarning (newTestsWithDynamicNames) {
1234
+ if (newTestsWithDynamicNames.size === 0) return
1235
+
1236
+ const MAX_SHOWN = 10
1237
+ const names = [...newTestsWithDynamicNames]
1238
+ const shown = names.slice(0, MAX_SHOWN)
1239
+ const more = names.length - shown.length
1240
+ const moreSuffix = more > 0 ? `\n ... and ${more} more` : ''
1241
+ const nameList = shown.map(n => ` • ${n}`).join('\n') + moreSuffix
1242
+
1243
+ const line = '-'.repeat(50)
1244
+ // eslint-disable-next-line no-console -- Intentional user-facing session summary
1245
+ console.warn(
1246
+ `\n${line}\nDatadog Test Optimization\n${line}\n` +
1247
+ `${newTestsWithDynamicNames.size} test(s) detected as new but their names contain ` +
1248
+ 'dynamic data (timestamps, UUIDs, etc.).\n' +
1249
+ 'Tests with changing names are always treated as new on every run, ' +
1250
+ 'causing unnecessary Early Flake Detection retries and preventing correct new test detection.\n' +
1251
+ 'Consider using stable, deterministic test names.\n\n' +
1252
+ `${nameList}\n`
1253
+ )
1254
+ newTestsWithDynamicNames.clear()
1255
+ }
1256
+
1181
1257
  function isModifiedTest (testPath, testStartLine, testEndLine, modifiedFiles, testFramework) {
1182
1258
  if (modifiedFiles === undefined) {
1183
1259
  return false
@@ -20,6 +20,7 @@ const TRACE_ENDPOINT_LABEL = 'trace endpoint'
20
20
  let beforeCh
21
21
  const enterCh = dc.channel('dd-trace:storage:enter')
22
22
  const spanFinishCh = dc.channel('dd-trace:span:finish')
23
+ const tagsUpdateCh = dc.channel('dd-trace:span:tags:update')
23
24
  const profilerTelemetryMetrics = telemetryMetrics.manager.namespace('profilers')
24
25
 
25
26
  const ProfilingContext = Symbol('NativeWallProfiler.ProfilingContext')
@@ -58,33 +59,30 @@ function ensureChannelsActivated (asyncContextFrameEnabled) {
58
59
  if (channelsActivated) return
59
60
 
60
61
  const shimmer = require('../../../../datadog-shimmer')
61
- const asyncHooks = require('async_hooks')
62
62
 
63
- // When using AsyncContextFrame to store sample context, we do not need to use
64
- // async_hooks.createHook to create a "before" callback anymore.
65
- if (!asyncContextFrameEnabled) {
66
- const { createHook } = asyncHooks
67
- beforeCh = dc.channel('dd-trace:storage:before')
68
- createHook({ before: () => beforeCh.publish() }).enable()
69
- }
70
-
71
- const { AsyncLocalStorage } = asyncHooks
72
-
73
- // We need to instrument AsyncLocalStorage.enterWith() both with and without AsyncContextFrame.
63
+ // We need to instrument enterWith() on the legacy storage that's the storage
64
+ // carrying span data and the only one the profiler cares about.
65
+ const legacyStorage = storage('legacy')
74
66
  let inRun = false
75
- shimmer.wrap(AsyncLocalStorage.prototype, 'enterWith', function (original) {
76
- return function (...args) {
77
- const retVal = original.apply(this, args)
67
+ shimmer.wrap(legacyStorage, 'enterWith', function (original) {
68
+ return function (store) {
69
+ const retVal = original.call(this, store)
78
70
  if (!inRun) enterCh.publish()
79
71
  return retVal
80
72
  }
81
73
  })
82
74
 
83
- // We only need to instrument AsyncLocalStorage.run() when not using AsyncContextFrame.
84
- // AsyncContextFrame-based implementation of AsyncLocalStorage.run() delegates
85
- // to AsyncLocalStorage.enterWith() so it doesn't need to be separately instrumented.
75
+ // When not using AsyncContextFrame, we need additional instrumentation.
86
76
  if (!asyncContextFrameEnabled) {
87
- shimmer.wrap(AsyncLocalStorage.prototype, 'run', function (original) {
77
+ // We need async_hooks.createHook to create a "before" callback.
78
+ const { createHook } = require('async_hooks')
79
+ beforeCh = dc.channel('dd-trace:storage:before')
80
+ createHook({ before: () => beforeCh.publish() }).enable()
81
+
82
+ // In ACF-based implementation run() delegates to enterWith() so it doesn't
83
+ // need to be separately instrumented. in non-ACF implementation run()
84
+ // doesn't delegate to enterWith(), so separate instrumentation is necessary.
85
+ shimmer.wrap(legacyStorage, 'run', function (original) {
88
86
  return function (store, callback, ...args) {
89
87
  const wrappedCb = shimmer.wrapFunction(callback, cb => function (...args) {
90
88
  inRun = false
@@ -125,6 +123,7 @@ class NativeWallProfiler {
125
123
  // Bind these to this so they can be used as callbacks
126
124
  #boundEnter = this.#enter.bind(this)
127
125
  #boundSpanFinished = this.#spanFinished.bind(this)
126
+ #boundSpanTagsUpdated = this.#spanTagsUpdated.bind(this)
128
127
  #boundGenerateLabels = this._generateLabels.bind(this)
129
128
 
130
129
  get type () { return 'wall' }
@@ -204,6 +203,9 @@ class NativeWallProfiler {
204
203
  }
205
204
  enterCh.subscribe(this.#boundEnter)
206
205
  spanFinishCh.subscribe(this.#boundSpanFinished)
206
+ if (this.#endpointCollectionEnabled) {
207
+ tagsUpdateCh.subscribe(this.#boundSpanTagsUpdated)
208
+ }
207
209
  }
208
210
  }
209
211
 
@@ -290,15 +292,7 @@ class NativeWallProfiler {
290
292
  }
291
293
 
292
294
  profilingContext = { spanId, rootSpanId, webTags }
293
- // Don't cache if endpoint collection is enabled and webTags is undefined but
294
- // the span's type hasn't been set yet. TracingPlugin.startSpan() calls
295
- // enterWith() before the plugin sets span.type='web' via addRequestTags(),
296
- // so the first enterCh event fires before the type is known. Without this
297
- // guard we'd cache webTags=undefined and then serve that stale value on the
298
- // subsequent activation (when span.type='web' is already set).
299
- if (!this.#endpointCollectionEnabled || webTags !== undefined || context._tags['span.type']) {
300
- span[ProfilingContext] = profilingContext
301
- }
295
+ span[ProfilingContext] = profilingContext
302
296
  }
303
297
  return profilingContext
304
298
  }
@@ -317,6 +311,16 @@ class NativeWallProfiler {
317
311
  }
318
312
  }
319
313
 
314
+ #spanTagsUpdated (span) {
315
+ if (!this.#started) return
316
+ const profilingContext = span[ProfilingContext]
317
+ if (profilingContext === undefined || profilingContext.webTags !== undefined) return
318
+ const tags = span.context()._tags
319
+ if (isWebServerSpan(tags)) {
320
+ profilingContext.webTags = tags
321
+ }
322
+ }
323
+
320
324
  #reportV8bug (maybeBug) {
321
325
  const tag = `v8_profiler_bug_workaround_enabled:${this.#v8ProfilerBugWorkaroundEnabled}`
322
326
  const metric = `v8_cpu_profiler${maybeBug ? '_maybe' : ''}_stuck_event_loop`
@@ -355,6 +359,9 @@ class NativeWallProfiler {
355
359
  }
356
360
  enterCh.unsubscribe(this.#boundEnter)
357
361
  spanFinishCh.unsubscribe(this.#boundSpanFinished)
362
+ if (this.#endpointCollectionEnabled) {
363
+ tagsUpdateCh.unsubscribe(this.#boundSpanTagsUpdated)
364
+ }
358
365
  this._profilerState = undefined
359
366
  }
360
367
  this.#started = false
package/webpack.js ADDED
@@ -0,0 +1,3 @@
1
+ 'use strict'
2
+
3
+ module.exports = require('./packages/datadog-webpack/index.js')