dd-trace 6.25.0 → 6.26.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 (141) 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/package.json +33 -30
  8. package/packages/datadog-esbuild/index.js +8 -15
  9. package/packages/datadog-esbuild/src/log.js +2 -29
  10. package/packages/datadog-instrumentations/src/ai.js +6 -1
  11. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  12. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +23 -1
  13. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  14. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +9 -6
  15. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  16. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +6 -0
  17. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +8 -0
  18. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  19. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  20. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +21 -0
  21. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +30 -0
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +6 -0
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  25. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  26. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  27. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
  28. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
  29. package/packages/datadog-instrumentations/src/next.js +184 -20
  30. package/packages/datadog-instrumentations/src/router.js +35 -12
  31. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  32. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  33. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  34. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  35. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  36. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  37. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  38. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  39. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  40. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +30 -4
  41. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  42. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  43. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  44. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -0
  45. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -0
  46. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +7 -0
  47. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  48. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  49. package/packages/datadog-plugin-http/src/client.js +2 -1
  50. package/packages/datadog-plugin-http2/src/client.js +2 -1
  51. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  52. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  53. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  54. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  55. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  56. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  57. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  58. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  59. package/packages/datadog-plugin-ws/src/close.js +3 -5
  60. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  61. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  62. package/packages/datadog-plugin-ws/src/server.js +44 -13
  63. package/packages/datadog-vite/index.js +537 -0
  64. package/packages/datadog-vite/src/log.js +5 -0
  65. package/packages/datadog-webpack/src/log.js +2 -29
  66. package/packages/dd-trace/src/aiguard/index.js +1 -1
  67. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  68. package/packages/dd-trace/src/appsec/blocking/index.js +17 -1
  69. package/packages/dd-trace/src/appsec/channels.js +7 -0
  70. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  71. package/packages/dd-trace/src/appsec/graphql.js +10 -13
  72. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  73. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  74. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  75. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  76. package/packages/dd-trace/src/appsec/index.js +6 -0
  77. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  78. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  79. package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
  80. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  81. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  82. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  83. package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
  84. package/packages/dd-trace/src/appsec/recommended.json +70 -3
  85. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  86. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  87. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  88. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  89. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  90. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  91. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  92. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  93. package/packages/dd-trace/src/config/generated-config-types.d.ts +4 -2
  94. package/packages/dd-trace/src/config/index.js +46 -26
  95. package/packages/dd-trace/src/config/parsers.js +35 -0
  96. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  97. package/packages/dd-trace/src/config/remote_config.js +2 -1
  98. package/packages/dd-trace/src/config/supported-configurations.json +14 -6
  99. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  100. package/packages/dd-trace/src/datastreams/pathway.js +113 -28
  101. package/packages/dd-trace/src/datastreams/processor.js +5 -4
  102. package/packages/dd-trace/src/debugger/config.js +1 -1
  103. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  104. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  105. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  106. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  107. package/packages/dd-trace/src/debugger/index.js +2 -2
  108. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  109. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +4 -1
  110. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  111. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  112. package/packages/dd-trace/src/git_metadata.js +2 -1
  113. package/packages/dd-trace/src/index.js +6 -3
  114. package/packages/dd-trace/src/lambda/facade.js +6 -5
  115. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
  116. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
  117. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +3 -0
  118. package/packages/dd-trace/src/openfeature/configuration_source.js +20 -0
  119. package/packages/dd-trace/src/plugins/ci_plugin.js +104 -9
  120. package/packages/dd-trace/src/plugins/index.js +2 -0
  121. package/packages/dd-trace/src/plugins/util/ci.js +34 -0
  122. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +9 -14
  123. package/packages/dd-trace/src/plugins/util/test.js +28 -1
  124. package/packages/dd-trace/src/plugins/util/url.js +8 -5
  125. package/packages/dd-trace/src/plugins/util/user-provided-git.js +4 -4
  126. package/packages/dd-trace/src/process-tags/index.js +1 -1
  127. package/packages/dd-trace/src/profiler.js +74 -12
  128. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  129. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  130. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  131. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  132. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  133. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  134. package/packages/dd-trace/src/proxy.js +13 -1
  135. package/vendor/dist/@opentelemetry/core/LICENSE +11 -0
  136. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  137. package/vite.d.ts +7 -0
  138. package/vite.js +3 -0
  139. package/lambda.d.ts +0 -39
  140. package/lambda.js +0 -3
  141. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
@@ -9,8 +9,8 @@ const web = require('../../dd-trace/src/plugins/util/web')
9
9
  class ExpressCodeOriginForSpansPlugin extends Plugin {
10
10
  static id = 'express'
11
11
 
12
- constructor (...args) {
13
- super(...args)
12
+ constructor (tracer, tracerConfig) {
13
+ super(tracer, tracerConfig)
14
14
 
15
15
  const layerTags = new WeakMap()
16
16
 
@@ -11,8 +11,8 @@ const kCodeOriginForSpansTagsSym = Symbol('datadog.codeOriginForSpansTags')
11
11
  class FastifyCodeOriginForSpansPlugin extends Plugin {
12
12
  static id = 'fastify'
13
13
 
14
- constructor (...args) {
15
- super(...args)
14
+ constructor (tracer, tracerConfig) {
15
+ super(tracer, tracerConfig)
16
16
 
17
17
  this.addSub('apm:fastify:request:handle', ({ req, routeConfig }) => {
18
18
  const tags = routeConfig?.[kCodeOriginForSpansTagsSym]
@@ -4,6 +4,7 @@ const { getHeadersSize, getSizeOrZero } = require('../../dd-trace/src/datastream
4
4
  const ConsumerPlugin = require('../../dd-trace/src/plugins/consumer')
5
5
  const { storage } = require('../../datadog-core')
6
6
  const reconstructPubSubRequestContext = require('./pubsub-request-context')
7
+ const { getSubscriptionName, getTopicName } = require('./resource-names')
7
8
 
8
9
  /**
9
10
  * PULL SUBSCRIPTION: Service explicitly pulls messages from Pub/Sub and processes them.
@@ -128,6 +129,15 @@ class GoogleCloudPubsubConsumerPlugin extends ConsumerPlugin {
128
129
  'messaging.operation': 'receive',
129
130
  }
130
131
 
132
+ // The topic is only known from cached subscription metadata or the name the producer propagated.
133
+ const topicFullName = getTopicName(topic)
134
+ if (topicFullName) meta['gcp.pubsub.topic.name'] = topicFullName
135
+ // Before 4.0 a subscription created with an auto-detected project keeps `{{projectId}}` in its name; the
136
+ // subscriber resolved it to open the stream that delivered this message.
137
+ const subscriptionFullName = getSubscriptionName(subscription.name) ??
138
+ getSubscriptionName(message._subscriber?.name)
139
+ if (subscriptionFullName) meta['gcp.pubsub.subscription.name'] = subscriptionFullName
140
+
131
141
  let batchContext
132
142
  if (batchRequestTraceId && batchRequestSpanId) {
133
143
  meta['pubsub.batch.request_trace_id'] = batchRequestTraceId
@@ -10,6 +10,7 @@ const { DsmPathwayCodec, getHeadersSize } = require('../../dd-trace/src/datastre
10
10
  const id = require('../../dd-trace/src/id')
11
11
  const SpanContext = require('../../dd-trace/src/opentracing/span_context')
12
12
  const ProducerPlugin = require('../../dd-trace/src/plugins/producer')
13
+ const { getTopicName } = require('./resource-names')
13
14
 
14
15
  class GoogleCloudPubsubProducerPlugin extends ProducerPlugin {
15
16
  static id = 'google-cloud-pubsub'
@@ -104,6 +105,7 @@ class GoogleCloudPubsubProducerPlugin extends ProducerPlugin {
104
105
  '_dd.serviceoverride.type': 'integration',
105
106
  'pubsub.linked_message_count': spanLinks.length || undefined,
106
107
  operation: messageCount > 1 ? 'batched.pubsub.request' : 'pubsub.request',
108
+ 'gcp.pubsub.topic.name': getTopicName(topic),
107
109
  },
108
110
  metrics: {
109
111
  'pubsub.batch.message_count': messageCount,
@@ -3,6 +3,7 @@
3
3
  const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
4
4
  const log = require('../../dd-trace/src/log')
5
5
  const reconstructPubSubRequestContext = require('./pubsub-request-context')
6
+ const { getSubscriptionName, getTopicName } = require('./resource-names')
6
7
 
7
8
  // WeakMap to track push receive spans by request
8
9
  const pushReceiveSpans = new WeakMap()
@@ -138,6 +139,12 @@ class GoogleCloudPubsubPushSubscriptionPlugin extends TracingPlugin {
138
139
  return null
139
140
  }
140
141
 
142
+ // The topic is only known from the name the producer propagated, never from the placeholder.
143
+ const topicFullName = getTopicName(attrs['pubsub.topic'])
144
+ if (topicFullName) span.setTag('gcp.pubsub.topic.name', topicFullName)
145
+ const subscriptionFullName = getSubscriptionName(subscription)
146
+ if (subscriptionFullName) span.setTag('gcp.pubsub.subscription.name', subscriptionFullName)
147
+
141
148
  span._integrationName = 'google-cloud-pubsub'
142
149
  // Calculate delivery latency (queue time from publish to delivery)
143
150
  if (publishStartTime) {
@@ -0,0 +1,24 @@
1
+ 'use strict'
2
+
3
+ // Full resource names: https://cloud.google.com/pubsub/docs/pubsub-basics#resource_names
4
+ // Project IDs never contain braces; the client library uses `{{projectId}}` until it resolved the project.
5
+ const TOPIC_NAME = /^projects\/[^/{}]+\/topics\/[^/]+$/
6
+ const SUBSCRIPTION_NAME = /^projects\/[^/{}]+\/subscriptions\/[^/]+$/
7
+
8
+ /**
9
+ * @param {unknown} value
10
+ * @returns {string | undefined} `value` when it is a full `projects/<project>/topics/<topic>` name.
11
+ */
12
+ function getTopicName (value) {
13
+ return typeof value === 'string' && TOPIC_NAME.test(value) ? value : undefined
14
+ }
15
+
16
+ /**
17
+ * @param {unknown} value
18
+ * @returns {string | undefined} `value` when it is a full `projects/<project>/subscriptions/<subscription>` name.
19
+ */
20
+ function getSubscriptionName (value) {
21
+ return typeof value === 'string' && SUBSCRIPTION_NAME.test(value) ? value : undefined
22
+ }
23
+
24
+ module.exports = { getSubscriptionName, getTopicName }
@@ -89,6 +89,7 @@ function transformForSignature (ast) {
89
89
  OperationDefinition (node) {
90
90
  return {
91
91
  ...node,
92
+ name: node.name ?? undefined,
92
93
  variableDefinitions: sortByVariableName(node.variableDefinitions),
93
94
  }
94
95
  },
@@ -14,7 +14,7 @@ const {
14
14
  otelHttpResourceName,
15
15
  } = require('../../dd-trace/src/plugins/util/http-otel-semantics')
16
16
  const { getClientStatusValidator } = require('../../dd-trace/src/plugins/util/status-validator')
17
- const { buildClientHttpUrl } = require('../../dd-trace/src/plugins/util/url')
17
+ const { buildClientHttpUrl, getQsObfuscator } = require('../../dd-trace/src/plugins/util/url')
18
18
  const { stripQueryAndFragment } = require('../../dd-trace/src/util')
19
19
  const { CLIENT_PORT_KEY, COMPONENT, ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
20
20
 
@@ -198,6 +198,7 @@ function normalizeClientConfig (config) {
198
198
 
199
199
  return {
200
200
  ...config,
201
+ queryStringObfuscation: config.DD_TRACE_OTEL_SEMANTICS_ENABLED && getQsObfuscator(config),
201
202
  validateStatus,
202
203
  filter,
203
204
  propagationFilter,
@@ -15,7 +15,7 @@ const {
15
15
  otelHttpResourceName,
16
16
  } = require('../../dd-trace/src/plugins/util/http-otel-semantics')
17
17
  const { getClientStatusValidator } = require('../../dd-trace/src/plugins/util/status-validator')
18
- const { buildClientHttpUrl } = require('../../dd-trace/src/plugins/util/url')
18
+ const { buildClientHttpUrl, getQsObfuscator } = require('../../dd-trace/src/plugins/util/url')
19
19
 
20
20
  const HTTP_HEADERS = formats.HTTP_HEADERS
21
21
  const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
@@ -197,6 +197,7 @@ function normalizeConfig (config) {
197
197
 
198
198
  return {
199
199
  ...config,
200
+ queryStringObfuscation: config.DD_TRACE_OTEL_SEMANTICS_ENABLED && getQsObfuscator(config),
200
201
  validateStatus,
201
202
  filter,
202
203
  headers,
@@ -5,6 +5,7 @@ const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
5
5
 
6
6
  const {
7
7
  DISTRIBUTED_TRACE_META_KEY,
8
+ setErrorTags,
8
9
  tagErrorResult,
9
10
  } = require('./utils')
10
11
 
@@ -133,6 +134,11 @@ class McpServerRequestPlugin extends McpPlugin {
133
134
  onEnd (span, ctx) {
134
135
  if (ctx.error) {
135
136
  span.setTag('error', ctx.error)
137
+ } else if (ctx.extra?.mcpReq?.signal?.aborted) {
138
+ // SDK 2.x drops the result of a handler whose connection closed, so the client never gets it.
139
+ setErrorTags(span, 'Connection closed')
140
+ } else if (ctx.result?.resultType === 'input_required') {
141
+ span.setTag('mcp.input_required', true)
136
142
  } else {
137
143
  tagErrorResult(span, ctx.result)
138
144
  }
@@ -160,14 +166,34 @@ class McpServerToolCallPlugin extends McpPlugin {
160
166
  onEnd (span, ctx) { tagErrorResult(span, ctx.result) }
161
167
  }
162
168
 
163
- module.exports = [
164
- McpPropagationPlugin,
169
+ class McpServerV2ToolCallPlugin extends McpServerToolCallPlugin {
170
+ static id = 'modelcontextprotocol_server_tool_v2'
171
+ static component = McpServerToolCallPlugin.id
172
+ static prefix = 'tracing:orchestrion:@modelcontextprotocol/server:McpServer_executeToolHandler'
173
+ }
174
+
175
+ const clientPlugins = [
165
176
  McpToolCallPlugin,
166
177
  McpListToolsPlugin,
167
178
  McpListResourcesPlugin,
168
179
  McpReadResourcePlugin,
169
180
  McpListPromptsPlugin,
170
181
  McpGetPromptPlugin,
182
+ ]
183
+
184
+ // SDK 2.x clients publish on the `@modelcontextprotocol/client` channels. The copies keep the 1.x component, so spans
185
+ // do not depend on the SDK major version.
186
+ const clientV2Plugins = clientPlugins.map(Plugin => class extends Plugin {
187
+ static id = `${Plugin.id}_v2`
188
+ static component = Plugin.id
189
+ static prefix = Plugin.prefix.replace('@modelcontextprotocol/sdk', '@modelcontextprotocol/client')
190
+ })
191
+
192
+ module.exports = [
193
+ McpPropagationPlugin,
194
+ ...clientPlugins,
195
+ ...clientV2Plugins,
171
196
  McpServerRequestPlugin,
172
197
  McpServerToolCallPlugin,
198
+ McpServerV2ToolCallPlugin,
173
199
  ]
@@ -26,5 +26,6 @@ function tagErrorResult (span, result) {
26
26
 
27
27
  module.exports = {
28
28
  DISTRIBUTED_TRACE_META_KEY,
29
+ setErrorTags,
29
30
  tagErrorResult,
30
31
  }
@@ -3,6 +3,7 @@
3
3
  const { isMap, isRegExp } = require('node:util').types
4
4
 
5
5
  const DatabasePlugin = require('../../dd-trace/src/plugins/database')
6
+ const { storage } = require('../../datadog-core')
6
7
 
7
8
  class MongodbCoreQueryPlugin extends DatabasePlugin {
8
9
  static id = 'mongodb-core'
@@ -31,7 +32,8 @@ class MongodbCoreQueryPlugin extends DatabasePlugin {
31
32
  const { ns, ops, options = {}, name } = ctx
32
33
  // heartbeat commands can be disabled if this.config.heartbeatEnabled is false
33
34
  if (!this.config.heartbeatEnabled && isHeartbeat(ops, this.config)) {
34
- return
35
+ ctx.parentStore = storage('legacy').getStore()
36
+ return ctx.parentStore
35
37
  }
36
38
  const query = getQuery(ops, this.config.obfuscateQuery)
37
39
  const resource = truncate(getResource(this, ns, query, name))
@@ -57,6 +59,20 @@ class MongodbCoreQueryPlugin extends DatabasePlugin {
57
59
  return ctx.currentStore
58
60
  }
59
61
 
62
+ /**
63
+ * @param {{ currentStore?: { span: import('../../..').Span } }} ctx
64
+ */
65
+ finish (ctx) {
66
+ if (ctx.currentStore) super.finish(ctx)
67
+ }
68
+
69
+ /**
70
+ * @param {{ currentStore?: { span: import('../../..').Span }, error?: unknown }} ctx
71
+ */
72
+ error (ctx) {
73
+ if (ctx.currentStore) super.error(ctx)
74
+ }
75
+
60
76
  /**
61
77
  * @override
62
78
  */
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { performance } = require('node:perf_hooks')
4
+ const { Readable, Writable } = require('node:stream')
4
5
 
5
6
  const { CLIENT_PORT_KEY } = require('../../dd-trace/src/constants')
6
7
  const DatabasePlugin = require('../../dd-trace/src/plugins/database')
@@ -8,10 +9,11 @@ const Plugin = require('../../dd-trace/src/plugins/plugin')
8
9
 
9
10
  const ACQUIRE_START_CHANNEL = 'tracing:orchestrion:postgres:query:acquire:start'
10
11
  const PREPARE_START_CHANNEL = 'tracing:orchestrion:postgres:query:prepare:start'
12
+ const COPY_START_CHANNEL = 'tracing:orchestrion:postgres:query:copy:start'
11
13
 
12
14
  /**
13
15
  * @typedef {object} PostgresContext
14
- * @property {{ span: import('../../..').Span }} currentStore
16
+ * @property {PostgresStore} currentStore
15
17
  * @property {string} database
16
18
  * @property {unknown} [error]
17
19
  * @property {string} [host]
@@ -23,16 +25,36 @@ const PREPARE_START_CHANNEL = 'tracing:orchestrion:postgres:query:prepare:start'
23
25
  * @property {string} [statement]
24
26
  * @property {string} user
25
27
  *
26
- * @typedef {object} PostgresDbmState
27
- * @property {string} service
28
+ * @typedef {object} PostgresStore
29
+ * @property {import('../../..').Span} span
30
+ * @property {string} [dbmService]
28
31
  * @property {string} [statement]
29
32
  *
30
33
  * @typedef {object} PostgresPreparationContext
34
+ * @property {string} [host]
35
+ * @property {number} [port]
31
36
  * @property {boolean} prepared
32
- * @property {PostgresQuery} query
37
+ * @property {PostgresQuery | NativePostgresContext} query
38
+ * @property {string} [resource]
33
39
  * @property {string} statement
34
40
  *
41
+ * @typedef {object} PostgresCopyContext
42
+ * @property {Error | null} error
43
+ * @property {NativePostgresContext} query
44
+ *
35
45
  * @typedef {object} PostgresQuery
46
+ *
47
+ * @typedef {object} NativePostgresContext
48
+ * @property {PostgresStore} [currentStore]
49
+ * @property {string} database
50
+ * @property {Error} [error]
51
+ * @property {string | null} host
52
+ * @property {number | null} port
53
+ * @property {string | null} query
54
+ * @property {number} queued
55
+ * @property {unknown} [result]
56
+ * @property {number | null} sent
57
+ * @property {string} user
36
58
  */
37
59
 
38
60
  class PostgresPreparationPlugin extends Plugin {
@@ -49,6 +71,20 @@ class PostgresPreparationPlugin extends Plugin {
49
71
  }
50
72
  }
51
73
 
74
+ class PostgresCopyPlugin extends Plugin {
75
+ static id = 'postgres'
76
+
77
+ /**
78
+ * @param {(ctx: PostgresCopyContext) => void} finish
79
+ * @param {object} tracer
80
+ * @param {import('../../dd-trace/src/config/config-base')} tracerConfig
81
+ */
82
+ constructor (finish, tracer, tracerConfig) {
83
+ super(tracer, tracerConfig)
84
+ this.addSub(COPY_START_CHANNEL, finish)
85
+ }
86
+ }
87
+
52
88
  class PostgresPlugin extends DatabasePlugin {
53
89
  static id = 'postgres'
54
90
  static prefix = 'tracing:orchestrion:postgres:query'
@@ -56,8 +92,8 @@ class PostgresPlugin extends DatabasePlugin {
56
92
  /** @type {boolean} */
57
93
  #dbmEnabled = false
58
94
 
59
- /** @type {WeakMap<PostgresQuery, PostgresDbmState>} */
60
- #dbmQueries = new WeakMap()
95
+ /** @type {WeakMap<PostgresQuery, PostgresContext>} */
96
+ #contexts = new WeakMap()
61
97
 
62
98
  /** @type {PostgresPreparationPlugin} */
63
99
  #preparation
@@ -65,12 +101,6 @@ class PostgresPlugin extends DatabasePlugin {
65
101
  /** @type {{ name: string, source: string | undefined }} */
66
102
  #service
67
103
 
68
- /** @type {WeakMap<PostgresQuery, import('../../..').Span>} */
69
- #spans = new WeakMap()
70
-
71
- /** @type {WeakMap<PostgresQuery, PostgresContext>} */
72
- #waitContexts = new WeakMap()
73
-
74
104
  /**
75
105
  * @param {object} tracer
76
106
  * @param {import('../../dd-trace/src/config/config-base')} tracerConfig
@@ -78,7 +108,22 @@ class PostgresPlugin extends DatabasePlugin {
78
108
  constructor (tracer, tracerConfig) {
79
109
  super(tracer, tracerConfig)
80
110
  this.addSub(ACQUIRE_START_CHANNEL, ctx => this.#finishPoolWait(ctx.query))
111
+ this.addBind('tracing:postgres:query:start', ctx => this.#nativeStart(ctx))
112
+ this.addSub('tracing:postgres:query:error', ctx => {
113
+ if (ctx.currentStore) this.addError(ctx.error, ctx.currentStore.span)
114
+ })
115
+ this.addSub('tracing:postgres:query:asyncEnd', ctx => {
116
+ if (!(ctx.result instanceof Readable || ctx.result instanceof Writable)) this.#finishNative(ctx)
117
+ })
81
118
  this.#preparation = new PostgresPreparationPlugin(this.#prepare.bind(this), tracer, tracerConfig)
119
+ const copy = new PostgresCopyPlugin(ctx => {
120
+ const store = ctx.query.currentStore
121
+ if (store === undefined) return
122
+ if (ctx.error) this.addError(ctx.error, store.span)
123
+ this.#finishNative(ctx.query)
124
+ }, tracer, tracerConfig)
125
+ // Complete in-flight COPY spans even after this plugin is disabled.
126
+ copy.configure(true)
82
127
  }
83
128
 
84
129
  /**
@@ -99,7 +144,15 @@ class PostgresPlugin extends DatabasePlugin {
99
144
  * @returns {object}
100
145
  */
101
146
  bindStart (ctx) {
102
- const { database, host, port, query, user } = ctx
147
+ this.#contexts.set(ctx.query, ctx)
148
+ return this.#start(ctx)
149
+ }
150
+
151
+ /**
152
+ * @param {PostgresContext | NativePostgresContext} ctx
153
+ */
154
+ #start (ctx) {
155
+ const { database, host, port, user } = ctx
103
156
  const service = this.#service
104
157
 
105
158
  const span = this.startSpan(this.operationName(), {
@@ -113,29 +166,53 @@ class PostgresPlugin extends DatabasePlugin {
113
166
  },
114
167
  }, ctx)
115
168
 
116
- if (host !== undefined) {
169
+ if (host != null) {
117
170
  span.addTags({
118
171
  'out.host': host,
119
172
  [CLIENT_PORT_KEY]: port,
120
173
  })
121
174
  }
122
175
 
123
- this.#spans.set(query, span)
124
- this.#waitContexts.set(query, ctx)
125
- if (this.#dbmEnabled) {
126
- this.#dbmQueries.set(query, { service: service.name })
127
- }
176
+ if (this.#dbmEnabled) ctx.currentStore.dbmService = service.name
128
177
  return ctx.currentStore
129
178
  }
130
179
 
180
+ /**
181
+ * @param {NativePostgresContext} ctx
182
+ */
183
+ #nativeStart (ctx) {
184
+ const store = this.#start(ctx)
185
+ if (typeof ctx.query === 'string') {
186
+ store.span.setTag('resource.name', this.maybeTruncate(ctx.query))
187
+ }
188
+ return store
189
+ }
190
+
191
+ /**
192
+ * @param {NativePostgresContext} ctx
193
+ */
194
+ #finishNative (ctx) {
195
+ const store = ctx.currentStore
196
+ if (store === undefined) return
197
+ const { span } = store
198
+
199
+ if (ctx.host !== null) {
200
+ span.addTags({ 'out.host': ctx.host, [CLIENT_PORT_KEY]: ctx.port })
201
+ }
202
+ span.setTag('db.pool.wait_time_ms', ctx.sent === null ? 0 : ctx.sent - ctx.queued)
203
+ if (store.statement === undefined && typeof ctx.query === 'string') {
204
+ span.setTag('resource.name', this.maybeTruncate(ctx.query))
205
+ }
206
+ this.finish(ctx)
207
+ }
208
+
131
209
  /**
132
210
  * @param {PostgresQuery} query
133
211
  */
134
212
  #finishPoolWait (query) {
135
- const ctx = this.#waitContexts.get(query)
136
- if (ctx === undefined) return
213
+ const ctx = this.#contexts.get(query)
214
+ if (ctx === undefined || ctx.poolWaitFinished) return
137
215
 
138
- this.#waitContexts.delete(query)
139
216
  ctx.poolWaitFinished = true
140
217
  const waitTime = ctx.poolWaitStartMs === undefined ? 0 : performance.now() - ctx.poolWaitStartMs
141
218
  ctx.currentStore.span.setTag('db.pool.wait_time_ms', waitTime)
@@ -145,20 +222,23 @@ class PostgresPlugin extends DatabasePlugin {
145
222
  * @param {PostgresPreparationContext} ctx
146
223
  */
147
224
  #prepare (ctx) {
148
- const state = this.#dbmQueries.get(ctx.query)
149
- const span = this.#spans.get(ctx.query)
150
- if (state === undefined || span === undefined) return
225
+ const store = ctx.query.currentStore ?? this.#contexts.get(ctx.query)?.currentStore
226
+ if (store?.dbmService === undefined) return
227
+ const { span } = store
151
228
 
152
- state.statement = ctx.statement
153
- span.setTag('resource.name', this.maybeTruncate(ctx.statement))
154
- ctx.statement = this.injectDbmQuery(span, ctx.statement, state.service, ctx.prepared)
229
+ store.statement = ctx.resource ?? ctx.statement
230
+ span.setTag('resource.name', this.maybeTruncate(store.statement))
231
+ if (ctx.host !== undefined) {
232
+ span.addTags({ 'out.host': ctx.host, [CLIENT_PORT_KEY]: ctx.port })
233
+ }
234
+ ctx.statement = this.injectDbmQuery(span, ctx.statement, store.dbmService, ctx.prepared)
155
235
  }
156
236
 
157
237
  /**
158
- * @param {PostgresContext} ctx
238
+ * @param {Pick<PostgresContext, 'query' | 'error'>} ctx
159
239
  */
160
240
  error (ctx) {
161
- const span = this.#spans.get(ctx.query)
241
+ const span = this.#contexts.get(ctx.query)?.currentStore.span
162
242
  if (span !== undefined) {
163
243
  this.addError(ctx.error, span)
164
244
  }
@@ -168,23 +248,20 @@ class PostgresPlugin extends DatabasePlugin {
168
248
  * @param {PostgresContext} result
169
249
  */
170
250
  asyncEnd (result) {
171
- const { query } = result
172
- const dbmState = this.#dbmQueries.get(query)
173
- const span = this.#spans.get(query)
174
-
175
- this.#dbmQueries.delete(query)
176
- if (span === undefined) return
251
+ const { query, statement, pid } = result
252
+ const ctx = this.#contexts.get(query)
253
+ if (ctx === undefined) return
254
+ const { currentStore: { span, statement: originalStatement } } = ctx
177
255
 
178
256
  this.#finishPoolWait(query)
179
- this.#spans.delete(query)
257
+ this.#contexts.delete(query)
180
258
 
181
- const statement = dbmState?.statement ?? result.statement
182
- if (dbmState?.statement === undefined && typeof statement === 'string') {
259
+ if (originalStatement === undefined && typeof statement === 'string') {
183
260
  span.setTag('resource.name', this.maybeTruncate(statement))
184
261
  }
185
262
 
186
- span.setTag('db.pid', result.pid)
187
- this.finish({ currentStore: { span } })
263
+ span.setTag('db.pid', pid)
264
+ this.finish(ctx)
188
265
  }
189
266
  }
190
267
 
@@ -1,6 +1,10 @@
1
1
  'use strict'
2
2
 
3
3
  const ClientPlugin = require('../../dd-trace/src/plugins/client')
4
+ const {
5
+ INSTRUMENTATION_HTTP_RESOURCE,
6
+ otelHttpResourceName,
7
+ } = require('../../dd-trace/src/plugins/util/http-otel-semantics')
4
8
  const { extractPathFromUrl } = require('../../dd-trace/src/plugins/util/url')
5
9
  const { stripQueryAndFragment } = require('../../dd-trace/src/util')
6
10
  const normalizeError = require('./error')
@@ -23,18 +27,24 @@ class SupabaseGoTrueClientGetUserPlugin extends ClientPlugin {
23
27
  bindStart (ctx) {
24
28
  const method = 'GET'
25
29
  const url = stripQueryAndFragment(`${ctx.self?.url}/user`)
30
+ const otelSemantics = this.config.DD_TRACE_OTEL_SEMANTICS_ENABLED
31
+ const resource = otelSemantics ? otelHttpResourceName(method) : `${method} ${extractPathFromUrl(url)}`
32
+ const meta = {
33
+ component: 'supabase',
34
+ 'span.kind': 'client',
35
+ 'http.method': method,
36
+ 'http.url': url,
37
+ 'out.host': getHostname(url),
38
+ }
39
+ if (otelSemantics) {
40
+ meta[INSTRUMENTATION_HTTP_RESOURCE] = resource
41
+ }
26
42
 
27
43
  this.startSpan('supabase.http.getuser', {
28
44
  service: { name: this.tracer._service },
29
45
  type: 'http',
30
- resource: `${method} ${extractPathFromUrl(url)}`,
31
- meta: {
32
- component: 'supabase',
33
- 'span.kind': 'client',
34
- 'http.method': method,
35
- 'http.url': url,
36
- 'out.host': getHostname(url),
37
- },
46
+ resource,
47
+ meta,
38
48
  }, ctx)
39
49
 
40
50
  return ctx.currentStore
@@ -1,15 +1,22 @@
1
1
  'use strict'
2
2
 
3
+ const { URL } = require('node:url')
4
+
3
5
  const ClientPlugin = require('../../dd-trace/src/plugins/client')
4
- const { extractPathFromUrl } = require('../../dd-trace/src/plugins/util/url')
6
+ const {
7
+ INSTRUMENTATION_HTTP_RESOURCE,
8
+ otelHttpResourceName,
9
+ } = require('../../dd-trace/src/plugins/util/http-otel-semantics')
10
+ const { buildClientHttpUrl, extractPathFromUrl, getQsObfuscator } = require('../../dd-trace/src/plugins/util/url')
5
11
  const { stripQueryAndFragment } = require('../../dd-trace/src/util')
12
+ const log = require('../../dd-trace/src/log')
6
13
  const normalizeError = require('./error')
7
14
  const getHostname = require('./url')
8
15
 
9
16
  /**
10
17
  * @typedef {{
11
- * arguments: { [index: number]: unknown, length: number, 0?: string, 1?: { method?: string } },
12
- * self: { url: string },
18
+ * arguments: { [index: number]: unknown, length: number, 0?: string, 1?: { method?: string, region?: string } },
19
+ * self: { url: string, region?: string },
13
20
  * currentStore?: { span: import('../../..').Span },
14
21
  * result?: { error?: Error | { message?: string } | null, response?: { status?: number } },
15
22
  * error?: unknown
@@ -20,24 +27,53 @@ class SupabaseFunctionsClientInvokePlugin extends ClientPlugin {
20
27
  static id = 'supabase'
21
28
  static prefix = 'tracing:orchestrion:@supabase/functions-js:FunctionsClient_invoke'
22
29
 
30
+ configure (config) {
31
+ if (typeof config === 'boolean') return super.configure(config)
32
+ return super.configure({
33
+ ...config,
34
+ queryStringObfuscation: config.DD_TRACE_OTEL_SEMANTICS_ENABLED && getQsObfuscator(config),
35
+ })
36
+ }
37
+
23
38
  /** @param {FunctionsContext} ctx */
24
39
  bindStart (ctx) {
25
40
  const functionName = ctx.arguments?.[0]
26
41
  const method = String(ctx.arguments?.[1]?.method || 'POST').toUpperCase()
27
- const url = stripQueryAndFragment(`${ctx.self?.url}/${functionName}`)
42
+ const rawUrl = `${ctx.self?.url}/${functionName}`
43
+ const url = stripQueryAndFragment(rawUrl)
44
+ const otelSemantics = this.config.DD_TRACE_OTEL_SEMANTICS_ENABLED
45
+ const resource = otelSemantics ? otelHttpResourceName(method) : `${method} ${extractPathFromUrl(url)}`
46
+ let httpUrl = url
47
+ if (otelSemantics) {
48
+ try {
49
+ const requestUrl = new URL(rawUrl)
50
+ const region = ctx.arguments?.[1]?.region || ctx.self?.region
51
+ if (region && region !== 'any') {
52
+ requestUrl.searchParams.set('forceFunctionRegion', region)
53
+ }
54
+ requestUrl.hash = ''
55
+ httpUrl = buildClientHttpUrl(this.config, '', requestUrl.href, url)
56
+ } catch (error) {
57
+ log.debug('Could not build Supabase function request URL: %s', error.message)
58
+ }
59
+ }
60
+ const meta = {
61
+ component: 'supabase',
62
+ 'span.kind': 'client',
63
+ 'http.method': method,
64
+ 'http.url': httpUrl,
65
+ 'out.host': getHostname(url),
66
+ 'faas.invoked_name': functionName,
67
+ }
68
+ if (otelSemantics) {
69
+ meta[INSTRUMENTATION_HTTP_RESOURCE] = resource
70
+ }
28
71
 
29
72
  this.startSpan('supabase.http.invoke', {
30
73
  service: { name: this.tracer._service },
31
74
  type: 'http',
32
- resource: `${method} ${extractPathFromUrl(url)}`,
33
- meta: {
34
- component: 'supabase',
35
- 'span.kind': 'client',
36
- 'http.method': method,
37
- 'http.url': url,
38
- 'out.host': getHostname(url),
39
- 'faas.invoked_name': functionName,
40
- },
75
+ resource,
76
+ meta,
41
77
  }, ctx)
42
78
 
43
79
  return ctx.currentStore