dd-trace 5.61.1 → 5.63.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 (215) hide show
  1. package/README.md +0 -5
  2. package/package.json +2 -2
  3. package/packages/datadog-instrumentations/src/ai.js +140 -0
  4. package/packages/datadog-instrumentations/src/apollo-server.js +50 -8
  5. package/packages/datadog-instrumentations/src/aws-sdk.js +49 -60
  6. package/packages/datadog-instrumentations/src/couchbase.js +102 -65
  7. package/packages/datadog-instrumentations/src/fastify.js +61 -55
  8. package/packages/datadog-instrumentations/src/graphql.js +90 -122
  9. package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -0
  10. package/packages/datadog-instrumentations/src/helpers/register.js +2 -22
  11. package/packages/datadog-instrumentations/src/hono.js +11 -8
  12. package/packages/datadog-instrumentations/src/http2/server.js +14 -20
  13. package/packages/datadog-instrumentations/src/knex.js +15 -17
  14. package/packages/datadog-instrumentations/src/microgateway-core.js +16 -15
  15. package/packages/datadog-instrumentations/src/mongodb-core.js +35 -32
  16. package/packages/datadog-instrumentations/src/mongodb.js +9 -13
  17. package/packages/datadog-instrumentations/src/mongoose.js +25 -29
  18. package/packages/datadog-instrumentations/src/next.js +4 -8
  19. package/packages/datadog-instrumentations/src/openai.js +0 -2
  20. package/packages/datadog-instrumentations/src/oracledb.js +39 -33
  21. package/packages/datadog-instrumentations/src/pg.js +38 -48
  22. package/packages/datadog-plugin-aerospike/src/index.js +11 -11
  23. package/packages/datadog-plugin-ai/src/index.js +17 -0
  24. package/packages/datadog-plugin-ai/src/tracing.js +33 -0
  25. package/packages/datadog-plugin-ai/src/utils.js +28 -0
  26. package/packages/datadog-plugin-amqp10/src/consumer.js +2 -2
  27. package/packages/datadog-plugin-amqp10/src/index.js +1 -1
  28. package/packages/datadog-plugin-amqp10/src/producer.js +3 -3
  29. package/packages/datadog-plugin-amqplib/src/client.js +3 -3
  30. package/packages/datadog-plugin-amqplib/src/consumer.js +2 -2
  31. package/packages/datadog-plugin-amqplib/src/index.js +1 -1
  32. package/packages/datadog-plugin-amqplib/src/producer.js +2 -2
  33. package/packages/datadog-plugin-apollo/src/gateway/execute.js +2 -4
  34. package/packages/datadog-plugin-apollo/src/gateway/fetch.js +2 -4
  35. package/packages/datadog-plugin-apollo/src/gateway/index.js +1 -1
  36. package/packages/datadog-plugin-apollo/src/gateway/plan.js +2 -4
  37. package/packages/datadog-plugin-apollo/src/gateway/postprocessing.js +2 -4
  38. package/packages/datadog-plugin-apollo/src/gateway/request.js +2 -4
  39. package/packages/datadog-plugin-apollo/src/gateway/validate.js +2 -4
  40. package/packages/datadog-plugin-apollo/src/index.js +1 -1
  41. package/packages/datadog-plugin-avsc/src/index.js +2 -2
  42. package/packages/datadog-plugin-aws-sdk/src/base.js +70 -46
  43. package/packages/datadog-plugin-aws-sdk/src/index.js +1 -3
  44. package/packages/datadog-plugin-aws-sdk/src/services/bedrockruntime/index.js +1 -3
  45. package/packages/datadog-plugin-aws-sdk/src/services/bedrockruntime/tracing.js +1 -1
  46. package/packages/datadog-plugin-aws-sdk/src/services/cloudwatchlogs.js +1 -1
  47. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +3 -3
  48. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +2 -2
  49. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +22 -20
  50. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +1 -1
  51. package/packages/datadog-plugin-aws-sdk/src/services/redshift.js +1 -1
  52. package/packages/datadog-plugin-aws-sdk/src/services/s3.js +3 -3
  53. package/packages/datadog-plugin-aws-sdk/src/services/sfn.js +1 -1
  54. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +3 -3
  55. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +17 -15
  56. package/packages/datadog-plugin-aws-sdk/src/services/states.js +1 -1
  57. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +1 -1
  58. package/packages/datadog-plugin-azure-functions/src/index.js +5 -5
  59. package/packages/datadog-plugin-azure-service-bus/src/index.js +1 -1
  60. package/packages/datadog-plugin-azure-service-bus/src/producer.js +2 -2
  61. package/packages/datadog-plugin-bunyan/src/index.js +3 -5
  62. package/packages/datadog-plugin-cassandra-driver/src/index.js +3 -3
  63. package/packages/datadog-plugin-child_process/src/index.js +2 -2
  64. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/batch-consumer.js +1 -3
  65. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/consumer.js +1 -3
  66. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/index.js +1 -1
  67. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/producer.js +1 -3
  68. package/packages/datadog-plugin-connect/src/index.js +1 -3
  69. package/packages/datadog-plugin-couchbase/src/index.js +39 -19
  70. package/packages/datadog-plugin-cucumber/src/index.js +1 -3
  71. package/packages/datadog-plugin-cypress/src/index.js +1 -3
  72. package/packages/datadog-plugin-dd-trace-api/src/index.js +1 -3
  73. package/packages/datadog-plugin-dns/src/index.js +1 -1
  74. package/packages/datadog-plugin-dns/src/lookup.js +2 -2
  75. package/packages/datadog-plugin-dns/src/lookup_service.js +2 -2
  76. package/packages/datadog-plugin-dns/src/resolve.js +2 -2
  77. package/packages/datadog-plugin-dns/src/reverse.js +2 -2
  78. package/packages/datadog-plugin-elasticsearch/src/index.js +1 -1
  79. package/packages/datadog-plugin-express/src/code_origin.js +1 -3
  80. package/packages/datadog-plugin-express/src/index.js +1 -1
  81. package/packages/datadog-plugin-express/src/tracing.js +1 -3
  82. package/packages/datadog-plugin-fastify/src/code_origin.js +1 -3
  83. package/packages/datadog-plugin-fastify/src/index.js +1 -1
  84. package/packages/datadog-plugin-fastify/src/tracing.js +18 -3
  85. package/packages/datadog-plugin-fetch/src/index.js +2 -2
  86. package/packages/datadog-plugin-find-my-way/src/index.js +1 -3
  87. package/packages/datadog-plugin-fs/src/index.js +2 -2
  88. package/packages/datadog-plugin-google-cloud-pubsub/src/client.js +3 -3
  89. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +2 -2
  90. package/packages/datadog-plugin-google-cloud-pubsub/src/index.js +1 -1
  91. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -2
  92. package/packages/datadog-plugin-google-cloud-vertexai/src/index.js +1 -1
  93. package/packages/datadog-plugin-google-cloud-vertexai/src/tracing.js +2 -4
  94. package/packages/datadog-plugin-graphql/src/execute.js +16 -9
  95. package/packages/datadog-plugin-graphql/src/index.js +1 -1
  96. package/packages/datadog-plugin-graphql/src/parse.js +12 -7
  97. package/packages/datadog-plugin-graphql/src/resolve.js +50 -16
  98. package/packages/datadog-plugin-graphql/src/validate.js +13 -7
  99. package/packages/datadog-plugin-grpc/src/client.js +4 -4
  100. package/packages/datadog-plugin-grpc/src/index.js +1 -1
  101. package/packages/datadog-plugin-grpc/src/server.js +3 -3
  102. package/packages/datadog-plugin-hapi/src/index.js +1 -3
  103. package/packages/datadog-plugin-hono/src/index.js +1 -3
  104. package/packages/datadog-plugin-http/src/client.js +2 -2
  105. package/packages/datadog-plugin-http/src/index.js +1 -1
  106. package/packages/datadog-plugin-http/src/server.js +3 -7
  107. package/packages/datadog-plugin-http2/src/client.js +2 -2
  108. package/packages/datadog-plugin-http2/src/index.js +1 -1
  109. package/packages/datadog-plugin-http2/src/server.js +22 -11
  110. package/packages/datadog-plugin-ioredis/src/index.js +1 -3
  111. package/packages/datadog-plugin-iovalkey/src/index.js +2 -4
  112. package/packages/datadog-plugin-jest/src/index.js +1 -3
  113. package/packages/datadog-plugin-kafkajs/src/batch-consumer.js +2 -2
  114. package/packages/datadog-plugin-kafkajs/src/consumer.js +2 -2
  115. package/packages/datadog-plugin-kafkajs/src/index.js +1 -1
  116. package/packages/datadog-plugin-kafkajs/src/producer.js +3 -3
  117. package/packages/datadog-plugin-koa/src/index.js +1 -3
  118. package/packages/datadog-plugin-langchain/src/index.js +2 -2
  119. package/packages/datadog-plugin-langchain/src/tracing.js +30 -48
  120. package/packages/datadog-plugin-mariadb/src/index.js +2 -2
  121. package/packages/datadog-plugin-memcached/src/index.js +1 -1
  122. package/packages/datadog-plugin-microgateway-core/src/index.js +4 -4
  123. package/packages/datadog-plugin-mocha/src/index.js +1 -3
  124. package/packages/datadog-plugin-moleculer/src/client.js +2 -2
  125. package/packages/datadog-plugin-moleculer/src/index.js +1 -1
  126. package/packages/datadog-plugin-moleculer/src/server.js +2 -2
  127. package/packages/datadog-plugin-mongodb-core/src/index.js +9 -5
  128. package/packages/datadog-plugin-mongoose/src/index.js +20 -0
  129. package/packages/datadog-plugin-mysql/src/index.js +2 -2
  130. package/packages/datadog-plugin-mysql2/src/index.js +1 -1
  131. package/packages/datadog-plugin-net/src/index.js +1 -1
  132. package/packages/datadog-plugin-net/src/ipc.js +2 -2
  133. package/packages/datadog-plugin-net/src/tcp.js +2 -2
  134. package/packages/datadog-plugin-next/src/index.js +1 -3
  135. package/packages/datadog-plugin-nyc/src/index.js +1 -3
  136. package/packages/datadog-plugin-openai/src/index.js +1 -1
  137. package/packages/datadog-plugin-openai/src/tracing.js +7 -411
  138. package/packages/datadog-plugin-opensearch/src/index.js +1 -3
  139. package/packages/datadog-plugin-oracledb/src/index.js +9 -5
  140. package/packages/datadog-plugin-pg/src/index.js +8 -5
  141. package/packages/datadog-plugin-pino/src/index.js +3 -5
  142. package/packages/datadog-plugin-playwright/src/index.js +1 -3
  143. package/packages/datadog-plugin-prisma/src/client.js +4 -6
  144. package/packages/datadog-plugin-prisma/src/engine.js +3 -3
  145. package/packages/datadog-plugin-prisma/src/index.js +1 -1
  146. package/packages/datadog-plugin-protobufjs/src/index.js +2 -6
  147. package/packages/datadog-plugin-redis/src/index.js +2 -2
  148. package/packages/datadog-plugin-restify/src/index.js +1 -3
  149. package/packages/datadog-plugin-rhea/src/consumer.js +1 -1
  150. package/packages/datadog-plugin-rhea/src/index.js +1 -1
  151. package/packages/datadog-plugin-rhea/src/producer.js +2 -2
  152. package/packages/datadog-plugin-router/src/index.js +1 -3
  153. package/packages/datadog-plugin-selenium/src/index.js +1 -3
  154. package/packages/datadog-plugin-sharedb/src/index.js +1 -1
  155. package/packages/datadog-plugin-tedious/src/index.js +3 -3
  156. package/packages/datadog-plugin-undici/src/index.js +2 -4
  157. package/packages/datadog-plugin-vitest/src/index.js +1 -3
  158. package/packages/datadog-plugin-web/src/index.js +1 -3
  159. package/packages/datadog-plugin-winston/src/index.js +3 -5
  160. package/packages/dd-trace/src/appsec/channels.js +1 -0
  161. package/packages/dd-trace/src/appsec/graphql.js +14 -12
  162. package/packages/dd-trace/src/appsec/iast/analyzers/sql-injection-analyzer.js +14 -7
  163. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +4 -4
  164. package/packages/dd-trace/src/appsec/recommended.json +271 -2
  165. package/packages/dd-trace/src/appsec/waf/waf_manager.js +1 -1
  166. package/packages/dd-trace/src/ci-visibility/log-submission/log-submission-plugin.js +1 -3
  167. package/packages/dd-trace/src/ci-visibility/test-api-manual/test-api-manual-plugin.js +1 -3
  168. package/packages/dd-trace/src/config.js +1 -1
  169. package/packages/dd-trace/src/datastreams/checkpointer.js +23 -2
  170. package/packages/dd-trace/src/datastreams/processor.js +4 -3
  171. package/packages/dd-trace/src/guardrails/telemetry.js +18 -2
  172. package/packages/dd-trace/src/llmobs/plugins/ai/index.js +351 -0
  173. package/packages/dd-trace/src/llmobs/plugins/ai/util.js +179 -0
  174. package/packages/dd-trace/src/llmobs/plugins/langchain/index.js +30 -50
  175. package/packages/dd-trace/src/llmobs/plugins/openai.js +3 -5
  176. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +3 -5
  177. package/packages/dd-trace/src/llmobs/writers/base.js +3 -2
  178. package/packages/dd-trace/src/opentracing/propagation/text_map.js +25 -2
  179. package/packages/dd-trace/src/opentracing/span_context.js +4 -0
  180. package/packages/dd-trace/src/plugin_manager.js +8 -4
  181. package/packages/dd-trace/src/plugins/apollo.js +3 -3
  182. package/packages/dd-trace/src/plugins/cache.js +1 -1
  183. package/packages/dd-trace/src/plugins/client.js +3 -3
  184. package/packages/dd-trace/src/plugins/consumer.js +3 -3
  185. package/packages/dd-trace/src/plugins/database.js +2 -2
  186. package/packages/dd-trace/src/plugins/index.js +2 -0
  187. package/packages/dd-trace/src/plugins/log_plugin.js +1 -5
  188. package/packages/dd-trace/src/plugins/outbound.js +1 -1
  189. package/packages/dd-trace/src/plugins/plugin.js +1 -1
  190. package/packages/dd-trace/src/plugins/producer.js +3 -3
  191. package/packages/dd-trace/src/plugins/server.js +3 -3
  192. package/packages/dd-trace/src/plugins/storage.js +1 -1
  193. package/packages/dd-trace/src/plugins/tracing.js +24 -6
  194. package/packages/dd-trace/src/plugins/util/ci.js +11 -7
  195. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +15 -19
  196. package/packages/dd-trace/src/plugins/util/ip_extractor.js +44 -3
  197. package/packages/dd-trace/src/plugins/util/tags.js +2 -0
  198. package/packages/dd-trace/src/plugins/util/web.js +26 -7
  199. package/packages/dd-trace/src/profiling/config.js +2 -0
  200. package/packages/dd-trace/src/profiling/exporters/event_serializer.js +2 -21
  201. package/packages/dd-trace/src/profiling/libuv-size.js +49 -0
  202. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns.js +2 -6
  203. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_lookup.js +1 -3
  204. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_lookupservice.js +1 -3
  205. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_resolve.js +1 -3
  206. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_reverse.js +1 -3
  207. package/packages/dd-trace/src/profiling/profilers/event_plugins/event.js +24 -23
  208. package/packages/dd-trace/src/profiling/profilers/event_plugins/fs.js +3 -9
  209. package/packages/dd-trace/src/profiling/profilers/event_plugins/net.js +3 -9
  210. package/packages/dd-trace/src/profiling/profilers/events.js +83 -64
  211. package/packages/dd-trace/src/profiling/profilers/poisson.js +105 -0
  212. package/packages/dd-trace/src/profiling/profilers/wall.js +3 -3
  213. package/packages/dd-trace/src/remote_config/manager.js +1 -1
  214. package/packages/dd-trace/src/supported-configurations.json +2 -0
  215. package/packages/dd-trace/src/tracer_metadata.js +1 -1
package/README.md CHANGED
@@ -22,11 +22,6 @@ Most of the documentation for `dd-trace` is available on these webpages:
22
22
 
23
23
  ## Version Release Lines and Maintenance
24
24
 
25
- > **Node.js v24 Notice**: We're currently adding compatibility for Node.js v24. To use the tracer with your application either continue to use Node.js v22 (LTS), or do both of the following as a workaround:
26
- > * Install v5.52.0 (or newer) of the tracer
27
- > * Set `--no-async-context-frame` either using a CLI argument or via `NODE_OPTIONS`
28
- > Once support for Node.js v24 is complete this flag will no longer be needed.
29
-
30
25
  | Release Line | Latest Version | Node.js | [SSI](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/single-step-apm/?tab=linuxhostorvm) | [K8s Injection](https://docs.datadoghq.com/tracing/trace_collection/library_injection_local/?tab=kubernetes) |Status |Initial Release | End of Life |
31
26
  | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
32
27
  | [`v1`](https://github.com/DataDog/dd-trace-js/tree/v1.x) | ![npm v1](https://img.shields.io/npm/v/dd-trace/legacy-v1?color=white&label=%20&style=flat-square) | `>= v12` | NO | NO | **EOL** | 2021-07-13 | 2022-02-25 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "5.61.1",
3
+ "version": "5.63.0",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -114,7 +114,7 @@
114
114
  ],
115
115
  "dependencies": {
116
116
  "@datadog/libdatadog": "0.7.0",
117
- "@datadog/native-appsec": "10.0.1",
117
+ "@datadog/native-appsec": "10.1.0",
118
118
  "@datadog/native-iast-taint-tracking": "4.0.0",
119
119
  "@datadog/native-metrics": "3.1.1",
120
120
  "@datadog/pprof": "5.9.0",
@@ -0,0 +1,140 @@
1
+ 'use strict'
2
+
3
+ const { addHook } = require('./helpers/instrument')
4
+ const shimmer = require('../../datadog-shimmer')
5
+
6
+ const { channel, tracingChannel } = require('dc-polyfill')
7
+ const toolCreationChannel = channel('dd-trace:vercel-ai:tool')
8
+
9
+ const TRACED_FUNCTIONS = {
10
+ generateText: wrapWithTracer,
11
+ streamText: wrapWithTracer,
12
+ generateObject: wrapWithTracer,
13
+ streamObject: wrapWithTracer,
14
+ embed: wrapWithTracer,
15
+ embedMany: wrapWithTracer,
16
+ tool: wrapTool
17
+ }
18
+
19
+ const vercelAiTracingChannel = tracingChannel('dd-trace:vercel-ai')
20
+ const vercelAiSpanSetAttributesChannel = channel('dd-trace:vercel-ai:span:setAttributes')
21
+
22
+ const noopTracer = {
23
+ startActiveSpan () {
24
+ const fn = arguments[arguments.length - 1]
25
+
26
+ const span = {
27
+ spanContext () { return { traceId: '', spanId: '', traceFlags: 0 } },
28
+ setAttribute () { return this },
29
+ setAttributes () { return this },
30
+ addEvent () { return this },
31
+ addLink () { return this },
32
+ addLinks () { return this },
33
+ setStatus () { return this },
34
+ updateName () { return this },
35
+ end () { return this },
36
+ isRecording () { return false },
37
+ recordException () { return this }
38
+ }
39
+
40
+ return fn(span)
41
+ }
42
+ }
43
+
44
+ function wrapTracer (tracer) {
45
+ if (Object.hasOwn(tracer, Symbol.for('_dd.wrapped'))) return
46
+
47
+ shimmer.wrap(tracer, 'startActiveSpan', function (startActiveSpan) {
48
+ return function () {
49
+ const name = arguments[0]
50
+ const options = arguments.length > 2 ? (arguments[1] ?? {}) : {} // startActiveSpan(name, fn)
51
+ const cb = arguments[arguments.length - 1]
52
+
53
+ const ctx = {
54
+ name,
55
+ attributes: options.attributes ?? {}
56
+ }
57
+
58
+ arguments[arguments.length - 1] = shimmer.wrapFunction(cb, function (originalCb) {
59
+ return function (span) {
60
+ shimmer.wrap(span, 'end', function (spanEnd) {
61
+ return function () {
62
+ vercelAiTracingChannel.asyncEnd.publish(ctx)
63
+ return spanEnd.apply(this, arguments)
64
+ }
65
+ })
66
+
67
+ shimmer.wrap(span, 'setAttributes', function (setAttributes) {
68
+ return function (attributes) {
69
+ vercelAiSpanSetAttributesChannel.publish({ ctx, attributes })
70
+ return setAttributes.apply(this, arguments)
71
+ }
72
+ })
73
+
74
+ shimmer.wrap(span, 'recordException', function (recordException) {
75
+ return function (exception) {
76
+ ctx.error = exception
77
+ vercelAiTracingChannel.error.publish(ctx)
78
+ return recordException.apply(this, arguments)
79
+ }
80
+ })
81
+
82
+ return originalCb.apply(this, arguments)
83
+ }
84
+ })
85
+
86
+ return vercelAiTracingChannel.start.runStores(ctx, () => {
87
+ const result = startActiveSpan.apply(this, arguments)
88
+ vercelAiTracingChannel.end.publish(ctx)
89
+ return result
90
+ })
91
+ }
92
+ })
93
+
94
+ Object.defineProperty(tracer, Symbol.for('_dd.wrapped'), { value: true })
95
+ }
96
+
97
+ function wrapWithTracer (fn) {
98
+ return function () {
99
+ const options = arguments[0]
100
+
101
+ options.experimental_telemetry ??= { isEnabled: true, tracer: noopTracer }
102
+ wrapTracer(options.experimental_telemetry.tracer)
103
+
104
+ return fn.apply(this, arguments)
105
+ }
106
+ }
107
+
108
+ function wrapTool (tool) {
109
+ return function () {
110
+ const args = arguments[0]
111
+ toolCreationChannel.publish(args)
112
+
113
+ return tool.apply(this, arguments)
114
+ }
115
+ }
116
+
117
+ // CJS exports
118
+ addHook({
119
+ name: 'ai',
120
+ versions: ['>=4.0.0'],
121
+ }, exports => {
122
+ for (const [fnName, patchingFn] of Object.entries(TRACED_FUNCTIONS)) {
123
+ exports = shimmer.wrap(exports, fnName, patchingFn, { replaceGetter: true })
124
+ }
125
+
126
+ return exports
127
+ })
128
+
129
+ // ESM exports
130
+ addHook({
131
+ name: 'ai',
132
+ versions: ['>=4.0.0'],
133
+ file: 'dist/index.mjs'
134
+ }, exports => {
135
+ for (const [fnName, patchingFn] of Object.entries(TRACED_FUNCTIONS)) {
136
+ exports = shimmer.wrap(exports, fnName, patchingFn, { replaceGetter: true })
137
+ }
138
+
139
+ return exports
140
+ })
@@ -6,6 +6,7 @@ const { addHook } = require('./helpers/instrument')
6
6
  const shimmer = require('../../datadog-shimmer')
7
7
 
8
8
  const graphqlMiddlewareChannel = dc.tracingChannel('datadog:apollo:middleware')
9
+ const apolloHttpServerChannel = dc.tracingChannel('datadog:apollo:httpserver')
9
10
 
10
11
  const requestChannel = dc.tracingChannel('datadog:apollo:request')
11
12
 
@@ -77,17 +78,58 @@ function apolloServerHook (apolloServer) {
77
78
  return apolloServer
78
79
  }
79
80
 
80
- addHook(
81
- { name: '@apollo/server', file: 'dist/cjs/ApolloServer.js', versions: ['>=4.0.0 <5.0.0'] },
82
- apolloServerHook
83
- )
81
+ function wrapEmit (emit) {
82
+ return function wrappedEmit (event, req, res) {
83
+ if (event === 'request' && req && res && apolloHttpServerChannel.start.hasSubscribers) {
84
+ return apolloHttpServerChannel.traceSync(emit, { req }, this, ...arguments)
85
+ }
86
+
87
+ return emit.apply(this, arguments)
88
+ }
89
+ }
90
+
91
+ function wrapListen (originalListen) {
92
+ return function wrappedListen () {
93
+ shimmer.wrap(this, 'emit', wrapEmit)
94
+
95
+ return originalListen.apply(this, arguments)
96
+ }
97
+ }
98
+
99
+ function wrapHttpServer (original) {
100
+ return function wrappedHttpServer (options) {
101
+ if (options.httpServer && typeof options.httpServer.listen === 'function') {
102
+ shimmer.wrap(options.httpServer, 'listen', wrapListen)
103
+ }
104
+
105
+ return original.apply(this, arguments)
106
+ }
107
+ }
108
+
109
+ function apolloDrainHttpServerHook (drainModule) {
110
+ shimmer.wrap(drainModule, 'ApolloServerPluginDrainHttpServer', wrapHttpServer)
111
+
112
+ return drainModule
113
+ }
114
+
115
+ addHook({ name: '@apollo/server', file: 'dist/cjs/ApolloServer.js', versions: ['4'] }, apolloServerHook)
116
+
117
+ addHook({ name: '@apollo/server', file: 'dist/cjs/express4/index.js', versions: ['4'] }, apolloExpress4Hook)
118
+
119
+ addHook({ name: '@apollo/server', file: 'dist/cjs/utils/HeaderMap.js', versions: ['4'] }, apolloHeaderMapHook)
84
120
 
85
121
  addHook(
86
- { name: '@apollo/server', file: 'dist/cjs/express4/index.js', versions: ['>=4.0.0 <5.0.0'] },
87
- apolloExpress4Hook
122
+ { name: '@apollo/server', file: 'dist/cjs/plugin/drainHttpServer/index.js', versions: ['>=5.0.0'] },
123
+ apolloDrainHttpServerHook
88
124
  )
89
125
 
90
126
  addHook(
91
- { name: '@apollo/server', file: 'dist/cjs/utils/HeaderMap.js', versions: ['>=4.0.0 <5.0.0'] },
92
- apolloHeaderMapHook
127
+ { name: '@apollo/server', file: 'dist/cjs/runHttpQuery.js', versions: ['>=5.0.0'] },
128
+ (runHttpQueryModule) => {
129
+ shimmer.wrap(runHttpQueryModule, 'runHttpQuery', function wrapRunHttpQuery (originalRunHttpQuery) {
130
+ return wrapExecuteHTTPGraphQLRequest(originalRunHttpQuery)
131
+ })
132
+
133
+ return runHttpQueryModule
134
+ }
93
135
  )
@@ -1,10 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const {
4
- channel,
5
- addHook,
6
- AsyncResource
7
- } = require('./helpers/instrument')
3
+ const { channel, addHook } = require('./helpers/instrument')
8
4
  const shimmer = require('../../datadog-shimmer')
9
5
 
10
6
  function wrapRequest (send) {
@@ -15,28 +11,26 @@ function wrapRequest (send) {
15
11
  const channelSuffix = getChannelSuffix(serviceIdentifier)
16
12
  const startCh = channel(`apm:aws:request:start:${channelSuffix}`)
17
13
  if (!startCh.hasSubscribers) return send.apply(this, arguments)
18
- const innerAr = new AsyncResource('apm:aws:request:inner')
19
- const outerAr = new AsyncResource('apm:aws:request:outer')
20
-
21
- return innerAr.runInAsyncScope(() => {
22
- this.on('complete', innerAr.bind(response => {
23
- const cbExists = typeof cb === 'function'
24
- channel(`apm:aws:request:complete:${channelSuffix}`).publish({ response, cbExists })
25
- }))
26
-
27
- startCh.publish({
28
- serviceIdentifier,
29
- operation: this.operation,
30
- awsRegion: this.service.config && this.service.config.region,
31
- awsService: this.service.api && this.service.api.className,
32
- request: this
33
- })
34
14
 
35
- if (typeof cb === 'function') {
36
- arguments[0] = wrapCb(cb, channelSuffix, this, outerAr)
37
- }
38
- return send.apply(this, arguments)
15
+ const ctx = {
16
+ serviceIdentifier,
17
+ operation: this.operation,
18
+ awsRegion: this.service.config && this.service.config.region,
19
+ awsService: this.service.api && this.service.api.className,
20
+ request: this,
21
+ cbExists: typeof cb === 'function'
22
+ }
23
+
24
+ this.on('complete', response => {
25
+ ctx.response = response
26
+ channel(`apm:aws:request:complete:${channelSuffix}`).publish(ctx)
39
27
  })
28
+
29
+ if (ctx.cbExists) {
30
+ arguments[0] = wrapCb(cb, channelSuffix, ctx)
31
+ }
32
+
33
+ return startCh.runStores(ctx, send, this, ...arguments)
40
34
  }
41
35
  }
42
36
 
@@ -55,8 +49,6 @@ function wrapDeserialize (deserialize, channelSuffix) {
55
49
  function wrapSmithySend (send) {
56
50
  return function (command, ...args) {
57
51
  const cb = args.at(-1)
58
- const innerAr = new AsyncResource('apm:aws:request:inner')
59
- const outerAr = new AsyncResource('apm:aws:request:outer')
60
52
  const serviceIdentifier = this.config.serviceId.toLowerCase()
61
53
  const channelSuffix = getChannelSuffix(serviceIdentifier)
62
54
  const commandName = command.constructor.name
@@ -77,46 +69,45 @@ function wrapSmithySend (send) {
77
69
  shimmer.wrap(command, 'deserialize', deserialize => wrapDeserialize(deserialize, channelSuffix))
78
70
  }
79
71
 
80
- return innerAr.runInAsyncScope(() => {
81
- startCh.publish({
82
- serviceIdentifier,
83
- operation,
84
- awsService: clientName,
85
- request
86
- })
72
+ const ctx = {
73
+ serviceIdentifier,
74
+ operation,
75
+ awsService: clientName,
76
+ request
77
+ }
87
78
 
79
+ return startCh.runStores(ctx, () => {
88
80
  // When the region is not set this never resolves so we can't await.
89
81
  this.config.region().then(region => {
90
- regionCh.publish(region)
82
+ ctx.region = region
83
+ regionCh.publish(ctx)
91
84
  })
92
85
 
93
86
  if (typeof cb === 'function') {
94
87
  args[args.length - 1] = shimmer.wrapFunction(cb, cb => function (err, result) {
95
- const message = getMessage(request, err, result)
88
+ addResponse(ctx, err, result)
96
89
 
97
- completeChannel.publish(message)
90
+ completeChannel.publish(ctx)
98
91
 
99
- outerAr.runInAsyncScope(() => {
100
- responseStartChannel.publish(message)
92
+ const responseCtx = { request, response: ctx.response }
101
93
 
94
+ responseStartChannel.runStores(responseCtx, () => {
102
95
  cb.apply(this, arguments)
103
96
 
104
- if (message.needsFinish) {
105
- responseFinishChannel.publish(message.response.error)
106
- }
97
+ responseFinishChannel.publish(responseCtx)
107
98
  })
108
99
  })
109
100
  } else { // always a promise
110
101
  return send.call(this, command, ...args)
111
102
  .then(
112
103
  result => {
113
- const message = getMessage(request, null, result)
114
- completeChannel.publish(message)
104
+ addResponse(ctx, null, result)
105
+ completeChannel.publish(ctx)
115
106
  return result
116
107
  },
117
108
  error => {
118
- const message = getMessage(request, error)
119
- completeChannel.publish(message)
109
+ addResponse(ctx, error)
110
+ completeChannel.publish(ctx)
120
111
  throw error
121
112
  }
122
113
  )
@@ -127,47 +118,45 @@ function wrapSmithySend (send) {
127
118
  }
128
119
  }
129
120
 
130
- function wrapCb (cb, serviceName, request, ar) {
121
+ function wrapCb (cb, serviceName, ctx) {
131
122
  // eslint-disable-next-line n/handle-callback-err
132
123
  return shimmer.wrapFunction(cb, cb => function wrappedCb (err, response) {
133
- const obj = { request, response }
134
- return ar.runInAsyncScope(() => {
135
- channel(`apm:aws:response:start:${serviceName}`).publish(obj)
136
- // TODO(bengl) make this work without needing a needsFinish property added to the object
137
- if (!obj.needsFinish) {
138
- return cb.apply(this, arguments)
139
- }
124
+ ctx = { request: ctx.request, response }
125
+ return channel(`apm:aws:response:start:${serviceName}`).runStores(ctx, () => {
140
126
  const finishChannel = channel(`apm:aws:response:finish:${serviceName}`)
141
127
  try {
142
128
  let result = cb.apply(this, arguments)
143
129
  if (result && result.then) {
144
130
  result = result.then(x => {
145
- finishChannel.publish()
131
+ finishChannel.publish(ctx)
146
132
  return x
147
133
  }, e => {
148
- finishChannel.publish(e)
134
+ ctx.error = e
135
+ finishChannel.publish(ctx)
149
136
  throw e
150
137
  })
151
138
  } else {
152
- finishChannel.publish()
139
+ finishChannel.publish(ctx)
153
140
  }
154
141
  return result
155
142
  } catch (e) {
156
- finishChannel.publish(e)
143
+ ctx.error = e
144
+ finishChannel.publish(ctx)
157
145
  throw e
158
146
  }
159
147
  })
160
148
  })
161
149
  }
162
150
 
163
- function getMessage (request, error, result) {
151
+ function addResponse (ctx, error, result) {
152
+ const request = ctx.request
164
153
  const response = { request, error, ...result }
165
154
 
166
155
  if (result && result.$metadata) {
167
156
  response.requestId = result.$metadata.requestId
168
157
  }
169
158
 
170
- return { request, response }
159
+ ctx.response = response
171
160
  }
172
161
 
173
162
  function getChannelSuffix (name) {