dd-trace 6.25.0 → 6.27.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 (190) hide show
  1. package/README.md +3 -0
  2. package/ci/cypress/support.js +2 -1
  3. package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
  4. package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
  5. package/ci/vitest-no-worker-init-setup.mjs +729 -724
  6. package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
  7. package/index.d.ts +58 -1
  8. package/initialize.mjs +5 -3
  9. package/package.json +36 -32
  10. package/packages/datadog-esbuild/index.js +8 -15
  11. package/packages/datadog-esbuild/src/log.js +2 -29
  12. package/packages/datadog-instrumentations/src/ai.js +88 -122
  13. package/packages/datadog-instrumentations/src/aws-lambda.js +9 -0
  14. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  15. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +23 -1
  16. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  17. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +9 -6
  18. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  19. package/packages/datadog-instrumentations/src/helpers/hooks.js +0 -1
  20. package/packages/datadog-instrumentations/src/helpers/instrument.js +1 -3
  21. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +6 -0
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +12 -1
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/ai.js +2 -0
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +5 -3
  25. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  26. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  27. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +21 -0
  28. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  29. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +30 -0
  30. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +6 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  32. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  33. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  34. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +32 -2
  35. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +43 -0
  36. package/packages/datadog-instrumentations/src/next.js +184 -20
  37. package/packages/datadog-instrumentations/src/path-to-regexp.js +11 -11
  38. package/packages/datadog-instrumentations/src/router.js +52 -13
  39. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +1 -1
  40. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  41. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  42. package/packages/datadog-plugin-aws-lambda/src/context/events.js +167 -0
  43. package/packages/datadog-plugin-aws-lambda/src/context/extractor.js +153 -0
  44. package/packages/datadog-plugin-aws-lambda/src/context/http.js +50 -0
  45. package/packages/datadog-plugin-aws-lambda/src/context/span-context.js +26 -0
  46. package/packages/datadog-plugin-aws-lambda/src/context/step-functions.js +142 -0
  47. package/packages/datadog-plugin-aws-lambda/src/context/types.d.ts +17 -0
  48. package/packages/datadog-plugin-aws-lambda/src/context/xray.js +67 -0
  49. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  50. package/packages/datadog-plugin-aws-lambda/src/index.js +52 -4
  51. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  52. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  53. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  54. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  55. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  56. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  57. package/packages/datadog-plugin-azure-functions/src/index.js +68 -12
  58. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +30 -4
  59. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  60. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  61. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  62. package/packages/datadog-plugin-fetch/src/index.js +7 -0
  63. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -0
  64. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -0
  65. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +7 -0
  66. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  67. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  68. package/packages/datadog-plugin-http/src/client.js +14 -1
  69. package/packages/datadog-plugin-http/src/server.js +18 -4
  70. package/packages/datadog-plugin-http2/src/client.js +2 -1
  71. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  72. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  73. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  74. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  75. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  76. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  77. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  78. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  79. package/packages/datadog-plugin-ws/src/close.js +3 -5
  80. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  81. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  82. package/packages/datadog-plugin-ws/src/server.js +44 -13
  83. package/packages/datadog-vite/index.js +537 -0
  84. package/packages/datadog-vite/src/log.js +5 -0
  85. package/packages/datadog-webpack/src/log.js +2 -29
  86. package/packages/dd-trace/src/aiguard/client.js +7 -9
  87. package/packages/dd-trace/src/aiguard/evaluation.js +7 -2
  88. package/packages/dd-trace/src/aiguard/index.js +1 -1
  89. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  90. package/packages/dd-trace/src/aiguard/messages/utils.js +2 -2
  91. package/packages/dd-trace/src/aiguard/sdk.js +7 -2
  92. package/packages/dd-trace/src/appsec/api_security/normalized-route.js +273 -167
  93. package/packages/dd-trace/src/appsec/blocking/index.js +17 -1
  94. package/packages/dd-trace/src/appsec/channels.js +8 -0
  95. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  96. package/packages/dd-trace/src/appsec/graphql.js +10 -13
  97. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  98. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  99. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  100. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  101. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +21 -14
  102. package/packages/dd-trace/src/appsec/index.js +9 -0
  103. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  104. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  105. package/packages/dd-trace/src/appsec/rasp/index.js +64 -3
  106. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  107. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  108. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  109. package/packages/dd-trace/src/appsec/rasp/utils.js +47 -3
  110. package/packages/dd-trace/src/appsec/recommended.json +70 -3
  111. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  112. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  113. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  114. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  115. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  116. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  117. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  118. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  119. package/packages/dd-trace/src/carrier.js +7 -1
  120. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js +4 -2
  121. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +31 -19
  122. package/packages/dd-trace/src/config/generated-config-types.d.ts +14 -5
  123. package/packages/dd-trace/src/config/index.js +73 -27
  124. package/packages/dd-trace/src/config/parsers.js +60 -0
  125. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  126. package/packages/dd-trace/src/config/remote_config.js +2 -1
  127. package/packages/dd-trace/src/config/supported-configurations.json +41 -13
  128. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  129. package/packages/dd-trace/src/datastreams/http.js +57 -0
  130. package/packages/dd-trace/src/datastreams/index.js +24 -0
  131. package/packages/dd-trace/src/datastreams/pathway.js +142 -28
  132. package/packages/dd-trace/src/datastreams/processor.js +27 -7
  133. package/packages/dd-trace/src/debugger/config.js +1 -1
  134. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  135. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  136. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  137. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  138. package/packages/dd-trace/src/debugger/index.js +2 -2
  139. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  140. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +4 -1
  141. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  142. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  143. package/packages/dd-trace/src/git_metadata.js +2 -1
  144. package/packages/dd-trace/src/index.js +6 -3
  145. package/packages/dd-trace/src/lambda/facade.js +6 -5
  146. package/packages/dd-trace/src/lambda/handler-paths.js +1 -1
  147. package/packages/dd-trace/src/llmobs/constants/tags.js +1 -0
  148. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +86 -3
  149. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +182 -1
  150. package/packages/dd-trace/src/llmobs/span_processor.js +3 -0
  151. package/packages/dd-trace/src/llmobs/tagger.js +9 -0
  152. package/packages/dd-trace/src/noop/span.js +0 -4
  153. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +3 -0
  154. package/packages/dd-trace/src/openfeature/configuration_source.js +20 -0
  155. package/packages/dd-trace/src/openfeature/flagging_provider.js +17 -0
  156. package/packages/dd-trace/src/opentelemetry/logs/otlp_transformer.js +2 -1
  157. package/packages/dd-trace/src/opentelemetry/metrics/otlp_transformer.js +2 -1
  158. package/packages/dd-trace/src/opentelemetry/otlp/otlp_transformer_base.js +16 -1
  159. package/packages/dd-trace/src/opentelemetry/trace/index.js +5 -4
  160. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +10 -6
  161. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +42 -26
  162. package/packages/dd-trace/src/opentracing/propagation/text_map.js +4 -4
  163. package/packages/dd-trace/src/plugin_manager.js +2 -0
  164. package/packages/dd-trace/src/plugins/ci_plugin.js +104 -9
  165. package/packages/dd-trace/src/plugins/index.js +2 -0
  166. package/packages/dd-trace/src/plugins/util/ci.js +34 -0
  167. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +9 -14
  168. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +3 -5
  169. package/packages/dd-trace/src/plugins/util/test.js +28 -1
  170. package/packages/dd-trace/src/plugins/util/url.js +9 -6
  171. package/packages/dd-trace/src/plugins/util/user-provided-git.js +4 -4
  172. package/packages/dd-trace/src/process-tags/index.js +1 -1
  173. package/packages/dd-trace/src/profiler.js +74 -12
  174. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  175. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  176. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  177. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  178. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  179. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  180. package/packages/dd-trace/src/proxy.js +13 -1
  181. package/packages/dd-trace/src/register-loader.js +43 -0
  182. package/register.js +10 -44
  183. package/register.mjs +27 -0
  184. package/vendor/dist/@opentelemetry/core/LICENSE +11 -0
  185. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  186. package/vite.d.ts +7 -0
  187. package/vite.js +3 -0
  188. package/lambda.d.ts +0 -39
  189. package/lambda.js +0 -3
  190. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
@@ -39,10 +39,15 @@ function getProtocol (req) {
39
39
  return req.socket?.encrypted ? 'https' : 'http'
40
40
  }
41
41
 
42
+ /**
43
+ * @typedef {object} NormalizedUrlConfig
44
+ * @property {boolean | RegExp} queryStringObfuscation
45
+ */
46
+
42
47
  /**
43
48
  * Obfuscate query string
44
49
  *
45
- * @param {object} config
50
+ * @param {NormalizedUrlConfig} config
46
51
  * @param {string} url
47
52
  */
48
53
  function obfuscateQs (config, url) {
@@ -115,16 +120,14 @@ function getQsObfuscator (config) {
115
120
  * Callers gate this behind `DD_TRACE_OTEL_SEMANTICS_ENABLED`, so the default
116
121
  * (flag off) hot path stays a plain tag assignment.
117
122
  *
118
- * @param {{ queryStringObfuscation?: boolean | string }} config
123
+ * @param {NormalizedUrlConfig} config
119
124
  * @param {string} base `scheme://host[:port]`
120
125
  * @param {string} [pathname] raw request path, may include `?query`
121
126
  * @param {string} strippedUrl `base` + query-stripped path (used when there is no query)
122
127
  */
123
128
  function buildClientHttpUrl (config, base, pathname, strippedUrl) {
124
129
  if (pathname?.includes('?')) {
125
- // `config.queryStringObfuscation` is the raw config value here (client plugins
126
- // don't normalize it the way the server does), so compile it first.
127
- return obfuscateQs({ queryStringObfuscation: getQsObfuscator(config) }, `${base}${pathname}`)
130
+ return obfuscateQs(config, `${base}${pathname}`)
128
131
  }
129
132
  return strippedUrl
130
133
  }
@@ -140,7 +143,7 @@ function buildClientHttpUrl (config, base, pathname, strippedUrl) {
140
143
  */
141
144
  function extractPathFromUrl (url) {
142
145
  if (!url) return '/'
143
- const match = url.match(PATH_REGEX)
146
+ const match = PATH_REGEX.exec(url)
144
147
 
145
148
  return match?.groups?.path || '/'
146
149
  }
@@ -18,7 +18,7 @@ const {
18
18
  GIT_COMMIT_HEAD_SHA,
19
19
  } = require('./tags')
20
20
 
21
- const { normalizeRef } = require('./ci')
21
+ const { normalizeCommitSha, normalizeRef } = require('./ci')
22
22
  const { filterSensitiveInfoFromRepository, isScpStyleRepositoryUrl } = require('./url')
23
23
 
24
24
  function removeEmptyValues (tagsAndValues) {
@@ -73,7 +73,7 @@ function getUserProviderGitMetadata () {
73
73
 
74
74
  // Key value pairs are grouped in pairs of two
75
75
  return removeEmptyValues([
76
- GIT_COMMIT_SHA, DD_GIT_COMMIT_SHA,
76
+ GIT_COMMIT_SHA, normalizeCommitSha(DD_GIT_COMMIT_SHA),
77
77
  GIT_BRANCH, branch,
78
78
  GIT_REPOSITORY_URL, filterSensitiveInfoFromRepository(DD_GIT_REPOSITORY_URL),
79
79
  GIT_TAG, tag,
@@ -85,8 +85,8 @@ function getUserProviderGitMetadata () {
85
85
  GIT_COMMIT_AUTHOR_EMAIL, DD_GIT_COMMIT_AUTHOR_EMAIL,
86
86
  GIT_COMMIT_AUTHOR_DATE, DD_GIT_COMMIT_AUTHOR_DATE,
87
87
  GIT_PULL_REQUEST_BASE_BRANCH, DD_GIT_PULL_REQUEST_BASE_BRANCH,
88
- GIT_PULL_REQUEST_BASE_BRANCH_SHA, DD_GIT_PULL_REQUEST_BASE_BRANCH_SHA,
89
- GIT_COMMIT_HEAD_SHA, DD_GIT_COMMIT_HEAD_SHA,
88
+ GIT_PULL_REQUEST_BASE_BRANCH_SHA, normalizeCommitSha(DD_GIT_PULL_REQUEST_BASE_BRANCH_SHA),
89
+ GIT_COMMIT_HEAD_SHA, normalizeCommitSha(DD_GIT_COMMIT_HEAD_SHA),
90
90
  ])
91
91
  }
92
92
 
@@ -72,7 +72,7 @@ function buildProcessTags (config) {
72
72
  // Singleton with constant defaults so pre-init reads don't blow up
73
73
  const processTags = module.exports = {
74
74
  /**
75
- * @param {import('../config/config-base')} config
75
+ * @param {import('../config/config-base')} [config]
76
76
  */
77
77
  initialize (config) {
78
78
  // check if one of the properties added during build exist and if so return
@@ -6,6 +6,18 @@ const log = require('./log')
6
6
 
7
7
  const configUpdateChannel = dc.channel('datadog:config:update')
8
8
 
9
+ // The profiler reads these settings at startup, regardless of which RC product supplies them.
10
+ const RESTART_SETTINGS = /** @type {const} */ ([
11
+ 'DD_PROFILING_CPU_ENABLED',
12
+ 'DD_PROFILING_WALLTIME_ENABLED',
13
+ 'DD_PROFILING_HEAP_ENABLED',
14
+ 'DD_PROFILING_ALLOCATION_ENABLED',
15
+ 'DD_PROFILING_UPLOAD_PERIOD',
16
+ ])
17
+
18
+ /** @type {ReturnType<typeof getRemotelyConfigurableSettings> | undefined} */
19
+ let startedSettings
20
+
9
21
  /** @type {import('./profiling/ssi-heuristics').SSIHeuristics | undefined} */
10
22
  let activeSSIHeuristics
11
23
 
@@ -20,6 +32,9 @@ let profilingModule
20
32
  */
21
33
  let deferredConfig
22
34
 
35
+ // The profiler is stopping to apply new settings, not to stay disabled.
36
+ let restartPending = false
37
+
23
38
  function getProfilingModule () {
24
39
  if (profilingModule === undefined) {
25
40
  profilingModule = require('./profiling')
@@ -38,6 +53,16 @@ function isStarted () {
38
53
  return profilingModule !== undefined && profilingModule.profiler.enabled
39
54
  }
40
55
 
56
+ /**
57
+ * Types of the running profilers (see the profiling layer's getRunningTypes()). Like isStarted(),
58
+ * this doesn't load the profiling engine just to answer.
59
+ *
60
+ * @returns {string[]}
61
+ */
62
+ function getRunningProfileTypes () {
63
+ return profilingModule?.profiler.getRunningTypes() ?? []
64
+ }
65
+
41
66
  // A stopped profiler still exports one final profile, and until that settles it reports neither
42
67
  // the state a decision would be based on nor any willingness to start again.
43
68
  function isStopping () {
@@ -62,6 +87,7 @@ function disableSSIHeuristics () {
62
87
  * @param {import('./config/config-base')} config - Tracer configuration
63
88
  */
64
89
  function start (config) {
90
+ startedSettings = getRemotelyConfigurableSettings(config)
65
91
  try {
66
92
  // Forward the full tracer config to the profiling layer.
67
93
  // Profiling code is responsible for deriving the specific options it needs.
@@ -79,6 +105,7 @@ function stop () {
79
105
  // An explicit stop is the latest desired state, so it retracts a configuration still waiting
80
106
  // for an in-flight shutdown to settle.
81
107
  deferredConfig = undefined
108
+ restartPending = false
82
109
 
83
110
  // A stop command for a profiler that has never been loaded is already satisfied.
84
111
  if (profilingModule === undefined) return
@@ -115,35 +142,70 @@ function runWithLabels (labels, fn) {
115
142
  return getProfilingModule().profiler.runWithLabels(labels, fn)
116
143
  }
117
144
 
145
+ /**
146
+ * @param {import('./config/config-base')} config - Tracer configuration
147
+ */
148
+ function getRemotelyConfigurableSettings (config) {
149
+ return RESTART_SETTINGS.map(name => config[name])
150
+ }
151
+
152
+ /**
153
+ * Settings are read at startup, so changes require a restart and may leave a profile gap.
154
+ *
155
+ * @param {import('./config/config-base')} config - Tracer configuration
156
+ */
157
+ function restartIfSettingsChanged (config) {
158
+ const settings = getRemotelyConfigurableSettings(config)
159
+ if (settings.every((value, index) => value === startedSettings?.[index])) return
160
+
161
+ log.debug('Restarting the profiler to apply changed settings')
162
+ stop()
163
+ if (isStopping()) {
164
+ // The 'stopped' event applies the config after the final export.
165
+ deferredConfig = config
166
+ restartPending = true
167
+ } else {
168
+ start(config)
169
+ }
170
+ }
171
+
118
172
  function applyDeferredConfig () {
119
173
  const config = deferredConfig
120
174
  if (config === undefined) return
121
175
  deferredConfig = undefined
122
- applyConfig(config)
176
+ const restarting = restartPending
177
+ restartPending = false
178
+ applyConfig(config, restarting)
123
179
  }
124
180
 
125
181
  /**
126
182
  * @param {import('./config/config-base')} config - Tracer configuration
183
+ * @param {boolean} [restarting] - Resume a run stopped for a settings change
127
184
  */
128
- function applyConfig (config) {
185
+ function applyConfig (config, restarting = false) {
129
186
  const enabled = config.profiling.DD_PROFILING_ENABLED
130
187
  if (enabled === 'true') {
131
188
  // A non-auto value means the SSI heuristics no longer get a say; disable them so a trigger that
132
189
  // fires after this publish can't start the profiler behind this decision's back.
133
190
  disableSSIHeuristics()
134
- // Leave an already-running profiler alone; otherwise an unrelated remote-config publish
135
- // (e.g. an unrelated sampling-rate change) would restart it on every update.
136
- if (!isStarted()) start(config)
191
+ // Unrelated config updates must not restart the profiler.
192
+ if (isStarted()) {
193
+ restartIfSettingsChanged(config)
194
+ } else {
195
+ start(config)
196
+ }
137
197
  } else if (enabled === 'false') {
138
198
  disableSSIHeuristics()
139
199
  stop()
140
200
  } else if (enabled === 'auto') {
141
- if (!isStarted() && !activeSSIHeuristics) {
142
- // 'auto' defers the start decision to SSI heuristics. A running profiler already reflects a
143
- // decision that was made (by SSI or a prior unconditional enablement), so leave it alone
144
- // rather than stopping and re-enabling it on every subsequent config publication. Also guard
145
- // against creating another active instance; each SSIHeuristics instance owns listeners and a
146
- // timer until the heuristic makes its decision or is explicitly disabled.
201
+ // A running or restarting profiler has already passed the start decision; don't
202
+ // re-run SSI heuristics on config updates.
203
+ if (isStarted()) {
204
+ restartIfSettingsChanged(config)
205
+ } else if (restarting) {
206
+ start(config)
207
+ } else if (!activeSSIHeuristics) {
208
+ // Reuse active heuristics; each instance owns listeners and a timer.
147
209
  const { SSIHeuristics } = getSSIHeuristicsModule()
148
210
  const heuristics = new SSIHeuristics(config)
149
211
  activeSSIHeuristics = heuristics
@@ -182,4 +244,4 @@ configUpdateChannel.subscribe((config) => {
182
244
 
183
245
  globalThis[Symbol.for('dd-trace')].beforeExitHandlers.add(stop)
184
246
 
185
- module.exports = { isStarted, start, stop, setCustomLabelKeys, runWithLabels }
247
+ module.exports = { isStarted, getRunningProfileTypes, start, stop, setCustomLabelKeys, runWithLabels }
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { EventEmitter } = require('events')
4
+ const { setImmediate: yieldToEventLoop } = require('node:timers/promises')
4
5
  const dc = require('dc-polyfill')
5
6
  const crashtracker = require('../crashtracking')
6
7
  const log = require('../log')
@@ -94,6 +95,24 @@ class Profiler extends EventEmitter {
94
95
  return this.#stopping !== undefined
95
96
  }
96
97
 
98
+ /**
99
+ * Types of the running profilers (e.g. 'wall', 'space', 'events'), plus 'cpu' when the wall
100
+ * profiler also collects CPU time and 'allocations' when the space profiler also collects
101
+ * allocations. Empty while the profiler isn't running.
102
+ *
103
+ * @returns {string[]}
104
+ */
105
+ getRunningTypes () {
106
+ if (!this.#enabled) return []
107
+ const types = []
108
+ for (const profiler of this.#profilers) {
109
+ types.push(profiler.type)
110
+ if (profiler.collectsCpuTime) types.push('cpu')
111
+ if (profiler.collectsAllocations) types.push('allocations')
112
+ }
113
+ return types
114
+ }
115
+
97
116
  /**
98
117
  * Declares the set of custom label keys that will be used with
99
118
  * {@link runWithLabels}. This is used for profile upload metadata and
@@ -424,7 +443,7 @@ class Profiler extends EventEmitter {
424
443
  const compressionFn = this.#getCompressionFn()
425
444
 
426
445
  // encode and export asynchronously
427
- await Promise.all(profiles.map(async ({ profiler, profile, info }) => {
446
+ const encodeProfile = async ({ profiler, profile, info }) => {
428
447
  try {
429
448
  const encoded = await profiler.encode(profile)
430
449
  const compressed = encoded instanceof Buffer && compressionFn !== undefined
@@ -448,7 +467,16 @@ class Profiler extends EventEmitter {
448
467
  // encode and submit the other profile types.
449
468
  log.error(error)
450
469
  }
451
- }))
470
+ }
471
+
472
+ const encodings = []
473
+ for (const profile of profiles) {
474
+ // Separate synchronous encoder prefixes without waiting for compression/export to finish.
475
+ // eslint-disable-next-line no-await-in-loop -- Encoder starts must yield, not their entire async tasks.
476
+ await yieldToEventLoop()
477
+ encodings.push(encodeProfile(profile))
478
+ }
479
+ await Promise.all(encodings)
452
480
 
453
481
  if (hasEncoded) {
454
482
  await this.#submit(encodedProfiles, infos, startDate, endDate, snapshotTags, snapshotKind)
@@ -17,6 +17,7 @@ const STACK_DEPTH = 64
17
17
  const identityRefreshChannel = channel('datadog:identity:refresh')
18
18
 
19
19
  class NativeSpaceProfiler {
20
+ collectsAllocations = false
20
21
  #config
21
22
  #exporters
22
23
  #identityRefreshListener
@@ -46,7 +47,8 @@ class NativeSpaceProfiler {
46
47
 
47
48
  this.#mapper = mapper
48
49
  this.#pprof = require('@datadog/pprof')
49
- this.#pprof.heap.start(this.#samplingInterval, STACK_DEPTH, this.#config.DD_PROFILING_ALLOCATION_ENABLED)
50
+ this.collectsAllocations = this.#config.DD_PROFILING_ALLOCATION_ENABLED
51
+ this.#pprof.heap.start(this.#samplingInterval, STACK_DEPTH, this.collectsAllocations)
50
52
 
51
53
  if (this.#config.DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED) {
52
54
  const config = this.#config
@@ -64,7 +64,7 @@ class NativeWallProfiler {
64
64
  #captureSpanData = false
65
65
  #codeHotspotsEnabled = false
66
66
  #contextCountGaugeUpdater
67
- #cpuProfilingEnabled = false
67
+ collectsCpuTime = false
68
68
  #currentContext
69
69
  #customLabelsActive = false
70
70
  #customLabelKeys
@@ -96,7 +96,7 @@ class NativeWallProfiler {
96
96
  constructor (config, { asyncContextFrameEnabled, flushInterval }) {
97
97
  this.#asyncContextFrameEnabled = asyncContextFrameEnabled
98
98
  this.#codeHotspotsEnabled = config.DD_PROFILING_CODEHOTSPOTS_ENABLED
99
- this.#cpuProfilingEnabled = config.DD_PROFILING_CPU_ENABLED
99
+ this.collectsCpuTime = config.DD_PROFILING_CPU_ENABLED
100
100
  this.#endpointCollectionEnabled = config.DD_PROFILING_ENDPOINT_COLLECTION_ENABLED
101
101
  this.#flushIntervalMillis = flushInterval
102
102
  this.#samplingIntervalMicros = SAMPLING_INTERVAL * 1000
@@ -111,7 +111,7 @@ class NativeWallProfiler {
111
111
  // capturing span data or timeline is enabled (so we need sample timestamps, and for now
112
112
  // timestamps require the sample contexts feature in the pprof wall profiler), or
113
113
  // cpu profiling is enabled.
114
- this.#withContexts = this.#captureSpanData || this.#timelineEnabled || this.#cpuProfilingEnabled
114
+ this.#withContexts = this.#captureSpanData || this.#timelineEnabled || this.collectsCpuTime
115
115
  }
116
116
 
117
117
  codeHotspotsEnabled () {
@@ -130,7 +130,7 @@ class NativeWallProfiler {
130
130
  kSampleCount = this.#pprof.time.constants.kSampleCount
131
131
 
132
132
  this.#pprof.time.start({
133
- collectCpuTime: this.#cpuProfilingEnabled,
133
+ collectCpuTime: this.collectsCpuTime,
134
134
  columnNumbers: 'emit',
135
135
  durationMillis: this.#flushIntervalMillis,
136
136
  intervalMicros: this.#samplingIntervalMicros,