dd-trace 5.134.0 → 5.135.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.
Files changed (168) hide show
  1. package/LICENSE-3rdparty.csv +0 -1
  2. package/ci/cypress/support.js +15 -1
  3. package/ci/diagnose.js +4 -4
  4. package/ci/test-annotations.d.ts +5 -0
  5. package/ci/test-annotations.js +5 -0
  6. package/ci/test-optimization-validation/approval-artifacts.js +2 -2
  7. package/ci/test-optimization-validation/approval.js +2 -2
  8. package/ci/test-optimization-validation/artifact-id.js +2 -3
  9. package/ci/test-optimization-validation/ci-package-scripts.js +1 -1
  10. package/ci/test-optimization-validation/cli.js +1 -1
  11. package/ci/test-optimization-validation/command-runner.js +2 -3
  12. package/ci/test-optimization-validation/executable.js +1 -1
  13. package/ci/test-optimization-validation/framework-adapters/cucumber.js +1 -1
  14. package/ci/test-optimization-validation/literal-environment.js +2 -2
  15. package/ci/test-optimization-validation/manifest-scaffold.js +1 -1
  16. package/ci/test-optimization-validation/offline-fixtures.js +1 -1
  17. package/ci/test-optimization-validation/redaction.js +3 -3
  18. package/ci/test-optimization-validation/runner-contract.js +10 -10
  19. package/ci/test-optimization-validation/scenarios/ci-wiring.js +1 -1
  20. package/ci/vitest-no-worker-init-annotations.mjs +4 -0
  21. package/ci/vitest-no-worker-init-node-setup.mjs +22 -0
  22. package/ci/vitest-no-worker-init-runner-setup.mjs +7 -32
  23. package/ci/vitest-no-worker-init-setup.mjs +34 -12
  24. package/ci/vitest-no-worker-init-v5-setup.mjs +6 -0
  25. package/ext/exporters.js +1 -0
  26. package/index.d.ts +19 -0
  27. package/loader-hook.mjs +53 -0
  28. package/openfeature/exposure-hook.d.ts +66 -0
  29. package/openfeature/exposure-hook.js +5 -0
  30. package/package.json +11 -9
  31. package/packages/datadog-core/src/storage.js +1 -1
  32. package/packages/datadog-esbuild/index.js +70 -7
  33. package/packages/datadog-instrumentations/src/cucumber.js +53 -19
  34. package/packages/datadog-instrumentations/src/cypress-config.js +163 -15
  35. package/packages/datadog-instrumentations/src/graphql.js +9 -3
  36. package/packages/datadog-instrumentations/src/grpc/server.js +23 -6
  37. package/packages/datadog-instrumentations/src/helpers/fetch.js +5 -1
  38. package/packages/datadog-instrumentations/src/helpers/graphql-jit-runtime.js +30 -8
  39. package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -1
  40. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +16 -6
  41. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +8 -0
  42. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +3 -1
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/react-router.js +49 -0
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/typesafe.js +23 -0
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/webdriverio.js +12 -0
  46. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +8 -0
  47. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.js +19 -2
  48. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +8 -0
  49. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +1 -1
  50. package/packages/datadog-instrumentations/src/jest.js +265 -82
  51. package/packages/datadog-instrumentations/src/mariadb-bundle.js +69 -11
  52. package/packages/datadog-instrumentations/src/mariadb.js +13 -6
  53. package/packages/datadog-instrumentations/src/mocha/common.js +89 -8
  54. package/packages/datadog-instrumentations/src/mocha/main.js +11 -2
  55. package/packages/datadog-instrumentations/src/mocha/utils.js +58 -12
  56. package/packages/datadog-instrumentations/src/mocha/worker.js +1 -1
  57. package/packages/datadog-instrumentations/src/oracledb.js +33 -0
  58. package/packages/datadog-instrumentations/src/pg.js +2 -1
  59. package/packages/datadog-instrumentations/src/playwright.js +81 -28
  60. package/packages/datadog-instrumentations/src/react-router.js +106 -0
  61. package/packages/datadog-instrumentations/src/typesafe.js +89 -0
  62. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +84 -48
  63. package/packages/datadog-instrumentations/src/vitest-main.js +26 -0
  64. package/packages/datadog-instrumentations/src/vitest-test-annotations.js +20 -0
  65. package/packages/datadog-instrumentations/src/vitest-worker.js +39 -17
  66. package/packages/datadog-instrumentations/src/vitest.js +10 -8
  67. package/packages/datadog-plugin-cucumber/src/index.js +5 -0
  68. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +274 -44
  69. package/packages/datadog-plugin-cypress/src/index.js +1 -1
  70. package/packages/datadog-plugin-cypress/src/lifecycle-owner.js +20 -0
  71. package/packages/datadog-plugin-cypress/src/plugin.js +10 -0
  72. package/packages/datadog-plugin-cypress/src/retry-config-observer.js +281 -0
  73. package/packages/datadog-plugin-cypress/src/setup-handoff.js +125 -0
  74. package/packages/datadog-plugin-cypress/src/support.js +1217 -439
  75. package/packages/datadog-plugin-graphql/src/execute.js +2 -2
  76. package/packages/datadog-plugin-graphql/src/utils.js +5 -5
  77. package/packages/datadog-plugin-jest/src/index.js +25 -3
  78. package/packages/datadog-plugin-mocha/src/index.js +47 -6
  79. package/packages/datadog-plugin-oracledb/src/connection-parser.js +21 -11
  80. package/packages/datadog-plugin-playwright/src/index.js +4 -0
  81. package/packages/datadog-plugin-react-router/src/index.js +118 -0
  82. package/packages/datadog-plugin-typesafe/src/index.js +17 -0
  83. package/packages/datadog-plugin-typesafe/src/tracing.js +31 -0
  84. package/packages/datadog-plugin-vitest/src/index.js +8 -0
  85. package/packages/dd-trace/src/agent/info.js +2 -1
  86. package/packages/dd-trace/src/aiguard/sdk.js +0 -11
  87. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +9 -16
  88. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +4 -2
  89. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +9 -16
  90. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +9 -14
  91. package/packages/dd-trace/src/ci-visibility/requests/route.js +44 -0
  92. package/packages/dd-trace/src/ci-visibility/telemetry-logs.js +5 -0
  93. package/packages/dd-trace/src/ci-visibility/test-annotations.js +72 -0
  94. package/packages/dd-trace/src/ci-visibility/test-declaration-annotations.js +170 -0
  95. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +9 -16
  96. package/packages/dd-trace/src/config/defaults.js +4 -3
  97. package/packages/dd-trace/src/config/generated-config-types.d.ts +4 -0
  98. package/packages/dd-trace/src/config/index.js +95 -39
  99. package/packages/dd-trace/src/config/parsers.js +9 -1
  100. package/packages/dd-trace/src/config/supported-configurations.json +16 -0
  101. package/packages/dd-trace/src/debugger/probe_sampler.js +6 -6
  102. package/packages/dd-trace/src/encode/0.4.js +2 -2
  103. package/packages/dd-trace/src/esm-module-load.js +58 -0
  104. package/packages/dd-trace/src/exporter.js +25 -6
  105. package/packages/dd-trace/src/exporters/agent/index.js +3 -1
  106. package/packages/dd-trace/src/exporters/agentless/index.js +3 -1
  107. package/packages/dd-trace/src/exporters/common/request.js +4 -4
  108. package/packages/dd-trace/src/exporters/llmobs/index.js +135 -0
  109. package/packages/dd-trace/src/exporters/log/index.js +4 -3
  110. package/packages/dd-trace/src/llmobs/constants/tags.js +1 -0
  111. package/packages/dd-trace/src/llmobs/experiments/index.js +2 -3
  112. package/packages/dd-trace/src/llmobs/experiments/noop.js +5 -2
  113. package/packages/dd-trace/src/llmobs/index.js +13 -2
  114. package/packages/dd-trace/src/llmobs/plugins/claude-agent-sdk/index.js +1 -1
  115. package/packages/dd-trace/src/llmobs/plugins/genai/index.js +1 -1
  116. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +8 -4
  117. package/packages/dd-trace/src/llmobs/plugins/typesafe/index.js +102 -0
  118. package/packages/dd-trace/src/llmobs/prompts/cache.js +3 -1
  119. package/packages/dd-trace/src/llmobs/sdk.js +2 -1
  120. package/packages/dd-trace/src/llmobs/span_processor.js +210 -12
  121. package/packages/dd-trace/src/llmobs/tagger.js +3 -0
  122. package/packages/dd-trace/src/llmobs/writers/util.js +3 -3
  123. package/packages/dd-trace/src/log/writer.js +7 -1
  124. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +64 -6
  125. package/packages/dd-trace/src/openfeature/configuration_source.js +18 -2
  126. package/packages/dd-trace/src/openfeature/debug-logging-hook.js +66 -3
  127. package/packages/dd-trace/src/openfeature/exposure-hook.js +185 -0
  128. package/packages/dd-trace/src/openfeature/flagging_provider.js +39 -13
  129. package/packages/dd-trace/src/openfeature/remote_config.js +16 -3
  130. package/packages/dd-trace/src/openfeature/writers/base.js +62 -27
  131. package/packages/dd-trace/src/openfeature/writers/exposures.js +161 -1
  132. package/packages/dd-trace/src/openfeature/writers/flag-eval-evp-hook.js +5 -0
  133. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-aggregation.js +9 -4
  134. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-payload.js +2 -0
  135. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-pii.js +2 -0
  136. package/packages/dd-trace/src/openfeature/writers/split-features.js +25 -0
  137. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +2 -2
  138. package/packages/dd-trace/src/opentelemetry/tracer_provider.js +5 -1
  139. package/packages/dd-trace/src/opentracing/tracer.js +9 -13
  140. package/packages/dd-trace/src/otel-thread-ctx.js +47 -21
  141. package/packages/dd-trace/src/plugins/ci_plugin.js +4 -4
  142. package/packages/dd-trace/src/plugins/index.js +3 -0
  143. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  144. package/packages/dd-trace/src/plugins/util/git.js +11 -5
  145. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +2 -4
  146. package/packages/dd-trace/src/plugins/util/test.js +26 -6
  147. package/packages/dd-trace/src/plugins/util/web.js +16 -0
  148. package/packages/dd-trace/src/process-tags/index.js +1 -1
  149. package/packages/dd-trace/src/profiling/config.js +6 -3
  150. package/packages/dd-trace/src/proxy.js +15 -4
  151. package/packages/dd-trace/src/serverless.js +3 -2
  152. package/packages/dd-trace/src/span_processor.js +21 -9
  153. package/packages/dd-trace/src/span_stats.js +28 -2
  154. package/packages/dd-trace/src/startup-log.js +2 -1
  155. package/packages/dd-trace/src/tagger.js +7 -5
  156. package/packages/dd-trace/src/telemetry/dependencies.js +4 -0
  157. package/packages/dd-trace/src/telemetry/index.js +3 -0
  158. package/packages/dd-trace/src/telemetry/logs/index.js +21 -2
  159. package/packages/dd-trace/src/telemetry/logs/log-collector.js +1 -0
  160. package/packages/dd-trace/src/telemetry/telemetry.js +10 -3
  161. package/packages/dd-trace/src/util.js +1 -1
  162. package/register.js +2 -1
  163. package/vendor/dist/@datadog/openfeature-node-server/index.js +1 -1
  164. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.d.ts +78 -0
  165. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.js +1 -1
  166. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  167. package/vendor/dist/@opentelemetry/resources/LICENSE +0 -201
  168. package/vendor/dist/@opentelemetry/resources/index.js +0 -1
@@ -106,6 +106,49 @@ function normalizeSql (command) {
106
106
  return command?.sql ?? command
107
107
  }
108
108
 
109
+ /**
110
+ * @param {unknown} original
111
+ * @param {unknown} injected
112
+ * @param {unknown} originalSql
113
+ * @param {boolean} [mirrorOptions]
114
+ * @returns {unknown}
115
+ */
116
+ function withInjectedSql (original, injected, originalSql, mirrorOptions) {
117
+ if (typeof originalSql !== 'string' || !originalSql || typeof injected !== 'string') {
118
+ return original
119
+ }
120
+
121
+ if (typeof original === 'string') return injected
122
+
123
+ const command = { ...original, sql: injected }
124
+ if (mirrorOptions) {
125
+ const enumerable = Object.getOwnPropertyDescriptor(original, 'metaAsArray')?.enumerable ?? true
126
+ Object.defineProperty(command, 'metaAsArray', {
127
+ configurable: true,
128
+ enumerable,
129
+ get () { return original.metaAsArray },
130
+ /** @param {boolean} value */
131
+ set (value) { original.metaAsArray = value },
132
+ })
133
+ }
134
+ return command
135
+ }
136
+
137
+ /**
138
+ * @param {{ sql: unknown }} ctx
139
+ * @param {Function} command
140
+ * @param {ArgumentsLike} args
141
+ * @param {unknown} originalSql
142
+ * @param {unknown} [preparedSql]
143
+ * @param {boolean} [mirrorOptions]
144
+ */
145
+ function runSqlCommand (ctx, command, args, originalSql, preparedSql, mirrorOptions) {
146
+ if (preparedSql === undefined && ctx.sql !== originalSql) {
147
+ args[0] = withInjectedSql(args[0], ctx.sql, originalSql, mirrorOptions)
148
+ }
149
+ return command.apply(this, args)
150
+ }
151
+
109
152
  /**
110
153
  * Parses connection options without allowing instrumentation to break the factory call.
111
154
  *
@@ -584,7 +627,8 @@ function createWrapPromiseCommand (
584
627
  if (!startCh.hasSubscribers) return command.apply(this, arguments)
585
628
 
586
629
  const owner = trackActiveCommands ? (commandOwner ?? this) : undefined
587
- const ctx = { sql: preparedSql ?? normalizeSql(sql), conf: options }
630
+ const originalSql = preparedSql ?? normalizeSql(sql)
631
+ const ctx = { sql: originalSql, conf: options }
588
632
  const acquisition = poolAcquisition === undefined
589
633
  ? undefined
590
634
  : createPoolAcquisition(this, options, ctx, poolAcquisition)
@@ -594,7 +638,7 @@ function createWrapPromiseCommand (
594
638
  let result
595
639
  try {
596
640
  result = acquisition === undefined
597
- ? startCh.runStores(ctx, command, this, ...arguments)
641
+ ? startCh.runStores(ctx, runSqlCommand, this, ctx, command, arguments, originalSql, preparedSql)
598
642
  : runPoolAcquisition(acquisition, command, this, arguments)
599
643
  } catch (error) {
600
644
  finishPoolCommandAcquisition(acquisition, error)
@@ -624,7 +668,7 @@ function createWrapPromiseCommand (
624
668
  * @param {number} [commandArity] Original arity when command is wrapped by a variadic forwarding function.
625
669
  * @param {boolean} [trackActiveCommands]
626
670
  * @param {'measure' | 'observe'} [poolAcquisition]
627
- * @returns {(command: Function) => Function}
671
+ * @returns {(command: Function, method?: string) => Function}
628
672
  */
629
673
  function createWrapCallbackCommand (
630
674
  options,
@@ -634,7 +678,11 @@ function createWrapCallbackCommand (
634
678
  trackActiveCommands = true,
635
679
  poolAcquisition
636
680
  ) {
637
- return function wrapCommand (command) {
681
+ /**
682
+ * @param {Function} command
683
+ * @param {string} [method]
684
+ */
685
+ return function wrapCommand (command, method) {
638
686
  const callbackIndex = (commandArity ?? command.length) - 1
639
687
 
640
688
  return function (sql) {
@@ -642,7 +690,10 @@ function createWrapCallbackCommand (
642
690
 
643
691
  const owner = trackActiveCommands ? (commandOwner ?? this) : undefined
644
692
  const callback = arguments[arguments.length - 1]
645
- const ctx = { sql: preparedSql ?? normalizeSql(sql), conf: options }
693
+ const mirrorOptions = method === 'execute' ||
694
+ (method === 'query' && (typeof callback === 'function' || typeof arguments[1] === 'function'))
695
+ const originalSql = preparedSql ?? normalizeSql(sql)
696
+ const ctx = { sql: originalSql, conf: options }
646
697
  const acquisition = poolAcquisition === undefined
647
698
  ? undefined
648
699
  : createPoolAcquisition(this, options, ctx, poolAcquisition)
@@ -667,7 +718,9 @@ function createWrapCallbackCommand (
667
718
 
668
719
  try {
669
720
  return acquisition === undefined
670
- ? startCh.runStores(ctx, command, this, ...arguments)
721
+ ? startCh.runStores(
722
+ ctx, runSqlCommand, this, ctx, command, arguments, originalSql, preparedSql, mirrorOptions
723
+ )
671
724
  : runPoolAcquisition(acquisition, command, this, arguments)
672
725
  } catch (error) {
673
726
  finishPoolCommandAcquisition(acquisition, error)
@@ -692,12 +745,13 @@ function createWrapStream (options, preparedSql, commandOwner) {
692
745
  if (!startCh.hasSubscribers) return streamMethod.apply(this, arguments)
693
746
 
694
747
  const owner = commandOwner ?? this
695
- const ctx = { sql: preparedSql ?? normalizeSql(sql), conf: options }
748
+ const originalSql = preparedSql ?? normalizeSql(sql)
749
+ const ctx = { sql: originalSql, conf: options }
696
750
  let stream
697
751
 
698
752
  startCommand(owner)
699
753
  try {
700
- stream = startCh.runStores(ctx, streamMethod, this, ...arguments)
754
+ stream = startCh.runStores(ctx, runSqlCommand, this, ctx, streamMethod, arguments, originalSql, preparedSql)
701
755
  } catch (error) {
702
756
  finishCommand(ctx, owner, error)
703
757
  throw error
@@ -732,14 +786,18 @@ function createWrapStream (options, preparedSql, commandOwner) {
732
786
  * Wraps command methods exposed by a bundled client.
733
787
  *
734
788
  * @param {object} client
735
- * @param {(command: Function) => Function} wrapper
789
+ * @param {(command: Function, method?: string) => Function} wrapper
736
790
  */
737
791
  function wrapClientCommands (client, wrapper) {
738
792
  if (wrappedClients.has(client)) return
739
793
 
740
794
  wrappedClients.add(client)
741
795
  for (const method of commandMethods) {
742
- if (typeof client[method] === 'function') shimmer.wrap(client, method, wrapper)
796
+ if (typeof client[method] === 'function') {
797
+ /** @param {Function} command */
798
+ function wrapMethod (command) { return wrapper(command, method) }
799
+ shimmer.wrap(client, method, wrapMethod)
800
+ }
743
801
  }
744
802
  }
745
803
 
@@ -1570,4 +1628,4 @@ function wrapCallbackBundle (mariadb) {
1570
1628
  ])
1571
1629
  }
1572
1630
 
1573
- module.exports = { wrapCallbackBundle, wrapPromiseBundle }
1631
+ module.exports = { normalizeSql, runSqlCommand, wrapCallbackBundle, wrapPromiseBundle }
@@ -15,7 +15,7 @@ const {
15
15
  takePoolWaitTime,
16
16
  wrapPoolQueryMethod,
17
17
  } = require('./helpers/pool-acquire')
18
- const { wrapCallbackBundle, wrapPromiseBundle } = require('./mariadb-bundle')
18
+ const { normalizeSql, runSqlCommand, wrapCallbackBundle, wrapPromiseBundle } = require('./mariadb-bundle')
19
19
 
20
20
  const commandAddCh = channel('apm:mariadb:command:add')
21
21
  const connectionStartCh = channel('apm:mariadb:connection:start')
@@ -72,7 +72,12 @@ function wrapCommandStart (start, ctx) {
72
72
  }
73
73
  })
74
74
 
75
- return startCh.runStores(ctx, start, this, ...args)
75
+ // Subscribers may rewrite `ctx.sql` (DBM propagation comment). The command encodes `this.sql` when it
76
+ // starts, so copy the rewritten text back first, as the mysql and mysql2 instrumentations do.
77
+ return startCh.runStores(ctx, function (...startArgs) {
78
+ if (typeof this.sql === 'string' && this.sql && typeof ctx.sql === 'string') this.sql = ctx.sql
79
+ return start.apply(this, startArgs)
80
+ }, this, ...args)
76
81
  })
77
82
  }
78
83
 
@@ -97,11 +102,12 @@ function createWrapQuery (options) {
97
102
  return function (sql) {
98
103
  if (!startCh.hasSubscribers) return query.apply(this, arguments)
99
104
 
100
- const ctx = { sql, conf: options }
105
+ const originalSql = normalizeSql(sql)
106
+ const ctx = { sql: originalSql, conf: options }
101
107
  const poolWaitTime = takePoolWaitTime(this)
102
108
  if (poolWaitTime !== undefined) ctx.poolWaitTime = poolWaitTime
103
109
 
104
- return startCh.runStores(ctx, query, this, ...arguments)
110
+ return startCh.runStores(ctx, runSqlCommand, this, ctx, query, arguments, originalSql)
105
111
  .then(result => {
106
112
  ctx.result = result
107
113
  finishCh.publish(ctx)
@@ -122,7 +128,8 @@ function createWrapQueryCallback (options) {
122
128
  if (!startCh.hasSubscribers) return query.apply(this, arguments)
123
129
 
124
130
  const cb = arguments[arguments.length - 1]
125
- const ctx = { sql, conf: options }
131
+ const originalSql = normalizeSql(sql)
132
+ const ctx = { sql: originalSql, conf: options }
126
133
  const wrapper = (cb) => function (err) {
127
134
  if (err) {
128
135
  ctx.error = err
@@ -141,7 +148,7 @@ function createWrapQueryCallback (options) {
141
148
  arguments[arguments.length - 1] = wrapper()
142
149
  }
143
150
 
144
- return startCh.runStores(ctx, query, this, ...arguments)
151
+ return startCh.runStores(ctx, runSqlCommand, this, ctx, query, arguments, originalSql)
145
152
  }
146
153
  }
147
154
  }
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { addHook, channel } = require('../helpers/instrument')
4
4
  const shimmer = require('../../../datadog-shimmer')
5
+ const { getTestAnnotations, copyTestAnnotations } = require('../../../dd-trace/src/ci-visibility/test-annotations')
5
6
  const { getCallSites } = require('../../../dd-trace/src/plugins/util/stacktrace')
6
7
  const { DD_MAJOR } = require('../../../../version')
7
8
  const { testToStartLine } = require('./utils')
@@ -38,6 +39,13 @@ function addMochaRunHooks (versions, wrapMochaRun) {
38
39
  }
39
40
  metadata = { moduleBaseDir: hookMeta?.moduleBaseDir }
40
41
  patchedMochaConstructors.set(Mocha, metadata)
42
+ // ui is exposed on the constructor already observed by the require hooks, so no source rewrite is needed.
43
+ // Attach our listener after the interface installs its declarations, before skipped callbacks are discarded.
44
+ shimmer.wrap(Mocha.prototype, 'ui', ui => function () {
45
+ const result = ui.apply(this, arguments)
46
+ this.suite.on('pre-require', context => preserveDeclarationAnnotations(context, Mocha))
47
+ return result
48
+ })
41
49
  return wrapMochaRun(Mocha, frameworkVersion, metadata)
42
50
  }
43
51
 
@@ -76,6 +84,61 @@ function addMochaRunHooks (versions, wrapMochaRun) {
76
84
  }, wrapMochaPackage)
77
85
  }
78
86
 
87
+ /**
88
+ * @param {object} context
89
+ * @param {Function} Mocha
90
+ */
91
+ function preserveDeclarationAnnotations (context, Mocha) {
92
+ let declarationAnnotations
93
+ /** @param {Function} original */
94
+ function wrapDeclaration (original) {
95
+ return function (title, fn) {
96
+ const previousAnnotations = declarationAnnotations
97
+ // TDD test.skip delegates to test(title), dropping its callback and the returned Test.
98
+ declarationAnnotations ??= getTestAnnotations(fn)
99
+ try {
100
+ const test = original.apply(this, arguments)
101
+ if (test && !test.fn) test._ddDeclaredAnnotations = declarationAnnotations
102
+ return test
103
+ } finally {
104
+ declarationAnnotations = previousAnnotations
105
+ }
106
+ }
107
+ }
108
+
109
+ for (const key of ['it', 'specify', 'test', 'xit', 'xspecify']) {
110
+ const declare = context[key]
111
+ if (typeof declare !== 'function') continue
112
+ context[key] = shimmer.wrapFunction(declare, wrapDeclaration)
113
+ // Older Mocha interfaces copy declarations to exports before this listener runs.
114
+ for (const exported of ['it', 'specify', 'test', 'xit', 'xspecify']) {
115
+ if (Mocha[exported] === declare) Mocha[exported] = context[key]
116
+ }
117
+ for (const modifier of ['skip', 'only']) {
118
+ if (typeof declare[modifier] === 'function') {
119
+ context[key][modifier] = shimmer.wrapFunction(declare[modifier], wrapDeclaration)
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ // Carry declaration policy alongside each serialized execution, including duplicate full titles.
126
+ addHook({
127
+ name: 'mocha',
128
+ versions: ['>=8.0.0'],
129
+ filePattern: String.raw`lib/test\.(?:c?js)$`,
130
+ }, TestPackage => {
131
+ const Test = TestPackage.Test ?? TestPackage
132
+ shimmer.wrap(Test.prototype, 'serialize', serialize => function () {
133
+ const serialized = serialize.apply(this, arguments)
134
+ const { mustFail, noAutoRetry } = this._ddTestAnnotations || this._ddDeclaredAnnotations ||
135
+ getTestAnnotations(this.fn)
136
+ serialized._ddTestAnnotations = { mustFail, noAutoRetry }
137
+ return serialized
138
+ })
139
+ return TestPackage
140
+ })
141
+
79
142
  // mocha-each support
80
143
  addHook({
81
144
  name: 'mocha-each',
@@ -86,15 +149,33 @@ addHook({
86
149
  patched.add(mochaEach)
87
150
 
88
151
  return shimmer.wrapFunction(mochaEach, mochaEach => function (...args) {
89
- const [params] = args
90
- const { it, ...rest } = mochaEach.apply(this, args)
91
- return {
92
- it: function (title) {
93
- parameterizedTestCh.publish({ title, params })
94
- it.apply(this, arguments)
95
- },
96
- ...rest,
152
+ const [params, declare = global.it] = args
153
+ let sourceTestFn
154
+ /** @param {Function} original */
155
+ const wrapRow = original => function (title, fn) {
156
+ copyTestAnnotations(sourceTestFn, fn)
157
+ return original.apply(this, arguments)
158
+ }
159
+ const rowDeclaration = shimmer.wrapFunction(declare, wrapRow)
160
+ for (const modifier of ['skip', 'only']) {
161
+ if (typeof declare?.[modifier] === 'function') {
162
+ rowDeclaration[modifier] = shimmer.wrapFunction(declare[modifier], wrapRow)
163
+ }
164
+ }
165
+ const declarations = mochaEach.call(this, params, rowDeclaration, ...args.slice(2))
166
+ /** @param {Function} original */
167
+ const wrapEachDeclaration = original => function (title, fn) {
168
+ sourceTestFn = fn
169
+ parameterizedTestCh.publish({ title, params })
170
+ return original.apply(this, arguments)
171
+ }
172
+ declarations.it = shimmer.wrapFunction(declarations.it, wrapEachDeclaration)
173
+ for (const modifier of ['skip', 'only']) {
174
+ if (typeof declarations.it[modifier] === 'function') {
175
+ declarations.it[modifier] = shimmer.wrapFunction(declarations.it[modifier], wrapEachDeclaration)
176
+ }
97
177
  }
178
+ return declarations
98
179
  })
99
180
  })
100
181
 
@@ -1319,11 +1319,13 @@ addHook({
1319
1319
  shimmer.wrap(Runner.prototype, 'run', run => function (...args) {
1320
1320
  if (!testFinishCh.hasSubscribers) {
1321
1321
  resetRunState(this.suite)
1322
+ workerTestAnnotations.clear()
1322
1323
  return runMochaRunner(run, this, args)
1323
1324
  }
1324
1325
 
1325
1326
  const { onRunDone, onFlushDone } = getRunCompletionCallbacks(args[0])
1326
1327
  resetRunState(this.suite)
1328
+ workerTestAnnotations.clear()
1327
1329
  runnerFailuresAdjusted.delete(this)
1328
1330
  runnerFrameworkErrors.delete(this)
1329
1331
  runnerStarted.delete(this)
@@ -1451,7 +1453,7 @@ addHook({
1451
1453
  // instead of suiteA -> suiteB -> testA -> ... -> testB)
1452
1454
  // when the suite has tests that are in the top level
1453
1455
  // (no describe(...))
1454
- this.prependListener('test', getOnTestHandler(true))
1456
+ this.prependListener('test', getOnTestHandler(true, config))
1455
1457
  // Reporters are registered before Runner#run, so prepend this after the test handler
1456
1458
  // to start the suite first and preserve the test event if a reporter throws.
1457
1459
  this.prependListener('test', startRootSuite)
@@ -1658,12 +1660,15 @@ addHook({
1658
1660
  file: 'lib/runnable.js',
1659
1661
  }, (runnablePackage) => runnableWrapper(runnablePackage, config))
1660
1662
 
1663
+ const workerTestAnnotations = new Map()
1664
+
1661
1665
  function onMessage (message) {
1662
1666
  if (Array.isArray(message)) {
1663
1667
  const [messageCode, payload] = message
1664
1668
  if (messageCode === MOCHA_WORKER_TRACE_PAYLOAD_CODE) {
1665
1669
  collectTestOptimizationSummariesFromTraces(payload, {
1666
1670
  newTestsWithDynamicNames,
1671
+ testAnnotations: workerTestAnnotations,
1667
1672
  attemptToFixExecutions,
1668
1673
  })
1669
1674
  workerReportTraceCh.publish(payload)
@@ -1731,6 +1736,7 @@ addHook({
1731
1736
  filePattern: String.raw`lib/nodejs/parallel-buffered-runner\.(?:c?js)$`,
1732
1737
  }, (ParallelBufferedRunner, frameworkVersion) => {
1733
1738
  shimmer.wrap(ParallelBufferedRunner.prototype, 'run', run => function (cb, { files, options = {} }) {
1739
+ workerTestAnnotations.clear()
1734
1740
  if (!testFinishCh.hasSubscribers) {
1735
1741
  return run.apply(this, arguments)
1736
1742
  }
@@ -1914,6 +1920,9 @@ addHook({
1914
1920
  const isModified = config.isImpactedTestsEnabled && isModifiedTestSuite(testSuiteAbsolutePath)
1915
1921
 
1916
1922
  for (const test of tests) {
1923
+ test._ddTestAnnotations ||= workerTestAnnotations.get(
1924
+ `${getTestSuitePath(test.file, process.cwd())} › ${test.fullTitle()}`
1925
+ )
1917
1926
  const testProperties = getTestProperties(test, config.testManagementTests)
1918
1927
  const isAttemptToFix = config.isTestManagementTestsEnabled && testProperties.isAttemptToFix
1919
1928
 
@@ -1930,7 +1939,7 @@ addHook({
1930
1939
  }
1931
1940
  }
1932
1941
  // `efdTests` is filled in the worker process, so we need to use the test results to fill it here too.
1933
- if (!isAttemptToFix && (isNew || isModified)) {
1942
+ if (!test._ddTestAnnotations?.noAutoRetry && !isAttemptToFix && (isNew || isModified)) {
1934
1943
  const testFullName = getTestFullName(test)
1935
1944
  const tests = efdTests[testFullName]
1936
1945
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  const { performance } = require('node:perf_hooks')
4
4
 
5
+ const { getTestAnnotations } = require('../../../dd-trace/src/ci-visibility/test-annotations')
6
+
5
7
  const { getEnvironmentVariable } = require('../../../dd-trace/src/config/helper')
6
8
  const {
7
9
  getEfdRetryCountForDuration,
@@ -133,6 +135,21 @@ function getAfterEachHooks (testOrHook) {
133
135
  return hooks
134
136
  }
135
137
 
138
+ /**
139
+ * @param {{ file: string, fullTitle: () => string }} test
140
+ * @param {object} config
141
+ * @param {boolean} [config.isTestManagementTestsEnabled]
142
+ * @param {{ mocha?: { suites?: Record<string, {
143
+ * tests?: Record<string, { properties?: Record<string, boolean> }>
144
+ * }> } }} [config.testManagementTests]
145
+ */
146
+ function getAssignedProperties (test, config) {
147
+ return config.isTestManagementTestsEnabled
148
+ ? config.testManagementTests?.mocha?.suites
149
+ ?.[getTestSuitePath(test.file, process.cwd())]?.tests?.[test.fullTitle()]?.properties
150
+ : undefined
151
+ }
152
+
136
153
  function getTestProperties (test, testManagementTests) {
137
154
  const testSuite = getTestSuitePath(test.file, process.cwd())
138
155
  const testName = test.fullTitle()
@@ -140,7 +157,13 @@ function getTestProperties (test, testManagementTests) {
140
157
  const { attempt_to_fix: isAttemptToFix, disabled: isDisabled, quarantined: isQuarantined } =
141
158
  testManagementTests?.mocha?.suites?.[testSuite]?.tests?.[testName]?.properties || {}
142
159
 
143
- return { isAttemptToFix, isDisabled, isQuarantined }
160
+ const { mustFail, noAutoRetry } =
161
+ test._ddTestAnnotations || test._ddDeclaredAnnotations || getTestAnnotations(test.fn)
162
+ return {
163
+ isAttemptToFix: isAttemptToFix && !noAutoRetry,
164
+ isDisabled: isDisabled && !mustFail,
165
+ isQuarantined: isQuarantined && !mustFail,
166
+ }
144
167
  }
145
168
 
146
169
  function isNewTest (test, knownTests) {
@@ -258,7 +281,8 @@ function isDatadogManagedRetryTest (test, config) {
258
281
  * }} config
259
282
  */
260
283
  function isEarlyFlakeDetectionTest (test, config) {
261
- return !test._ddIsAttemptToFix &&
284
+ return !getTestAnnotations(test.fn).noAutoRetry && !test._ddTestAnnotations?.noAutoRetry &&
285
+ !test._ddIsAttemptToFix &&
262
286
  !test._ddIsDisabled &&
263
287
  config.isEarlyFlakeDetectionEnabled &&
264
288
  hasEfdRetries(config.earlyFlakeDetectionRetryPolicy) &&
@@ -281,6 +305,8 @@ function retryTest (test, numRetries, tags, retryPolicy) {
281
305
  }
282
306
  for (let retryIndex = 0; retryIndex < numRetries; retryIndex++) {
283
307
  const clonedTest = test.clone()
308
+ clonedTest._ddTestAnnotations = test._ddTestAnnotations
309
+ clonedTest._ddAssignedProperties = test._ddAssignedProperties
284
310
  disableMochaRetries(clonedTest)
285
311
  datadogRetryOriginals.set(clonedTest, test)
286
312
  suite.addTest(clonedTest)
@@ -378,6 +404,8 @@ function resetRunState (rootSuite) {
378
404
  if (test._retriedTest) originalRetriesByTest.delete(test._retriedTest)
379
405
  }
380
406
 
407
+ delete test._ddAssignedProperties
408
+ delete test._ddTestAnnotations
381
409
  delete test._ddIsAttemptToFix
382
410
  delete test._ddIsDisabled
383
411
  delete test._ddIsQuarantined
@@ -460,7 +488,7 @@ function recordRetryTrackedAttempt (test) {
460
488
  if (isNew) {
461
489
  recordTestAttempt(newTests, test)
462
490
  }
463
- if (!isAttemptToFix && (isNew || isModified)) {
491
+ if (!test._ddTestAnnotations?.noAutoRetry && !isAttemptToFix && (isNew || isModified)) {
464
492
  recordTestAttempt(efdTests, test)
465
493
  }
466
494
  }
@@ -525,6 +553,8 @@ function suppressSyntheticTestLifecycle (test) {
525
553
  function inheritDatadogPropertiesFromRetriedTest (test) {
526
554
  const retriedTest = test._retriedTest
527
555
  if (!retriedTest) return
556
+ test._ddTestAnnotations = retriedTest._ddTestAnnotations
557
+ test._ddAssignedProperties = retriedTest._ddAssignedProperties
528
558
 
529
559
  if (retriedTest._ddIsDisabled) {
530
560
  test._ddIsDisabled = true
@@ -577,7 +607,8 @@ function runnableWrapper (RunnablePackage, libraryConfig) {
577
607
  return onRunnableFinished.apply(this, arguments)
578
608
  }
579
609
  }
580
- } else if (libraryConfig?.isFlakyTestRetriesEnabled && !nativeRetryTests.has(test._retriedTest || test)) {
610
+ } else if (libraryConfig?.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
611
+ !nativeRetryTests.has(test._retriedTest || test)) {
581
612
  if (!originalRetriesByTest.has(test)) {
582
613
  const originalRetries = originalRetriesByTest.get(test._retriedTest) ?? test.retries()
583
614
  originalRetriesByTest.set(test, originalRetries)
@@ -633,7 +664,7 @@ function runnableWrapper (RunnablePackage, libraryConfig) {
633
664
  return RunnablePackage
634
665
  }
635
666
 
636
- function getOnTestHandler (isMain) {
667
+ function getOnTestHandler (isMain, config = {}) {
637
668
  return function (test) {
638
669
  if (test._ddSuppressSyntheticTestLifecycle || hasHandledHookFailure(test)) return
639
670
 
@@ -644,6 +675,9 @@ function getOnTestHandler (isMain) {
644
675
  restoreRunnableFunction(test)
645
676
 
646
677
  inheritDatadogPropertiesFromRetriedTest(test)
678
+ // Mocha 5/6 clones lack _retriedTest, but retain the annotated callback.
679
+ test._ddTestAnnotations ||= test._ddDeclaredAnnotations || getTestAnnotations(test.fn)
680
+ test._ddAssignedProperties ||= getAssignedProperties(test, config)
647
681
 
648
682
  if (isInFailedSuite(test.parent)) {
649
683
  recordRetryTrackedAttempt(test)
@@ -674,6 +708,8 @@ function getOnTestHandler (isMain) {
674
708
  }
675
709
 
676
710
  const testInfo = {
711
+ assignedProperties: test._ddAssignedProperties,
712
+ testAnnotations: test._ddTestAnnotations || test._ddDeclaredAnnotations || getTestAnnotations(test.fn),
677
713
  testName: test.fullTitle(),
678
714
  testSuiteAbsolutePath,
679
715
  title,
@@ -686,6 +722,7 @@ function getOnTestHandler (isMain) {
686
722
 
687
723
  testInfo.isNew = isNew
688
724
  testInfo.isEfdRetry = isEfdRetry
725
+ testInfo.assignedProperties = test._ddAssignedProperties
689
726
  testInfo.isAttemptToFix = isAttemptToFix
690
727
  testInfo.isDisabled = isDisabled
691
728
  testInfo.isQuarantined = isQuarantined
@@ -794,7 +831,7 @@ function getTestFinishInfo (test, status, config, error) {
794
831
  const originalTest = test._retriedTest || test
795
832
  if (
796
833
  config.isDynamicAtrEnabled &&
797
- config.isFlakyTestRetriesEnabled &&
834
+ config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
798
835
  !test._ddIsAttemptToFix &&
799
836
  !test._ddIsEfdRetry &&
800
837
  !dynamicAtrRetryCountByTest.has(originalTest) &&
@@ -823,7 +860,7 @@ function getTestFinishInfo (test, status, config, error) {
823
860
  // Mocha aborts native retries on hook failure, even when the retry budget is not exhausted.
824
861
  const isTerminalHookFailure = test._ddHookFailed && !isDatadogManagedRetryTest(test, config)
825
862
  const isLastAtrAttempt = isTerminalHookFailure || getIsLastRetry(test) ||
826
- (config.isFlakyTestRetriesEnabled && status === 'pass')
863
+ (config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry && status === 'pass')
827
864
 
828
865
  // Needed for the getFinalStatus call. This is because EFD does NOT tag as
829
866
  // EFD retry the first run of the test. It only tags as retries the clones
@@ -846,19 +883,20 @@ function getTestFinishInfo (test, status, config, error) {
846
883
  }
847
884
 
848
885
  // ATR: mark terminal attempts when every executed attempt failed.
849
- if (config.isFlakyTestRetriesEnabled && !test._ddIsAttemptToFix && !test._ddIsEfdRetry &&
886
+ if (config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
887
+ !test._ddIsAttemptToFix && !test._ddIsEfdRetry &&
850
888
  isLastAtrAttempt && testStatuses.every(status => status === 'fail')) {
851
889
  hasFailedAllRetries = true
852
890
  }
853
891
 
854
892
  const isAttemptToFixRetry = test._ddIsAttemptToFix && testStatuses.length > 1
855
- const isAtrRetry = config.isFlakyTestRetriesEnabled &&
893
+ const isAtrRetry = config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
856
894
  automaticRetryAttempts.get(test._retriedTest || test)?.has(test._currentRetry) &&
857
895
  !test._ddIsAttemptToFix &&
858
896
  !test._ddIsEfdRetry
859
897
  const isFinalAttempt = isTerminalHookFailure || status !== 'fail' || test._currentRetry >= test._retries
860
898
 
861
- const { isFlakyTestRetriesEnabled } = config
899
+ const isFlakyTestRetriesEnabled = config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry
862
900
  const { _ddIsAttemptToFix, _ddIsQuarantined, _ddIsDisabled } = test
863
901
 
864
902
  const finalStatus = getFinalStatus({
@@ -1211,7 +1249,7 @@ function getOnTestRetryHandler (config) {
1211
1249
  }
1212
1250
  const isFirstAttempt = test._currentRetry === 0
1213
1251
  const isDynamicAtrTest = config.isDynamicAtrEnabled &&
1214
- config.isFlakyTestRetriesEnabled &&
1252
+ config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
1215
1253
  automaticRetryTests.has(test) &&
1216
1254
  !test._ddIsAttemptToFix &&
1217
1255
  !isEarlyFlakeDetectionTest(test, config)
@@ -1248,7 +1286,7 @@ function publishTestRetry (test, err, config) {
1248
1286
  if (ctx) {
1249
1287
  const willBeRetried = test._currentRetry < test._retries
1250
1288
  const isAtrRetry = !isFirstAttempt &&
1251
- config.isFlakyTestRetriesEnabled &&
1289
+ config.isFlakyTestRetriesEnabled && !test._ddTestAnnotations?.noAutoRetry &&
1252
1290
  automaticRetryAttempts.get(test._retriedTest || test)?.has(test._currentRetry) &&
1253
1291
  !test._ddIsAttemptToFix &&
1254
1292
  !test._ddIsEfdRetry
@@ -1281,6 +1319,8 @@ function getOnPendingHandler () {
1281
1319
  } = test
1282
1320
 
1283
1321
  const testInfo = {
1322
+ assignedProperties: test._ddAssignedProperties,
1323
+ testAnnotations: test._ddTestAnnotations || test._ddDeclaredAnnotations || getTestAnnotations(test.fn),
1284
1324
  testName: test.fullTitle(),
1285
1325
  testSuiteAbsolutePath,
1286
1326
  title,
@@ -1307,6 +1347,12 @@ function getOnPendingHandler () {
1307
1347
  // Hook to add retries to tests if Test Management or EFD is enabled
1308
1348
  function getRunTestsWrapper (runTests, config) {
1309
1349
  return function (suite) {
1350
+ for (const test of suite.tests) {
1351
+ test._ddTestAnnotations = test.fn
1352
+ ? getTestAnnotations(originalFns.get(test.fn) || test.fn)
1353
+ : test._ddDeclaredAnnotations || getTestAnnotations()
1354
+ test._ddAssignedProperties = getAssignedProperties(test, config)
1355
+ }
1310
1356
  if (config.isTestManagementTestsEnabled) {
1311
1357
  suite.tests.forEach((test) => {
1312
1358
  const { isAttemptToFix, isDisabled, isQuarantined } = getTestProperties(test, config.testManagementTests)
@@ -484,7 +484,7 @@ addHook({
484
484
  }
485
485
  })
486
486
  }
487
- this.on('test', getOnTestHandler(false))
487
+ this.on('test', getOnTestHandler(false, config))
488
488
 
489
489
  this.on('test end', getOnTestEndHandler(config))
490
490