dd-trace 2.6.0 → 2.8.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 (118) hide show
  1. package/LICENSE-3rdparty.csv +1 -4
  2. package/index.d.ts +0 -7
  3. package/package.json +18 -24
  4. package/packages/datadog-instrumentations/index.js +16 -0
  5. package/packages/datadog-instrumentations/src/amqp10.js +33 -32
  6. package/packages/datadog-instrumentations/src/amqplib.js +16 -11
  7. package/packages/datadog-instrumentations/src/aws-sdk.js +104 -0
  8. package/packages/datadog-instrumentations/src/cassandra-driver.js +53 -51
  9. package/packages/datadog-instrumentations/src/connect.js +111 -0
  10. package/packages/datadog-instrumentations/src/couchbase.js +41 -39
  11. package/packages/datadog-instrumentations/src/cucumber.js +38 -38
  12. package/packages/datadog-instrumentations/src/dns.js +20 -19
  13. package/packages/datadog-instrumentations/src/elasticsearch.js +30 -32
  14. package/packages/datadog-instrumentations/src/express.js +27 -0
  15. package/packages/datadog-instrumentations/src/fastify.js +187 -0
  16. package/packages/datadog-instrumentations/src/find-my-way.js +30 -0
  17. package/packages/datadog-instrumentations/src/google-cloud-pubsub.js +100 -0
  18. package/packages/datadog-instrumentations/src/hapi.js +210 -0
  19. package/packages/datadog-instrumentations/src/http/client.js +44 -44
  20. package/packages/datadog-instrumentations/src/http/server.js +15 -13
  21. package/packages/datadog-instrumentations/src/ioredis.js +16 -17
  22. package/packages/datadog-instrumentations/src/jest.js +22 -17
  23. package/packages/datadog-instrumentations/src/kafkajs.js +112 -0
  24. package/packages/datadog-instrumentations/src/knex.js +20 -0
  25. package/packages/datadog-instrumentations/src/koa.js +159 -0
  26. package/packages/datadog-instrumentations/src/limitd-client.js +21 -0
  27. package/packages/datadog-instrumentations/src/memcached.js +14 -12
  28. package/packages/datadog-instrumentations/src/mocha.js +37 -39
  29. package/packages/datadog-instrumentations/src/moleculer/client.js +46 -0
  30. package/packages/datadog-instrumentations/src/moleculer/server.js +59 -0
  31. package/packages/datadog-instrumentations/src/moleculer.js +4 -0
  32. package/packages/datadog-instrumentations/src/mongodb-core.js +29 -33
  33. package/packages/datadog-instrumentations/src/mysql.js +30 -29
  34. package/packages/datadog-instrumentations/src/mysql2.js +8 -9
  35. package/packages/datadog-instrumentations/src/net.js +23 -24
  36. package/packages/datadog-instrumentations/src/oracledb.js +128 -0
  37. package/packages/datadog-instrumentations/src/paperplane.js +77 -0
  38. package/packages/datadog-instrumentations/src/pg.js +32 -32
  39. package/packages/datadog-instrumentations/src/redis.js +49 -47
  40. package/packages/datadog-instrumentations/src/restify.js +58 -0
  41. package/packages/datadog-instrumentations/src/rhea.js +52 -50
  42. package/packages/datadog-instrumentations/src/router.js +177 -0
  43. package/packages/datadog-instrumentations/src/sharedb.js +20 -20
  44. package/packages/datadog-instrumentations/src/tedious.js +19 -19
  45. package/packages/datadog-plugin-amqp10/src/index.js +2 -7
  46. package/packages/datadog-plugin-amqplib/src/index.js +1 -2
  47. package/packages/datadog-plugin-aws-sdk/src/base.js +146 -0
  48. package/packages/datadog-plugin-aws-sdk/src/index.js +16 -106
  49. package/packages/datadog-plugin-aws-sdk/src/services/cloudwatchlogs.js +4 -2
  50. package/packages/datadog-plugin-aws-sdk/src/services/default.js +7 -0
  51. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +3 -1
  52. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +5 -4
  53. package/packages/datadog-plugin-aws-sdk/src/services/index.js +12 -0
  54. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +4 -3
  55. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +4 -3
  56. package/packages/datadog-plugin-aws-sdk/src/services/redshift.js +3 -1
  57. package/packages/datadog-plugin-aws-sdk/src/services/s3.js +3 -1
  58. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +4 -3
  59. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +45 -6
  60. package/packages/datadog-plugin-cassandra-driver/src/index.js +1 -5
  61. package/packages/datadog-plugin-connect/src/index.js +10 -114
  62. package/packages/datadog-plugin-couchbase/src/index.js +3 -4
  63. package/packages/datadog-plugin-cucumber/src/index.js +1 -9
  64. package/packages/datadog-plugin-dns/src/index.js +15 -5
  65. package/packages/datadog-plugin-elasticsearch/src/index.js +1 -5
  66. package/packages/datadog-plugin-express/src/index.js +11 -25
  67. package/packages/datadog-plugin-fastify/src/index.js +17 -4
  68. package/packages/datadog-plugin-find-my-way/src/index.js +20 -0
  69. package/packages/datadog-plugin-google-cloud-pubsub/src/index.js +56 -111
  70. package/packages/datadog-plugin-hapi/src/index.js +34 -3
  71. package/packages/datadog-plugin-http/src/client.js +1 -3
  72. package/packages/datadog-plugin-http/src/server.js +2 -6
  73. package/packages/datadog-plugin-jest/src/index.js +2 -3
  74. package/packages/datadog-plugin-kafkajs/src/index.js +61 -91
  75. package/packages/datadog-plugin-koa/src/index.js +12 -164
  76. package/packages/datadog-plugin-memcached/src/index.js +1 -5
  77. package/packages/datadog-plugin-mocha/src/index.js +3 -7
  78. package/packages/datadog-plugin-moleculer/src/client.js +34 -44
  79. package/packages/datadog-plugin-moleculer/src/index.js +32 -3
  80. package/packages/datadog-plugin-moleculer/src/server.js +28 -50
  81. package/packages/datadog-plugin-mongodb-core/src/index.js +1 -5
  82. package/packages/datadog-plugin-mysql/src/index.js +1 -5
  83. package/packages/datadog-plugin-net/src/index.js +3 -7
  84. package/packages/datadog-plugin-oracledb/src/index.js +34 -100
  85. package/packages/datadog-plugin-paperplane/src/index.js +14 -100
  86. package/packages/datadog-plugin-paperplane/src/logger.js +11 -0
  87. package/packages/datadog-plugin-paperplane/src/server.js +24 -0
  88. package/packages/datadog-plugin-pg/src/index.js +1 -5
  89. package/packages/datadog-plugin-redis/src/index.js +1 -5
  90. package/packages/datadog-plugin-restify/src/index.js +13 -75
  91. package/packages/datadog-plugin-rhea/src/index.js +1 -5
  92. package/packages/datadog-plugin-router/src/index.js +67 -164
  93. package/packages/datadog-plugin-sharedb/src/index.js +1 -5
  94. package/packages/datadog-plugin-tedious/src/index.js +1 -5
  95. package/packages/datadog-plugin-web/src/index.js +20 -0
  96. package/packages/dd-trace/lib/version.js +1 -1
  97. package/packages/dd-trace/src/appsec/callbacks/ddwaf.js +8 -2
  98. package/packages/dd-trace/src/appsec/reporter.js +4 -0
  99. package/packages/dd-trace/src/encode/agentless-ci-visibility.js +3 -1
  100. package/packages/dd-trace/src/plugin_manager.js +13 -7
  101. package/packages/dd-trace/src/plugins/index.js +1 -2
  102. package/packages/dd-trace/src/plugins/log_plugin.js +8 -4
  103. package/packages/dd-trace/src/plugins/plugin.js +8 -8
  104. package/packages/dd-trace/src/plugins/util/test.js +5 -1
  105. package/packages/dd-trace/src/plugins/util/web.js +40 -12
  106. package/packages/dd-trace/src/profiling/config.js +8 -8
  107. package/packages/dd-trace/src/profiling/index.js +4 -4
  108. package/packages/dd-trace/src/profiling/profilers/{heap.js → space.js} +2 -2
  109. package/packages/dd-trace/src/profiling/profilers/{cpu.js → wall.js} +2 -2
  110. package/scripts/install_plugin_modules.js +23 -11
  111. package/packages/datadog-plugin-aws-sdk/src/helpers.js +0 -103
  112. package/packages/datadog-plugin-fastify/src/fastify.js +0 -198
  113. package/packages/datadog-plugin-fastify/src/find-my-way.js +0 -37
  114. package/packages/datadog-plugin-hapi/src/route.js +0 -75
  115. package/packages/datadog-plugin-hapi/src/server.js +0 -204
  116. package/packages/datadog-plugin-knex/src/index.js +0 -23
  117. package/packages/datadog-plugin-limitd-client/src/index.js +0 -30
  118. package/scripts/postpublish.js +0 -24
@@ -0,0 +1,30 @@
1
+ 'use strict'
2
+
3
+ const shimmer = require('../../datadog-shimmer')
4
+ const { addHook, channel } = require('./helpers/instrument')
5
+
6
+ const routeChannel = channel('apm:find-my-way:request:route')
7
+
8
+ function wrapOn (on) {
9
+ return function onWithTrace (method, path, opts) {
10
+ const index = typeof opts === 'function' ? 2 : 3
11
+ const handler = arguments[index]
12
+ const wrapper = function (req) {
13
+ routeChannel.publish({ req, route: path })
14
+
15
+ return handler.apply(this, arguments)
16
+ }
17
+
18
+ if (typeof handler === 'function') {
19
+ arguments[index] = wrapper
20
+ }
21
+
22
+ return on.apply(this, arguments)
23
+ }
24
+ }
25
+
26
+ addHook({ name: 'find-my-way', versions: ['>=1'] }, Router => {
27
+ shimmer.wrap(Router.prototype, 'on', wrapOn)
28
+
29
+ return Router
30
+ })
@@ -0,0 +1,100 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ channel,
5
+ addHook,
6
+ AsyncResource
7
+ } = require('./helpers/instrument')
8
+ const shimmer = require('../../datadog-shimmer')
9
+
10
+ const requestStartCh = channel('apm:google-cloud-pubsub:request:start')
11
+ const requestFinishCh = channel('apm:google-cloud-pubsub:request:finish')
12
+ const requestErrorCh = channel('apm:google-cloud-pubsub:request:error')
13
+
14
+ const receiveStartCh = channel(`apm:google-cloud-pubsub:receive:start`)
15
+ const receiveFinishCh = channel('apm:google-cloud-pubsub:receive:finish')
16
+ const receiveErrorCh = channel('apm:google-cloud-pubsub:receive:error')
17
+
18
+ addHook({ name: '@google-cloud/pubsub', versions: ['>=1.2'] }, (obj) => {
19
+ const PubSub = obj.PubSub
20
+ const Subscription = obj.Subscription
21
+
22
+ shimmer.wrap(PubSub.prototype, 'request', request => function (cfg = { reqOpts: {} }, cb) {
23
+ if (!requestStartCh.hasSubscribers) {
24
+ return request.apply(this, arguments)
25
+ }
26
+
27
+ const innerAsyncResource = new AsyncResource('bound-anonymous-fn')
28
+ const outerAsyncResource = new AsyncResource('bound-anonymous-fn')
29
+
30
+ return innerAsyncResource.runInAsyncScope(() => {
31
+ let messages = []
32
+ if (cfg.reqOpts && cfg.method === 'publish') {
33
+ messages = cfg.reqOpts.messages
34
+ }
35
+
36
+ requestStartCh.publish({ cfg, projectId: this.projectId, messages })
37
+ cb = outerAsyncResource.bind(cb)
38
+
39
+ const fn = () => {
40
+ arguments[1] = innerAsyncResource.bind(function (error) {
41
+ if (error) {
42
+ requestErrorCh.publish(error)
43
+ }
44
+ requestFinishCh.publish(undefined)
45
+ return cb.apply(this, arguments)
46
+ })
47
+ return request.apply(this, arguments)
48
+ }
49
+
50
+ try {
51
+ return fn.apply(this, arguments)
52
+ } catch (e) {
53
+ requestErrorCh.publish(e)
54
+ throw e
55
+ }
56
+ })
57
+ })
58
+
59
+ shimmer.wrap(Subscription.prototype, 'emit', emit => function (eventName, message) {
60
+ if (eventName !== 'message' || !message) return emit.apply(this, arguments)
61
+
62
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
63
+
64
+ return asyncResource.runInAsyncScope(() => {
65
+ try {
66
+ return emit.apply(this, arguments)
67
+ } catch (err) {
68
+ receiveErrorCh.publish({ err, message })
69
+ throw err
70
+ }
71
+ })
72
+ })
73
+
74
+ return obj
75
+ })
76
+
77
+ addHook({ name: '@google-cloud/pubsub', versions: ['>=1.2'], file: 'build/src/lease-manager.js' }, (obj) => {
78
+ const LeaseManager = obj.LeaseManager
79
+
80
+ shimmer.wrap(LeaseManager.prototype, '_dispense', dispense => function (message) {
81
+ if (receiveStartCh.hasSubscribers) {
82
+ receiveStartCh.publish({ message })
83
+ }
84
+ return dispense.apply(this, arguments)
85
+ })
86
+
87
+ shimmer.wrap(LeaseManager.prototype, 'remove', remove => function (message) {
88
+ receiveFinishCh.publish({ message })
89
+ return remove.apply(this, arguments)
90
+ })
91
+
92
+ shimmer.wrap(LeaseManager.prototype, 'clear', clear => function () {
93
+ for (const message of this._messages) {
94
+ receiveFinishCh.publish({ message })
95
+ }
96
+ return clear.apply(this, arguments)
97
+ })
98
+
99
+ return obj
100
+ })
@@ -0,0 +1,210 @@
1
+ 'use strict'
2
+
3
+ const shimmer = require('../../datadog-shimmer')
4
+ const { addHook, channel, AsyncResource } = require('./helpers/instrument')
5
+
6
+ const handleChannel = channel('apm:hapi:request:handle')
7
+ const routeChannel = channel('apm:hapi:request:route')
8
+ const errorChannel = channel('apm:hapi:request:error')
9
+ const enterChannel = channel('apm:hapi:extension:enter')
10
+
11
+ function wrapServer (server) {
12
+ return function (options) {
13
+ const app = server.apply(this, arguments)
14
+
15
+ if (!app) return app
16
+
17
+ if (typeof app.ext === 'function') {
18
+ app.ext = wrapExt(app.ext)
19
+ }
20
+
21
+ if (typeof app.start === 'function') {
22
+ app.start = wrapStart(app.start)
23
+ }
24
+
25
+ return app
26
+ }
27
+ }
28
+
29
+ function wrapStart (start) {
30
+ return function () {
31
+ if (this && typeof this.ext === 'function') {
32
+ this.ext('onPreResponse', onPreResponse)
33
+ }
34
+
35
+ return start.apply(this, arguments)
36
+ }
37
+ }
38
+
39
+ function wrapExt (ext) {
40
+ return function (events, method, options) {
41
+ if (typeof events === 'object') {
42
+ arguments[0] = wrapEvents(events)
43
+ } else {
44
+ arguments[1] = wrapExtension(method)
45
+ }
46
+
47
+ return ext.apply(this, arguments)
48
+ }
49
+ }
50
+
51
+ function wrapDispatch (dispatch) {
52
+ return function (options) {
53
+ const handler = dispatch.apply(this, arguments)
54
+
55
+ if (typeof handler !== 'function') return handler
56
+
57
+ return function (req, res) {
58
+ handleChannel.publish({ req, res })
59
+
60
+ return handler.apply(this, arguments)
61
+ }
62
+ }
63
+ }
64
+
65
+ function wrapLifecycle (lifecycle) {
66
+ return function () {
67
+ const result = lifecycle.apply(this, arguments)
68
+
69
+ if (Array.isArray(result)) return result.map(wrapHandler)
70
+
71
+ return result
72
+ }
73
+ }
74
+
75
+ function wrapRebuild (rebuild) {
76
+ return function (event) {
77
+ const result = rebuild.apply(this, arguments)
78
+
79
+ if (this && Array.isArray(this._cycle)) {
80
+ this._cycle = this._cycle.map(wrapHandler)
81
+ }
82
+
83
+ return result
84
+ }
85
+ }
86
+
87
+ function wrapExtension (method) {
88
+ return [].concat(method).map(wrapHandler)
89
+ }
90
+
91
+ function wrapEvents (events) {
92
+ return [].concat(events).map(event => {
93
+ if (!event || !event.method) return event
94
+
95
+ return Object.assign({}, event, {
96
+ method: wrapExtension(event.method)
97
+ })
98
+ })
99
+ }
100
+
101
+ function wrapHandler (handler) {
102
+ if (typeof handler !== 'function') return handler
103
+
104
+ return function (request, h) {
105
+ const req = request && request.raw && request.raw.req
106
+
107
+ if (!req) return handler.apply(this, arguments)
108
+
109
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
110
+
111
+ return asyncResource.runInAsyncScope(() => {
112
+ enterChannel.publish({ req })
113
+
114
+ return handler.apply(this, arguments)
115
+ })
116
+ }
117
+ }
118
+
119
+ function onPreResponse (request, h) {
120
+ if (!request || !request.raw) return reply(request, h)
121
+
122
+ const req = request.raw.req
123
+
124
+ if (request.response instanceof Error) {
125
+ errorChannel.publish(request.response)
126
+ }
127
+
128
+ if (request.route) {
129
+ routeChannel.publish({ req, route: request.route.path })
130
+ }
131
+
132
+ return reply(request, h)
133
+ }
134
+
135
+ function reply (request, h) {
136
+ if (h.continue) {
137
+ return typeof h.continue === 'function'
138
+ ? h.continue()
139
+ : h.continue
140
+ } else if (typeof h === 'function') {
141
+ return h()
142
+ }
143
+ }
144
+
145
+ addHook({ name: '@hapi/hapi', versions: ['>=17.9'] }, hapi => {
146
+ shimmer.massWrap(hapi, ['server', 'Server'], wrapServer)
147
+
148
+ return hapi
149
+ })
150
+
151
+ addHook({ name: '@hapi/hapi', versions: ['>=17.9'], file: 'lib/core.js' }, Core => {
152
+ shimmer.wrap(Core.prototype, '_dispatch', wrapDispatch)
153
+
154
+ return Core
155
+ })
156
+
157
+ addHook({ name: '@hapi/hapi', versions: ['>=17.9'], file: 'lib/route.js' }, Route => {
158
+ shimmer.wrap(Route.prototype, 'rebuild', wrapRebuild)
159
+
160
+ return Route
161
+ })
162
+
163
+ addHook({ name: 'hapi', versions: ['>=17'] }, hapi => {
164
+ shimmer.massWrap(hapi, ['server', 'Server'], wrapServer)
165
+
166
+ return hapi
167
+ })
168
+
169
+ addHook({ name: 'hapi', versions: ['2 - 7.1', '8 - 16'] }, hapi => {
170
+ shimmer.wrap(hapi.Server.prototype, 'start', wrapStart)
171
+ shimmer.wrap(hapi.Server.prototype, 'ext', wrapExt)
172
+
173
+ return hapi
174
+ })
175
+
176
+ addHook({ name: 'hapi', versions: ['^7.2'] }, hapi => {
177
+ shimmer.wrap(hapi, 'createServer', wrapServer)
178
+
179
+ return hapi
180
+ })
181
+
182
+ addHook({ name: 'hapi', versions: ['7.2 - 16'], file: 'lib/connection.js' }, Connection => {
183
+ shimmer.wrap(Connection.prototype, '_dispatch', wrapDispatch)
184
+
185
+ return Connection
186
+ })
187
+
188
+ addHook({ name: 'hapi', versions: ['>=17'], file: 'lib/core.js' }, Core => {
189
+ shimmer.wrap(Core.prototype, '_dispatch', wrapDispatch)
190
+
191
+ return Core
192
+ })
193
+
194
+ addHook({ name: 'hapi', versions: ['2 - 7.1'], file: 'lib/server.js' }, Server => {
195
+ shimmer.wrap(Server.prototype, '_dispatch', wrapDispatch)
196
+
197
+ return Server
198
+ })
199
+
200
+ addHook({ name: 'hapi', versions: ['>=10.4'], file: 'lib/route.js' }, Route => {
201
+ shimmer.wrap(Route.prototype, 'rebuild', wrapRebuild)
202
+
203
+ return Route
204
+ })
205
+
206
+ addHook({ name: 'hapi', versions: ['2 - 10.3'], file: 'lib/route.js' }, Route => {
207
+ shimmer.wrap(Route.prototype, 'lifecycle', wrapLifecycle)
208
+
209
+ return Route
210
+ })
@@ -13,8 +13,7 @@ const shimmer = require('../../../datadog-shimmer')
13
13
  const log = require('../../../dd-trace/src/log')
14
14
 
15
15
  const startClientCh = channel('apm:http:client:request:start')
16
- const asyncEndClientCh = channel('apm:http:client:request:async-end')
17
- const endClientCh = channel('apm:http:client:request:end')
16
+ const finishClientCh = channel('apm:http:client:request:finish')
18
17
  const errorClientCh = channel('apm:http:client:request:error')
19
18
 
20
19
  addHook({ name: 'https' }, hookFn)
@@ -36,7 +35,6 @@ function patch (http, methodName) {
36
35
  if (!startClientCh.hasSubscribers) {
37
36
  return request.apply(this, arguments)
38
37
  }
39
- const asyncResource = new AsyncResource('bound-anonymous-fn')
40
38
 
41
39
  let args
42
40
 
@@ -46,57 +44,59 @@ function patch (http, methodName) {
46
44
  log.error(e)
47
45
  return request.apply(this, arguments)
48
46
  }
49
- startClientCh.publish({ args, http })
50
-
51
- const ar = new AsyncResource('bound-anonymous-fn')
52
47
 
53
- let finished = false
54
- let callback = args.callback
48
+ const callbackResource = new AsyncResource('bound-anonymous-fn')
49
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
55
50
 
56
- if (callback) {
57
- callback = asyncResource.bind(callback)
58
- }
51
+ return asyncResource.runInAsyncScope(() => {
52
+ startClientCh.publish({ args, http })
59
53
 
60
- const options = args.options
61
- const req = ar.bind(request).call(this, options, callback)
62
- const emit = req.emit
54
+ let finished = false
55
+ let callback = args.callback
63
56
 
64
- const finish = (req, res) => {
65
- if (!finished) {
66
- finished = true
67
- asyncEndClientCh.publish({ req, res })
57
+ if (callback) {
58
+ callback = callbackResource.bind(callback)
68
59
  }
69
- }
70
60
 
71
- req.emit = function (eventName, arg) {
72
- ar.runInAsyncScope(() => {
73
- switch (eventName) {
74
- case 'response': {
75
- const res = arg
76
- const listener = ar.bind(() => finish(req, res))
77
- res.on('end', listener)
78
- res.on('error', listener)
79
- break
80
- }
81
- case 'connect':
82
- case 'upgrade':
83
- finish(req, arg)
84
- break
85
- case 'error':
86
- errorClientCh.publish(arg)
87
- case 'abort': // deprecated and replaced by `close` in node 17
88
- case 'timeout':
89
- case 'close':
90
- finish(req)
61
+ const options = args.options
62
+ const req = request.call(this, options, callback)
63
+ const emit = req.emit
64
+
65
+ const finish = (req, res) => {
66
+ if (!finished) {
67
+ finished = true
68
+ finishClientCh.publish({ req, res })
91
69
  }
92
- })
70
+ }
93
71
 
94
- return emit.apply(this, arguments)
95
- }
72
+ req.emit = function (eventName, arg) {
73
+ asyncResource.runInAsyncScope(() => {
74
+ switch (eventName) {
75
+ case 'response': {
76
+ const res = arg
77
+ const listener = asyncResource.bind(() => finish(req, res))
78
+ res.on('end', listener)
79
+ res.on('error', listener)
80
+ break
81
+ }
82
+ case 'connect':
83
+ case 'upgrade':
84
+ finish(req, arg)
85
+ break
86
+ case 'error':
87
+ errorClientCh.publish(arg)
88
+ case 'abort': // deprecated and replaced by `close` in node 17
89
+ case 'timeout':
90
+ case 'close':
91
+ finish(req)
92
+ }
93
+ })
96
94
 
97
- endClientCh.publish(undefined)
95
+ return emit.apply(this, arguments)
96
+ }
98
97
 
99
- return req
98
+ return req
99
+ })
100
100
  }
101
101
  }
102
102
 
@@ -2,14 +2,14 @@
2
2
 
3
3
  const {
4
4
  channel,
5
- addHook
5
+ addHook,
6
+ AsyncResource
6
7
  } = require('../helpers/instrument')
7
8
  const shimmer = require('../../../datadog-shimmer')
8
9
 
9
10
  const startServerCh = channel('apm:http:server:request:start')
10
- const endServerCh = channel('apm:http:server:request:end')
11
11
  const errorServerCh = channel('apm:http:server:request:error')
12
- const asyncEndServerCh = channel('apm:http:server:request:async-end')
12
+ const finishServerCh = channel('apm:http:server:request:finish')
13
13
 
14
14
  addHook({ name: 'https' }, http => {
15
15
  // http.ServerResponse not present on https
@@ -30,7 +30,7 @@ function wrapResponseEmit (emit) {
30
30
  }
31
31
 
32
32
  if (eventName === 'finish') {
33
- asyncEndServerCh.publish({ req: this.req })
33
+ finishServerCh.publish({ req: this.req })
34
34
  }
35
35
 
36
36
  return emit.apply(this, arguments)
@@ -44,17 +44,19 @@ function wrapEmit (emit) {
44
44
 
45
45
  if (eventName === 'request') {
46
46
  res.req = req
47
- startServerCh.publish({ req, res })
48
47
 
49
- try {
50
- return emit.apply(this, arguments)
51
- } catch (err) {
52
- errorServerCh.publish(err)
48
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
49
+ return asyncResource.runInAsyncScope(() => {
50
+ startServerCh.publish({ req, res })
53
51
 
54
- throw err
55
- } finally {
56
- endServerCh.publish(undefined)
57
- }
52
+ try {
53
+ return emit.apply(this, arguments)
54
+ } catch (err) {
55
+ errorServerCh.publish(err)
56
+
57
+ throw err
58
+ }
59
+ })
58
60
  }
59
61
  return emit.apply(this, arguments)
60
62
  }
@@ -8,8 +8,7 @@ const {
8
8
  const shimmer = require('../../datadog-shimmer')
9
9
 
10
10
  const startCh = channel('apm:ioredis:command:start')
11
- const asyncEndCh = channel('apm:ioredis:command:async-end')
12
- const endCh = channel('apm:ioredis:command:end')
11
+ const finishCh = channel('apm:ioredis:command:finish')
13
12
  const errorCh = channel('apm:ioredis:command:error')
14
13
 
15
14
  addHook({ name: 'ioredis', versions: ['>=2'] }, Redis => {
@@ -23,30 +22,30 @@ addHook({ name: 'ioredis', versions: ['>=2'] }, Redis => {
23
22
  const db = options.db
24
23
  const connectionOptions = { host: options.host, port: options.port }
25
24
 
26
- startCh.publish({ db, command: command.name, args: command.args, connectionOptions, connectionName })
27
-
28
25
  const asyncResource = new AsyncResource('bound-anonymous-fn')
29
- const onResolve = asyncResource.bind(() => finish(asyncEndCh, errorCh))
30
- const onReject = asyncResource.bind(err => finish(asyncEndCh, errorCh, err))
26
+ return asyncResource.runInAsyncScope(() => {
27
+ startCh.publish({ db, command: command.name, args: command.args, connectionOptions, connectionName })
28
+
29
+ const onResolve = asyncResource.bind(() => finish(finishCh, errorCh))
30
+ const onReject = asyncResource.bind(err => finish(finishCh, errorCh, err))
31
31
 
32
- command.promise.then(onResolve, onReject)
32
+ command.promise.then(onResolve, onReject)
33
33
 
34
- try {
35
- return sendCommand.apply(this, arguments)
36
- } catch (err) {
37
- errorCh.publish(err)
34
+ try {
35
+ return sendCommand.apply(this, arguments)
36
+ } catch (err) {
37
+ errorCh.publish(err)
38
38
 
39
- throw err
40
- } finally {
41
- endCh.publish(undefined)
42
- }
39
+ throw err
40
+ }
41
+ })
43
42
  })
44
43
  return Redis
45
44
  })
46
45
 
47
- function finish (asyncEndCh, errorCh, error) {
46
+ function finish (finishCh, errorCh, error) {
48
47
  if (error) {
49
48
  errorCh.publish(error)
50
49
  }
51
- asyncEndCh.publish(undefined)
50
+ finishCh.publish()
52
51
  }
@@ -5,9 +5,9 @@ const shimmer = require('../../datadog-shimmer')
5
5
 
6
6
  const testStartCh = channel('ci:jest:test:start')
7
7
  const testSkippedCh = channel('ci:jest:test:skip')
8
- const testRunEndCh = channel('ci:jest:test:end')
8
+ const testRunFinishCh = channel('ci:jest:test:finish')
9
9
  const testErrCh = channel('ci:jest:test:err')
10
- const testSuiteEnd = channel('ci:jest:test-suite:end')
10
+ const testSuiteFinish = channel('ci:jest:test-suite:finish')
11
11
 
12
12
  const {
13
13
  getTestSuitePath,
@@ -48,13 +48,14 @@ function getWrappedEnvironment (BaseEnvironment) {
48
48
  return class DatadogEnvironment extends BaseEnvironment {
49
49
  constructor (config, context) {
50
50
  super(config, context)
51
- this.testSuite = getTestSuitePath(context.testPath, config.rootDir)
51
+ const rootDir = config.globalConfig ? config.globalConfig.rootDir : config.rootDir
52
+ this.testSuite = getTestSuitePath(context.testPath, rootDir)
52
53
  this.nameToParams = {}
53
54
  this.global._ddtrace = global._ddtrace
54
55
  }
55
56
  async teardown () {
56
57
  super.teardown().finally(() => {
57
- testSuiteEnd.publish()
58
+ testSuiteFinish.publish()
58
59
  })
59
60
  }
60
61
 
@@ -63,13 +64,7 @@ function getWrappedEnvironment (BaseEnvironment) {
63
64
  await super.handleTestEvent(event, state)
64
65
  }
65
66
 
66
- let context
67
- if (this.getVmContext) {
68
- context = this.getVmContext()
69
- } else {
70
- context = this.context
71
- }
72
-
67
+ const globalExpect = this.global.expect
73
68
  const setNameToParams = (name, params) => { this.nameToParams[name] = params }
74
69
 
75
70
  if (event.name === 'setup') {
@@ -92,7 +87,7 @@ function getWrappedEnvironment (BaseEnvironment) {
92
87
  asyncResources.set(event.test, asyncResource)
93
88
  asyncResource.runInAsyncScope(() => {
94
89
  testStartCh.publish({
95
- name: context.expect.getState().currentTestName,
90
+ name: globalExpect.getState().currentTestName,
96
91
  suite: this.testSuite,
97
92
  runner: 'jest-circus',
98
93
  testParameters
@@ -110,14 +105,14 @@ function getWrappedEnvironment (BaseEnvironment) {
110
105
  const formattedError = formatJestError(event.test.errors[0])
111
106
  testErrCh.publish(formattedError)
112
107
  }
113
- testRunEndCh.publish(status)
108
+ testRunFinishCh.publish(status)
114
109
  // restore in case it is retried
115
110
  event.test.fn = originalTestFns.get(event.test)
116
111
  })
117
112
  }
118
113
  if (event.name === 'test_skip' || event.name === 'test_todo') {
119
114
  testSkippedCh.publish({
120
- name: context.expect.getState().currentTestName,
115
+ name: globalExpect.getState().currentTestName,
121
116
  suite: this.testSuite,
122
117
  runner: 'jest-circus'
123
118
  })
@@ -126,15 +121,25 @@ function getWrappedEnvironment (BaseEnvironment) {
126
121
  }
127
122
  }
128
123
 
124
+ function getTestEnvironment (pkg) {
125
+ if (pkg.default) {
126
+ const wrappedTestEnvironment = getWrappedEnvironment(pkg.default)
127
+ pkg.default = wrappedTestEnvironment
128
+ pkg.TestEnvironment = wrappedTestEnvironment
129
+ return pkg
130
+ }
131
+ return getWrappedEnvironment(pkg)
132
+ }
133
+
129
134
  addHook({
130
135
  name: 'jest-environment-node',
131
136
  versions: ['>=24.8.0']
132
- }, getWrappedEnvironment)
137
+ }, getTestEnvironment)
133
138
 
134
139
  addHook({
135
140
  name: 'jest-environment-jsdom',
136
141
  versions: ['>=24.8.0']
137
- }, getWrappedEnvironment)
142
+ }, getTestEnvironment)
138
143
 
139
144
  addHook({
140
145
  name: 'jest-jasmine2',
@@ -158,7 +163,7 @@ addHook({
158
163
  const formattedError = formatJestError(spec.result.failedExpectations[0].error)
159
164
  testErrCh.publish(formattedError)
160
165
  }
161
- testRunEndCh.publish(specStatusToTestStatus[spec.result.status])
166
+ testRunFinishCh.publish(specStatusToTestStatus[spec.result.status])
162
167
  onComplete.apply(this, arguments)
163
168
  })
164
169
  arguments[0] = callback