dd-trace 5.135.0 → 5.137.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 (352) hide show
  1. package/README.md +3 -0
  2. package/ci/cypress/support.js +2 -1
  3. package/ci/init.js +2 -22
  4. package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
  5. package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
  6. package/ci/vitest-no-worker-init-setup.mjs +729 -724
  7. package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
  8. package/ext/exporters.js +0 -1
  9. package/index.d.ts +105 -5
  10. package/initialize.mjs +5 -2
  11. package/package.json +41 -36
  12. package/packages/datadog-esbuild/index.js +16 -70
  13. package/packages/datadog-esbuild/src/log.js +2 -29
  14. package/packages/datadog-instrumentations/src/ai.js +6 -1
  15. package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
  16. package/packages/datadog-instrumentations/src/cucumber.js +5 -2
  17. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  18. package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
  19. package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
  20. package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
  21. package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
  22. package/packages/datadog-instrumentations/src/graphql.js +20 -17
  23. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +582 -0
  24. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  25. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +576 -0
  26. package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
  27. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  28. package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
  29. package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -7
  30. package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +8 -0
  32. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +26 -1
  33. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
  34. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
  35. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
  36. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  37. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  38. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +61 -0
  39. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
  40. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  41. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
  42. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +80 -0
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +23 -3
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
  46. package/packages/datadog-instrumentations/src/ioredis.js +101 -3
  47. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  48. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  49. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
  50. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
  51. package/packages/datadog-instrumentations/src/next.js +203 -26
  52. package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
  53. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
  54. package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
  55. package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
  56. package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
  57. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
  58. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
  59. package/packages/datadog-instrumentations/src/router.js +35 -12
  60. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  61. package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
  62. package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
  63. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  64. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  65. package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
  66. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  67. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  68. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  69. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  70. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  71. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  72. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +50 -13
  73. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  74. package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
  75. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  76. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  77. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +20 -7
  78. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +21 -52
  79. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +11 -2
  80. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
  81. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  82. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  83. package/packages/datadog-plugin-http/src/client.js +34 -10
  84. package/packages/datadog-plugin-http2/src/client.js +40 -14
  85. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  86. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  87. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  88. package/packages/datadog-plugin-next/src/index.js +112 -14
  89. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  90. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
  91. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  92. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  93. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  94. package/packages/datadog-plugin-trpc/src/index.js +15 -0
  95. package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
  96. package/packages/datadog-plugin-trpc/src/request.js +81 -0
  97. package/packages/datadog-plugin-undici/src/index.js +22 -7
  98. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  99. package/packages/datadog-plugin-ws/src/close.js +3 -5
  100. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  101. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  102. package/packages/datadog-plugin-ws/src/server.js +44 -13
  103. package/packages/datadog-turbopack/index.js +4 -28
  104. package/packages/datadog-turbopack/src/loader.js +14 -452
  105. package/packages/datadog-vite/index.js +537 -0
  106. package/packages/datadog-vite/src/log.js +5 -0
  107. package/packages/datadog-webpack/index.js +3 -55
  108. package/packages/datadog-webpack/src/log.js +2 -29
  109. package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
  110. package/packages/dd-trace/src/aiguard/index.js +1 -1
  111. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  112. package/packages/dd-trace/src/aiguard/sdk.js +8 -2
  113. package/packages/dd-trace/src/aiguard/tags.js +2 -0
  114. package/packages/dd-trace/src/appsec/blocking/index.js +21 -1
  115. package/packages/dd-trace/src/appsec/channels.js +7 -0
  116. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  117. package/packages/dd-trace/src/appsec/graphql.js +89 -7
  118. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  119. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  120. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  121. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  122. package/packages/dd-trace/src/appsec/index.js +6 -0
  123. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  124. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  125. package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
  126. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  127. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  128. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  129. package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
  130. package/packages/dd-trace/src/appsec/recommended.json +1358 -94
  131. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  132. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  133. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  134. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  135. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  136. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  137. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  138. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  139. package/packages/dd-trace/src/before-exit.js +32 -0
  140. package/packages/dd-trace/src/bootstrap.js +5 -3
  141. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +15 -2
  142. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +12 -34
  143. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
  144. package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
  145. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +1 -2
  146. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +1 -1
  147. package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
  148. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +1 -1
  149. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
  150. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
  151. package/packages/dd-trace/src/config/config-types.d.ts +3 -0
  152. package/packages/dd-trace/src/config/defaults.js +5 -0
  153. package/packages/dd-trace/src/config/generated-config-types.d.ts +46 -4
  154. package/packages/dd-trace/src/config/index.js +63 -31
  155. package/packages/dd-trace/src/config/parsers.js +44 -0
  156. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  157. package/packages/dd-trace/src/config/remote_config.js +2 -1
  158. package/packages/dd-trace/src/config/supported-configurations.json +172 -10
  159. package/packages/dd-trace/src/constants.js +1 -0
  160. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  161. package/packages/dd-trace/src/datastreams/pathway.js +113 -28
  162. package/packages/dd-trace/src/datastreams/processor.js +5 -4
  163. package/packages/dd-trace/src/debugger/config.js +1 -1
  164. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  165. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  166. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  167. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  168. package/packages/dd-trace/src/debugger/index.js +2 -2
  169. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  170. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +6 -2
  171. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  172. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  173. package/packages/dd-trace/src/exporter.js +0 -11
  174. package/packages/dd-trace/src/exporters/common/request.js +81 -48
  175. package/packages/dd-trace/src/exporters/common/retry.js +12 -7
  176. package/packages/dd-trace/src/exporters/common/url.js +1 -1
  177. package/packages/dd-trace/src/git_metadata.js +2 -1
  178. package/packages/dd-trace/src/id.js +1 -1
  179. package/packages/dd-trace/src/index.js +6 -3
  180. package/packages/dd-trace/src/lambda/facade.js +75 -0
  181. package/packages/dd-trace/src/lambda/handler.js +3 -41
  182. package/packages/dd-trace/src/lambda/index.js +38 -15
  183. package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
  184. package/packages/dd-trace/src/lambda/wrap.js +44 -0
  185. package/packages/dd-trace/src/llmobs/constants/tags.js +2 -0
  186. package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
  187. package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
  188. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +19 -6
  189. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
  190. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
  191. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
  192. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
  193. package/packages/dd-trace/src/llmobs/sdk.js +19 -0
  194. package/packages/dd-trace/src/llmobs/span_processor.js +9 -0
  195. package/packages/dd-trace/src/llmobs/tagger.js +22 -0
  196. package/packages/dd-trace/src/log/channels.js +4 -0
  197. package/packages/dd-trace/src/log/writer.js +6 -1
  198. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +7 -0
  199. package/packages/dd-trace/src/openfeature/configuration_source.js +55 -27
  200. package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
  201. package/packages/dd-trace/src/openfeature/flagging_provider.js +19 -1
  202. package/packages/dd-trace/src/openfeature/writers/base.js +1 -3
  203. package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
  204. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
  205. package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
  206. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
  207. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
  208. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
  209. package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
  210. package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
  211. package/packages/dd-trace/src/opentelemetry/span.js +2 -2
  212. package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
  213. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
  214. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
  215. package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
  216. package/packages/dd-trace/src/opentracing/tracer.js +24 -0
  217. package/packages/dd-trace/src/otel-thread-ctx.js +28 -31
  218. package/packages/dd-trace/src/plugin_manager.js +34 -7
  219. package/packages/dd-trace/src/plugins/ci_plugin.js +143 -17
  220. package/packages/dd-trace/src/plugins/index.js +8 -0
  221. package/packages/dd-trace/src/plugins/plugin.js +4 -2
  222. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  223. package/packages/dd-trace/src/plugins/util/ci.js +52 -1
  224. package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
  225. package/packages/dd-trace/src/plugins/util/git.js +263 -82
  226. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +80 -56
  227. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
  228. package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
  229. package/packages/dd-trace/src/plugins/util/tags.js +6 -0
  230. package/packages/dd-trace/src/plugins/util/test.js +92 -40
  231. package/packages/dd-trace/src/plugins/util/url.js +18 -7
  232. package/packages/dd-trace/src/plugins/util/user-provided-git.js +6 -6
  233. package/packages/dd-trace/src/plugins/util/web.js +65 -10
  234. package/packages/dd-trace/src/process-tags/index.js +1 -1
  235. package/packages/dd-trace/src/profiler.js +74 -12
  236. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  237. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  238. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  239. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  240. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  241. package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
  242. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  243. package/packages/dd-trace/src/proxy.js +46 -54
  244. package/packages/dd-trace/src/span_processor.js +20 -0
  245. package/packages/dd-trace/src/startup-log.js +8 -6
  246. package/packages/dd-trace/src/telemetry/metrics.js +2 -1
  247. package/packages/dd-trace/src/tracer.js +0 -2
  248. package/packages/dd-trace/src/web-tags-cache.js +8 -9
  249. package/register.js +2 -1
  250. package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
  251. package/vendor/dist/@datadog/flagging-core/LICENSE +221 -0
  252. package/vendor/dist/@datadog/flagging-core/index.js +1 -0
  253. package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
  254. package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
  255. package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
  256. package/vendor/dist/@datadog/source-map/LICENSE +1 -0
  257. package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
  258. package/vendor/dist/@opentelemetry/core/LICENSE +220 -1
  259. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  260. package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
  261. package/vendor/dist/escape-string-regexp/LICENSE +13 -0
  262. package/vendor/dist/esquery/LICENSE +2 -0
  263. package/vendor/dist/https-proxy-agent/LICENSE +80 -1
  264. package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
  265. package/vendor/dist/jest-docblock/LICENSE +16 -0
  266. package/vendor/dist/jsonpath-plus/LICENSE +13 -0
  267. package/vendor/dist/limiter/LICENSE +2 -0
  268. package/vendor/dist/lodash.sortby/LICENSE +2 -0
  269. package/vendor/dist/lru-cache/LICENSE +13 -0
  270. package/vendor/dist/meriyah/LICENSE +13 -0
  271. package/vendor/dist/module-details-from-path/LICENSE +2 -0
  272. package/vendor/dist/mutexify/promise/LICENSE +28 -0
  273. package/vendor/dist/pprof-format/LICENSE +13 -0
  274. package/vendor/dist/pprof-format/index.js +1 -1
  275. package/vendor/dist/protobufjs/LICENSE +219 -0
  276. package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
  277. package/vendor/dist/proxy-from-env/LICENSE +13 -0
  278. package/vendor/dist/retry/LICENSE +2 -0
  279. package/vendor/dist/rfdc/LICENSE +2 -0
  280. package/vendor/dist/semifies/LICENSE +3 -1
  281. package/vendor/dist/shell-quote/LICENSE +3 -1
  282. package/vendor/dist/source-map/LICENSE +1 -0
  283. package/vendor/dist/source-map/lib/util/LICENSE +1 -0
  284. package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
  285. package/vendor/dist/ttl-set/LICENSE +28 -0
  286. package/vite.d.ts +7 -0
  287. package/vite.js +3 -0
  288. package/ci/diagnose.js +0 -2215
  289. package/ci/runbook.md +0 -299
  290. package/ci/test-optimization-validation/approval-artifacts.js +0 -175
  291. package/ci/test-optimization-validation/approval.js +0 -386
  292. package/ci/test-optimization-validation/artifact-id.js +0 -18
  293. package/ci/test-optimization-validation/blocker-category.js +0 -24
  294. package/ci/test-optimization-validation/bounded-json.js +0 -120
  295. package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
  296. package/ci/test-optimization-validation/ci-discovery.js +0 -192
  297. package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
  298. package/ci/test-optimization-validation/ci-remediation.js +0 -283
  299. package/ci/test-optimization-validation/cli.js +0 -954
  300. package/ci/test-optimization-validation/command-blocker.js +0 -377
  301. package/ci/test-optimization-validation/command-output-policy.js +0 -188
  302. package/ci/test-optimization-validation/command-runner.js +0 -649
  303. package/ci/test-optimization-validation/environment.js +0 -88
  304. package/ci/test-optimization-validation/executable-approval.js +0 -47
  305. package/ci/test-optimization-validation/executable.js +0 -244
  306. package/ci/test-optimization-validation/execution-lock.js +0 -70
  307. package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
  308. package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
  309. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
  310. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
  311. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
  312. package/ci/test-optimization-validation/generated-file-policy.js +0 -63
  313. package/ci/test-optimization-validation/generated-files.js +0 -425
  314. package/ci/test-optimization-validation/generated-test-contract.js +0 -284
  315. package/ci/test-optimization-validation/generated-verifier.js +0 -235
  316. package/ci/test-optimization-validation/literal-environment.js +0 -56
  317. package/ci/test-optimization-validation/literal-glob.js +0 -52
  318. package/ci/test-optimization-validation/manifest-loader.js +0 -116
  319. package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
  320. package/ci/test-optimization-validation/manifest-schema.js +0 -695
  321. package/ci/test-optimization-validation/offline-fixtures.js +0 -335
  322. package/ci/test-optimization-validation/offline-output.js +0 -406
  323. package/ci/test-optimization-validation/package-check.js +0 -97
  324. package/ci/test-optimization-validation/payload-normalizer.js +0 -72
  325. package/ci/test-optimization-validation/plan-writer.js +0 -541
  326. package/ci/test-optimization-validation/preflight-runner.js +0 -186
  327. package/ci/test-optimization-validation/project-build-artifact.js +0 -31
  328. package/ci/test-optimization-validation/redaction.js +0 -319
  329. package/ci/test-optimization-validation/report-writer.js +0 -666
  330. package/ci/test-optimization-validation/result-semantics.js +0 -103
  331. package/ci/test-optimization-validation/runner-command.js +0 -264
  332. package/ci/test-optimization-validation/runner-contract.js +0 -695
  333. package/ci/test-optimization-validation/safe-files.js +0 -202
  334. package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
  335. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
  336. package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
  337. package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
  338. package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
  339. package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
  340. package/ci/test-optimization-validation/source-text.js +0 -84
  341. package/ci/test-optimization-validation/static-diagnosis.js +0 -166
  342. package/ci/test-optimization-validation/test-output.js +0 -120
  343. package/ci/test-optimization-validation/validation-blocker.js +0 -21
  344. package/ci/validate-test-optimization.js +0 -3
  345. package/packages/datadog-esbuild/src/resolver.js +0 -284
  346. package/packages/datadog-esbuild/src/utils.js +0 -318
  347. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
  348. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
  349. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
  350. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
  351. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
  352. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
@@ -0,0 +1,537 @@
1
+ 'use strict'
2
+
3
+ const fs = require('node:fs')
4
+ const { createRequire } = require('node:module')
5
+ const path = require('node:path')
6
+
7
+ const {
8
+ createEsmResolver,
9
+ createEsmWrapper,
10
+ processModule,
11
+ } = require('../datadog-instrumentations/src/helpers/bundler-esm-exports')
12
+ const {
13
+ classify,
14
+ instrument,
15
+ packagePathPattern,
16
+ } = require('../datadog-instrumentations/src/helpers/bundler-transform')
17
+ const { getGitMetadataBanner } = require('../datadog-instrumentations/src/helpers/bundler-utils')
18
+ const { getValueFromEnvSources } = require('../dd-trace/src/config/helper')
19
+ const log = require('./src/log')
20
+
21
+ const PLUGIN_NAME = 'datadog-vite'
22
+ // Ids starting with `\0dd-trace-vite:` are reserved for the wrappers this plugin generates to intercept the exports
23
+ // of ES modules, followed by the id of the wrapped module.
24
+ const GENERATED_ID_PREFIX = '\0dd-trace-vite:'
25
+ const LOAD_ID_PATTERN = /^\0dd-trace-vite:/
26
+ // Relative, absolute and package `imports` specifiers may resolve to an instrumented file, bare specifiers only when
27
+ // they name an instrumented package.
28
+ const RESOLVE_ID_PATTERN = new RegExp(String.raw`^(?:[./#]|[A-Za-z]:[/\\])|${packagePathPattern.source}`)
29
+ const NODE_MODULES = '/node_modules/'
30
+ const TRACER_DIRECTORY = `${fs.realpathSync(path.join(__dirname, '..', '..')).replaceAll('\\', '/')}/`
31
+ // Optional dependencies of the tracer, required lazily. They must stay out of the bundle: some are native addons and
32
+ // any of them may be missing at runtime.
33
+ const OPTIONAL_DEPENDENCY_PATTERN =
34
+ /^(@datadog\/(?:libdatadog(?:-extras)?|native-appsec|native-iast-taint-tracking|native-metrics|pprof|wasm-js-rewriter)|@opentelemetry\/api(?:-logs)?|oxc-parser)(?:\/|$)/
35
+ // Pure JavaScript optional dependencies the application may import too. When installed, they are bundled, so the
36
+ // tracer and the application share one copy that does not need to be deployed next to the bundle.
37
+ const BUNDLED_WHEN_INSTALLED = ['@opentelemetry/api', '@opentelemetry/api-logs']
38
+ // Rollup leaves `require`, `__filename` and `__dirname` as free variables in ESM output, where Node.js lacks them.
39
+ // Rollup reserves these names in every chunk, so declaring them cannot collide with bundled code, and keeping them
40
+ // chunk-local resolves each chunk against its own location instead of whichever bundle loaded first.
41
+ const ESM_COMMONJS_BINDINGS = 'import { createRequire as $dd_createRequire } from \'node:module\';\n' +
42
+ 'import { fileURLToPath as $dd_fileURLToPath } from \'node:url\';\n' +
43
+ 'import { dirname as $dd_dirname } from \'node:path\';\n' +
44
+ 'const require = $dd_createRequire(import.meta.url);\n' +
45
+ 'const __filename = $dd_fileURLToPath(import.meta.url);\n' +
46
+ 'const __dirname = $dd_dirname(__filename);\n'
47
+ const ESM_FORMATS = new Set(['es', 'esm', 'module'])
48
+ const mergedFunctions = new WeakSet()
49
+
50
+ /**
51
+ * @typedef {(id: string, parentId: string | undefined, isResolved: boolean) => unknown} ExternalFunction
52
+ * @typedef {string | RegExp | ExternalFunction | Array<string | RegExp | ExternalFunction>} External
53
+ * @typedef {string | ((chunk: object) => string | Promise<string>)} Addon
54
+ * @typedef {{ format?: string, intro?: Addon, keepNames?: boolean }} OutputOptions
55
+ * @typedef {{ external?: External, output?: OutputOptions | OutputOptions[] }} BundlerOptions
56
+ * @typedef {{
57
+ * commonjsOptions?: { ignore?: string[] | ((id: string) => boolean) },
58
+ * lib?: unknown,
59
+ * minify?: boolean | string,
60
+ * rolldownOptions?: BundlerOptions,
61
+ * rollupOptions?: BundlerOptions,
62
+ * ssr?: boolean | string,
63
+ * terserOptions?: { keep_classnames?: unknown, keep_fnames?: unknown },
64
+ * }} BuildOptions
65
+ * @typedef {{
66
+ * build: BuildOptions,
67
+ * builder?: { sharedConfigBuild?: boolean },
68
+ * environments: Record<string, { build: BuildOptions, consumer: string } | undefined>,
69
+ * esbuild: false | { keepNames?: boolean },
70
+ * }} ResolvedConfig
71
+ * @typedef {{ meta?: { rolldownVersion?: string } } | undefined} HookContext
72
+ * @typedef {{ warn: (message: string) => void }} TransformContext
73
+ * @typedef {{ name: string, config: { build: BuildOptions, consumer: string } }} Environment
74
+ * @typedef {{ external?: boolean | string, id: string }} ResolvedId
75
+ * @typedef {{ isEntry?: boolean, skipSelf?: boolean }} ResolveOptions
76
+ * @typedef {{
77
+ * environment?: object,
78
+ * resolve: (source: string, importer: string | undefined, options: ResolveOptions) => Promise<ResolvedId | null>,
79
+ * warn: (message: string) => void,
80
+ * }} ResolveContext
81
+ */
82
+
83
+ /**
84
+ * Creates the Datadog plugin for Vite builds producing Node.js code.
85
+ */
86
+ function datadogVite () {
87
+ const injectGitMetadata = getValueFromEnvSources('DD_BUNDLER_INJECT_GIT_METADATA') !== false
88
+ const installed = getInstalledDependencies()
89
+ /** @type {WeakMap<object, EsmInterception>} */
90
+ const interceptions = new WeakMap()
91
+ const defaultEnvironment = {}
92
+ /** @type {string|undefined} */
93
+ let gitIntro
94
+
95
+ if (!injectGitMetadata) log.debug('Skipping git metadata injection (DD_BUNDLER_INJECT_GIT_METADATA=false)')
96
+
97
+ /**
98
+ * @param {{ environment?: object }} context
99
+ */
100
+ function getInterception (context) {
101
+ const environment = context.environment ?? defaultEnvironment
102
+ let interception = interceptions.get(environment)
103
+ if (!interception) {
104
+ interception = new EsmInterception()
105
+ interceptions.set(environment, interception)
106
+ }
107
+ return interception
108
+ }
109
+
110
+ return {
111
+ name: PLUGIN_NAME,
112
+ apply: 'build',
113
+ enforce: 'pre',
114
+
115
+ /**
116
+ * Vite has normalized each environment's defaults, but has not created its CommonJS plugins yet.
117
+ * The shared esbuild options are read later, when chunks are rendered.
118
+ *
119
+ * @this {HookContext}
120
+ * @param {ResolvedConfig} config
121
+ */
122
+ configResolved (config) {
123
+ // Older versions lack the Environment API hooks, so the build would silently miss required configuration.
124
+ if (!config.environments) throw new Error('dd-trace/vite requires Vite 6 or newer. Refusing to bundle.')
125
+ const rolldown = isRolldown(this)
126
+ for (const [name, environment] of Object.entries(config.environments)) {
127
+ if (environment && isNodeBuild(environment.consumer, environment.build)) {
128
+ configureBuild(name, environment.build, rolldown, installed)
129
+ }
130
+ }
131
+ if (rolldown || !config.esbuild || !needsEsbuildKeepNames(config)) return
132
+ config.esbuild.keepNames = true
133
+ log.debug('Enabled esbuild keepNames so minification preserves function and class names')
134
+ },
135
+
136
+ /**
137
+ * Rolldown defines its own `require` in ESM output. Every chunk gets the bindings, since free variables in one
138
+ * chunk cannot see declarations in another.
139
+ *
140
+ * @this {HookContext}
141
+ * @param {OutputOptions} output
142
+ */
143
+ outputOptions (output) {
144
+ if (isRolldown(this) || !ESM_FORMATS.has(output.format ?? 'es')) return
145
+ return { ...output, intro: prependAddon(ESM_COMMONJS_BINDINGS, output.intro) }
146
+ },
147
+
148
+ /**
149
+ * @param {Environment} environment
150
+ */
151
+ applyToEnvironment (environment) {
152
+ const { build, consumer } = environment.config
153
+ return isNodeBuild(consumer, build)
154
+ },
155
+
156
+ resolveId: {
157
+ filter: { id: RESOLVE_ID_PATTERN },
158
+ /**
159
+ * @this {ResolveContext}
160
+ * @param {string} source
161
+ * @param {string} [importer]
162
+ * @param {ResolveOptions} [options]
163
+ */
164
+ handler (source, importer, options) {
165
+ return getInterception(this).resolveId(this, source, importer, options)
166
+ },
167
+ },
168
+
169
+ load: {
170
+ filter: { id: LOAD_ID_PATTERN },
171
+ /**
172
+ * @this {ResolveContext}
173
+ * @param {string} id
174
+ */
175
+ handler (id) {
176
+ return isWrapperId(id) ? getInterception(this).load(this, id.slice(GENERATED_ID_PREFIX.length)) : undefined
177
+ },
178
+ },
179
+
180
+ /**
181
+ * @this {{ environment?: object }}
182
+ */
183
+ buildEnd () {
184
+ return interceptions.get(this.environment ?? defaultEnvironment)?.buildEnd()
185
+ },
186
+
187
+ transform: {
188
+ // Vite versions without hook filters call the handler for every module, which checks the id again.
189
+ filter: { id: packagePathPattern },
190
+ /**
191
+ * @this {TransformContext}
192
+ * @param {string} code
193
+ * @param {string} id
194
+ */
195
+ handler (code, id) {
196
+ const result = isInstrumentable(id)
197
+ ? instrument({ source: code, resourcePath: id, warn: warning => this.warn(warning.message) })
198
+ : undefined
199
+ return result && result.code !== code ? { code: result.code, map: result.map ?? null } : undefined
200
+ },
201
+ },
202
+
203
+ /**
204
+ * Runs before the chunk's own code: in CommonJS output after the strict-mode directive and before the hoisted
205
+ * `require` calls, unlike a banner, which would precede the directive.
206
+ *
207
+ * @param {{ isEntry: boolean }} chunk
208
+ */
209
+ intro (chunk) {
210
+ if (!injectGitMetadata || !chunk.isEntry) return ''
211
+ gitIntro ??= getGitMetadataBanner(log)
212
+ return gitIntro
213
+ },
214
+ }
215
+ }
216
+
217
+ /**
218
+ * @param {string} id
219
+ */
220
+ function isInstrumentable (id) {
221
+ return id.charCodeAt(0) !== 0 && !id.includes('?') && id.includes(NODE_MODULES) &&
222
+ !id.startsWith(TRACER_DIRECTORY) && packagePathPattern.test(id)
223
+ }
224
+
225
+ /**
226
+ * @param {string} id
227
+ */
228
+ function isWrapperId (id) {
229
+ return id.startsWith(GENERATED_ID_PREFIX)
230
+ }
231
+
232
+ /**
233
+ * ES module namespaces are read-only, so hooks cannot replace the exports of ES modules. Importers of ES modules that
234
+ * hooks instrument are redirected to a wrapper module re-exporting them through bindings the hooks can replace.
235
+ * Orchestrion targets are rewritten in place instead. State is kept per environment.
236
+ */
237
+ class EsmInterception {
238
+ /**
239
+ * Kept across watch mode rebuilds, which load cached modules without resolving their imports again.
240
+ *
241
+ * @type {Map<string, string>}
242
+ */
243
+ #specifiers = new Map()
244
+ /** @type {Map<string, Promise<string[] | undefined>>} */
245
+ #exports = new Map()
246
+ /** @type {Map<string, string>} */
247
+ #moduleSources = new Map()
248
+ /** @type {ReturnType<typeof createEsmResolver> | undefined} */
249
+ #resolver
250
+
251
+ /**
252
+ * @param {ResolveContext} context
253
+ * @param {string} source
254
+ * @param {string | undefined} importer
255
+ * @param {ResolveOptions | undefined} options
256
+ */
257
+ resolveId (context, source, importer, options) {
258
+ if (importer === undefined || options?.isEntry) return
259
+ if (isWrapperId(importer)) {
260
+ // Imports from generated modules resolve relative to the original. The wrapper must reach that module unwrapped.
261
+ return context.resolve(source, importer.slice(GENERATED_ID_PREFIX.length), { ...options, skipSelf: true })
262
+ }
263
+ if (!mayResolveToTarget(source, importer)) return
264
+ return context.resolve(source, importer, { ...options, skipSelf: true })
265
+ .then(resolved => this.#redirect(context, source, importer, resolved))
266
+ }
267
+
268
+ /**
269
+ * @param {ResolveContext} context
270
+ * @param {string} id The id of the wrapped module.
271
+ */
272
+ load (context, id) {
273
+ return this.#getExports(context, id).then(setters => {
274
+ if (!setters) throw new Error(`Could not analyze the exports of ${id}`)
275
+ return createEsmWrapper({
276
+ path: id,
277
+ specifier: this.#specifiers.get(id),
278
+ setters,
279
+ })
280
+ })
281
+ }
282
+
283
+ buildEnd () {
284
+ const resolver = this.#resolver
285
+ this.#exports.clear()
286
+ this.#moduleSources.clear()
287
+ this.#resolver = undefined
288
+ return resolver?.close().catch(error => {
289
+ log.debug('The ESM resolver failed: %s', error instanceof Error ? error.message : String(error))
290
+ })
291
+ }
292
+
293
+ /**
294
+ * @param {ResolveContext} context
295
+ * @param {string} source
296
+ * @param {string} importer
297
+ * @param {ResolvedId | null} resolved
298
+ */
299
+ #redirect (context, source, importer, resolved) {
300
+ // A module importing itself must not reach its wrapper, which would import it in turn.
301
+ if (!resolved || resolved.external || resolved.id === importer || !isInstrumentable(resolved.id)) return resolved
302
+ const { id } = resolved
303
+ const kind = classify({
304
+ resourcePath: id,
305
+ // Package root hooks only apply to imports of the package name.
306
+ request: isBareSpecifier(source) ? source : undefined,
307
+ warn: warning => context.warn(warning.message),
308
+ })
309
+ if (kind !== 'esm-intercept') return resolved
310
+ this.#specifiers.set(id, source)
311
+ return this.#getExports(context, id).then(setters => {
312
+ if (!setters) return resolved
313
+ // IITM registration must run even when importers only re-export the module.
314
+ return { id: GENERATED_ID_PREFIX + id, moduleSideEffects: true }
315
+ })
316
+ }
317
+
318
+ /**
319
+ * @param {ResolveContext} context
320
+ * @param {string} id
321
+ * @returns {Promise<string[] | undefined>}
322
+ */
323
+ #getExports (context, id) {
324
+ const cached = this.#exports.get(id)
325
+ if (cached) return cached
326
+ this.#resolver ??= createEsmResolver()
327
+ const exports = /** @type {Promise<string[] | undefined>} */ (processModule({
328
+ path: id,
329
+ context: { format: 'module' },
330
+ moduleSources: this.#moduleSources,
331
+ resolver: this.#resolver,
332
+ }).then(setters => [...setters.values()], error => {
333
+ context.warn(`Skipped intercepting the exports of ${id}: ${error instanceof Error ? error.message : error}`)
334
+ }))
335
+ this.#exports.set(id, exports)
336
+ return exports
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Relative specifiers can only resolve to an instrumented file when imported from an instrumented package.
342
+ *
343
+ * @param {string} source
344
+ * @param {string} importer
345
+ */
346
+ function mayResolveToTarget (source, importer) {
347
+ if (source.charCodeAt(0) === 0 || source.includes('?')) return false
348
+ return source[0] === '.' || source[0] === '#' ? isInstrumentable(importer) : packagePathPattern.test(source)
349
+ }
350
+
351
+ /**
352
+ * @param {string} source
353
+ */
354
+ function isBareSpecifier (source) {
355
+ return source[0] !== '.' && source[0] !== '/' && source[0] !== '#' && !/^[A-Za-z]:[/\\]/.test(source)
356
+ }
357
+
358
+ /**
359
+ * @param {HookContext} context
360
+ */
361
+ function isRolldown (context) {
362
+ return Boolean(context?.meta?.rolldownVersion)
363
+ }
364
+
365
+ /**
366
+ * Electron preload, renderer and other client builds are never instrumented.
367
+ *
368
+ * @param {string | undefined} consumer
369
+ * @param {BuildOptions} [build]
370
+ */
371
+ function isNodeBuild (consumer, build) {
372
+ return consumer === 'server' || Boolean(build?.ssr) || Boolean(build?.lib)
373
+ }
374
+
375
+ /**
376
+ * Whether a built environment producing Node.js code minifies with esbuild. A builder sharing the configuration
377
+ * shares the esbuild options too, so client environments built alongside such an environment keep names as well.
378
+ *
379
+ * @param {ResolvedConfig} config
380
+ */
381
+ function needsEsbuildKeepNames (config) {
382
+ if (config.esbuild && config.esbuild.keepNames !== undefined) return false
383
+ // Each environment resolves its own configuration, whose build options are the environment's.
384
+ if (!config.builder?.sharedConfigBuild) return isMinifiedByEsbuild(undefined, config.build)
385
+ return Object.values(config.environments).some(environment =>
386
+ environment && isMinifiedByEsbuild(environment.consumer, environment.build))
387
+ }
388
+
389
+ /**
390
+ * @param {string | undefined} consumer
391
+ * @param {BuildOptions} build
392
+ */
393
+ function isMinifiedByEsbuild (consumer, build) {
394
+ return isNodeBuild(consumer, build) && build.minify === 'esbuild'
395
+ }
396
+
397
+ /**
398
+ * Resolves the optional dependencies that are bundled when installed the way the bundled tracer would.
399
+ *
400
+ * @returns {Set<string>}
401
+ */
402
+ function getInstalledDependencies () {
403
+ const tracerRequire = createRequire(`${TRACER_DIRECTORY}index.js`)
404
+ const installed = new Set()
405
+ for (const name of BUNDLED_WHEN_INSTALLED) {
406
+ try {
407
+ tracerRequire.resolve(name)
408
+ installed.add(name)
409
+ } catch {
410
+ log.debug('Keeping %s external to the bundle: it is not installed', name)
411
+ }
412
+ }
413
+ return installed
414
+ }
415
+
416
+ /**
417
+ * @param {string} id
418
+ * @param {Set<string>} installed
419
+ */
420
+ function isOptionalDependency (id, installed) {
421
+ const match = OPTIONAL_DEPENDENCY_PATTERN.exec(id)
422
+ return match !== null && !installed.has(match[1])
423
+ }
424
+
425
+ /**
426
+ * @param {string | undefined} id
427
+ */
428
+ function isTracerModule (id) {
429
+ if (id === undefined) return false
430
+ const normalized = id.replaceAll('\\', '/')
431
+ return normalized.startsWith(TRACER_DIRECTORY) || normalized.includes('/node_modules/dd-trace/')
432
+ }
433
+
434
+ /**
435
+ * @param {Addon} code
436
+ * @param {Addon | undefined} addon
437
+ * @returns {Addon}
438
+ */
439
+ function prependAddon (code, addon) {
440
+ if (typeof addon === 'function') return async chunk => `${code}${await addon(chunk)}`
441
+ return addon ? `${code}${addon}` : code
442
+ }
443
+
444
+ /**
445
+ * @param {string} name
446
+ * @param {BuildOptions} build
447
+ * @param {boolean} rolldown
448
+ * @param {Set<string>} installed
449
+ */
450
+ function configureBuild (name, build, rolldown, installed) {
451
+ const { minify } = build
452
+ if (rolldown) {
453
+ // Vite 8 aliases `rollupOptions` to `rolldownOptions`; reuse whichever object the configuration already has.
454
+ const options = build.rolldownOptions ?? build.rollupOptions ?? (build.rolldownOptions = {})
455
+ options.external = mergeExternal(options.external, installed)
456
+ if (minify) enableOutputKeepNames(name, options)
457
+ } else {
458
+ // Rollup hoists external `require` calls to the top of the chunk, which would load optional dependencies eagerly
459
+ // and crash when they are missing. Ignored `require` calls stay where they are. The option only receives the
460
+ // required id, so it also applies to other bundled CommonJS modules; those dependencies are native addons or
461
+ // missing, which these modules could not bundle either.
462
+ const commonjsOptions = build.commonjsOptions ??= {}
463
+ commonjsOptions.ignore = mergeIgnore(commonjsOptions.ignore, installed)
464
+ }
465
+ if (minify === 'terser') {
466
+ const terserOptions = build.terserOptions ??= {}
467
+ terserOptions.keep_classnames ??= true
468
+ terserOptions.keep_fnames ??= true
469
+ log.debug('Enabled terser keep_classnames and keep_fnames for the %s environment', name)
470
+ }
471
+ }
472
+
473
+ /**
474
+ * @param {string} name
475
+ * @param {BundlerOptions} options
476
+ */
477
+ function enableOutputKeepNames (name, options) {
478
+ options.output ??= {}
479
+ for (const output of Array.isArray(options.output) ? options.output : [options.output]) {
480
+ if (output.keepNames !== undefined) continue
481
+ output.keepNames = true
482
+ log.debug('Enabled output keepNames for the %s environment', name)
483
+ }
484
+ }
485
+
486
+ /**
487
+ * Only imports from the tracer are externalized, so the application still bundles its own imports of these packages.
488
+ *
489
+ * @param {External | null | undefined} external
490
+ * @param {Set<string>} installed
491
+ * @returns {ExternalFunction}
492
+ */
493
+ function mergeExternal (external, installed) {
494
+ if (typeof external === 'function' && mergedFunctions.has(external)) return external
495
+ const user = external === undefined || external === null ? undefined : matchAny(external)
496
+ /** @type {ExternalFunction} */
497
+ const merged = (id, parentId, isResolved) =>
498
+ (isOptionalDependency(id, installed) && isTracerModule(parentId)) || Boolean(user?.(id, parentId, isResolved))
499
+ mergedFunctions.add(merged)
500
+ return merged
501
+ }
502
+
503
+ /**
504
+ * Vite concatenates arrays when merging configurations, so a function returned by one plugin or the user can end up
505
+ * in an array, which the bundler rejects.
506
+ *
507
+ * @param {External} external
508
+ * @returns {ExternalFunction}
509
+ */
510
+ function matchAny (external) {
511
+ if (typeof external === 'function') return external
512
+ const externals = Array.isArray(external) ? external : [external]
513
+ return (id, parentId, isResolved) => externals.some(entry => {
514
+ if (typeof entry === 'function') return entry(id, parentId, isResolved)
515
+ return typeof entry === 'string' ? entry === id : entry.test(id)
516
+ })
517
+ }
518
+
519
+ /**
520
+ * @param {string[] | ((id: string) => boolean) | undefined} ignore
521
+ * @param {Set<string>} installed
522
+ * @returns {(id: string) => boolean}
523
+ */
524
+ function mergeIgnore (ignore, installed) {
525
+ if (typeof ignore === 'function' && mergedFunctions.has(ignore)) return ignore
526
+ let merged
527
+ if (typeof ignore === 'function') {
528
+ merged = (/** @type {string} */ id) => isOptionalDependency(id, installed) || ignore(id)
529
+ } else {
530
+ const ids = new Set(ignore)
531
+ merged = (/** @type {string} */ id) => isOptionalDependency(id, installed) || ids.has(id)
532
+ }
533
+ mergedFunctions.add(merged)
534
+ return merged
535
+ }
536
+
537
+ module.exports = { datadogVite }
@@ -0,0 +1,5 @@
1
+ 'use strict'
2
+
3
+ const createBundlerLog = require('../../datadog-instrumentations/src/helpers/bundler-log')
4
+
5
+ module.exports = createBundlerLog('vite')
@@ -1,13 +1,12 @@
1
1
  'use strict'
2
2
 
3
- const { execSync } = require('node:child_process')
4
3
  const fs = require('node:fs')
5
4
 
6
5
  const instrumentations = require('../datadog-instrumentations/src/helpers/instrumentations')
6
+ const { getGitMetadataBanner, isESMFile } = require('../datadog-instrumentations/src/helpers/bundler-utils')
7
7
  const extractPackageAndModulePath = require('../datadog-instrumentations/src/helpers/extract-package-and-module-path')
8
8
  const hooks = require('../datadog-instrumentations/src/helpers/hooks')
9
9
  const { getRewriteTarget } = require('../datadog-instrumentations/src/helpers/rewriter/targets')
10
- const { isESMFile } = require('../datadog-esbuild/src/utils')
11
10
  const { getValueFromEnvSources } = require('../dd-trace/src/config/helper')
12
11
  const log = require('./src/log')
13
12
 
@@ -35,38 +34,6 @@ for (const [name, instrumentation] of Object.entries(instrumentations)) {
35
34
  }
36
35
  }
37
36
 
38
- /**
39
- * @returns {{ repositoryURL: string | null, commitSHA: string | null }}
40
- */
41
- function getGitMetadata () {
42
- const gitMetadata = {
43
- repositoryURL: null,
44
- commitSHA: null,
45
- }
46
-
47
- try {
48
- gitMetadata.repositoryURL = execSync('git config --get remote.origin.url', {
49
- encoding: 'utf8',
50
- stdio: ['pipe', 'pipe', 'ignore'],
51
- cwd: process.cwd(),
52
- }).trim()
53
- } catch (e) {
54
- log.warn('failed to get git repository URL:', e.message)
55
- }
56
-
57
- try {
58
- gitMetadata.commitSHA = execSync('git rev-parse HEAD', {
59
- encoding: 'utf8',
60
- stdio: ['pipe', 'pipe', 'ignore'],
61
- cwd: process.cwd(),
62
- }).trim()
63
- } catch (e) {
64
- log.warn('failed to get git commit SHA:', e.message)
65
- }
66
-
67
- return gitMetadata
68
- }
69
-
70
37
  class DatadogWebpackPlugin {
71
38
  /**
72
39
  * @param {object} compiler
@@ -86,19 +53,8 @@ class DatadogWebpackPlugin {
86
53
 
87
54
  // Bake DD_GIT_* into assets unless DD_BUNDLER_INJECT_GIT_METADATA=false (stable artifact hashes).
88
55
  if (DD_BUNDLER_INJECT_GIT_METADATA) {
89
- const gitMetadata = getGitMetadata()
90
- if (gitMetadata.repositoryURL || gitMetadata.commitSHA) {
91
- const banner =
92
- 'if (typeof process === \'object\' && process !== null &&\n' +
93
- ' process.env !== null && typeof process.env === \'object\') {\n' +
94
- (gitMetadata.repositoryURL
95
- ? ` process.env.DD_GIT_REPOSITORY_URL = ${JSON.stringify(gitMetadata.repositoryURL)};\n`
96
- : '') +
97
- (gitMetadata.commitSHA
98
- ? ` process.env.DD_GIT_COMMIT_SHA = ${JSON.stringify(gitMetadata.commitSHA)};\n`
99
- : '') +
100
- '}\n'
101
-
56
+ const banner = getGitMetadataBanner(log)
57
+ if (banner) {
102
58
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
103
59
  compilation.hooks.processAssets.tap(
104
60
  { name: PLUGIN_NAME, stage: -2000 },
@@ -122,14 +78,6 @@ class DatadogWebpackPlugin {
122
78
  }
123
79
  )
124
80
  })
125
-
126
- log.debug(
127
- 'Automatically injected git metadata (DD_GIT_REPOSITORY_URL: %s, DD_GIT_COMMIT_SHA: %s)',
128
- gitMetadata.repositoryURL || 'not available',
129
- gitMetadata.commitSHA || 'not available'
130
- )
131
- } else {
132
- log.warn('No git metadata available - skipping injection')
133
81
  }
134
82
  } else {
135
83
  log.debug('Skipping git metadata injection (DD_BUNDLER_INJECT_GIT_METADATA=false)')
@@ -1,32 +1,5 @@
1
1
  'use strict'
2
2
 
3
- const { format } = require('util')
3
+ const createBundlerLog = require('../../datadog-instrumentations/src/helpers/bundler-log')
4
4
 
5
- // eslint-disable-next-line eslint-rules/eslint-process-env
6
- const DD_TRACE_DEBUG = (process.env.DD_TRACE_DEBUG || '').trim().toLowerCase()
7
- const DEBUG = DD_TRACE_DEBUG === 'true' || DD_TRACE_DEBUG === '1'
8
-
9
- const noop = () => {}
10
-
11
- const formatWithLogPrefix = (prefix, str, ...args) => {
12
- if (typeof str === 'string') {
13
- return format(`${prefix} ${str}`, ...args)
14
- }
15
- return format(prefix, str, ...args)
16
- }
17
-
18
- module.exports = DEBUG
19
- ? {
20
- debug (...args) {
21
- // eslint-disable-next-line no-console
22
- console.log(formatWithLogPrefix('[dd-trace/webpack]', ...args))
23
- },
24
- warn (...args) {
25
- // eslint-disable-next-line no-console
26
- console.warn(formatWithLogPrefix('[dd-trace/webpack] Warning:', ...args))
27
- },
28
- }
29
- : {
30
- debug: noop,
31
- warn: noop,
32
- }
5
+ module.exports = createBundlerLog('webpack')