dd-trace 5.136.0 → 5.137.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ // SDK 2.x bundles Server and McpServer into an `mcp-<hash>` chunk whose hash changes between releases.
4
+ const filePath = /^dist\/mcp-[\w-]+\.[cm]js$/
5
+
6
+ /**
7
+ * @param {string} className
8
+ * @param {string} methodName
9
+ * @param {'Sync'|'Async'} kind
10
+ */
11
+ function entry (className, methodName, kind) {
12
+ return {
13
+ module: { name: '@modelcontextprotocol/server', versionRange: '>=2', filePath },
14
+ functionQuery: { methodName, className, kind },
15
+ channelName: `${className}_${methodName}`,
16
+ }
17
+ }
18
+
19
+ module.exports = [
20
+ entry('Server', '_wrapHandler', 'Sync'),
21
+ entry('McpServer', 'registerTool', 'Sync'),
22
+ entry('McpServer', 'executeToolHandler', 'Async'),
23
+ ]
@@ -7,6 +7,27 @@ const astQuery = 'MethodDefinition[key.name="handleNextImageRequest"] > Function
7
7
  ' > Property[key.name="fn"] > ArrowFunctionExpression'
8
8
 
9
9
  module.exports = [
10
+ ...[
11
+ 'dist/server/future/route-handler-managers/route-handler-manager.js',
12
+ /^dist\/compiled\/next-server\/app-route[\w-]*\.runtime\.(?:dev|prod)\.js$/,
13
+ ].map(filePath => ({
14
+ module: { name: 'next', versionRange: '>=13.3', filePath },
15
+ functionQuery: { methodName: 'handle', kind: 'Async' },
16
+ channelName: 'appRouteResponse',
17
+ })),
18
+ ...[
19
+ 'dist/next-server/server/api-utils.js',
20
+ 'dist/server/api-utils.js',
21
+ 'dist/server/api-utils/node.js',
22
+ 'dist/server/api-utils/node/api-resolver.js',
23
+ /^dist\/compiled\/next-server\/pages-api[\w-]*\.runtime\.(?:dev|prod)\.js$/,
24
+ ].map(filePath => ({
25
+ module: { name: 'next', versionRange: '>=10.2', filePath },
26
+ // These response methods are assigned inside apiResolver, including in minified Pages runtimes.
27
+ astQuery: 'AssignmentExpression[left.property.name=/^(json|send)$/] > ArrowFunctionExpression',
28
+ functionQuery: { kind: 'Sync', index: null },
29
+ channelName: 'responseBody',
30
+ })),
10
31
  {
11
32
  module: {
12
33
  name: 'next',
@@ -6,7 +6,7 @@ for (const filePath of ['src/index.js', 'cjs/src/index.js']) {
6
6
  instrumentations.push({
7
7
  module: {
8
8
  name: 'postgres',
9
- versionRange: '>=3.0.0',
9
+ versionRange: '>=3.0.0 <4',
10
10
  filePath,
11
11
  },
12
12
  astQuery: 'Program',
@@ -19,7 +19,7 @@ for (const filePath of ['src/connection.js', 'cjs/src/connection.js']) {
19
19
  instrumentations.push({
20
20
  module: {
21
21
  name: 'postgres',
22
- versionRange: '>=3.0.0',
22
+ versionRange: '>=3.0.0 <4',
23
23
  filePath,
24
24
  },
25
25
  astQuery: 'Program',
@@ -28,7 +28,7 @@ for (const filePath of ['src/connection.js', 'cjs/src/connection.js']) {
28
28
  }, {
29
29
  module: {
30
30
  name: 'postgres',
31
- versionRange: '>=3.0.0',
31
+ versionRange: '>=3.0.0 <4',
32
32
  filePath,
33
33
  },
34
34
  astQuery: 'Program',
@@ -41,7 +41,7 @@ for (const filePath of ['src/query.js', 'cjs/src/query.js']) {
41
41
  instrumentations.push({
42
42
  module: {
43
43
  name: 'postgres',
44
- versionRange: '>=3.0.0',
44
+ versionRange: '>=3.0.0 <4',
45
45
  filePath,
46
46
  },
47
47
  astQuery: 'Program',
@@ -50,4 +50,33 @@ for (const filePath of ['src/query.js', 'cjs/src/query.js']) {
50
50
  })
51
51
  }
52
52
 
53
+ instrumentations.push({
54
+ module: {
55
+ name: 'postgres',
56
+ versionRange: '>=4.0.0',
57
+ filePath: 'src/pool.js',
58
+ },
59
+ astQuery: 'Program',
60
+ transform: 'postgresNativePreparation',
61
+ channelName: 'query:prepare',
62
+ }, {
63
+ module: {
64
+ name: 'postgres',
65
+ versionRange: '>=4.0.0',
66
+ filePath: 'src/connection.js',
67
+ },
68
+ astQuery: 'Program',
69
+ transform: 'postgresNativeTransactionEnd',
70
+ channelName: 'query:prepare',
71
+ }, {
72
+ module: {
73
+ name: 'postgres',
74
+ versionRange: '>=4.0.0',
75
+ filePath: 'src/connection.js',
76
+ },
77
+ astQuery: 'Program',
78
+ transform: 'postgresNativeCopy',
79
+ channelName: 'query:copy',
80
+ })
81
+
53
82
  module.exports = instrumentations
@@ -1,4 +1,34 @@
1
1
  [
2
+ {
3
+ "name": "@modelcontextprotocol/client",
4
+ "source": "^dist\\/index\\.[cm]js$",
5
+ "flags": ""
6
+ },
7
+ {
8
+ "name": "@modelcontextprotocol/client",
9
+ "source": "^dist\\/public-[\\w-]+\\.[cm]js$",
10
+ "flags": ""
11
+ },
12
+ {
13
+ "name": "@modelcontextprotocol/client",
14
+ "source": "^dist\\/src-[\\w-]+\\.[cm]js$",
15
+ "flags": ""
16
+ },
17
+ {
18
+ "name": "@modelcontextprotocol/server",
19
+ "source": "^dist\\/mcp-[\\w-]+\\.[cm]js$",
20
+ "flags": ""
21
+ },
22
+ {
23
+ "name": "next",
24
+ "source": "^dist\\/compiled\\/next-server\\/app-route[\\w-]*\\.runtime\\.(?:dev|prod)\\.js$",
25
+ "flags": ""
26
+ },
27
+ {
28
+ "name": "next",
29
+ "source": "^dist\\/compiled\\/next-server\\/pages-api[\\w-]*\\.runtime\\.(?:dev|prod)\\.js$",
30
+ "flags": ""
31
+ },
2
32
  {
3
33
  "name": "react-router",
4
34
  "source": "^dist\\/(?:development|production)\\/chunk-[A-Z0-9]+\\.mjs$",
@@ -96,7 +96,12 @@
96
96
  "jest-runner/build/runTest.js": "jest-runner",
97
97
  "jest-runner/build/testWorker.js": "jest-runner",
98
98
  "mercurius/index.js": "mercurius",
99
+ "next/dist/next-server/server/api-utils.js": "next",
99
100
  "next/dist/next-server/server/next-server.js": "next",
101
+ "next/dist/server/api-utils.js": "next",
102
+ "next/dist/server/api-utils/node.js": "next",
103
+ "next/dist/server/api-utils/node/api-resolver.js": "next",
104
+ "next/dist/server/future/route-handler-managers/route-handler-manager.js": "next",
100
105
  "next/dist/server/next-server.js": "next",
101
106
  "playwright-core/lib/coreBundle.js": "playwright-core",
102
107
  "playwright/lib/common/config.js": "playwright",
@@ -110,6 +115,7 @@
110
115
  "postgres/cjs/src/query.js": "postgres",
111
116
  "postgres/src/connection.js": "postgres",
112
117
  "postgres/src/index.js": "postgres",
118
+ "postgres/src/pool.js": "postgres",
113
119
  "postgres/src/query.js": "postgres",
114
120
  "react-router/dist/development/index.js": "react-router",
115
121
  "react-router/dist/development/lib/server-runtime/routeMatching.js": "react-router",
@@ -12,12 +12,134 @@ const POSTGRES_SIMPLE_DBM_QUERIES = '__ddTracePostgresSimpleDbmQueries'
12
12
  const POSTGRES_STATEMENT = 'this.string ?? (this.tagged && this.strings?.length !== 1 ? undefined : this.strings?.[0])'
13
13
 
14
14
  module.exports = {
15
+ postgresNativeCopy,
16
+ postgresNativePreparation,
17
+ postgresNativeTransactionEnd,
15
18
  postgresQueryAcquire,
16
19
  postgresQueryHandlers,
17
20
  postgresQueryLifecycle,
18
21
  postgresQueryPreparation,
19
22
  }
20
23
 
24
+ /**
25
+ * @param {object} state
26
+ * @param {import('estree').Program} program
27
+ */
28
+ function postgresNativeCopy (state, program) {
29
+ const channelVariable = injectPostgresTracingChannel(state, program)
30
+ const writable = query(program, 'FunctionDeclaration[id.name="writable"] Property[key.name="destroy"]')
31
+ const abandoned = query(program, 'FunctionDeclaration[id.name="abandoned"] > BlockStatement > ReturnStatement')
32
+ assert(writable.length === 1 && abandoned.length <= 1, 'postgresNativeCopy: stream destruction changed')
33
+
34
+ const publish = `
35
+ if (item.trace && ${channelVariable}.start.hasSubscribers) {
36
+ ${channelVariable}.start.publish({ query: item.trace, error });
37
+ }
38
+ `
39
+ writable[0].value.body.body.unshift(...parse(publish).body)
40
+
41
+ if (abandoned.length) {
42
+ abandoned[0].argument.body.body.unshift(...parse(publish).body)
43
+ } else {
44
+ const streams = query(program, 'FunctionDeclaration[id.name="CopyOutResponse"] NewExpression')
45
+ assert(streams.length === 1, 'postgresNativeCopy: readable construction changed')
46
+ streams[0].arguments[0].properties.push(...parse(`({
47
+ destroy(error, callback) {
48
+ ${publish}
49
+ callback(error);
50
+ }
51
+ })`).body[0].expression.properties)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * @param {object} state
57
+ * @param {import('estree').Program} program
58
+ */
59
+ function postgresNativeTransactionEnd (state, program) {
60
+ // Leading DBM comments must not hide transaction endings from the cursor fence.
61
+ for (const name of ['send', 'simple', 'batch']) {
62
+ const functions = query(program, `FunctionDeclaration[id.name="${name}"]`)
63
+ if (name === 'batch' && functions.length === 0) continue
64
+ assert(functions.length === 1, 'postgresNativeTransactionEnd: connection method changed')
65
+ const tests = query(functions[0], 'CallExpression[callee.object.name="ending"][callee.property.name="test"]')
66
+ assert(tests.length === 1, 'postgresNativeTransactionEnd: transaction classifier changed')
67
+ const original = name === 'batch' ? 'part.built.text' : 'query.built?.text'
68
+ const text = parse(original).body[0].expression
69
+ tests[0].arguments[0] = name === 'batch'
70
+ ? text
71
+ : {
72
+ type: 'LogicalExpression',
73
+ operator: '??',
74
+ left: text,
75
+ right: tests[0].arguments[0],
76
+ }
77
+ }
78
+ }
79
+
80
+ /**
81
+ * @param {object} state
82
+ * @param {import('estree').Program} program
83
+ */
84
+ function postgresNativePreparation (state, program) {
85
+ const channelVariable = injectPostgresTracingChannel(state, program)
86
+ const pools = query(program, 'FunctionDeclaration[id.name="Pool"]')
87
+ const queries = query(program, 'FunctionDeclaration[id.name="query"]')
88
+ assert(pools.length === 1 && queries.length === 1, 'postgresNativePreparation: pool query changed')
89
+
90
+ const body = queries[0].body.body
91
+ const buildIndex = body.findIndex(statement => statement.type === 'TryStatement')
92
+ assert(buildIndex !== -1, 'postgresNativePreparation: query build changed')
93
+ body.splice(buildIndex + 1, 0, ...parse(`
94
+ if (q.trace && ${channelVariable}.start.hasSubscribers) {
95
+ built = __ddTracePostgresPrepare(q.trace, built,
96
+ Boolean(options.prepare && q.options.prepare !== false && !(q.options.simple && !built.values.length)));
97
+ }
98
+ `).body)
99
+
100
+ pools[0].body.body.push(...parse(`
101
+ function __ddTracePostgresPrepare(trace, built, prepared, resource) {
102
+ const singleHost = options.host.length === 1 && !options.path;
103
+ const ctx = {
104
+ query: trace,
105
+ statement: built.text,
106
+ resource: resource ?? built.text,
107
+ prepared,
108
+ host: singleHost ? options.host[0] : undefined,
109
+ port: singleHost ? options.port[0] : undefined
110
+ };
111
+ ${channelVariable}.start.publish(ctx);
112
+ if (ctx.statement === built.text) return built;
113
+ return { ...built, text: ctx.statement, dynamic: true };
114
+ }
115
+ `).body)
116
+
117
+ const batches = query(program, 'FunctionDeclaration[id.name="batch"]')
118
+ assert(batches.length <= 1, 'postgresNativePreparation: batch changed')
119
+ if (batches.length === 0) return
120
+
121
+ const loops = query(batches[0], 'CallExpression[callee.object.name="parts"][callee.property.name="forEach"]')
122
+ assert(loops.length === 1, 'postgresNativePreparation: batch preparation changed')
123
+ const callback = loops[0].arguments[0]
124
+ const declaration = callback.body.body[0]
125
+ assert(declaration.type === 'VariableDeclaration' && declaration.declarations[0].id.name === 'built',
126
+ 'postgresNativePreparation: batch build changed')
127
+ declaration.kind = 'let'
128
+ callback.body.body.splice(1, 0, ...parse(`
129
+ if (q.trace && ${channelVariable}.start.hasSubscribers) {
130
+ built = __ddTracePostgresPrepare(q.trace, built,
131
+ Boolean(options.prepare && x.options.prepare !== false), q.trace.query);
132
+ }
133
+ `).body)
134
+ const batchBody = batches[0].body.body
135
+ const loopIndex = batchBody.findIndex(statement => statement.type === 'ExpressionStatement' &&
136
+ statement.expression === loops[0])
137
+ assert(loopIndex !== -1, 'postgresNativePreparation: batch loop changed')
138
+ batchBody.splice(loopIndex, 0, ...parse(`
139
+ if (q.trace) q.trace.query = parts.map(part => part.built.text).join('; ');
140
+ `).body)
141
+ }
142
+
21
143
  /**
22
144
  * @param {object} state
23
145
  * @param {import('estree').Program} program
@@ -9,8 +9,7 @@ const errorChannel = channel('apm:microgateway-core:request:error')
9
9
 
10
10
  const name = 'microgateway-core'
11
11
 
12
- // TODO Remove " <=3.0.0" when "volos-util-apigee" module is fixed
13
- const versions = ['>=2.1 <=3.0.0']
12
+ const versions = ['>=2.1']
14
13
  const requestContexts = new WeakMap()
15
14
 
16
15
  function wrapConfigProxyFactory (configProxyFactory) {
@@ -0,0 +1,21 @@
1
+ 'use strict'
2
+
3
+ const { channel, tracingChannel } = require('dc-polyfill')
4
+
5
+ const { addTraceContextToRequest } = require('./modelcontextprotocol-sdk')
6
+
7
+ const requestCh = tracingChannel('orchestrion:@modelcontextprotocol/client:Protocol_request')
8
+ const clientRequestInjectCh = channel('apm:mcp:client:request:inject')
9
+
10
+ const TRACED_METHOD_PREFIX = /^(?:tools|resources|prompts)\//
11
+
12
+ // Replacing the request argument before Protocol.request runs puts the trace context in `_meta`, so out-of-process MCP
13
+ // servers can parent their spans to the client span.
14
+ requestCh.start.subscribe(ctx => {
15
+ const request = ctx.arguments?.[0]
16
+ if (!clientRequestInjectCh.hasSubscribers || !TRACED_METHOD_PREFIX.test(request?.method)) return
17
+
18
+ const injectCtx = {}
19
+ clientRequestInjectCh.publish(injectCtx)
20
+ if (injectCtx.traceContext) ctx.arguments[0] = addTraceContextToRequest(request, injectCtx.traceContext)
21
+ })
@@ -147,7 +147,7 @@ function wrapProtocol (Protocol) {
147
147
  }
148
148
 
149
149
  wrapAbortControllers(this)
150
- const ctx = { request, extra }
150
+ const ctx = { request, extra, server: this }
151
151
  serverRequestCh.start.runStores(ctx, () => {
152
152
  let pending = pendingContexts.get(this)
153
153
  if (!pending) {
@@ -162,6 +162,10 @@ function wrapProtocol (Protocol) {
162
162
  ctx.error = err
163
163
  finishServerRequest(this, request.id)
164
164
  throw err
165
+ } finally {
166
+ // runStores restores only the APM store. `end` lets the LLM Obs plugin restore its own store. The handler
167
+ // keeps the server span because the SDK schedules it before `_onrequest` returns.
168
+ serverRequestCh.end.publish(ctx)
165
169
  }
166
170
 
167
171
  // The SDK registers an AbortController only when it actually dispatches a handler.
@@ -252,3 +256,5 @@ addHook({
252
256
  wrapMcpServer(exports.McpServer)
253
257
  return exports
254
258
  })
259
+
260
+ module.exports = { addTraceContextToRequest }
@@ -0,0 +1,41 @@
1
+ 'use strict'
2
+
3
+ const { channel, tracingChannel } = require('dc-polyfill')
4
+
5
+ const shimmer = require('../../datadog-shimmer')
6
+
7
+ const wrapHandlerCh = tracingChannel('orchestrion:@modelcontextprotocol/server:Server__wrapHandler')
8
+ const registerToolCh = tracingChannel('orchestrion:@modelcontextprotocol/server:McpServer_registerTool')
9
+ const serverRequestCh = tracingChannel('apm:mcp:server:request')
10
+ const serverToolRegisteredCh = channel('apm:mcp:server:tool:registered')
11
+
12
+ const TRACED_METHOD_PREFIX = /^(?:tools|resources|prompts)\//
13
+
14
+ // The SDK builds each request handler at runtime, so the handler that Server._wrapHandler returns is replaced to trace
15
+ // every call. The SDK runs it in the async context of the incoming request.
16
+ wrapHandlerCh.end.subscribe(ctx => {
17
+ const handler = ctx.result
18
+ if (typeof handler !== 'function' || !TRACED_METHOD_PREFIX.test(ctx.arguments?.[0])) return
19
+
20
+ const server = ctx.self
21
+ ctx.result = shimmer.wrapFunction(handler, handler => function tracedRequestHandler (request, extra) {
22
+ if (!serverRequestCh.hasSubscribers) return handler.apply(this, arguments)
23
+
24
+ return serverRequestCh.tracePromise(handler, { request, extra, server }, this, ...arguments)
25
+ })
26
+ })
27
+
28
+ // Tool objects are created at runtime, so their `update` method is wrapped to follow renames.
29
+ registerToolCh.end.subscribe(ctx => {
30
+ const tool = ctx.result
31
+ if (!tool || !serverToolRegisteredCh.hasSubscribers) return
32
+
33
+ serverToolRegisteredCh.publish({ tool, name: ctx.arguments[0] })
34
+ shimmer.wrap(tool, 'update', update => function updateWithTrace (updates) {
35
+ const result = update.apply(this, arguments)
36
+ if (updates && Object.hasOwn(updates, 'name')) {
37
+ serverToolRegisteredCh.publish({ tool, name: updates.name || undefined })
38
+ }
39
+ return result
40
+ })
41
+ })