dd-trace 6.15.0 → 6.17.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 (378) hide show
  1. package/README.md +15 -0
  2. package/ci/diagnose.js +0 -23
  3. package/ci/test-optimization-validation/approval-artifacts.js +0 -3
  4. package/ci/test-optimization-validation/approval.js +0 -6
  5. package/ci/test-optimization-validation/artifact-id.js +0 -1
  6. package/ci/test-optimization-validation/bounded-json.js +0 -1
  7. package/ci/test-optimization-validation/cli.js +0 -12
  8. package/ci/test-optimization-validation/command-output-policy.js +0 -1
  9. package/ci/test-optimization-validation/command-runner.js +0 -3
  10. package/ci/test-optimization-validation/environment.js +0 -4
  11. package/ci/test-optimization-validation/executable-approval.js +0 -1
  12. package/ci/test-optimization-validation/executable.js +0 -5
  13. package/ci/test-optimization-validation/framework-adapters/cucumber.js +4 -8
  14. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -3
  15. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -6
  16. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -4
  17. package/ci/test-optimization-validation/generated-test-contract.js +0 -5
  18. package/ci/test-optimization-validation/generated-verifier.js +0 -1
  19. package/ci/test-optimization-validation/literal-environment.js +0 -1
  20. package/ci/test-optimization-validation/manifest-scaffold.js +0 -17
  21. package/ci/test-optimization-validation/manifest-schema.js +0 -21
  22. package/ci/test-optimization-validation/offline-fixtures.js +0 -4
  23. package/ci/test-optimization-validation/package-check.js +4 -1
  24. package/ci/test-optimization-validation/plan-writer.js +0 -11
  25. package/ci/test-optimization-validation/preflight-runner.js +0 -1
  26. package/ci/test-optimization-validation/redaction.js +0 -4
  27. package/ci/test-optimization-validation/report-writer.js +0 -16
  28. package/ci/test-optimization-validation/runner-command.js +0 -3
  29. package/ci/test-optimization-validation/runner-contract.js +0 -4
  30. package/ci/test-optimization-validation/safe-files.js +0 -1
  31. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -1
  32. package/ci/test-optimization-validation/scenarios/ci-wiring.js +1 -2
  33. package/ci/test-optimization-validation/source-text.js +0 -3
  34. package/ci/vitest-no-worker-init-setup.mjs +41 -16
  35. package/index.d.ts +169 -0
  36. package/next.d.ts +27 -0
  37. package/next.js +3 -0
  38. package/package.json +28 -23
  39. package/packages/datadog-esbuild/index.js +14 -1
  40. package/packages/datadog-esbuild/src/resolver.js +284 -0
  41. package/packages/datadog-esbuild/src/utils.js +162 -120
  42. package/packages/datadog-instrumentations/src/aerospike.js +28 -6
  43. package/packages/datadog-instrumentations/src/ai.js +39 -97
  44. package/packages/datadog-instrumentations/src/anthropic.js +76 -134
  45. package/packages/datadog-instrumentations/src/aws-durable-execution-sdk-js.js +1 -3
  46. package/packages/datadog-instrumentations/src/aws-sdk.js +0 -2
  47. package/packages/datadog-instrumentations/src/azure-cosmos.js +1 -1
  48. package/packages/datadog-instrumentations/src/azure-durable-functions.js +23 -9
  49. package/packages/datadog-instrumentations/src/azure-functions.js +48 -0
  50. package/packages/datadog-instrumentations/src/bullmq.js +1 -1
  51. package/packages/datadog-instrumentations/src/claude-agent-sdk.js +1 -3
  52. package/packages/datadog-instrumentations/src/console.js +97 -0
  53. package/packages/datadog-instrumentations/src/cucumber.js +72 -12
  54. package/packages/datadog-instrumentations/src/cypress-config.js +0 -23
  55. package/packages/datadog-instrumentations/src/dd-trace-api.js +2 -2
  56. package/packages/datadog-instrumentations/src/graphql.js +1 -1
  57. package/packages/datadog-instrumentations/src/helpers/bundler-register.js +39 -14
  58. package/packages/datadog-instrumentations/src/helpers/channel.js +0 -2
  59. package/packages/datadog-instrumentations/src/helpers/graphql-jit-runtime.js +0 -5
  60. package/packages/datadog-instrumentations/src/helpers/hooks.js +8 -0
  61. package/packages/datadog-instrumentations/src/helpers/instrument.js +25 -8
  62. package/packages/datadog-instrumentations/src/helpers/instrumentation-utils.js +69 -0
  63. package/packages/datadog-instrumentations/src/helpers/pool-acquire.js +0 -6
  64. package/packages/datadog-instrumentations/src/helpers/register.js +13 -64
  65. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +109 -29
  66. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +39 -0
  67. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/azure-durable-functions.js +17 -0
  68. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/index.js +3 -16
  69. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/playwright.js +12 -0
  70. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +36 -0
  71. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/supabase.js +128 -0
  72. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/webdriverio.js +6 -7
  73. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +17 -0
  74. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +388 -0
  75. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +99 -75
  76. package/packages/datadog-instrumentations/src/helpers/router-helper.js +0 -1
  77. package/packages/datadog-instrumentations/src/jest.js +141 -51
  78. package/packages/datadog-instrumentations/src/knex.js +0 -1
  79. package/packages/datadog-instrumentations/src/langchain.js +1 -1
  80. package/packages/datadog-instrumentations/src/langgraph.js +1 -1
  81. package/packages/datadog-instrumentations/src/mariadb-bundle.js +0 -24
  82. package/packages/datadog-instrumentations/src/mariadb.js +0 -2
  83. package/packages/datadog-instrumentations/src/mercurius.js +1 -1
  84. package/packages/datadog-instrumentations/src/mocha/common.js +0 -1
  85. package/packages/datadog-instrumentations/src/mocha/main.js +22 -25
  86. package/packages/datadog-instrumentations/src/mocha/utils.js +119 -49
  87. package/packages/datadog-instrumentations/src/mocha/webdriverio-protocol.js +0 -6
  88. package/packages/datadog-instrumentations/src/mocha/worker.js +14 -17
  89. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +1 -1
  90. package/packages/datadog-instrumentations/src/mysql2.js +0 -2
  91. package/packages/datadog-instrumentations/src/openai-agents.js +1 -1
  92. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +241 -0
  93. package/packages/datadog-instrumentations/src/openai-realtime/events.js +76 -0
  94. package/packages/datadog-instrumentations/src/openai-realtime/index.js +301 -0
  95. package/packages/datadog-instrumentations/src/openai-realtime/session.js +998 -0
  96. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +211 -0
  97. package/packages/datadog-instrumentations/src/openai.js +97 -80
  98. package/packages/datadog-instrumentations/src/oracledb.js +9 -3
  99. package/packages/datadog-instrumentations/src/playwright-reporter.js +3 -16
  100. package/packages/datadog-instrumentations/src/playwright.js +238 -55
  101. package/packages/datadog-instrumentations/src/postgres.js +7 -0
  102. package/packages/datadog-instrumentations/src/router.js +0 -2
  103. package/packages/datadog-instrumentations/src/rum-browser-scripts.js +0 -2
  104. package/packages/datadog-instrumentations/src/sequelize.js +0 -1
  105. package/packages/datadog-instrumentations/src/supabase.js +15 -0
  106. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +18 -18
  107. package/packages/datadog-instrumentations/src/vitest-main.js +37 -17
  108. package/packages/datadog-instrumentations/src/vitest-util.js +7 -1
  109. package/packages/datadog-instrumentations/src/vitest-worker.js +56 -23
  110. package/packages/datadog-instrumentations/src/webdriverio.js +23 -42
  111. package/packages/datadog-instrumentations/src/ws.js +0 -1
  112. package/packages/datadog-plugin-ai/src/utils.js +0 -2
  113. package/packages/datadog-plugin-aws-durable-execution-sdk-js/src/trace-checkpoint.js +0 -1
  114. package/packages/datadog-plugin-aws-durable-execution-sdk-js/src/util.js +0 -2
  115. package/packages/datadog-plugin-aws-sdk/src/base.js +0 -1
  116. package/packages/datadog-plugin-aws-sdk/src/services/bedrockruntime/utils.js +7 -2
  117. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +0 -1
  118. package/packages/datadog-plugin-aws-sdk/src/util.js +0 -1
  119. package/packages/datadog-plugin-azure-cosmos/src/index.js +1 -1
  120. package/packages/datadog-plugin-azure-durable-functions/src/index.js +95 -1
  121. package/packages/datadog-plugin-cucumber/src/index.js +10 -0
  122. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +171 -75
  123. package/packages/datadog-plugin-cypress/src/finalization.js +0 -3
  124. package/packages/datadog-plugin-cypress/src/source-map-utils.js +0 -4
  125. package/packages/datadog-plugin-cypress/src/support.js +52 -7
  126. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +2 -24
  127. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +2 -36
  128. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +31 -0
  129. package/packages/datadog-plugin-graphql/src/resolve-error.js +32 -11
  130. package/packages/datadog-plugin-graphql/src/utils.js +0 -5
  131. package/packages/datadog-plugin-jest/src/index.js +13 -0
  132. package/packages/datadog-plugin-kafkajs/src/producer.js +0 -1
  133. package/packages/datadog-plugin-mocha/src/index.js +39 -11
  134. package/packages/datadog-plugin-mongodb-core/src/query.js +0 -1
  135. package/packages/datadog-plugin-openai/src/index.js +16 -1
  136. package/packages/datadog-plugin-openai/src/realtime.js +144 -0
  137. package/packages/datadog-plugin-openai-agents/src/integration.js +0 -4
  138. package/packages/datadog-plugin-openai-agents/src/util.js +0 -1
  139. package/packages/datadog-plugin-playwright/src/index.js +76 -9
  140. package/packages/datadog-plugin-postgres/src/index.js +167 -0
  141. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +0 -1
  142. package/packages/datadog-plugin-redis/src/index.js +15 -1
  143. package/packages/datadog-plugin-supabase/src/error.js +18 -0
  144. package/packages/datadog-plugin-supabase/src/index.js +21 -0
  145. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +69 -0
  146. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +70 -0
  147. package/packages/datadog-plugin-supabase/src/supabase-postgrest-js-postgrestbuilder-then.js +122 -0
  148. package/packages/datadog-plugin-supabase/src/supabase-realtime-js-realtimechannel-send.js +66 -0
  149. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +93 -0
  150. package/packages/datadog-plugin-supabase/src/url.js +17 -0
  151. package/packages/datadog-plugin-vitest/src/index.js +10 -0
  152. package/packages/datadog-plugin-ws/src/util.js +0 -4
  153. package/packages/datadog-turbopack/index.js +206 -0
  154. package/packages/datadog-turbopack/src/constants.js +5 -0
  155. package/packages/datadog-turbopack/src/loader.js +484 -0
  156. package/packages/datadog-webpack/src/loader.js +0 -1
  157. package/packages/dd-trace/src/aiguard/client.js +2 -3
  158. package/packages/dd-trace/src/aiguard/evaluation.js +6 -12
  159. package/packages/dd-trace/src/aiguard/index.js +3 -2
  160. package/packages/dd-trace/src/aiguard/integrations/anthropic.js +53 -25
  161. package/packages/dd-trace/src/aiguard/integrations/evaluate.js +49 -25
  162. package/packages/dd-trace/src/aiguard/integrations/index.js +4 -3
  163. package/packages/dd-trace/src/aiguard/integrations/openai.js +132 -29
  164. package/packages/dd-trace/src/aiguard/integrations/vercel-ai.js +116 -42
  165. package/packages/dd-trace/src/aiguard/messages/anthropic.js +0 -2
  166. package/packages/dd-trace/src/aiguard/messages/openai.js +64 -1
  167. package/packages/dd-trace/src/aiguard/messages/utils.js +29 -2
  168. package/packages/dd-trace/src/aiguard/messages/vercel-ai.js +25 -0
  169. package/packages/dd-trace/src/aiguard/redaction.js +0 -1
  170. package/packages/dd-trace/src/aiguard/sdk.js +1 -1
  171. package/packages/dd-trace/src/appsec/activation.js +1 -1
  172. package/packages/dd-trace/src/appsec/api_security/normalized-route.js +0 -5
  173. package/packages/dd-trace/src/appsec/api_security/sampler.js +0 -4
  174. package/packages/dd-trace/src/appsec/blocking/index.js +3 -3
  175. package/packages/dd-trace/src/appsec/downstream_requests.js +1 -4
  176. package/packages/dd-trace/src/appsec/iast/index.js +1 -1
  177. package/packages/dd-trace/src/appsec/iast/overhead-controller.js +5 -5
  178. package/packages/dd-trace/src/appsec/iast/taint-tracking/index.js +1 -1
  179. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +1 -1
  180. package/packages/dd-trace/src/appsec/iast/taint-tracking/rewriter.js +3 -3
  181. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-analyzers/sql-sensitive-analyzer.js +8 -10
  182. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-analyzers/url-sensitive-analyzer.js +2 -2
  183. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-handler.js +2 -2
  184. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/utils.js +4 -1
  185. package/packages/dd-trace/src/appsec/iast/vulnerability-reporter.js +7 -7
  186. package/packages/dd-trace/src/appsec/index.js +2 -2
  187. package/packages/dd-trace/src/appsec/lambda.js +71 -2
  188. package/packages/dd-trace/src/appsec/rasp/utils.js +5 -1
  189. package/packages/dd-trace/src/appsec/remote_config.js +8 -6
  190. package/packages/dd-trace/src/appsec/reporter.js +10 -6
  191. package/packages/dd-trace/src/appsec/rule_manager.js +3 -3
  192. package/packages/dd-trace/src/appsec/sdk/track_event.js +0 -2
  193. package/packages/dd-trace/src/appsec/telemetry/api_security.js +1 -3
  194. package/packages/dd-trace/src/appsec/telemetry/common.js +4 -0
  195. package/packages/dd-trace/src/appsec/telemetry/index.js +23 -5
  196. package/packages/dd-trace/src/appsec/telemetry/rasp.js +1 -20
  197. package/packages/dd-trace/src/appsec/telemetry/user.js +1 -3
  198. package/packages/dd-trace/src/appsec/telemetry/waf.js +1 -20
  199. package/packages/dd-trace/src/appsec/waf/index.js +2 -2
  200. package/packages/dd-trace/src/appsec/waf/waf_manager.js +8 -5
  201. package/packages/dd-trace/src/carrier.js +0 -5
  202. package/packages/dd-trace/src/ci-visibility/dynamic-atr-retries.js +104 -0
  203. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +0 -2
  204. package/packages/dd-trace/src/ci-visibility/efd-retry-policy.js +34 -3
  205. package/packages/dd-trace/src/ci-visibility/exporters/agentless/coverage-writer.js +0 -1
  206. package/packages/dd-trace/src/ci-visibility/exporters/agentless/di-logs-writer.js +0 -1
  207. package/packages/dd-trace/src/ci-visibility/exporters/agentless/writer.js +0 -1
  208. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -4
  209. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -1
  210. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -2
  211. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +37 -15
  212. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +0 -3
  213. package/packages/dd-trace/src/ci-visibility/exporters/request.js +0 -11
  214. package/packages/dd-trace/src/ci-visibility/exporters/settings-cache-key.js +0 -1
  215. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +0 -1
  216. package/packages/dd-trace/src/ci-visibility/requests/fs-cache.js +0 -6
  217. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +2 -2
  218. package/packages/dd-trace/src/ci-visibility/requests/rate-limit.js +0 -1
  219. package/packages/dd-trace/src/ci-visibility/requests/upload-test-screenshot.js +0 -5
  220. package/packages/dd-trace/src/ci-visibility/requests/video-request.js +0 -1
  221. package/packages/dd-trace/src/ci-visibility/telemetry.js +13 -0
  222. package/packages/dd-trace/src/ci-visibility/test-optimization-cache.js +0 -1
  223. package/packages/dd-trace/src/ci-visibility/test-screenshot.js +0 -2
  224. package/packages/dd-trace/src/ci-visibility/test-video.js +0 -1
  225. package/packages/dd-trace/src/config/defaults.js +3 -2
  226. package/packages/dd-trace/src/config/generated-config-types.d.ts +74 -66
  227. package/packages/dd-trace/src/config/helper.js +9 -2
  228. package/packages/dd-trace/src/config/index.js +44 -29
  229. package/packages/dd-trace/src/config/parsers.js +34 -6
  230. package/packages/dd-trace/src/config/remote_config.js +25 -109
  231. package/packages/dd-trace/src/config/supported-configurations.json +146 -9
  232. package/packages/dd-trace/src/datastreams/encoding.js +0 -1
  233. package/packages/dd-trace/src/datastreams/pathway.js +0 -1
  234. package/packages/dd-trace/src/debugger/constants.js +10 -0
  235. package/packages/dd-trace/src/debugger/devtools_client/breakpoints.js +76 -17
  236. package/packages/dd-trace/src/debugger/devtools_client/config.js +2 -0
  237. package/packages/dd-trace/src/debugger/devtools_client/guardrail-metrics.js +15 -0
  238. package/packages/dd-trace/src/debugger/devtools_client/index.js +103 -27
  239. package/packages/dd-trace/src/debugger/devtools_client/json-buffer.js +50 -7
  240. package/packages/dd-trace/src/debugger/devtools_client/log.js +3 -1
  241. package/packages/dd-trace/src/debugger/devtools_client/probe_sampler.js +34 -8
  242. package/packages/dd-trace/src/debugger/devtools_client/send.js +40 -14
  243. package/packages/dd-trace/src/debugger/devtools_client/snapshot/index.js +53 -13
  244. package/packages/dd-trace/src/debugger/devtools_client/snapshot/processor.js +118 -71
  245. package/packages/dd-trace/src/debugger/devtools_client/snapshot-pruner.js +0 -3
  246. package/packages/dd-trace/src/debugger/devtools_client/status.js +20 -6
  247. package/packages/dd-trace/src/debugger/guardrail-metrics.js +167 -0
  248. package/packages/dd-trace/src/debugger/index.js +50 -2
  249. package/packages/dd-trace/src/debugger/inspect-segment.js +0 -3
  250. package/packages/dd-trace/src/debugger/probe_sampler.js +133 -9
  251. package/packages/dd-trace/src/debugger/probe_sampler_constants.js +31 -1
  252. package/packages/dd-trace/src/dogstatsd.js +0 -23
  253. package/packages/dd-trace/src/encode/0.4-cross-payload.js +0 -3
  254. package/packages/dd-trace/src/encode/0.4.js +0 -7
  255. package/packages/dd-trace/src/encode/0.5.js +0 -2
  256. package/packages/dd-trace/src/encode/tags-processors.js +0 -1
  257. package/packages/dd-trace/src/evp_proxy/discovery.js +3 -5
  258. package/packages/dd-trace/src/evp_proxy/path.js +11 -4
  259. package/packages/dd-trace/src/exporters/agent/writer.js +0 -1
  260. package/packages/dd-trace/src/exporters/agentless/index.js +0 -1
  261. package/packages/dd-trace/src/exporters/agentless/intake.js +0 -1
  262. package/packages/dd-trace/src/exporters/agentless/writer.js +0 -3
  263. package/packages/dd-trace/src/exporters/common/docker.js +4 -1
  264. package/packages/dd-trace/src/exporters/common/final-flush-request-tracker.js +0 -6
  265. package/packages/dd-trace/src/exporters/common/proxy.js +3 -0
  266. package/packages/dd-trace/src/exporters/common/request.js +1 -0
  267. package/packages/dd-trace/src/exporters/common/url.js +0 -2
  268. package/packages/dd-trace/src/exporters/common/writer.js +0 -2
  269. package/packages/dd-trace/src/id.js +0 -11
  270. package/packages/dd-trace/src/llmobs/audio-codec.js +192 -0
  271. package/packages/dd-trace/src/llmobs/audio-utils.js +121 -0
  272. package/packages/dd-trace/src/llmobs/constants/audio.js +35 -0
  273. package/packages/dd-trace/src/llmobs/constants/prompts.js +14 -0
  274. package/packages/dd-trace/src/llmobs/constants/tags.js +17 -0
  275. package/packages/dd-trace/src/llmobs/eval-metric.js +0 -6
  276. package/packages/dd-trace/src/llmobs/experiments/dataset.js +0 -4
  277. package/packages/dd-trace/src/llmobs/experiments/experiment.js +0 -3
  278. package/packages/dd-trace/src/llmobs/experiments/index.js +7 -6
  279. package/packages/dd-trace/src/llmobs/experiments/util.js +1 -11
  280. package/packages/dd-trace/src/llmobs/index.js +1 -3
  281. package/packages/dd-trace/src/llmobs/noop.js +10 -0
  282. package/packages/dd-trace/src/llmobs/plugins/ai/util.js +0 -4
  283. package/packages/dd-trace/src/llmobs/plugins/anthropic/util.js +0 -1
  284. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +0 -4
  285. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +0 -2
  286. package/packages/dd-trace/src/llmobs/plugins/openai/constants.js +0 -2
  287. package/packages/dd-trace/src/llmobs/plugins/openai/index.js +4 -6
  288. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +285 -0
  289. package/packages/dd-trace/src/llmobs/plugins/openai/utils.js +16 -3
  290. package/packages/dd-trace/src/llmobs/prompts/cache.js +272 -0
  291. package/packages/dd-trace/src/llmobs/prompts/manager.js +639 -0
  292. package/packages/dd-trace/src/llmobs/prompts/noop.js +29 -0
  293. package/packages/dd-trace/src/llmobs/prompts/prompt.js +92 -0
  294. package/packages/dd-trace/src/llmobs/sdk.js +21 -6
  295. package/packages/dd-trace/src/llmobs/span_processor.js +81 -2
  296. package/packages/dd-trace/src/llmobs/tagger.js +30 -7
  297. package/packages/dd-trace/src/llmobs/telemetry.js +36 -4
  298. package/packages/dd-trace/src/llmobs/util.js +0 -34
  299. package/packages/dd-trace/src/llmobs/writers/base.js +0 -1
  300. package/packages/dd-trace/src/llmobs/writers/util.js +1 -2
  301. package/packages/dd-trace/src/log/channels.js +3 -0
  302. package/packages/dd-trace/src/log/index.js +23 -5
  303. package/packages/dd-trace/src/log-submission/log-submission-plugin.js +74 -6
  304. package/packages/dd-trace/src/msgpack/chunk.js +0 -1
  305. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +0 -12
  306. package/packages/dd-trace/src/openfeature/encoding.js +0 -2
  307. package/packages/dd-trace/src/openfeature/eval-metrics-hook.js +0 -1
  308. package/packages/dd-trace/src/openfeature/index.js +0 -4
  309. package/packages/dd-trace/src/openfeature/span-enrichment-hook.js +0 -1
  310. package/packages/dd-trace/src/openfeature/span-enrichment.js +0 -4
  311. package/packages/dd-trace/src/openfeature/writers/base.js +0 -6
  312. package/packages/dd-trace/src/openfeature/writers/exposures.js +0 -5
  313. package/packages/dd-trace/src/openfeature/writers/util.js +0 -4
  314. package/packages/dd-trace/src/opentelemetry/logs/logger.js +0 -1
  315. package/packages/dd-trace/src/opentelemetry/logs/logger_provider.js +0 -1
  316. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +0 -1
  317. package/packages/dd-trace/src/opentelemetry/logs/otlp_transformer.js +0 -2
  318. package/packages/dd-trace/src/opentelemetry/metrics/index.js +1 -1
  319. package/packages/dd-trace/src/opentelemetry/metrics/meter_provider.js +4 -4
  320. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_transformer.js +0 -1
  321. package/packages/dd-trace/src/opentelemetry/metrics/otlp_transformer.js +0 -1
  322. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +103 -35
  323. package/packages/dd-trace/src/opentelemetry/metrics/time.js +0 -1
  324. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +36 -13
  325. package/packages/dd-trace/src/opentelemetry/otlp/otlp_transformer_base.js +0 -1
  326. package/packages/dd-trace/src/opentelemetry/span-helpers.js +0 -1
  327. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +0 -1
  328. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +0 -3
  329. package/packages/dd-trace/src/opentracing/propagation/text_map.js +0 -6
  330. package/packages/dd-trace/src/opentracing/span_context.js +0 -1
  331. package/packages/dd-trace/src/otel-thread-ctx.js +38 -11
  332. package/packages/dd-trace/src/payload-tagging/tagging.js +15 -3
  333. package/packages/dd-trace/src/plugin_manager.js +9 -3
  334. package/packages/dd-trace/src/plugins/ci_plugin.js +0 -1
  335. package/packages/dd-trace/src/plugins/database.js +2 -6
  336. package/packages/dd-trace/src/plugins/index.js +8 -0
  337. package/packages/dd-trace/src/plugins/plugin.js +0 -4
  338. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  339. package/packages/dd-trace/src/plugins/util/git.js +1 -3
  340. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +0 -1
  341. package/packages/dd-trace/src/plugins/util/llm.js +0 -2
  342. package/packages/dd-trace/src/plugins/util/status-validator.js +0 -1
  343. package/packages/dd-trace/src/plugins/util/test.js +25 -21
  344. package/packages/dd-trace/src/plugins/util/url.js +0 -5
  345. package/packages/dd-trace/src/priority_sampler.js +0 -6
  346. package/packages/dd-trace/src/process-tags/index.js +0 -1
  347. package/packages/dd-trace/src/profiler.js +132 -35
  348. package/packages/dd-trace/src/profiling/exporters/event_serializer.js +0 -1
  349. package/packages/dd-trace/src/profiling/profiler.js +101 -41
  350. package/packages/dd-trace/src/profiling/ssi-heuristics.js +29 -15
  351. package/packages/dd-trace/src/propagation-hash/index.js +0 -1
  352. package/packages/dd-trace/src/proxy.js +21 -45
  353. package/packages/dd-trace/src/random_sampler.js +0 -4
  354. package/packages/dd-trace/src/rate_limiter.js +0 -4
  355. package/packages/dd-trace/src/remote_config/capabilities.js +1 -9
  356. package/packages/dd-trace/src/remote_config/index.js +0 -1
  357. package/packages/dd-trace/src/runtime_metrics/otlp_runtime_metrics.js +0 -3
  358. package/packages/dd-trace/src/sampler.js +0 -4
  359. package/packages/dd-trace/src/sampling_rule.js +0 -7
  360. package/packages/dd-trace/src/serverless/vercel.js +0 -1
  361. package/packages/dd-trace/src/serverless.js +0 -1
  362. package/packages/dd-trace/src/service-naming/index.js +0 -1
  363. package/packages/dd-trace/src/service-naming/schemas/util.js +0 -1
  364. package/packages/dd-trace/src/service-naming/schemas/v0/storage.js +7 -0
  365. package/packages/dd-trace/src/service-naming/schemas/v1/storage.js +5 -0
  366. package/packages/dd-trace/src/span_processor.js +2 -2
  367. package/packages/dd-trace/src/span_sampler.js +0 -1
  368. package/packages/dd-trace/src/standalone/product.js +2 -2
  369. package/packages/dd-trace/src/standalone/tracesource.js +0 -1
  370. package/packages/dd-trace/src/startup-log.js +1 -1
  371. package/packages/dd-trace/src/telemetry/endpoints.js +0 -8
  372. package/packages/dd-trace/src/telemetry/logs/index.js +16 -5
  373. package/packages/dd-trace/src/telemetry/logs/log-collector.js +16 -6
  374. package/packages/dd-trace/src/telemetry/metrics.js +0 -2
  375. package/packages/dd-trace/src/telemetry/telemetry.js +4 -1
  376. package/packages/dd-trace/src/util.js +13 -1
  377. package/vendor/dist/@datadog/openfeature-node-server/index.js +1 -1
  378. package/packages/dd-trace/src/llmobs/plugins/claude-agent-sdk/utils.js +0 -10
@@ -0,0 +1,128 @@
1
+ 'use strict'
2
+
3
+ const versionRange = '>=2.112.2'
4
+
5
+ module.exports = [
6
+ {
7
+ module: {
8
+ name: '@supabase/storage-js',
9
+ versionRange,
10
+ filePath: 'dist/index.cjs',
11
+ },
12
+ functionQuery: { functionName: '_handleRequest', kind: 'Async' },
13
+ channelName: 'handleRequest',
14
+ },
15
+ {
16
+ module: {
17
+ name: '@supabase/storage-js',
18
+ versionRange,
19
+ filePath: 'dist/index.mjs',
20
+ },
21
+ functionQuery: { functionName: '_handleRequest', kind: 'Async' },
22
+ channelName: 'handleRequest',
23
+ },
24
+ {
25
+ module: {
26
+ name: '@supabase/auth-js',
27
+ versionRange,
28
+ filePath: 'dist/main/GoTrueClient.js',
29
+ },
30
+ functionQuery: {
31
+ methodName: 'getUser',
32
+ className: 'GoTrueClient',
33
+ kind: 'Async',
34
+ },
35
+ channelName: 'GoTrueClient_getUser',
36
+ },
37
+ {
38
+ module: {
39
+ name: '@supabase/auth-js',
40
+ versionRange,
41
+ filePath: 'dist/module/GoTrueClient.js',
42
+ },
43
+ functionQuery: {
44
+ methodName: 'getUser',
45
+ className: 'GoTrueClient',
46
+ kind: 'Async',
47
+ },
48
+ channelName: 'GoTrueClient_getUser',
49
+ },
50
+ {
51
+ module: {
52
+ name: '@supabase/realtime-js',
53
+ versionRange,
54
+ filePath: 'dist/main/RealtimeChannel.js',
55
+ },
56
+ functionQuery: {
57
+ methodName: 'send',
58
+ className: 'RealtimeChannel',
59
+ kind: 'Async',
60
+ },
61
+ channelName: 'RealtimeChannel_send',
62
+ },
63
+ {
64
+ module: {
65
+ name: '@supabase/realtime-js',
66
+ versionRange,
67
+ filePath: 'dist/module/RealtimeChannel.js',
68
+ },
69
+ functionQuery: {
70
+ methodName: 'send',
71
+ className: 'RealtimeChannel',
72
+ kind: 'Async',
73
+ },
74
+ channelName: 'RealtimeChannel_send',
75
+ },
76
+ {
77
+ module: {
78
+ name: '@supabase/functions-js',
79
+ versionRange,
80
+ filePath: 'dist/main/FunctionsClient.js',
81
+ },
82
+ functionQuery: {
83
+ methodName: 'invoke',
84
+ className: 'FunctionsClient',
85
+ kind: 'Async',
86
+ },
87
+ channelName: 'FunctionsClient_invoke',
88
+ },
89
+ {
90
+ module: {
91
+ name: '@supabase/functions-js',
92
+ versionRange,
93
+ filePath: 'dist/module/FunctionsClient.js',
94
+ },
95
+ functionQuery: {
96
+ methodName: 'invoke',
97
+ className: 'FunctionsClient',
98
+ kind: 'Async',
99
+ },
100
+ channelName: 'FunctionsClient_invoke',
101
+ },
102
+ {
103
+ module: {
104
+ name: '@supabase/postgrest-js',
105
+ versionRange,
106
+ filePath: 'dist/index.cjs',
107
+ },
108
+ functionQuery: {
109
+ methodName: 'then',
110
+ className: 'PostgrestBuilder',
111
+ kind: 'Async',
112
+ },
113
+ channelName: 'PostgrestBuilder_then',
114
+ },
115
+ {
116
+ module: {
117
+ name: '@supabase/postgrest-js',
118
+ versionRange,
119
+ filePath: 'dist/index.mjs',
120
+ },
121
+ functionQuery: {
122
+ methodName: 'then',
123
+ className: 'PostgrestBuilder',
124
+ kind: 'Async',
125
+ },
126
+ channelName: 'PostgrestBuilder_then',
127
+ },
128
+ ]
@@ -428,9 +428,9 @@ module.exports = [
428
428
  versionRange: '>=9.0.0',
429
429
  filePath: 'build/index.js',
430
430
  },
431
- astQuery: 'VariableDeclarator[id.name="testFrameworkFnWrapper"] > FunctionExpression TryStatement',
431
+ astQuery: 'VariableDeclarator[id.name="testFrameworkFnWrapper"] > FunctionExpression',
432
432
  channelName: 'testFrameworkFnWrapper',
433
- transform: 'awaitContextCallbackAtFunctionStart',
433
+ transform: 'awaitContextCallback',
434
434
  transformOptions: {
435
435
  callbackName: 'rumCleanupCallback',
436
436
  },
@@ -453,12 +453,11 @@ module.exports = [
453
453
  versionRange: '>=9.0.0',
454
454
  filePath: 'build/index.js',
455
455
  },
456
- astQuery: 'FunctionDeclaration[id.name="executeAsync"] CatchClause ' +
457
- 'IfStatement[test.operator=">"][test.left.object.name="retries"]' +
458
- '[test.left.property.name="limit"][test.right.object.name="retries"]' +
459
- '[test.right.property.name="attempts"]',
456
+ astQuery: 'TryStatement:has(CatchClause IfStatement[test.operator=">"]' +
457
+ '[test.left.object.name="retries"][test.left.property.name="limit"]' +
458
+ '[test.right.object.name="retries"][test.right.property.name="attempts"]) > BlockStatement',
460
459
  channelName: 'executeAsync',
461
- transform: 'awaitContextCallbackAtTryStart',
460
+ transform: 'awaitContextCallback',
462
461
  transformOptions: {
463
462
  callbackName: 'rumStartCallback',
464
463
  },
@@ -30,6 +30,16 @@
30
30
  "@modelcontextprotocol/sdk/dist/esm/server/mcp.js": "@modelcontextprotocol/sdk",
31
31
  "@openai/agents-openai/dist/openaiChatCompletionsModel.js": "@openai/agents-openai",
32
32
  "@openai/agents-openai/dist/openaiChatCompletionsModel.mjs": "@openai/agents-openai",
33
+ "@supabase/auth-js/dist/main/GoTrueClient.js": "@supabase/auth-js",
34
+ "@supabase/auth-js/dist/module/GoTrueClient.js": "@supabase/auth-js",
35
+ "@supabase/functions-js/dist/main/FunctionsClient.js": "@supabase/functions-js",
36
+ "@supabase/functions-js/dist/module/FunctionsClient.js": "@supabase/functions-js",
37
+ "@supabase/postgrest-js/dist/index.cjs": "@supabase/postgrest-js",
38
+ "@supabase/postgrest-js/dist/index.mjs": "@supabase/postgrest-js",
39
+ "@supabase/realtime-js/dist/main/RealtimeChannel.js": "@supabase/realtime-js",
40
+ "@supabase/realtime-js/dist/module/RealtimeChannel.js": "@supabase/realtime-js",
41
+ "@supabase/storage-js/dist/index.cjs": "@supabase/storage-js",
42
+ "@supabase/storage-js/dist/index.mjs": "@supabase/storage-js",
33
43
  "@wdio/cli/build/index.js": "@wdio/cli",
34
44
  "@wdio/jasmine-framework/build/index.js": "@wdio/jasmine-framework",
35
45
  "@wdio/local-runner/build/index.js": "@wdio/local-runner",
@@ -43,6 +53,7 @@
43
53
  "bullmq/dist/esm/classes/flow-producer.js": "bullmq",
44
54
  "bullmq/dist/esm/classes/queue.js": "bullmq",
45
55
  "bullmq/dist/esm/classes/worker.js": "bullmq",
56
+ "durable-functions/lib/src/orchestrations/TaskOrchestrationExecutor.js": "durable-functions",
46
57
  "graphql-jit/dist/cjs/execution.js": "graphql-jit",
47
58
  "graphql-jit/dist/esm/execution.js": "graphql-jit",
48
59
  "graphql-jit/dist/execution.js": "graphql-jit",
@@ -66,6 +77,12 @@
66
77
  "playwright-core/lib/coreBundle.js": "playwright-core",
67
78
  "playwright/lib/index.js": "playwright",
68
79
  "playwright/lib/runner/index.js": "playwright",
80
+ "postgres/cjs/src/connection.js": "postgres",
81
+ "postgres/cjs/src/index.js": "postgres",
82
+ "postgres/cjs/src/query.js": "postgres",
83
+ "postgres/src/connection.js": "postgres",
84
+ "postgres/src/index.js": "postgres",
85
+ "postgres/src/query.js": "postgres",
69
86
  "webdriver/build/index.js": "webdriver",
70
87
  "webdriver/build/node.js": "webdriver",
71
88
  "webdriverio/build/index.js": "webdriverio",
@@ -0,0 +1,388 @@
1
+ 'use strict'
2
+
3
+ const assert = require('node:assert')
4
+
5
+ const { parse, query } = require('../compiler')
6
+
7
+ const POSTGRES_OPTIONS = '__ddTracePostgresOptions'
8
+ const POSTGRES_QUERY_REGISTRY = '__ddTracePostgresQueries'
9
+ const POSTGRES_QUERY_REGISTRY_SYMBOL = '__ddTracePostgresQueriesSymbol'
10
+ const POSTGRES_READY = '__ddTracePostgresQueryReady'
11
+ const POSTGRES_SIMPLE_DBM_QUERIES = '__ddTracePostgresSimpleDbmQueries'
12
+ const POSTGRES_STATEMENT = 'this.string ?? (this.tagged && this.strings?.length !== 1 ? undefined : this.strings?.[0])'
13
+
14
+ module.exports = {
15
+ postgresQueryHandlers,
16
+ postgresQueryLifecycle,
17
+ postgresQueryPreparation,
18
+ }
19
+
20
+ /**
21
+ * Starts a Postgres.js query at the pool handler that owns its connection options.
22
+ *
23
+ * @param {object} state
24
+ * @param {import('estree').Program} program
25
+ */
26
+ function postgresQueryHandlers (state, program) {
27
+ const channelVariable = injectPostgresTracingChannel(state, program)
28
+ injectPostgresReadyCheck(program, findPostgresQueryIdentifier(program))
29
+ capturePostgresOptions(program)
30
+
31
+ const handlers = query(program, 'FunctionDeclaration[id.name="handler"]')
32
+ assert(handlers.length >= 2 && handlers.length <= 3, 'postgresQueryHandlers: unexpected handler count')
33
+
34
+ for (const handler of handlers) {
35
+ const queryParameter = handler.params[0]
36
+ assert(queryParameter?.type === 'Identifier', 'postgresQueryHandlers: handler query parameter changed')
37
+ wrapPostgresHandler(handler, queryParameter.name, channelVariable)
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Publishes the final Postgres.js query settlement without changing its Promise implementation.
43
+ *
44
+ * @param {object} state
45
+ * @param {import('estree').Program} program
46
+ */
47
+ function postgresQueryLifecycle (state, program) {
48
+ const channelVariable = injectPostgresTracingChannel(state, program)
49
+ injectPostgresQueryRegistration(program)
50
+
51
+ const resolveAssignments = query(
52
+ program,
53
+ 'AssignmentExpression[left.object.type="ThisExpression"][left.property.name="resolve"]'
54
+ )
55
+ const rejectAssignments = query(
56
+ program,
57
+ 'AssignmentExpression[left.object.type="ThisExpression"][left.property.name="reject"]'
58
+ )
59
+
60
+ assert(resolveAssignments.length === 2, 'postgresQueryLifecycle: unexpected resolve assignment count')
61
+ assert(rejectAssignments.length === 2, 'postgresQueryLifecycle: unexpected reject assignment count')
62
+
63
+ for (const assignment of resolveAssignments) {
64
+ wrapPostgresResolution(assignment, channelVariable)
65
+ }
66
+ for (const assignment of rejectAssignments) {
67
+ wrapPostgresRejection(assignment, channelVariable)
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Injects DBM data after Postgres.js builds the final statement and before it creates wire messages.
73
+ *
74
+ * @param {object} state
75
+ * @param {import('estree').Program} program
76
+ */
77
+ function postgresQueryPreparation (state, program) {
78
+ const channelVariable = injectPostgresTracingChannel(state, program)
79
+ const buildFunctions = query(program, 'FunctionDeclaration[id.name="build"]')
80
+ const toBufferFunctions = query(program, 'FunctionDeclaration[id.name="toBuffer"]')
81
+
82
+ assert(buildFunctions.length === 1, 'postgresQueryPreparation: build function changed')
83
+ assert(toBufferFunctions.length === 1, 'postgresQueryPreparation: toBuffer function changed')
84
+
85
+ const build = buildFunctions[0]
86
+ const queryParameter = build.params[0]
87
+ assert(queryParameter?.type === 'Identifier', 'postgresQueryPreparation: build query parameter changed')
88
+ assert(build.body.type === 'BlockStatement', 'postgresQueryPreparation: build body changed')
89
+
90
+ const queryName = queryParameter.name
91
+ const body = build.body.body
92
+ const stringDeclaration = body.find(statement =>
93
+ statement.type === 'VariableDeclaration' &&
94
+ statement.declarations.some(({ id }) => id.type === 'Identifier' && id.name === 'string')
95
+ )
96
+ assert(stringDeclaration?.kind === 'const', 'postgresQueryPreparation: string declaration changed')
97
+
98
+ const prepareIndex = body.findIndex(statement =>
99
+ statement.type === 'ExpressionStatement' &&
100
+ statement.expression.type === 'AssignmentExpression' &&
101
+ statement.expression.left.type === 'MemberExpression' &&
102
+ statement.expression.left.object.type === 'Identifier' &&
103
+ statement.expression.left.object.name === queryName &&
104
+ statement.expression.left.property.type === 'Identifier' &&
105
+ statement.expression.left.property.name === 'prepare'
106
+ )
107
+ assert(prepareIndex !== -1, 'postgresQueryPreparation: prepare assignment changed')
108
+
109
+ const legacySimpleString = findLegacyPostgresSimpleString(toBufferFunctions[0], queryName)
110
+ const contextStatements = parse(`
111
+ if (${channelVariable}.start.hasSubscribers) {
112
+ const __ddTraceContext = {
113
+ query: ${queryName},
114
+ statement: string,
115
+ prepared: Boolean(${queryName}.prepare && !${queryName}.options.simple)
116
+ };
117
+ ${channelVariable}.start.publish(__ddTraceContext);
118
+ ${legacySimpleString === undefined
119
+ ? ''
120
+ : `if (${queryName}.options.simple && __ddTraceContext.statement !== string) {
121
+ ${POSTGRES_SIMPLE_DBM_QUERIES}.add(${queryName});
122
+ }`}
123
+ string = __ddTraceContext.statement;
124
+ }
125
+ `).body
126
+
127
+ stringDeclaration.kind = 'let'
128
+ body.splice(prepareIndex + 1, 0, ...contextStatements)
129
+
130
+ if (legacySimpleString !== undefined) {
131
+ injectPostgresModuleStatements(
132
+ program,
133
+ parse(`const ${POSTGRES_SIMPLE_DBM_QUERIES} = new WeakSet();`).body
134
+ )
135
+ const replacement = parse(`
136
+ ${channelVariable}.start.hasSubscribers && ${POSTGRES_SIMPLE_DBM_QUERIES}.delete(${queryName})
137
+ ? ${queryName}.string
138
+ : ${queryName}.strings[0]
139
+ `).body[0].expression
140
+
141
+ for (const key of Object.keys(legacySimpleString)) {
142
+ delete legacySimpleString[key]
143
+ }
144
+ Object.assign(legacySimpleString, replacement)
145
+ }
146
+ }
147
+
148
+ /**
149
+ * @param {import('estree').FunctionDeclaration|import('estree').ArrowFunctionExpression} handler
150
+ * @param {string} queryParameter
151
+ * @param {string} channelVariable
152
+ */
153
+ function wrapPostgresHandler (handler, queryParameter, channelVariable) {
154
+ assert(handler.body.type === 'BlockStatement', 'postgresQueryHandlers: handler body changed')
155
+ assert(!handler.async && !handler.generator, 'postgresQueryHandlers: unsupported handler kind')
156
+
157
+ const originalBody = handler.body.body
158
+ const wrapperBody = parse(`
159
+ function wrapper () {
160
+ if (${channelVariable}.start.hasSubscribers && ${POSTGRES_READY} && !${queryParameter}.cancelled) {
161
+ const __ddTraceContext = {
162
+ query: ${queryParameter},
163
+ database: ${POSTGRES_OPTIONS}.database,
164
+ user: ${POSTGRES_OPTIONS}.user
165
+ };
166
+ if (${POSTGRES_OPTIONS}.host.length === 1 && !${POSTGRES_OPTIONS}.path) {
167
+ __ddTraceContext.host = ${POSTGRES_OPTIONS}.host[0];
168
+ __ddTraceContext.port = ${POSTGRES_OPTIONS}.port[0];
169
+ }
170
+ return ${channelVariable}.start.runStores(__ddTraceContext, () => {});
171
+ }
172
+ }
173
+ `).body[0].body.body
174
+
175
+ wrapperBody[0].consequent.body.at(-1).argument.arguments[1].body.body = originalBody
176
+ handler.body.body = [...wrapperBody, ...originalBody]
177
+ }
178
+
179
+ /**
180
+ * @param {import('estree').Program} program
181
+ */
182
+ function capturePostgresOptions (program) {
183
+ const postgresFunctions = query(program, 'FunctionDeclaration[id.name="Postgres"]')
184
+ assert(postgresFunctions.length === 1, 'postgresQueryHandlers: Postgres function changed')
185
+
186
+ const body = postgresFunctions[0].body.body
187
+ const optionsIndex = body.findIndex(statement =>
188
+ statement.type === 'VariableDeclaration' && statement.declarations.some(({ id }) => id.name === 'options')
189
+ )
190
+ assert(optionsIndex !== -1, 'postgresQueryHandlers: options declaration changed')
191
+
192
+ const capture = parse(`const ${POSTGRES_OPTIONS} = options;`).body[0]
193
+ body.splice(optionsIndex + 1, 0, capture)
194
+ }
195
+
196
+ /**
197
+ * @param {import('estree').Program} program
198
+ * @param {string} queryIdentifier
199
+ */
200
+ function injectPostgresReadyCheck (program, queryIdentifier) {
201
+ const statements = parse(`
202
+ const ${POSTGRES_QUERY_REGISTRY} = globalThis[Symbol.for('dd-trace:postgres:query')];
203
+ const ${POSTGRES_READY} = ${POSTGRES_QUERY_REGISTRY} instanceof WeakSet &&
204
+ ${POSTGRES_QUERY_REGISTRY}.has(${queryIdentifier});
205
+ `).body
206
+ injectPostgresModuleStatements(program, statements)
207
+ }
208
+
209
+ /**
210
+ * @param {import('estree').FunctionDeclaration} toBuffer
211
+ * @param {string} queryName
212
+ * @returns {import('estree').MemberExpression | undefined}
213
+ */
214
+ function findLegacyPostgresSimpleString (toBuffer, queryName) {
215
+ const simpleConditions = query(toBuffer, 'ConditionalExpression').filter(({ test }) =>
216
+ test.type === 'MemberExpression' &&
217
+ test.object.type === 'MemberExpression' &&
218
+ test.object.object.type === 'Identifier' &&
219
+ test.object.object.name === queryName &&
220
+ test.object.property.type === 'Identifier' &&
221
+ test.object.property.name === 'options' &&
222
+ test.property.type === 'Identifier' &&
223
+ test.property.name === 'simple'
224
+ )
225
+ assert(simpleConditions.length === 1, 'postgresQueryPreparation: simple query branch changed')
226
+
227
+ const legacyStrings = query(simpleConditions[0].consequent, 'MemberExpression').filter(node =>
228
+ node.computed &&
229
+ node.object.type === 'MemberExpression' &&
230
+ node.object.object.type === 'Identifier' &&
231
+ node.object.object.name === queryName &&
232
+ node.object.property.type === 'Identifier' &&
233
+ node.object.property.name === 'strings' &&
234
+ node.property.type === 'Literal' &&
235
+ node.property.value === 0
236
+ )
237
+ assert(legacyStrings.length <= 1, 'postgresQueryPreparation: simple query string changed')
238
+ return legacyStrings[0]
239
+ }
240
+
241
+ /**
242
+ * @param {import('estree').Program} program
243
+ * @param {import('estree').Statement[]} statements
244
+ */
245
+ function injectPostgresModuleStatements (program, statements) {
246
+ let importIndex = program.body.length - 1
247
+ while (importIndex >= 0) {
248
+ const statement = program.body[importIndex]
249
+ if (statement.type === 'ImportDeclaration' || statement.type === 'VariableDeclaration') break
250
+ importIndex--
251
+ }
252
+
253
+ program.body.splice(importIndex + 1, 0, ...statements)
254
+ }
255
+
256
+ /**
257
+ * @param {import('estree').Program} program
258
+ */
259
+ function findPostgresQueryIdentifier (program) {
260
+ const identifiers = []
261
+
262
+ const imports = query(program, 'ImportDeclaration')
263
+ for (const declaration of imports) {
264
+ if (declaration.source.value !== './query.js') continue
265
+
266
+ for (const specifier of declaration.specifiers) {
267
+ if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'Query') {
268
+ identifiers.push(specifier.local.name)
269
+ }
270
+ }
271
+ }
272
+
273
+ const declarations = query(program, 'VariableDeclarator[id.type="ObjectPattern"]')
274
+ for (const declaration of declarations) {
275
+ if (declaration.init?.type !== 'CallExpression' || declaration.init.callee.name !== 'require') continue
276
+ if (declaration.init.arguments[0]?.value !== './query.js') continue
277
+
278
+ for (const property of declaration.id.properties) {
279
+ if (property.type === 'Property' && property.key.name === 'Query' && property.value.type === 'Identifier') {
280
+ identifiers.push(property.value.name)
281
+ }
282
+ }
283
+ }
284
+
285
+ assert(identifiers.length === 1, 'postgresQueryHandlers: Query import changed')
286
+ return identifiers[0]
287
+ }
288
+
289
+ /**
290
+ * @param {import('estree').Program} program
291
+ */
292
+ function injectPostgresQueryRegistration (program) {
293
+ const queryClassIndex = program.body.findIndex(statement =>
294
+ statement.type === 'VariableDeclaration' &&
295
+ statement.declarations.some(({ id }) => id.type === 'Identifier' && id.name === 'Query') ||
296
+ statement.type === 'ExportNamedDeclaration' && statement.declaration?.type === 'ClassDeclaration' &&
297
+ statement.declaration.id?.name === 'Query'
298
+ )
299
+ assert(queryClassIndex !== -1, 'postgresQueryLifecycle: Query class changed')
300
+
301
+ const statements = parse(`
302
+ const ${POSTGRES_QUERY_REGISTRY_SYMBOL} = Symbol.for('dd-trace:postgres:query');
303
+ const ${POSTGRES_QUERY_REGISTRY} = globalThis[${POSTGRES_QUERY_REGISTRY_SYMBOL}] instanceof WeakSet
304
+ ? globalThis[${POSTGRES_QUERY_REGISTRY_SYMBOL}]
305
+ : (globalThis[${POSTGRES_QUERY_REGISTRY_SYMBOL}] = new WeakSet());
306
+ ${POSTGRES_QUERY_REGISTRY}.add(Query);
307
+ `).body
308
+ program.body.splice(queryClassIndex + 1, 0, ...statements)
309
+ }
310
+
311
+ /**
312
+ * @param {import('estree').AssignmentExpression} assignment
313
+ * @param {string} channelVariable
314
+ */
315
+ function wrapPostgresResolution (assignment, channelVariable) {
316
+ const resolution = assignment.right
317
+ assert(
318
+ resolution.type === 'ArrowFunctionExpression' && resolution.body.type !== 'BlockStatement',
319
+ 'postgresQueryLifecycle: resolve function changed'
320
+ )
321
+
322
+ const originalBody = resolution.body
323
+ const body = parse(`
324
+ function wrapper () {
325
+ if (${channelVariable}.asyncEnd.hasSubscribers && (!this.streaming || !this.active)) {
326
+ const statement = ${POSTGRES_STATEMENT};
327
+ ${channelVariable}.asyncEnd.publish({ query: this, statement, pid: this.state?.pid });
328
+ }
329
+ return undefined;
330
+ }
331
+ `).body[0].body
332
+ resolution.body = body
333
+ resolution.expression = false
334
+ body.body.at(-1).argument = originalBody
335
+ }
336
+
337
+ /**
338
+ * @param {import('estree').AssignmentExpression} assignment
339
+ * @param {string} channelVariable
340
+ */
341
+ function wrapPostgresRejection (assignment, channelVariable) {
342
+ const rejection = assignment.right
343
+ assert(
344
+ rejection.type === 'ArrowFunctionExpression' &&
345
+ rejection.body.type !== 'BlockStatement',
346
+ 'postgresQueryLifecycle: reject function changed'
347
+ )
348
+ const errorParameter = rejection.params[0]
349
+ assert(errorParameter?.type === 'Identifier', 'postgresQueryLifecycle: reject parameter changed')
350
+
351
+ const originalBody = rejection.body
352
+ const body = parse(`
353
+ function wrapper () {
354
+ if (${channelVariable}.error.hasSubscribers || ${channelVariable}.asyncEnd.hasSubscribers) {
355
+ const statement = ${POSTGRES_STATEMENT};
356
+ const __ddTraceContext = { query: this, error: ${errorParameter.name}, statement, pid: this.state?.pid };
357
+ if (${channelVariable}.error.hasSubscribers) {
358
+ ${channelVariable}.error.publish(__ddTraceContext);
359
+ }
360
+ if (${channelVariable}.asyncEnd.hasSubscribers) {
361
+ ${channelVariable}.asyncEnd.publish(__ddTraceContext);
362
+ }
363
+ }
364
+ return undefined;
365
+ }
366
+ `).body[0].body
367
+ rejection.body = body
368
+ rejection.expression = false
369
+ body.body.at(-1).argument = originalBody
370
+ }
371
+
372
+ /**
373
+ * @param {object} state
374
+ * @param {import('estree').Program} program
375
+ */
376
+ function injectPostgresTracingChannel (state, program) {
377
+ state.transforms.tracingChannelDeclaration(state, program)
378
+
379
+ const channelName = `orchestrion:${state.module.name}:${state.channelName}`
380
+ const declarations = query(
381
+ program,
382
+ 'VariableDeclarator[init.type="CallExpression"][init.callee.name="tr_ch_apm_tracingChannel"]'
383
+ ).filter(({ init }) => init.arguments[0]?.value === channelName)
384
+
385
+ assert(declarations.length === 1, 'postgres: tracing channel declaration changed')
386
+ assert(declarations[0].id.type === 'Identifier', 'postgres: tracing channel identifier changed')
387
+ return declarations[0].id.name
388
+ }