dd-trace 5.61.0 → 5.62.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 (192) hide show
  1. package/package.json +1 -1
  2. package/packages/datadog-instrumentations/src/apollo-server.js +50 -8
  3. package/packages/datadog-instrumentations/src/aws-sdk.js +49 -60
  4. package/packages/datadog-instrumentations/src/fastify.js +61 -55
  5. package/packages/datadog-instrumentations/src/graphql.js +90 -122
  6. package/packages/datadog-instrumentations/src/http2/server.js +14 -20
  7. package/packages/datadog-instrumentations/src/microgateway-core.js +16 -15
  8. package/packages/datadog-instrumentations/src/mongodb-core.js +34 -29
  9. package/packages/datadog-instrumentations/src/mongodb.js +9 -13
  10. package/packages/datadog-instrumentations/src/mongoose.js +25 -29
  11. package/packages/datadog-instrumentations/src/openai.js +0 -2
  12. package/packages/datadog-instrumentations/src/oracledb.js +39 -33
  13. package/packages/datadog-instrumentations/src/router.js +1 -1
  14. package/packages/datadog-plugin-aerospike/src/index.js +5 -9
  15. package/packages/datadog-plugin-amqp10/src/consumer.js +2 -2
  16. package/packages/datadog-plugin-amqp10/src/index.js +1 -1
  17. package/packages/datadog-plugin-amqp10/src/producer.js +3 -3
  18. package/packages/datadog-plugin-amqplib/src/client.js +3 -3
  19. package/packages/datadog-plugin-amqplib/src/consumer.js +2 -2
  20. package/packages/datadog-plugin-amqplib/src/index.js +1 -1
  21. package/packages/datadog-plugin-amqplib/src/producer.js +2 -2
  22. package/packages/datadog-plugin-apollo/src/gateway/execute.js +2 -4
  23. package/packages/datadog-plugin-apollo/src/gateway/fetch.js +2 -4
  24. package/packages/datadog-plugin-apollo/src/gateway/index.js +1 -1
  25. package/packages/datadog-plugin-apollo/src/gateway/plan.js +2 -4
  26. package/packages/datadog-plugin-apollo/src/gateway/postprocessing.js +2 -4
  27. package/packages/datadog-plugin-apollo/src/gateway/request.js +2 -4
  28. package/packages/datadog-plugin-apollo/src/gateway/validate.js +2 -4
  29. package/packages/datadog-plugin-apollo/src/index.js +1 -1
  30. package/packages/datadog-plugin-avsc/src/index.js +2 -2
  31. package/packages/datadog-plugin-aws-sdk/src/base.js +70 -46
  32. package/packages/datadog-plugin-aws-sdk/src/index.js +1 -3
  33. package/packages/datadog-plugin-aws-sdk/src/services/bedrockruntime/index.js +1 -3
  34. package/packages/datadog-plugin-aws-sdk/src/services/bedrockruntime/tracing.js +1 -1
  35. package/packages/datadog-plugin-aws-sdk/src/services/cloudwatchlogs.js +1 -1
  36. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +3 -3
  37. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +2 -2
  38. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +22 -20
  39. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +1 -1
  40. package/packages/datadog-plugin-aws-sdk/src/services/redshift.js +1 -1
  41. package/packages/datadog-plugin-aws-sdk/src/services/s3.js +3 -3
  42. package/packages/datadog-plugin-aws-sdk/src/services/sfn.js +1 -1
  43. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +3 -3
  44. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +17 -15
  45. package/packages/datadog-plugin-aws-sdk/src/services/states.js +1 -1
  46. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +1 -1
  47. package/packages/datadog-plugin-azure-functions/src/index.js +5 -5
  48. package/packages/datadog-plugin-azure-service-bus/src/index.js +1 -1
  49. package/packages/datadog-plugin-azure-service-bus/src/producer.js +2 -2
  50. package/packages/datadog-plugin-bunyan/src/index.js +3 -5
  51. package/packages/datadog-plugin-cassandra-driver/src/index.js +3 -3
  52. package/packages/datadog-plugin-child_process/src/index.js +2 -2
  53. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/batch-consumer.js +1 -3
  54. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/consumer.js +1 -3
  55. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/index.js +1 -1
  56. package/packages/datadog-plugin-confluentinc-kafka-javascript/src/producer.js +1 -3
  57. package/packages/datadog-plugin-connect/src/index.js +1 -3
  58. package/packages/datadog-plugin-couchbase/src/index.js +2 -2
  59. package/packages/datadog-plugin-cucumber/src/index.js +1 -3
  60. package/packages/datadog-plugin-cypress/src/index.js +1 -3
  61. package/packages/datadog-plugin-dd-trace-api/src/index.js +1 -3
  62. package/packages/datadog-plugin-dns/src/index.js +1 -1
  63. package/packages/datadog-plugin-dns/src/lookup.js +2 -2
  64. package/packages/datadog-plugin-dns/src/lookup_service.js +2 -2
  65. package/packages/datadog-plugin-dns/src/resolve.js +2 -2
  66. package/packages/datadog-plugin-dns/src/reverse.js +2 -2
  67. package/packages/datadog-plugin-elasticsearch/src/index.js +1 -1
  68. package/packages/datadog-plugin-express/src/code_origin.js +1 -3
  69. package/packages/datadog-plugin-express/src/index.js +1 -1
  70. package/packages/datadog-plugin-express/src/tracing.js +1 -3
  71. package/packages/datadog-plugin-fastify/src/code_origin.js +1 -3
  72. package/packages/datadog-plugin-fastify/src/index.js +1 -1
  73. package/packages/datadog-plugin-fastify/src/tracing.js +18 -3
  74. package/packages/datadog-plugin-fetch/src/index.js +2 -2
  75. package/packages/datadog-plugin-find-my-way/src/index.js +1 -3
  76. package/packages/datadog-plugin-fs/src/index.js +2 -2
  77. package/packages/datadog-plugin-google-cloud-pubsub/src/client.js +3 -3
  78. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +2 -2
  79. package/packages/datadog-plugin-google-cloud-pubsub/src/index.js +1 -1
  80. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -2
  81. package/packages/datadog-plugin-google-cloud-vertexai/src/index.js +1 -1
  82. package/packages/datadog-plugin-google-cloud-vertexai/src/tracing.js +2 -4
  83. package/packages/datadog-plugin-graphql/src/execute.js +16 -9
  84. package/packages/datadog-plugin-graphql/src/index.js +1 -1
  85. package/packages/datadog-plugin-graphql/src/parse.js +12 -7
  86. package/packages/datadog-plugin-graphql/src/resolve.js +50 -16
  87. package/packages/datadog-plugin-graphql/src/validate.js +13 -7
  88. package/packages/datadog-plugin-grpc/src/client.js +4 -4
  89. package/packages/datadog-plugin-grpc/src/index.js +1 -1
  90. package/packages/datadog-plugin-grpc/src/server.js +3 -3
  91. package/packages/datadog-plugin-hapi/src/index.js +1 -3
  92. package/packages/datadog-plugin-hono/src/index.js +1 -3
  93. package/packages/datadog-plugin-http/src/client.js +2 -2
  94. package/packages/datadog-plugin-http/src/index.js +1 -1
  95. package/packages/datadog-plugin-http/src/server.js +3 -7
  96. package/packages/datadog-plugin-http2/src/client.js +2 -2
  97. package/packages/datadog-plugin-http2/src/index.js +1 -1
  98. package/packages/datadog-plugin-http2/src/server.js +22 -11
  99. package/packages/datadog-plugin-ioredis/src/index.js +1 -3
  100. package/packages/datadog-plugin-iovalkey/src/index.js +2 -4
  101. package/packages/datadog-plugin-jest/src/index.js +1 -3
  102. package/packages/datadog-plugin-kafkajs/src/batch-consumer.js +2 -2
  103. package/packages/datadog-plugin-kafkajs/src/consumer.js +2 -2
  104. package/packages/datadog-plugin-kafkajs/src/index.js +1 -1
  105. package/packages/datadog-plugin-kafkajs/src/producer.js +3 -3
  106. package/packages/datadog-plugin-koa/src/index.js +1 -3
  107. package/packages/datadog-plugin-langchain/src/index.js +2 -2
  108. package/packages/datadog-plugin-langchain/src/tracing.js +30 -48
  109. package/packages/datadog-plugin-mariadb/src/index.js +2 -2
  110. package/packages/datadog-plugin-memcached/src/index.js +1 -1
  111. package/packages/datadog-plugin-microgateway-core/src/index.js +4 -4
  112. package/packages/datadog-plugin-mocha/src/index.js +1 -3
  113. package/packages/datadog-plugin-moleculer/src/client.js +2 -2
  114. package/packages/datadog-plugin-moleculer/src/index.js +1 -1
  115. package/packages/datadog-plugin-moleculer/src/server.js +2 -2
  116. package/packages/datadog-plugin-mongodb-core/src/index.js +9 -5
  117. package/packages/datadog-plugin-mongoose/src/index.js +20 -0
  118. package/packages/datadog-plugin-mysql/src/index.js +2 -2
  119. package/packages/datadog-plugin-mysql2/src/index.js +1 -1
  120. package/packages/datadog-plugin-net/src/index.js +1 -1
  121. package/packages/datadog-plugin-net/src/ipc.js +2 -2
  122. package/packages/datadog-plugin-net/src/tcp.js +2 -2
  123. package/packages/datadog-plugin-next/src/index.js +1 -3
  124. package/packages/datadog-plugin-nyc/src/index.js +1 -3
  125. package/packages/datadog-plugin-openai/src/index.js +1 -1
  126. package/packages/datadog-plugin-openai/src/tracing.js +7 -411
  127. package/packages/datadog-plugin-opensearch/src/index.js +1 -3
  128. package/packages/datadog-plugin-oracledb/src/index.js +9 -5
  129. package/packages/datadog-plugin-pg/src/index.js +3 -3
  130. package/packages/datadog-plugin-pino/src/index.js +3 -5
  131. package/packages/datadog-plugin-playwright/src/index.js +1 -3
  132. package/packages/datadog-plugin-prisma/src/client.js +4 -6
  133. package/packages/datadog-plugin-prisma/src/engine.js +3 -3
  134. package/packages/datadog-plugin-prisma/src/index.js +1 -1
  135. package/packages/datadog-plugin-protobufjs/src/index.js +2 -6
  136. package/packages/datadog-plugin-redis/src/index.js +2 -2
  137. package/packages/datadog-plugin-restify/src/index.js +1 -3
  138. package/packages/datadog-plugin-rhea/src/consumer.js +1 -1
  139. package/packages/datadog-plugin-rhea/src/index.js +1 -1
  140. package/packages/datadog-plugin-rhea/src/producer.js +2 -2
  141. package/packages/datadog-plugin-router/src/index.js +1 -3
  142. package/packages/datadog-plugin-selenium/src/index.js +1 -3
  143. package/packages/datadog-plugin-sharedb/src/index.js +1 -1
  144. package/packages/datadog-plugin-tedious/src/index.js +3 -3
  145. package/packages/datadog-plugin-undici/src/index.js +2 -4
  146. package/packages/datadog-plugin-vitest/src/index.js +1 -3
  147. package/packages/datadog-plugin-web/src/index.js +1 -3
  148. package/packages/datadog-plugin-winston/src/index.js +3 -5
  149. package/packages/dd-trace/src/appsec/channels.js +1 -0
  150. package/packages/dd-trace/src/appsec/graphql.js +14 -12
  151. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +1 -1
  152. package/packages/dd-trace/src/appsec/waf/waf_manager.js +1 -1
  153. package/packages/dd-trace/src/ci-visibility/log-submission/log-submission-plugin.js +1 -3
  154. package/packages/dd-trace/src/ci-visibility/test-api-manual/test-api-manual-plugin.js +1 -3
  155. package/packages/dd-trace/src/config.js +1 -1
  156. package/packages/dd-trace/src/datastreams/checkpointer.js +23 -2
  157. package/packages/dd-trace/src/datastreams/processor.js +4 -3
  158. package/packages/dd-trace/src/llmobs/plugins/langchain/index.js +30 -50
  159. package/packages/dd-trace/src/llmobs/plugins/openai.js +3 -5
  160. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +3 -5
  161. package/packages/dd-trace/src/opentracing/propagation/text_map.js +25 -2
  162. package/packages/dd-trace/src/plugins/apollo.js +3 -3
  163. package/packages/dd-trace/src/plugins/cache.js +1 -1
  164. package/packages/dd-trace/src/plugins/client.js +3 -3
  165. package/packages/dd-trace/src/plugins/consumer.js +3 -3
  166. package/packages/dd-trace/src/plugins/database.js +2 -2
  167. package/packages/dd-trace/src/plugins/index.js +1 -0
  168. package/packages/dd-trace/src/plugins/log_plugin.js +1 -5
  169. package/packages/dd-trace/src/plugins/outbound.js +1 -1
  170. package/packages/dd-trace/src/plugins/plugin.js +1 -1
  171. package/packages/dd-trace/src/plugins/producer.js +3 -3
  172. package/packages/dd-trace/src/plugins/server.js +3 -3
  173. package/packages/dd-trace/src/plugins/storage.js +1 -1
  174. package/packages/dd-trace/src/plugins/tracing.js +24 -6
  175. package/packages/dd-trace/src/plugins/util/ci.js +11 -7
  176. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +15 -19
  177. package/packages/dd-trace/src/plugins/util/tags.js +2 -0
  178. package/packages/dd-trace/src/plugins/util/web.js +26 -7
  179. package/packages/dd-trace/src/profiling/config.js +2 -0
  180. package/packages/dd-trace/src/profiling/exporters/event_serializer.js +2 -21
  181. package/packages/dd-trace/src/profiling/libuv-size.js +49 -0
  182. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns.js +2 -6
  183. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_lookup.js +1 -3
  184. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_lookupservice.js +1 -3
  185. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_resolve.js +1 -3
  186. package/packages/dd-trace/src/profiling/profilers/event_plugins/dns_reverse.js +1 -3
  187. package/packages/dd-trace/src/profiling/profilers/event_plugins/fs.js +3 -9
  188. package/packages/dd-trace/src/profiling/profilers/event_plugins/net.js +3 -9
  189. package/packages/dd-trace/src/profiling/profilers/events.js +80 -62
  190. package/packages/dd-trace/src/profiling/profilers/poisson.js +105 -0
  191. package/packages/dd-trace/src/profiling/profilers/wall.js +1 -1
  192. package/packages/dd-trace/src/remote_config/manager.js +1 -1
@@ -2,8 +2,7 @@
2
2
 
3
3
  const {
4
4
  addHook,
5
- channel,
6
- AsyncResource
5
+ channel
7
6
  } = require('./helpers/instrument')
8
7
  const shimmer = require('../../datadog-shimmer')
9
8
 
@@ -89,29 +88,28 @@ function wrapParse (parse) {
89
88
  return parse.apply(this, arguments)
90
89
  }
91
90
 
92
- const asyncResource = new AsyncResource('bound-anonymous-fn')
93
-
94
- return asyncResource.runInAsyncScope(() => {
95
- parseStartCh.publish()
96
- let document
91
+ const ctx = { source }
92
+ return parseStartCh.runStores(ctx, () => {
97
93
  try {
98
- document = parse.apply(this, arguments)
99
- const operation = getOperation(document)
94
+ ctx.document = parse.apply(this, arguments)
95
+ const operation = getOperation(ctx.document)
100
96
 
101
- if (!operation) return document
97
+ if (!operation) return ctx.document
102
98
 
103
99
  if (source) {
104
- documentSources.set(document, source.body || source)
100
+ documentSources.set(ctx.document, source.body || source)
105
101
  }
102
+ ctx.docSource = documentSources.get(ctx.document)
106
103
 
107
- return document
104
+ return ctx.document
108
105
  } catch (err) {
109
106
  err.stack
110
- parseErrorCh.publish(err)
107
+ ctx.error = err
108
+ parseErrorCh.publish(ctx)
111
109
 
112
110
  throw err
113
111
  } finally {
114
- parseFinishCh.publish({ source, document, docSource: documentSources.get(document) })
112
+ parseFinishCh.publish(ctx)
115
113
  }
116
114
  })
117
115
  }
@@ -123,25 +121,25 @@ function wrapValidate (validate) {
123
121
  return validate.apply(this, arguments)
124
122
  }
125
123
 
126
- const asyncResource = new AsyncResource('bound-anonymous-fn')
127
-
128
- return asyncResource.runInAsyncScope(() => {
129
- validateStartCh.publish({ docSource: documentSources.get(document), document })
130
-
124
+ const ctx = { docSource: documentSources.get(document), document }
125
+ return validateStartCh.runStores(ctx, () => {
131
126
  let errors
132
127
  try {
133
128
  errors = validate.apply(this, arguments)
134
129
  if (errors && errors[0]) {
135
- validateErrorCh.publish(errors && errors[0])
130
+ ctx.error = errors && errors[0]
131
+ validateErrorCh.publish(ctx)
136
132
  }
137
133
  return errors
138
134
  } catch (err) {
139
135
  err.stack
140
- validateErrorCh.publish(err)
136
+ ctx.error = err
137
+ validateErrorCh.publish(ctx)
141
138
 
142
139
  throw err
143
140
  } finally {
144
- validateFinishCh.publish({ document, errors })
141
+ ctx.errors = errors
142
+ validateFinishCh.publish(ctx)
145
143
  }
146
144
  })
147
145
  }
@@ -155,44 +153,46 @@ function wrapExecute (execute) {
155
153
  return exe.apply(this, arguments)
156
154
  }
157
155
 
158
- const asyncResource = new AsyncResource('bound-anonymous-fn')
159
- return asyncResource.runInAsyncScope(() => {
160
- const args = normalizeArgs(arguments, defaultFieldResolver)
161
- const schema = args.schema
162
- const document = args.document
163
- const source = documentSources.get(document)
164
- const contextValue = args.contextValue
165
- const operation = getOperation(document, args.operationName)
166
-
167
- if (contexts.has(contextValue)) {
168
- return exe.apply(this, arguments)
169
- }
156
+ const args = normalizeArgs(arguments, defaultFieldResolver)
157
+ const schema = args.schema
158
+ const document = args.document
159
+ const source = documentSources.get(document)
160
+ const contextValue = args.contextValue
161
+ const operation = getOperation(document, args.operationName)
162
+
163
+ if (contexts.has(contextValue)) {
164
+ return exe.apply(this, arguments)
165
+ }
166
+
167
+ const ctx = {
168
+ operation,
169
+ args,
170
+ docSource: documentSources.get(document),
171
+ source,
172
+ fields: {},
173
+ abortController: new AbortController()
174
+ }
170
175
 
176
+ return startExecuteCh.runStores(ctx, () => {
171
177
  if (schema) {
172
178
  wrapFields(schema._queryType)
173
179
  wrapFields(schema._mutationType)
174
180
  }
175
181
 
176
- startExecuteCh.publish({
177
- operation,
178
- args,
179
- docSource: documentSources.get(document)
180
- })
181
-
182
- const context = { source, asyncResource, fields: {}, abortController: new AbortController() }
183
-
184
- contexts.set(contextValue, context)
182
+ contexts.set(contextValue, ctx)
185
183
 
186
- return callInAsyncScope(exe, asyncResource, this, arguments, context.abortController, (err, res) => {
187
- if (finishResolveCh.hasSubscribers) finishResolvers(context)
184
+ return callInAsyncScope(exe, this, arguments, ctx.abortController, (err, res) => {
185
+ if (finishResolveCh.hasSubscribers) finishResolvers(ctx)
188
186
 
189
187
  const error = err || (res && res.errors && res.errors[0])
190
188
 
191
189
  if (error) {
192
- executeErrorCh.publish(error)
190
+ ctx.error = error
191
+ executeErrorCh.publish(ctx)
193
192
  }
194
193
 
195
- finishExecuteCh.publish({ res, args, context })
194
+ ctx.res = res
195
+ finishExecuteCh.publish(ctx)
196
196
  })
197
197
  })
198
198
  }
@@ -205,14 +205,17 @@ function wrapResolve (resolve) {
205
205
  function resolveAsync (source, args, contextValue, info) {
206
206
  if (!startResolveCh.hasSubscribers) return resolve.apply(this, arguments)
207
207
 
208
- const context = contexts.get(contextValue)
208
+ const ctx = contexts.get(contextValue)
209
209
 
210
- if (!context) return resolve.apply(this, arguments)
210
+ if (!ctx) return resolve.apply(this, arguments)
211
211
 
212
- const field = assertField(context, info, args)
212
+ const field = assertField(ctx, info, args)
213
213
 
214
- return callInAsyncScope(resolve, field.asyncResource, this, arguments, context.abortController, (err) => {
215
- updateFieldCh.publish({ field, info, err })
214
+ return callInAsyncScope(resolve, this, arguments, ctx.abortController, (err) => {
215
+ field.ctx.error = err
216
+ field.ctx.info = info
217
+ field.ctx.field = field
218
+ updateFieldCh.publish(field.ctx)
216
219
  })
217
220
  }
218
221
 
@@ -221,32 +224,30 @@ function wrapResolve (resolve) {
221
224
  return resolveAsync
222
225
  }
223
226
 
224
- function callInAsyncScope (fn, aR, thisArg, args, abortController, cb) {
227
+ function callInAsyncScope (fn, thisArg, args, abortController, cb) {
225
228
  cb = cb || (() => {})
226
229
 
227
- return aR.runInAsyncScope(() => {
228
- if (abortController?.signal.aborted) {
229
- cb(null, null)
230
- throw new AbortError('Aborted')
231
- }
230
+ if (abortController?.signal.aborted) {
231
+ cb(null, null)
232
+ throw new AbortError('Aborted')
233
+ }
232
234
 
233
- try {
234
- const result = fn.apply(thisArg, args)
235
- if (result && typeof result.then === 'function') {
236
- // bind callback to this scope
237
- result.then(
238
- aR.bind(res => cb(null, res)),
239
- aR.bind(err => cb(err))
240
- )
241
- } else {
242
- cb(null, result)
243
- }
244
- return result
245
- } catch (err) {
246
- cb(err)
247
- throw err
235
+ try {
236
+ const result = fn.apply(thisArg, args)
237
+ if (result && typeof result.then === 'function') {
238
+ // bind callback to this scope
239
+ result.then(
240
+ res => cb(null, res),
241
+ err => cb(err)
242
+ )
243
+ } else {
244
+ cb(null, result)
248
245
  }
249
- })
246
+ return result
247
+ } catch (err) {
248
+ cb(err)
249
+ throw err
250
+ }
250
251
  }
251
252
 
252
253
  function pathToArray (path) {
@@ -259,59 +260,26 @@ function pathToArray (path) {
259
260
  return flattened.reverse()
260
261
  }
261
262
 
262
- function assertField (context, info, args) {
263
+ function assertField (rootCtx, info, args) {
263
264
  const pathInfo = info && info.path
264
265
 
265
266
  const path = pathToArray(pathInfo)
266
267
 
267
268
  const pathString = path.join('.')
268
- const fields = context.fields
269
+ const fields = rootCtx.fields
269
270
 
270
271
  let field = fields[pathString]
271
272
 
272
273
  if (!field) {
273
- const parent = getParentField(context, path)
274
-
275
- // we want to spawn the new span off of the parent, not a new async resource
276
- parent.asyncResource.runInAsyncScope(() => {
277
- /* this child resource will run a branched scope off of the parent resource, which
278
- accesses the parent span from the storage unit in its own scope */
279
- const childResource = new AsyncResource('bound-anonymous-fn')
280
-
281
- childResource.runInAsyncScope(() => {
282
- startResolveCh.publish({
283
- info,
284
- context,
285
- args
286
- })
287
- })
288
-
289
- field = fields[pathString] = {
290
- parent,
291
- asyncResource: childResource,
292
- error: null
293
- }
294
- })
295
- }
296
-
297
- return field
298
- }
299
-
300
- function getParentField (context, path) {
301
- for (let i = path.length - 1; i > 0; i--) {
302
- const field = getField(context, path.slice(0, i))
303
- if (field) {
304
- return field
274
+ const fieldCtx = { info, rootCtx, args }
275
+ startResolveCh.publish(fieldCtx)
276
+ field = fields[pathString] = {
277
+ error: null,
278
+ ctx: fieldCtx
305
279
  }
306
280
  }
307
281
 
308
- return {
309
- asyncResource: context.asyncResource
310
- }
311
- }
312
-
313
- function getField (context, path) {
314
- return context.fields[path.join('.')]
282
+ return field
315
283
  }
316
284
 
317
285
  function wrapFields (type) {
@@ -349,13 +317,13 @@ function wrapFieldType (field) {
349
317
  function finishResolvers ({ fields }) {
350
318
  Object.keys(fields).reverse().forEach(key => {
351
319
  const field = fields[key]
352
- const asyncResource = field.asyncResource
353
- asyncResource.runInAsyncScope(() => {
354
- if (field.error) {
355
- resolveErrorCh.publish(field.error)
356
- }
357
- finishResolveCh.publish(field.finishTime)
358
- })
320
+ field.ctx.finishTime = field.finishTime
321
+ field.ctx.field = field
322
+ if (field.error) {
323
+ field.ctx.error = field.error
324
+ resolveErrorCh.publish(field.ctx)
325
+ }
326
+ finishResolveCh.publish(field.ctx)
359
327
  })
360
328
  }
361
329
 
@@ -5,14 +5,13 @@
5
5
 
6
6
  const {
7
7
  channel,
8
- addHook,
9
- AsyncResource
8
+ addHook
10
9
  } = require('../helpers/instrument')
11
10
  const shimmer = require('../../../datadog-shimmer')
12
11
 
13
12
  const startServerCh = channel('apm:http2:server:request:start')
14
13
  const errorServerCh = channel('apm:http2:server:request:error')
15
- const finishServerCh = channel('apm:http2:server:request:finish')
14
+ const emitCh = channel('apm:http2:server:response:emit')
16
15
 
17
16
  const names = ['http2', 'node:http2']
18
17
 
@@ -30,18 +29,14 @@ function wrapCreateServer (createServer) {
30
29
  }
31
30
  }
32
31
 
33
- function wrapResponseEmit (emit) {
34
- const asyncResource = new AsyncResource('bound-anonymous-fn')
32
+ function wrapResponseEmit (emit, ctx) {
35
33
  return function (eventName, event) {
36
- return asyncResource.runInAsyncScope(() => {
37
- if (eventName === 'close' && finishServerCh.hasSubscribers) {
38
- finishServerCh.publish({ req: this.req })
39
- }
40
-
41
- return emit.apply(this, arguments)
42
- })
34
+ ctx.req = this.req
35
+ ctx.eventName = eventName
36
+ return emitCh.runStores(ctx, emit, this, ...arguments)
43
37
  }
44
38
  }
39
+
45
40
  function wrapEmit (emit) {
46
41
  return function (eventName, req, res) {
47
42
  if (!startServerCh.hasSubscribers) {
@@ -51,18 +46,17 @@ function wrapEmit (emit) {
51
46
  if (eventName === 'request') {
52
47
  res.req = req
53
48
 
54
- const asyncResource = new AsyncResource('bound-anonymous-fn')
55
- return asyncResource.runInAsyncScope(() => {
56
- startServerCh.publish({ req, res })
57
-
58
- shimmer.wrap(res, 'emit', wrapResponseEmit)
49
+ const ctx = { req, res }
50
+ return startServerCh.runStores(ctx, () => {
51
+ shimmer.wrap(res, 'emit', emit => wrapResponseEmit(emit, ctx))
59
52
 
60
53
  try {
61
54
  return emit.apply(this, arguments)
62
- } catch (err) {
63
- errorServerCh.publish(err)
55
+ } catch (error) {
56
+ ctx.error = error
57
+ errorServerCh.publish(ctx)
64
58
 
65
- throw err
59
+ throw error
66
60
  }
67
61
  })
68
62
  }
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const shimmer = require('../../datadog-shimmer')
4
- const { addHook, channel, AsyncResource } = require('./helpers/instrument')
4
+ const { addHook, channel } = require('./helpers/instrument')
5
5
 
6
6
  const handleChannel = channel('apm:microgateway-core:request:handle')
7
7
  const routeChannel = channel('apm:microgateway-core:request:route')
@@ -11,18 +11,18 @@ const name = 'microgateway-core'
11
11
 
12
12
  // TODO Remove " <=3.0.0" when "volos-util-apigee" module is fixed
13
13
  const versions = ['>=2.1 <=3.0.0']
14
- const requestResources = new WeakMap()
14
+ const requestContexts = new WeakMap()
15
15
 
16
16
  function wrapConfigProxyFactory (configProxyFactory) {
17
17
  return function () {
18
18
  const configProxy = configProxyFactory.apply(this, arguments)
19
19
 
20
20
  return function (req, res, next) {
21
- const requestResource = new AsyncResource('bound-anonymous-fn')
21
+ const ctx = { req, res }
22
22
 
23
- requestResources.set(req, requestResource)
23
+ requestContexts.set(req, ctx)
24
24
 
25
- handleChannel.publish({ req, res })
25
+ handleChannel.publish(ctx)
26
26
 
27
27
  return configProxy.apply(this, arguments)
28
28
  }
@@ -43,18 +43,19 @@ function wrapPluginsFactory (pluginsFactory) {
43
43
 
44
44
  function wrapNext (req, res, next) {
45
45
  return shimmer.wrapFunction(next, next => function nextWithTrace (err) {
46
- const requestResource = requestResources.get(req)
46
+ const ctx = requestContexts.get(req)
47
47
 
48
- requestResource.runInAsyncScope(() => {
49
- if (err) {
50
- errorChannel.publish(err)
51
- }
52
-
53
- if (res.proxy && res.proxy.base_path) {
54
- routeChannel.publish({ req, res, route: res.proxy.base_path })
55
- }
56
- })
48
+ if (err) {
49
+ ctx.error = err
50
+ errorChannel.publish(ctx)
51
+ }
57
52
 
53
+ if (res.proxy && res.proxy.base_path) {
54
+ ctx.req = req
55
+ ctx.res = res
56
+ ctx.route = res.proxy.base_path
57
+ routeChannel.publish(ctx)
58
+ }
58
59
  return next.apply(this, arguments)
59
60
  })
60
61
  }
@@ -148,61 +148,66 @@ function wrapCommand (command, operation, name) {
148
148
  return wrapped
149
149
  }
150
150
 
151
- function instrument (operation, command, ctx, args, server, ns, ops, options = {}) {
151
+ function instrument (operation, command, instance, args, server, ns, ops, options = {}) {
152
152
  const name = options.name || (ops && Object.keys(ops)[0])
153
153
  const index = args.length - 1
154
- let callback = args[index]
154
+ const callback = args[index]
155
155
 
156
- if (typeof callback !== 'function') return command.apply(ctx, args)
156
+ if (typeof callback !== 'function') return command.apply(instance, args)
157
157
 
158
158
  const serverInfo = server && server.s && server.s.options
159
- const callbackResource = new AsyncResource('bound-anonymous-fn')
160
- const asyncResource = new AsyncResource('bound-anonymous-fn')
161
159
 
162
- callback = callbackResource.bind(callback)
163
-
164
- return asyncResource.runInAsyncScope(() => {
165
- startCh.publish({ ns, ops, options: serverInfo, name })
166
-
167
- args[index] = shimmer.wrapFunction(callback, callback => asyncResource.bind(function (err, res) {
160
+ const ctx = {
161
+ ns,
162
+ ops,
163
+ options: serverInfo,
164
+ name
165
+ }
166
+ return startCh.runStores(ctx, () => {
167
+ args[index] = shimmer.wrapFunction(callback, callback => function (err, res) {
168
168
  if (err) {
169
- errorCh.publish(err)
169
+ ctx.error = err
170
+ errorCh.publish(ctx)
170
171
  }
171
172
 
172
- finishCh.publish()
173
-
174
- if (callback) {
175
- return callback.apply(this, arguments)
176
- }
177
- }))
173
+ return finishCh.runStores(ctx, callback, this, ...arguments)
174
+ })
178
175
 
179
176
  try {
180
- return command.apply(ctx, args)
177
+ return command.apply(instance, args)
181
178
  } catch (err) {
182
- errorCh.publish(err)
179
+ ctx.error = err
180
+ errorCh.publish(ctx)
183
181
 
184
182
  throw err
185
183
  }
186
184
  })
187
185
  }
188
186
 
189
- function instrumentPromise (operation, command, ctx, args, server, ns, ops, options = {}) {
187
+ function instrumentPromise (operation, command, instance, args, server, ns, ops, options = {}) {
190
188
  const name = options.name || (ops && Object.keys(ops)[0])
191
189
 
192
190
  const serverInfo = server && server.s && server.s.options
193
- const asyncResource = new AsyncResource('bound-anonymous-fn')
194
191
 
195
- return asyncResource.runInAsyncScope(() => {
196
- startCh.publish({ ns, ops, options: serverInfo, name })
192
+ const ctx = {
193
+ ns,
194
+ ops,
195
+ options: serverInfo,
196
+ name
197
+ }
197
198
 
198
- const promise = command.apply(ctx, args)
199
+ return startCh.runStores(ctx, () => {
200
+ const promise = command.apply(instance, args)
199
201
 
200
202
  return promise.then(function (res) {
201
- finishCh.publish()
202
- return res
203
+ ctx.result = res
204
+ return finishCh.runStores(ctx, () => {
205
+ return res
206
+ })
203
207
  }, function (err) {
204
- errorCh.publish(err)
205
- finishCh.publish()
208
+ ctx.error = err
209
+ errorCh.publish(ctx)
210
+ finishCh.publish(ctx)
206
211
 
207
212
  throw err
208
213
  })
@@ -4,8 +4,7 @@ require('./mongodb-core')
4
4
 
5
5
  const {
6
6
  channel,
7
- addHook,
8
- AsyncResource
7
+ addHook
9
8
  } = require('./helpers/instrument')
10
9
  const shimmer = require('../../datadog-shimmer')
11
10
 
@@ -41,19 +40,16 @@ addHook({ name: 'mongodb', versions: ['>=3.3 <5', '5', '>=6'] }, mongodb => {
41
40
  return method.apply(this, arguments)
42
41
  }
43
42
 
44
- const asyncResource = new AsyncResource('bound-anonymous-fn')
45
-
46
- return asyncResource.runInAsyncScope(() => {
47
- const filters = [arguments[0]]
48
- if (useTwoArguments) {
49
- filters.push(arguments[1])
50
- }
43
+ const ctx = {
44
+ filters: [arguments[0]],
45
+ methodName
46
+ }
51
47
 
52
- startCh.publish({
53
- filters,
54
- methodName
55
- })
48
+ if (useTwoArguments) {
49
+ ctx.filters.push(arguments[1])
50
+ }
56
51
 
52
+ return startCh.runStores(ctx, () => {
57
53
  return method.apply(this, arguments)
58
54
  })
59
55
  }