dd-trace 6.22.0 → 6.24.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 (194) 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 -5
  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 +35 -5
  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 +23 -20
  31. package/packages/datadog-core/src/storage.js +1 -1
  32. package/packages/datadog-esbuild/index.js +70 -7
  33. package/packages/datadog-instrumentations/index.js +2 -1
  34. package/packages/datadog-instrumentations/src/cucumber.js +64 -21
  35. package/packages/datadog-instrumentations/src/cypress-config.js +163 -15
  36. package/packages/datadog-instrumentations/src/graphql.js +9 -3
  37. package/packages/datadog-instrumentations/src/grpc/server.js +23 -6
  38. package/packages/datadog-instrumentations/src/helpers/bundler-register.js +27 -8
  39. package/packages/datadog-instrumentations/src/helpers/fetch.js +5 -1
  40. package/packages/datadog-instrumentations/src/helpers/graphql-jit-runtime.js +30 -8
  41. package/packages/datadog-instrumentations/src/helpers/hooks.js +2 -1
  42. package/packages/datadog-instrumentations/src/helpers/register.js +9 -0
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +16 -6
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +10 -0
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +3 -1
  46. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/jest.js +12 -0
  47. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/react-router.js +49 -0
  48. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/typesafe.js +23 -0
  49. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/vitest.js +8 -0
  50. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/webdriverio.js +12 -0
  51. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +8 -0
  52. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.js +19 -2
  53. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +13 -0
  54. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +1 -1
  55. package/packages/datadog-instrumentations/src/jest.js +332 -137
  56. package/packages/datadog-instrumentations/src/mariadb-bundle.js +69 -11
  57. package/packages/datadog-instrumentations/src/mariadb.js +13 -6
  58. package/packages/datadog-instrumentations/src/mocha/common.js +89 -8
  59. package/packages/datadog-instrumentations/src/mocha/main.js +11 -2
  60. package/packages/datadog-instrumentations/src/mocha/utils.js +92 -13
  61. package/packages/datadog-instrumentations/src/mocha/worker.js +1 -1
  62. package/packages/datadog-instrumentations/src/nova-sonic/session.js +25 -16
  63. package/packages/datadog-instrumentations/src/oracledb.js +33 -0
  64. package/packages/datadog-instrumentations/src/pg.js +2 -1
  65. package/packages/datadog-instrumentations/src/playwright.js +81 -26
  66. package/packages/datadog-instrumentations/src/react-router.js +106 -0
  67. package/packages/datadog-instrumentations/src/stripe.js +23 -12
  68. package/packages/datadog-instrumentations/src/typesafe.js +89 -0
  69. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +84 -47
  70. package/packages/datadog-instrumentations/src/vitest-main.js +33 -3
  71. package/packages/datadog-instrumentations/src/vitest-test-annotations.js +20 -0
  72. package/packages/datadog-instrumentations/src/vitest-util.js +5 -2
  73. package/packages/datadog-instrumentations/src/vitest-worker.js +39 -17
  74. package/packages/datadog-instrumentations/src/vitest.js +16 -6
  75. package/packages/datadog-plugin-cucumber/src/index.js +5 -0
  76. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +277 -44
  77. package/packages/datadog-plugin-cypress/src/index.js +1 -1
  78. package/packages/datadog-plugin-cypress/src/lifecycle-owner.js +20 -0
  79. package/packages/datadog-plugin-cypress/src/plugin.js +10 -0
  80. package/packages/datadog-plugin-cypress/src/retry-config-observer.js +281 -0
  81. package/packages/datadog-plugin-cypress/src/setup-handoff.js +125 -0
  82. package/packages/datadog-plugin-cypress/src/support.js +1217 -437
  83. package/packages/datadog-plugin-graphql/src/execute.js +2 -2
  84. package/packages/datadog-plugin-graphql/src/utils.js +5 -5
  85. package/packages/datadog-plugin-jest/src/index.js +25 -3
  86. package/packages/datadog-plugin-mocha/src/index.js +47 -6
  87. package/packages/datadog-plugin-oracledb/src/connection-parser.js +21 -11
  88. package/packages/datadog-plugin-playwright/src/index.js +4 -0
  89. package/packages/datadog-plugin-react-router/src/index.js +118 -0
  90. package/packages/datadog-plugin-typesafe/src/index.js +17 -0
  91. package/packages/datadog-plugin-typesafe/src/tracing.js +31 -0
  92. package/packages/datadog-plugin-vitest/src/index.js +8 -0
  93. package/packages/dd-trace/src/agent/info.js +2 -1
  94. package/packages/dd-trace/src/aiguard/evaluation.js +143 -1
  95. package/packages/dd-trace/src/aiguard/sdk.js +0 -11
  96. package/packages/dd-trace/src/appsec/iast/path-line.js +2 -1
  97. package/packages/dd-trace/src/appsec/stack_trace.js +3 -2
  98. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +3 -2
  99. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +9 -16
  100. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +4 -2
  101. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +9 -16
  102. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +9 -14
  103. package/packages/dd-trace/src/ci-visibility/requests/route.js +44 -0
  104. package/packages/dd-trace/src/ci-visibility/telemetry-logs.js +5 -0
  105. package/packages/dd-trace/src/ci-visibility/test-annotations.js +72 -0
  106. package/packages/dd-trace/src/ci-visibility/test-declaration-annotations.js +170 -0
  107. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +9 -16
  108. package/packages/dd-trace/src/config/defaults.js +4 -3
  109. package/packages/dd-trace/src/config/generated-config-types.d.ts +6 -0
  110. package/packages/dd-trace/src/config/index.js +140 -39
  111. package/packages/dd-trace/src/config/parsers.js +9 -1
  112. package/packages/dd-trace/src/config/supported-configurations.json +25 -0
  113. package/packages/dd-trace/src/debugger/devtools_client/breakpoints.js +6 -5
  114. package/packages/dd-trace/src/debugger/devtools_client/condition.js +5 -5
  115. package/packages/dd-trace/src/debugger/devtools_client/snapshot/collector.js +4 -6
  116. package/packages/dd-trace/src/debugger/probe_sampler.js +6 -6
  117. package/packages/dd-trace/src/debugger/unsupported-runtime.js +16 -0
  118. package/packages/dd-trace/src/encode/0.4.js +2 -2
  119. package/packages/dd-trace/src/esm-module-load.js +58 -0
  120. package/packages/dd-trace/src/exporter.js +25 -6
  121. package/packages/dd-trace/src/exporters/agent/index.js +3 -1
  122. package/packages/dd-trace/src/exporters/agentless/index.js +3 -1
  123. package/packages/dd-trace/src/exporters/common/request.js +4 -4
  124. package/packages/dd-trace/src/exporters/llmobs/index.js +135 -0
  125. package/packages/dd-trace/src/exporters/log/index.js +4 -3
  126. package/packages/dd-trace/src/llmobs/agent-manifest.js +19 -2
  127. package/packages/dd-trace/src/llmobs/constants/tags.js +4 -0
  128. package/packages/dd-trace/src/llmobs/experiments/index.js +2 -3
  129. package/packages/dd-trace/src/llmobs/experiments/noop.js +5 -2
  130. package/packages/dd-trace/src/llmobs/index.js +13 -2
  131. package/packages/dd-trace/src/llmobs/plugins/claude-agent-sdk/index.js +1 -1
  132. package/packages/dd-trace/src/llmobs/plugins/genai/index.js +1 -1
  133. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +8 -4
  134. package/packages/dd-trace/src/llmobs/plugins/openai/constants.js +5 -0
  135. package/packages/dd-trace/src/llmobs/plugins/openai/index.js +9 -1
  136. package/packages/dd-trace/src/llmobs/plugins/typesafe/index.js +102 -0
  137. package/packages/dd-trace/src/llmobs/prompts/cache.js +3 -1
  138. package/packages/dd-trace/src/llmobs/sdk.js +17 -5
  139. package/packages/dd-trace/src/llmobs/span_processor.js +222 -12
  140. package/packages/dd-trace/src/llmobs/tagger.js +21 -8
  141. package/packages/dd-trace/src/llmobs/util.js +10 -4
  142. package/packages/dd-trace/src/llmobs/writers/util.js +3 -3
  143. package/packages/dd-trace/src/log/writer.js +7 -1
  144. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +64 -6
  145. package/packages/dd-trace/src/openfeature/configuration_source.js +18 -2
  146. package/packages/dd-trace/src/openfeature/debug-logging-hook.js +66 -3
  147. package/packages/dd-trace/src/openfeature/exposure-hook.js +185 -0
  148. package/packages/dd-trace/src/openfeature/flagging_provider.js +39 -13
  149. package/packages/dd-trace/src/openfeature/index.js +6 -0
  150. package/packages/dd-trace/src/openfeature/remote_config.js +16 -3
  151. package/packages/dd-trace/src/openfeature/writers/base.js +62 -27
  152. package/packages/dd-trace/src/openfeature/writers/exposures.js +161 -1
  153. package/packages/dd-trace/src/openfeature/writers/flag-eval-evp-hook.js +5 -0
  154. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-aggregation.js +9 -4
  155. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-payload.js +2 -0
  156. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-pii.js +2 -0
  157. package/packages/dd-trace/src/openfeature/writers/split-features.js +25 -0
  158. package/packages/dd-trace/src/openfeature/writers/util.js +4 -4
  159. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +2 -2
  160. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +18 -1
  161. package/packages/dd-trace/src/opentelemetry/tracer_provider.js +5 -1
  162. package/packages/dd-trace/src/opentracing/tracer.js +9 -13
  163. package/packages/dd-trace/src/otel-thread-ctx.js +47 -21
  164. package/packages/dd-trace/src/plugin_manager.js +1 -2
  165. package/packages/dd-trace/src/plugins/ci_plugin.js +4 -4
  166. package/packages/dd-trace/src/plugins/index.js +3 -0
  167. package/packages/dd-trace/src/plugins/tracing.js +1 -2
  168. package/packages/dd-trace/src/plugins/util/git.js +102 -40
  169. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +2 -4
  170. package/packages/dd-trace/src/plugins/util/stacktrace.js +2 -1
  171. package/packages/dd-trace/src/plugins/util/test.js +28 -6
  172. package/packages/dd-trace/src/plugins/util/web.js +106 -20
  173. package/packages/dd-trace/src/process-tags/index.js +1 -1
  174. package/packages/dd-trace/src/profiling/config.js +6 -3
  175. package/packages/dd-trace/src/proxy.js +15 -4
  176. package/packages/dd-trace/src/serverless.js +3 -2
  177. package/packages/dd-trace/src/span_processor.js +21 -9
  178. package/packages/dd-trace/src/span_stats.js +32 -3
  179. package/packages/dd-trace/src/startup-log.js +2 -1
  180. package/packages/dd-trace/src/tagger.js +7 -5
  181. package/packages/dd-trace/src/telemetry/dependencies.js +4 -0
  182. package/packages/dd-trace/src/telemetry/index.js +3 -0
  183. package/packages/dd-trace/src/telemetry/logs/index.js +21 -2
  184. package/packages/dd-trace/src/telemetry/logs/log-collector.js +2 -1
  185. package/packages/dd-trace/src/telemetry/telemetry.js +10 -3
  186. package/packages/dd-trace/src/util.js +9 -5
  187. package/register.js +2 -1
  188. package/vendor/dist/@apm-js-collab/code-transformer/index.js +8 -8
  189. package/vendor/dist/@datadog/openfeature-node-server/index.js +1 -1
  190. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.d.ts +78 -0
  191. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.js +1 -1
  192. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  193. package/vendor/dist/@opentelemetry/resources/LICENSE +0 -201
  194. package/vendor/dist/@opentelemetry/resources/index.js +0 -1
@@ -1,10 +1,24 @@
1
1
  'use strict'
2
2
 
3
- const { HTTP_CLIENT_IP, HTTP_USERAGENT, NETWORK_CLIENT_IP } = require('../../../../ext/tags')
3
+ const {
4
+ ERROR,
5
+ HTTP_CLIENT_IP,
6
+ HTTP_USERAGENT,
7
+ MANUAL_DROP,
8
+ MANUAL_KEEP,
9
+ NETWORK_CLIENT_IP,
10
+ RESOURCE_NAME,
11
+ SAMPLING_PRIORITY,
12
+ SERVICE_NAME,
13
+ SPAN_KIND,
14
+ SPAN_TYPE,
15
+ } = require('../../../../ext/tags')
4
16
  const createRfdc = require('../../../../vendor/dist/rfdc')
5
17
  const clone = createRfdc({ proto: false, circles: false })
6
18
  const { USER_ID, USER_SESSION_ID } = require('../appsec/addresses')
7
19
  const { getActiveRequest } = require('../appsec/store')
20
+ const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE, PROCESS_ID, SAMPLING_PRIORITY_KEY } = require('../constants')
21
+ const log = require('../log')
8
22
  const { keepTrace } = require('../priority_sampler')
9
23
  const { extractIp } = require('../plugins/util/ip_extractor')
10
24
  const { AI_GUARD } = require('../standalone/product')
@@ -20,6 +34,12 @@ const ALLOW = 'ALLOW'
20
34
  /** @typedef {import('../../../../index').aiguard.RedactionReplacement} RedactionReplacement */
21
35
  /** @typedef {import('../opentracing/span')} Span */
22
36
 
37
+ /**
38
+ * @typedef {object} ExtraEntry
39
+ * @property {string} key
40
+ * @property {unknown} value
41
+ */
42
+
23
43
  /**
24
44
  * @typedef {object} EvaluationResponse
25
45
  * @property {string} action
@@ -30,6 +50,8 @@ const ALLOW = 'ALLOW'
30
50
  * @property {boolean} hasTagProbabilities
31
51
  * @property {boolean} blockingEnabled
32
52
  * @property {unknown} redactionReplacements
53
+ * @property {ExtraEntry[]} extraTags
54
+ * @property {ExtraEntry[]} extraMetastruct
33
55
  */
34
56
 
35
57
  /**
@@ -40,6 +62,8 @@ const ALLOW = 'ALLOW'
40
62
  * @property {boolean} shouldBlock
41
63
  * @property {boolean} hasTagProbabilities
42
64
  * @property {{ enabled: boolean, applied: boolean, failures: number }} redaction
65
+ * @property {ExtraEntry[]} extraTags
66
+ * @property {ExtraEntry[]} extraMetastruct
43
67
  */
44
68
 
45
69
  /**
@@ -68,6 +92,38 @@ function isRecord (value) {
68
92
  return value !== null && typeof value === 'object' && !Array.isArray(value)
69
93
  }
70
94
 
95
+ // Shared by every evaluation without extra entries, so the common case allocates nothing.
96
+ const NO_EXTRA_ENTRIES = /** @type {ExtraEntry[]} */ (/** @type {unknown} */ (Object.freeze([])))
97
+
98
+ /**
99
+ * Normalizes a backend extra field into well-formed key/value entries. The backend sends lists of
100
+ * entries rather than objects because the dotted keys are not valid JSON:API member names. Values
101
+ * are kept as-is: which of them reach the span is decided when they are applied.
102
+ *
103
+ * @param {string} field
104
+ * @param {unknown} entries
105
+ * @returns {ExtraEntry[]}
106
+ */
107
+ function normalizeExtraEntries (field, entries) {
108
+ if (entries === undefined) return NO_EXTRA_ENTRIES
109
+ if (!Array.isArray(entries)) {
110
+ log.debug('AI Guard: ignoring %s of type %s', field, entries === null ? 'null' : typeof entries)
111
+ return NO_EXTRA_ENTRIES
112
+ }
113
+
114
+ const normalized = []
115
+ for (const entry of entries) {
116
+ const key = isRecord(entry) ? entry.key : undefined
117
+ // A __proto__ key is never valid: it cannot become an own property on the span tags or the meta-struct.
118
+ if (typeof key === 'string' && key.length > 0 && key !== '__proto__') {
119
+ normalized.push({ key, value: entry.value })
120
+ } else {
121
+ log.debug('AI Guard: ignoring malformed %s entry', field)
122
+ }
123
+ }
124
+ return normalized.length === 0 ? NO_EXTRA_ENTRIES : normalized
125
+ }
126
+
71
127
  /**
72
128
  * Parses the backend evaluation response into the tracer's internal contract.
73
129
  *
@@ -89,6 +145,9 @@ function parseEvaluationResponse (body) {
89
145
  hasTagProbabilities: isRecord(attributes.tag_probs),
90
146
  blockingEnabled: attributes.is_blocking_enabled === true,
91
147
  redactionReplacements: attributes.redaction_replacements,
148
+ // Malformed extra entries are dropped here instead of turning the response into a bad_response.
149
+ extraTags: normalizeExtraEntries('extra_tags', attributes.extra_tags),
150
+ extraMetastruct: normalizeExtraEntries('extra_metastruct', attributes.extra_metastruct),
92
151
  }
93
152
  }
94
153
 
@@ -132,6 +191,8 @@ function createEvaluationOutcome (messages, evaluation, options) {
132
191
  applied: redaction.redacted,
133
192
  failures: redaction.failures,
134
193
  },
194
+ extraTags: evaluation.extraTags,
195
+ extraMetastruct: evaluation.extraMetastruct,
135
196
  }
136
197
  }
137
198
 
@@ -147,6 +208,86 @@ const SERVICE_ENTRY_TAG_MAPPINGS = [
147
208
  [USER_SESSION_ID, TAGS.USR_SESSION_ID_TAG_KEY],
148
209
  ]
149
210
 
211
+ // Backend extra tags never override what the tracer computes itself, nor keys that control the
212
+ // span or its trace (sampling, service, resource, error state). Internal _dd keys are covered by
213
+ // isReservedExtraTag instead of being listed here.
214
+ const RESERVED_EXTRA_TAGS = new Set([
215
+ TAGS.ACTION_TAG_KEY,
216
+ TAGS.REASON_TAG_KEY,
217
+ TAGS.TARGET_TAG_KEY,
218
+ TAGS.TOOL_NAME_TAG_KEY,
219
+ TAGS.BLOCKED_TAG_KEY,
220
+ TAGS.REDACTED_TAG_KEY,
221
+ TAGS.EVENT_TAG_KEY,
222
+ ...SERVICE_ENTRY_TAG_MAPPINGS.map(([, destinationTag]) => destinationTag),
223
+ MANUAL_KEEP,
224
+ MANUAL_DROP,
225
+ SAMPLING_PRIORITY,
226
+ SERVICE_NAME,
227
+ RESOURCE_NAME,
228
+ SPAN_TYPE,
229
+ SPAN_KIND,
230
+ ERROR,
231
+ ERROR_TYPE,
232
+ ERROR_MESSAGE,
233
+ ERROR_STACK,
234
+ SAMPLING_PRIORITY_KEY,
235
+ PROCESS_ID,
236
+ 'analytics.event',
237
+ 'language',
238
+ 'runtime-id',
239
+ 'service.version',
240
+ 'version',
241
+ 'env',
242
+ ])
243
+
244
+ /**
245
+ * Returns whether a backend extra tag key must not be set on the span.
246
+ *
247
+ * @param {string} key
248
+ */
249
+ function isReservedExtraTag (key) {
250
+ // _dd is the tracer's internal namespace (_dd.*, _dd1.*): the span formatter writes sampling,
251
+ // top-level and propagation keys there and a span tag with the same key would overwrite them.
252
+ // This is not an AI Guard namespace filter; the backend owns ai_guard.* keys.
253
+ return key.startsWith('_dd') || RESERVED_EXTRA_TAGS.has(key)
254
+ }
255
+
256
+ const RESERVED_EXTRA_META_STRUCT_KEYS = new Set(['messages', 'attack_categories', 'sds', 'tag_probs'])
257
+
258
+ /**
259
+ * Copies backend-provided extra_tags to the span and extra_metastruct to the AI Guard meta-struct,
260
+ * skipping values and keys the span must not receive. Keys are used verbatim and entries are
261
+ * applied in order, so a repeated key keeps its last value.
262
+ *
263
+ * @param {Span} span
264
+ * @param {EvaluationMetaStruct} metaStruct
265
+ * @param {ExtraEntry[]} extraTags
266
+ * @param {ExtraEntry[]} extraMetastruct
267
+ */
268
+ function applyExtraSpanData (span, metaStruct, extraTags, extraMetastruct) {
269
+ // The client decodes values with JSON.parse, so a string check and a null check cover every JSON type.
270
+ for (const { key, value } of extraTags) {
271
+ if (typeof value !== 'string') {
272
+ log.debug('AI Guard: ignoring extra_tags entry %s with non-string value', key)
273
+ } else if (isReservedExtraTag(key)) {
274
+ log.debug('AI Guard: ignoring reserved extra tag %s', key)
275
+ } else {
276
+ span.setTag(key, value)
277
+ }
278
+ }
279
+
280
+ for (const { key, value } of extraMetastruct) {
281
+ if (value === undefined || value === null) {
282
+ log.debug('AI Guard: ignoring extra_metastruct entry %s with null value', key)
283
+ } else if (RESERVED_EXTRA_META_STRUCT_KEYS.has(key)) {
284
+ log.debug('AI Guard: ignoring reserved extra_metastruct key %s', key)
285
+ } else {
286
+ metaStruct[key] = value
287
+ }
288
+ }
289
+ }
290
+
150
291
  class EvaluationReporter {
151
292
  #config
152
293
  #maxMessagesLength
@@ -230,6 +371,7 @@ class EvaluationReporter {
230
371
  if (result.tags.length > 0) report.metaStruct.attack_categories = result.tags
231
372
  if (result.sds.length > 0) report.metaStruct.sds = result.sds
232
373
  if (outcome.hasTagProbabilities) report.metaStruct.tag_probs = result.tagProbabilities
374
+ applyExtraSpanData(report.span, report.metaStruct, outcome.extraTags, outcome.extraMetastruct)
233
375
 
234
376
  if (redaction.enabled) {
235
377
  report.span.setTag(TAGS.REDACTED_TAG_KEY, redaction.applied ? 'true' : 'false')
@@ -1,6 +1,5 @@
1
1
  'use strict'
2
2
 
3
- const log = require('../log')
4
3
  const AIGuardClient = require('./client')
5
4
  const { createEvaluationOutcome, EvaluationReporter } = require('./evaluation')
6
5
  const { AIGuardAbortError } = require('./errors')
@@ -8,7 +7,6 @@ const NoopAIGuard = require('./noop')
8
7
  const TAGS = require('./tags')
9
8
 
10
9
  class AIGuard extends NoopAIGuard {
11
- #initialized
12
10
  #tracer
13
11
  #client
14
12
  #reporter
@@ -22,23 +20,14 @@ class AIGuard extends NoopAIGuard {
22
20
  constructor (tracer, config) {
23
21
  super()
24
22
 
25
- if (!config.DD_API_KEY || !config.DD_APP_KEY) {
26
- log.error('AIGuard: missing api and/or app keys, use env DD_API_KEY and DD_APP_KEY')
27
- this.#initialized = false
28
- return
29
- }
30
23
  this.#tracer = tracer
31
24
  this.#client = new AIGuardClient(config)
32
25
  this.#reporter = new EvaluationReporter(config)
33
26
  this.#redactionEnabled = config.aiguard.DD_AI_GUARD_REDACTION_ENABLED
34
27
  this.#meta = { service: config.service, env: config.env }
35
- this.#initialized = true
36
28
  }
37
29
 
38
30
  evaluate (messages, opts) {
39
- if (!this.#initialized) {
40
- return super.evaluate(messages, opts)
41
- }
42
31
  const { block = true, source = TAGS.SOURCE_SDK, integration = TAGS.INTEGRATION_NONE, childOf } = opts ?? {}
43
32
  // Only pass `childOf` when truthy so `tracer.trace`'s default (`scope().active()`)
44
33
  // still applies for SDK callers that don't supply an explicit parent.
@@ -66,7 +66,8 @@ function getCallSiteFramesForLocation (callSiteFrames, externallyExcludedPaths)
66
66
 
67
67
  if (
68
68
  !isExcluded(callsite, externallyExcludedPaths) &&
69
- (!filepath.includes(pathLine.ddBasePath) || globalThis.__DD_ESBUILD_IAST_WITH_NO_SM)
69
+ (pathLine.ddBasePath === undefined || !filepath.includes(pathLine.ddBasePath) ||
70
+ globalThis.__DD_ESBUILD_IAST_WITH_NO_SM)
70
71
  ) {
71
72
  clientFrames.push(callsite)
72
73
  }
@@ -34,8 +34,9 @@ function getCallSiteList (maxDepth = 100, constructorOpt) {
34
34
 
35
35
  function filterOutFramesFromLibrary (callSiteList) {
36
36
  return callSiteList.filter(callSite => {
37
- if (globalThis.__DD_ESBUILD_IAST_WITH_NO_SM) {
38
- // bundled and no SourceMap, not possible to discriminate if the frame comes from dd-trace code or not
37
+ if (globalThis.__DD_ESBUILD_IAST_WITH_NO_SM || ddBasePath === undefined) {
38
+ // bundled without a SourceMap, or dd-trace's location is unknown: not possible to discriminate if the frame
39
+ // comes from dd-trace code or not
39
40
  return true
40
41
  }
41
42
 
@@ -228,9 +228,10 @@ async function addBreakpoint (probe) {
228
228
  }
229
229
  }
230
230
 
231
- function start () {
231
+ async function start () {
232
+ await session.post('Debugger.enable')
233
+ // Only flag the session as started once enabled, so the next probe retries a failed start and reports its cause
232
234
  sessionStarted = true
233
- return session.post('Debugger.enable') // return instead of await to reduce number of promises created
234
235
  }
235
236
 
236
237
  function drainBreakpointHitRequests () {
@@ -1,8 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const getConfig = require('../../config')
4
- const { EVP_SUBDOMAIN_HEADER_NAME } = require('../../evp_proxy/constants')
5
- const { joinEVPProxyPath } = require('../../evp_proxy/path')
6
4
  const id = require('../../id')
7
5
  const log = require('../../log')
8
6
  const {
@@ -16,6 +14,7 @@ const {
16
14
  } = require('../../ci-visibility/telemetry')
17
15
  const { getNumFromKnownTests } = require('../../plugins/util/test')
18
16
  const request = require('../requests/request')
17
+ const { createApiRequestRoute } = require('../requests/route')
19
18
  const { buildCacheKey, writeToCache, withCache } = require('../requests/fs-cache')
20
19
  const { validateKnownTestsResponse } = require('../test-optimization-http-cache-schema')
21
20
 
@@ -147,32 +146,26 @@ function fetchFromApi ({
147
146
  custom,
148
147
  cacheKey,
149
148
  }, done) {
149
+ const path = '/api/v2/ci/libraries/tests'
150
+ const route = createApiRequestRoute(getConfig(), { url, path, isEvpProxy, evpProxyPrefix })
151
+ if (!route) {
152
+ return done(new Error('Known tests were not fetched because Datadog API key is not defined.'))
153
+ }
154
+
150
155
  const options = {
151
- path: '/api/v2/ci/libraries/tests',
156
+ ...route,
152
157
  method: 'POST',
153
158
  headers: {
159
+ ...route.headers,
154
160
  'Content-Type': 'application/json',
155
161
  },
156
162
  timeout: 20_000,
157
- url,
158
163
  }
159
164
 
160
165
  if (isGzipCompatible) {
161
166
  options.headers['accept-encoding'] = 'gzip'
162
167
  }
163
168
 
164
- if (isEvpProxy) {
165
- options.path = joinEVPProxyPath(evpProxyPrefix, '/api/v2/ci/libraries/tests')
166
- options.headers[EVP_SUBDOMAIN_HEADER_NAME] = 'api'
167
- } else {
168
- const { DD_API_KEY } = getConfig()
169
- if (!DD_API_KEY) {
170
- return done(new Error('Known tests were not fetched because Datadog API key is not defined.'))
171
- }
172
-
173
- options.headers['dd-api-key'] = DD_API_KEY
174
- }
175
-
176
169
  const configurations = {
177
170
  'os.platform': osPlatform,
178
171
  'os.version': osVersion,
@@ -27,6 +27,7 @@ const {
27
27
  } = require('../requests/upload-test-screenshot')
28
28
  const { parsers } = require('../../config/parsers')
29
29
  const log = require('../../log')
30
+ const telemetry = require('../../telemetry')
30
31
  const { getSegment } = require('../../util')
31
32
  const BufferingExporter = require('../../exporters/common/buffering-exporter')
32
33
  const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA } = require('../../plugins/util/tags')
@@ -264,7 +265,8 @@ class CiVisibilityExporter extends BufferingExporter {
264
265
  }
265
266
  })
266
267
 
267
- globalThis[Symbol.for('dd-trace')].beforeExitHandlers.add(() => this.flush(() => {}))
268
+ // Final upload errors are recorded after telemetry's initial app-closing drain.
269
+ globalThis[Symbol.for('dd-trace')].beforeExitHandlers.add(() => this.flush(() => telemetry.flush()))
268
270
  }
269
271
 
270
272
  shouldRequestSkippableSuites () {
@@ -624,7 +626,7 @@ class CiVisibilityExporter extends BufferingExporter {
624
626
  repositoryUrl,
625
627
  (err) => {
626
628
  if (err) {
627
- log.error('Error uploading git metadata: %s', err.message)
629
+ log.error('Error uploading git metadata', err)
628
630
  } else {
629
631
  log.debug('Successfully uploaded git metadata')
630
632
  }
@@ -1,8 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const getConfig = require('../../config')
4
- const { EVP_SUBDOMAIN_HEADER_NAME } = require('../../evp_proxy/constants')
5
- const { joinEVPProxyPath } = require('../../evp_proxy/path')
6
4
  const log = require('../../log')
7
5
  const {
8
6
  incrementCountMetric,
@@ -15,6 +13,7 @@ const {
15
13
  TELEMETRY_ITR_SKIPPABLE_TESTS_RESPONSE_BYTES,
16
14
  } = require('../../ci-visibility/telemetry')
17
15
  const request = require('../requests/request')
16
+ const { createApiRequestRoute } = require('../requests/route')
18
17
  const { buildCacheKey, writeToCache, withCache } = require('../requests/fs-cache')
19
18
  const { validateSkippableTestsResponse } = require('../test-optimization-http-cache-schema')
20
19
 
@@ -213,32 +212,26 @@ function fetchFromApi ({
213
212
  isLineCoverageSupported,
214
213
  cacheKey,
215
214
  }, done) {
215
+ const path = '/api/v2/ci/tests/skippable'
216
+ const route = createApiRequestRoute(getConfig(), { url, path, isEvpProxy, evpProxyPrefix })
217
+ if (!route) {
218
+ return done(new Error('Skippable suites were not fetched because Datadog API key is not defined.'))
219
+ }
220
+
216
221
  const options = {
217
- path: '/api/v2/ci/tests/skippable',
222
+ ...route,
218
223
  method: 'POST',
219
224
  headers: {
225
+ ...route.headers,
220
226
  'Content-Type': 'application/json',
221
227
  },
222
228
  timeout: 20_000,
223
- url,
224
229
  }
225
230
 
226
231
  if (isGzipCompatible) {
227
232
  options.headers['accept-encoding'] = 'gzip'
228
233
  }
229
234
 
230
- if (isEvpProxy) {
231
- options.path = joinEVPProxyPath(evpProxyPrefix, '/api/v2/ci/tests/skippable')
232
- options.headers[EVP_SUBDOMAIN_HEADER_NAME] = 'api'
233
- } else {
234
- const { DD_API_KEY } = getConfig()
235
- if (!DD_API_KEY) {
236
- return done(new Error('Skippable suites were not fetched because Datadog API key is not defined.'))
237
- }
238
-
239
- options.headers['dd-api-key'] = DD_API_KEY
240
- }
241
-
242
235
  const data = JSON.stringify({
243
236
  data: {
244
237
  type: 'test_params',
@@ -1,8 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const getConfig = require('../../config')
4
- const { EVP_SUBDOMAIN_HEADER_NAME } = require('../../evp_proxy/constants')
5
- const { joinEVPProxyPath } = require('../../evp_proxy/path')
6
4
  const id = require('../../id')
7
5
  const log = require('../../log')
8
6
  const { EARLY_FLAKE_DETECTION_RETRY_BUCKETS, createEfdRetryPolicy } = require('../efd-retry-policy')
@@ -16,6 +14,7 @@ const {
16
14
  } = require('../telemetry')
17
15
  const { MAX_RETRIES, validateSettingsResponse } = require('../test-optimization-http-cache-schema')
18
16
  const request = require('./request')
17
+ const { createApiRequestRoute } = require('./route')
19
18
 
20
19
  const DEFAULT_EARLY_FLAKE_DETECTION_SLOW_TEST_RETRIES = Object.freeze({
21
20
  '5s': 10,
@@ -228,26 +227,22 @@ function getLibraryConfiguration ({
228
227
  tag,
229
228
  }, done) {
230
229
  const config = getConfig()
230
+ const path = '/api/v2/libraries/tests/services/setting'
231
+ const route = createApiRequestRoute(config, { url, path, isEvpProxy, evpProxyPrefix })
232
+ if (!route) {
233
+ return done(new Error('Request to settings endpoint was not done because Datadog API key is not defined.'))
234
+ }
235
+
231
236
  const options = {
232
- path: '/api/v2/libraries/tests/services/setting',
237
+ ...route,
233
238
  method: 'POST',
234
239
  headers: {
240
+ ...route.headers,
235
241
  'Content-Type': 'application/json',
236
242
  },
237
- url,
238
243
  timeout: 20_000,
239
244
  }
240
245
 
241
- if (isEvpProxy) {
242
- options.path = joinEVPProxyPath(evpProxyPrefix, '/api/v2/libraries/tests/services/setting')
243
- options.headers[EVP_SUBDOMAIN_HEADER_NAME] = 'api'
244
- } else {
245
- if (!config.DD_API_KEY) {
246
- return done(new Error('Request to settings endpoint was not done because Datadog API key is not defined.'))
247
- }
248
- options.headers['dd-api-key'] = config.DD_API_KEY
249
- }
250
-
251
246
  const data = JSON.stringify({
252
247
  data: {
253
248
  id: id().toString(10),
@@ -0,0 +1,44 @@
1
+ 'use strict'
2
+
3
+ const { EVP_SUBDOMAIN_HEADER_NAME } = require('../../evp_proxy/constants')
4
+ const { joinEVPProxyPath } = require('../../evp_proxy/path')
5
+
6
+ /**
7
+ * @typedef {object} ApiRequestRouteOptions
8
+ * @property {URL} url
9
+ * @property {string} path
10
+ * @property {boolean} isEvpProxy
11
+ * @property {string} evpProxyPrefix
12
+ */
13
+
14
+ /**
15
+ * @typedef {object} ApiRequestRoute
16
+ * @property {URL} url
17
+ * @property {string} path
18
+ * @property {Record<string, string>} headers
19
+ */
20
+
21
+ /**
22
+ * @param {import('../../config/config-base')} config
23
+ * @param {ApiRequestRouteOptions} options
24
+ * @returns {ApiRequestRoute|undefined}
25
+ */
26
+ function createApiRequestRoute (config, { url, path, isEvpProxy, evpProxyPrefix }) {
27
+ if (isEvpProxy) {
28
+ return {
29
+ url,
30
+ path: joinEVPProxyPath(evpProxyPrefix, path),
31
+ headers: { [EVP_SUBDOMAIN_HEADER_NAME]: 'api' },
32
+ }
33
+ }
34
+
35
+ if (config.DD_API_KEY === undefined) return
36
+
37
+ return {
38
+ url,
39
+ path,
40
+ headers: { 'dd-api-key': config.DD_API_KEY },
41
+ }
42
+ }
43
+
44
+ module.exports = { createApiRequestRoute }
@@ -0,0 +1,5 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ tags: 'test_optimization_enabled:true',
5
+ }
@@ -0,0 +1,72 @@
1
+ 'use strict'
2
+
3
+ // Symbols cross Jest's VM/module-registry boundary without initializing the tracer.
4
+ const annotationsKey = Symbol.for('dd-trace:test-annotations')
5
+ /** @type {{ mustFail: boolean, noAutoRetry: boolean, source?: Function }} */
6
+ const emptyAnnotations = Object.freeze({ mustFail: false, noAutoRetry: false })
7
+
8
+ /** @param {Function|undefined} fn */
9
+ function getTestAnnotations (fn) {
10
+ return (fn && fn[annotationsKey]) || emptyAnnotations
11
+ }
12
+
13
+ /**
14
+ * @param {Function|undefined} source
15
+ * @param {Function} target
16
+ */
17
+ function copyTestAnnotations (source, target) {
18
+ const annotations = source && source[annotationsKey]
19
+ if (annotations && target[annotationsKey] !== annotations) {
20
+ Object.defineProperty(target, annotationsKey, { value: annotations, configurable: true })
21
+ }
22
+ }
23
+
24
+ /**
25
+ * @template {Function} T
26
+ * @param {T} fn
27
+ * @param {'mustFail'|'noAutoRetry'} annotation
28
+ * @returns {T}
29
+ */
30
+ function annotate (fn, annotation) {
31
+ if (typeof fn !== 'function') throw new TypeError('Test annotations require a test callback')
32
+ const inherited = getTestAnnotations(fn)
33
+ const annotated = function (...args) {
34
+ return fn.apply(this, args)
35
+ }
36
+ Object.setPrototypeOf(annotated, Object.getPrototypeOf(fn))
37
+ Object.defineProperties(annotated, {
38
+ toString: { value: () => fn.toString(), writable: true, configurable: true },
39
+ ...Object.getOwnPropertyDescriptors(fn),
40
+ length: { value: fn.length },
41
+ name: { value: fn.name },
42
+ [annotationsKey]: {
43
+ value: Object.freeze({
44
+ ...inherited,
45
+ [annotation]: true,
46
+ source: inherited.source || fn,
47
+ }),
48
+ },
49
+ })
50
+ if (Object.isFrozen(fn)) Object.freeze(annotated)
51
+ return /** @type {T} */ (/** @type {unknown} */ (annotated))
52
+ }
53
+
54
+ /**
55
+ * @template {Function} T
56
+ * @param {T} fn
57
+ * @returns {T}
58
+ */
59
+ function preventFailureSuppression (fn) {
60
+ return annotate(fn, 'mustFail')
61
+ }
62
+
63
+ /**
64
+ * @template {Function} T
65
+ * @param {T} fn
66
+ * @returns {T}
67
+ */
68
+ function noAutoRetry (fn) {
69
+ return annotate(fn, 'noAutoRetry')
70
+ }
71
+
72
+ module.exports = { preventFailureSuppression, noAutoRetry, getTestAnnotations, copyTestAnnotations }