dd-trace 2.2.0 → 2.3.1

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 (55) hide show
  1. package/LICENSE-3rdparty.csv +0 -2
  2. package/index.d.ts +6 -12
  3. package/package.json +3 -5
  4. package/packages/datadog-instrumentations/index.js +9 -0
  5. package/packages/datadog-instrumentations/src/bunyan.js +22 -0
  6. package/packages/datadog-instrumentations/src/cucumber.js +116 -0
  7. package/packages/datadog-instrumentations/src/elasticsearch.js +15 -5
  8. package/packages/datadog-instrumentations/src/generic-pool.js +48 -0
  9. package/packages/datadog-instrumentations/src/ioredis.js +52 -0
  10. package/packages/datadog-instrumentations/src/mongoose.js +30 -0
  11. package/packages/datadog-instrumentations/src/pino.js +105 -0
  12. package/packages/datadog-instrumentations/src/redis.js +118 -0
  13. package/packages/datadog-instrumentations/src/sharedb.js +78 -0
  14. package/packages/datadog-instrumentations/src/winston.js +57 -0
  15. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +2 -2
  16. package/packages/datadog-plugin-bunyan/src/index.js +5 -22
  17. package/packages/datadog-plugin-cucumber/src/index.js +83 -128
  18. package/packages/datadog-plugin-fastify/src/find-my-way.js +0 -1
  19. package/packages/datadog-plugin-google-cloud-pubsub/src/index.js +8 -6
  20. package/packages/datadog-plugin-graphql/src/index.js +34 -28
  21. package/packages/datadog-plugin-grpc/src/client.js +20 -6
  22. package/packages/datadog-plugin-http2/src/server.js +2 -0
  23. package/packages/datadog-plugin-ioredis/src/index.js +5 -35
  24. package/packages/datadog-plugin-jest/src/jest-environment.js +26 -30
  25. package/packages/datadog-plugin-koa/src/index.js +6 -2
  26. package/packages/datadog-plugin-microgateway-core/src/index.js +1 -3
  27. package/packages/datadog-plugin-mocha/src/index.js +5 -3
  28. package/packages/datadog-plugin-mongodb-core/src/util.js +31 -7
  29. package/packages/datadog-plugin-next/src/index.js +9 -4
  30. package/packages/datadog-plugin-oracledb/src/index.js +10 -7
  31. package/packages/datadog-plugin-pino/src/index.js +5 -158
  32. package/packages/datadog-plugin-redis/src/index.js +96 -80
  33. package/packages/datadog-plugin-restify/src/index.js +18 -3
  34. package/packages/datadog-plugin-rhea/src/index.js +8 -5
  35. package/packages/datadog-plugin-router/src/index.js +23 -14
  36. package/packages/datadog-plugin-sharedb/src/index.js +47 -87
  37. package/packages/datadog-plugin-winston/src/index.js +5 -113
  38. package/packages/datadog-shimmer/src/shimmer.js +1 -1
  39. package/packages/dd-trace/lib/version.js +1 -1
  40. package/packages/dd-trace/src/appsec/index.js +2 -1
  41. package/packages/dd-trace/src/appsec/reporter.js +3 -2
  42. package/packages/dd-trace/src/config.js +7 -1
  43. package/packages/dd-trace/src/constants.js +1 -6
  44. package/packages/dd-trace/src/opentracing/propagation/text_map.js +0 -34
  45. package/packages/dd-trace/src/opentracing/tracer.js +1 -1
  46. package/packages/dd-trace/src/plugins/index.js +0 -2
  47. package/packages/dd-trace/src/plugins/log_plugin.js +49 -0
  48. package/packages/dd-trace/src/plugins/plugin.js +7 -0
  49. package/packages/dd-trace/src/plugins/util/git.js +1 -1
  50. package/packages/dd-trace/src/plugins/util/web.js +102 -84
  51. package/packages/dd-trace/src/priority_sampler.js +1 -49
  52. package/packages/dd-trace/src/scope.js +47 -23
  53. package/packages/dd-trace/src/span_processor.js +22 -7
  54. package/packages/datadog-plugin-generic-pool/src/index.js +0 -52
  55. package/packages/datadog-plugin-mongoose/src/index.js +0 -51
@@ -22,8 +22,6 @@ require,path-to-regexp,MIT,Copyright 2014 Blake Embrey
22
22
  require,performance-now,MIT,Copyright 2013 Braveg1rl
23
23
  require,retry,MIT,Copyright 2011 Tim Koschützki Felix Geisendörfer
24
24
  require,semver,ISC,Copyright Isaac Z. Schlueter and Contributors
25
- require,source-map,BSD-3-Clause,Copyright 2009-2011 Mozilla Foundation and contributors
26
- require,source-map-resolve,MIT,Copyright 2014-2020 Simon Lydell 2019 Jinxiang
27
25
  dev,autocannon,MIT,Copyright 2016 Matteo Collina
28
26
  dev,axios,MIT,Copyright 2014-present Matt Zabriskie
29
27
  dev,benchmark,MIT,Copyright 2010-2016 Mathias Bynens Robert Kieffer John-David Dalton
package/index.d.ts CHANGED
@@ -186,18 +186,6 @@ export declare interface SamplingRule {
186
186
  * List of options available to the tracer.
187
187
  */
188
188
  export declare interface TracerOptions {
189
- /**
190
- * Whether to enable the tracer.
191
- * @default true
192
- */
193
- enabled?: boolean;
194
-
195
- /**
196
- * Enable debug logging in the tracer.
197
- * @default false
198
- */
199
- debug?: boolean;
200
-
201
189
  /**
202
190
  * Whether to enable trace ID injection in log records to be able to correlate
203
191
  * traces with logs.
@@ -278,6 +266,12 @@ export declare interface TracerOptions {
278
266
  */
279
267
  flushInterval?: number;
280
268
 
269
+ /**
270
+ * Number of spans before partially exporting a trace. This prevents keeping all the spans in memory for very large traces.
271
+ * @default 1000
272
+ */
273
+ flushMinSpans?: number;
274
+
281
275
  /**
282
276
  * Whether to enable runtime metrics.
283
277
  * @default false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -69,7 +69,7 @@
69
69
  "crypto-randomuuid": "^1.0.0",
70
70
  "diagnostics_channel": "^1.1.0",
71
71
  "form-data": "^3.0.0",
72
- "import-in-the-middle": "^1.1.2",
72
+ "import-in-the-middle": "^1.2.1",
73
73
  "koalas": "^1.0.2",
74
74
  "limiter": "^1.1.4",
75
75
  "lodash.kebabcase": "^4.1.1",
@@ -83,9 +83,7 @@
83
83
  "path-to-regexp": "^0.1.2",
84
84
  "performance-now": "^2.1.0",
85
85
  "retry": "^0.10.1",
86
- "semver": "^5.5.0",
87
- "source-map": "^0.7.3",
88
- "source-map-resolve": "^0.6.0"
86
+ "semver": "^5.5.0"
89
87
  },
90
88
  "devDependencies": {
91
89
  "autocannon": "^4.5.2",
@@ -1,13 +1,22 @@
1
1
  'use strict'
2
2
 
3
3
  require('./src/bluebird')
4
+ require('./src/bunyan')
4
5
  require('./src/couchbase')
6
+ require('./src/cucumber')
5
7
  require('./src/dns')
6
8
  require('./src/elasticsearch')
9
+ require('./src/generic-pool')
10
+ require('./src/ioredis')
7
11
  require('./src/memcached')
12
+ require('./src/mongoose')
8
13
  require('./src/mysql')
9
14
  require('./src/mysql2')
15
+ require('./src/pino')
10
16
  require('./src/promise')
11
17
  require('./src/promise-js')
12
18
  require('./src/q')
19
+ require('./src/redis')
20
+ require('./src/sharedb')
13
21
  require('./src/when')
22
+ require('./src/winston')
@@ -0,0 +1,22 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ channel,
5
+ addHook
6
+ } = require('./helpers/instrument')
7
+ const shimmer = require('../../datadog-shimmer')
8
+
9
+ addHook({ name: 'bunyan', versions: ['>=1'] }, Logger => {
10
+ const logCh = channel('apm:bunyan:log')
11
+ shimmer.wrap(Logger.prototype, '_emit', emit => {
12
+ return function wrappedEmit (rec) {
13
+ if (logCh.hasSubscribers) {
14
+ const payload = { message: rec }
15
+ logCh.publish(payload)
16
+ arguments[0] = payload.message
17
+ }
18
+ return emit.apply(this, arguments)
19
+ }
20
+ })
21
+ return Logger
22
+ })
@@ -0,0 +1,116 @@
1
+ 'use strict'
2
+
3
+ const { addHook, channel } = require('./helpers/instrument')
4
+ const shimmer = require('../../datadog-shimmer')
5
+
6
+ const runStartCh = channel('ci:cucumber:run:start')
7
+ const runEndCh = channel('ci:cucumber:run:end')
8
+ const runAsyncEndCh = channel('ci:cucumber:run:async-end')
9
+ const runStepStartCh = channel('ci:cucumber:run-step:start')
10
+ const runStepEndCh = channel('ci:cucumber:run-step:end')
11
+ const errorCh = channel('ci:cucumber:error')
12
+
13
+ function getStatusFromResult (result) {
14
+ if (result.status === 1) {
15
+ return { status: 'pass' }
16
+ }
17
+ if (result.status === 2) {
18
+ return { status: 'skip' }
19
+ }
20
+ if (result.status === 4) {
21
+ return { status: 'skip', skipReason: 'not implemented' }
22
+ }
23
+ return { status: 'fail', errorMessage: result.message }
24
+ }
25
+
26
+ function getStatusFromResultLatest (result) {
27
+ if (result.status === 'PASSED') {
28
+ return { status: 'pass' }
29
+ }
30
+ if (result.status === 'SKIPPED' || result.status === 'PENDING') {
31
+ return { status: 'skip' }
32
+ }
33
+ if (result.status === 'UNDEFINED') {
34
+ return { status: 'skip', skipReason: 'not implemented' }
35
+ }
36
+ return { status: 'fail', errorMessage: result.message }
37
+ }
38
+
39
+ function wrapRun (pl, isLatestVersion) {
40
+ shimmer.wrap(pl.prototype, 'run', run => function () {
41
+ if (!runStartCh.hasSubscribers) {
42
+ return run.apply(this, arguments)
43
+ }
44
+
45
+ runStartCh.publish({ pickleName: this.pickle.name, pickleUri: this.pickle.uri })
46
+ try {
47
+ const promise = run.apply(this, arguments)
48
+ promise.finally(() => {
49
+ const result = this.getWorstStepResult()
50
+ const { status, skipReason, errorMessage } = isLatestVersion
51
+ ? getStatusFromResultLatest(result) : getStatusFromResult(result)
52
+
53
+ runAsyncEndCh.publish({ status, skipReason, errorMessage })
54
+ })
55
+ return promise
56
+ } catch (err) {
57
+ errorCh.publish(err)
58
+ } finally {
59
+ runEndCh.publish(undefined)
60
+ }
61
+ })
62
+ shimmer.wrap(pl.prototype, 'runStep', runStep => function () {
63
+ if (!runStepStartCh.hasSubscribers) {
64
+ return runStep.apply(this, arguments)
65
+ }
66
+ const testStep = arguments[0]
67
+ let resource
68
+
69
+ if (isLatestVersion) {
70
+ resource = testStep.text
71
+ } else {
72
+ resource = testStep.isHook ? 'hook' : testStep.pickleStep.text
73
+ }
74
+
75
+ runStepStartCh.publish({ resource })
76
+ try {
77
+ const promise = runStep.apply(this, arguments)
78
+
79
+ promise.then((result) => {
80
+ const { status, skipReason, errorMessage } = isLatestVersion
81
+ ? getStatusFromResultLatest(result) : getStatusFromResult(result)
82
+
83
+ runAsyncEndCh.publish({ isStep: true, status, skipReason, errorMessage })
84
+ })
85
+ return promise
86
+ } catch (err) {
87
+ errorCh.publish(err)
88
+ } finally {
89
+ runStepEndCh.publish(undefined)
90
+ }
91
+ })
92
+ }
93
+
94
+ addHook({
95
+ name: '@cucumber/cucumber',
96
+ versions: ['7.0.0 - 7.2.1'],
97
+ file: 'lib/runtime/pickle_runner.js'
98
+ }, (PickleRunner) => {
99
+ const pl = PickleRunner.default
100
+
101
+ wrapRun(pl, false)
102
+
103
+ return PickleRunner
104
+ })
105
+
106
+ addHook({
107
+ name: '@cucumber/cucumber',
108
+ versions: ['>=7.3.0'],
109
+ file: 'lib/runtime/test_case_runner.js'
110
+ }, (TestCaseRunner) => {
111
+ const pl = TestCaseRunner.default
112
+
113
+ wrapRun(pl, true)
114
+
115
+ return TestCaseRunner
116
+ })
@@ -12,7 +12,12 @@ const asyncEndCh = channel('apm:elasticsearch:query:async-end')
12
12
  const endCh = channel('apm:elasticsearch:query:end')
13
13
  const errorCh = channel('apm:elasticsearch:query:error')
14
14
 
15
- addHook({ name: '@elastic/elasticsearch', file: 'lib/Transport.js', versions: ['>=5.6.16'] }, Transport => {
15
+ addHook({ name: '@elastic/transport', file: 'lib/Transport.js', versions: ['>=8'] }, (exports) => {
16
+ shimmer.wrap(exports.default.prototype, 'request', wrapRequest)
17
+ return exports
18
+ })
19
+
20
+ addHook({ name: '@elastic/elasticsearch', file: 'lib/Transport.js', versions: ['>=5.6.16 <8', '>=8'] }, Transport => {
16
21
  shimmer.wrap(Transport.prototype, 'request', wrapRequest)
17
22
  return Transport
18
23
  })
@@ -30,18 +35,20 @@ function wrapRequest (request) {
30
35
 
31
36
  if (!params) return request.apply(this, arguments)
32
37
 
33
- const asyncResource = new AsyncResource('bound-anonymous-fn')
38
+ const parentResource = new AsyncResource('bound-anonymous-fn')
34
39
 
35
40
  startCh.publish({ params })
36
41
 
42
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
43
+
37
44
  try {
38
45
  const lastIndex = arguments.length - 1
39
46
  cb = arguments[lastIndex]
40
47
 
41
48
  if (typeof cb === 'function') {
42
- cb = asyncResource.bind(cb)
49
+ cb = parentResource.bind(cb)
43
50
 
44
- arguments[lastIndex] = AsyncResource.bind(function (error) {
51
+ arguments[lastIndex] = asyncResource.bind(function (error) {
45
52
  finish(params, error)
46
53
  return cb.apply(null, arguments)
47
54
  })
@@ -49,7 +56,10 @@ function wrapRequest (request) {
49
56
  } else {
50
57
  const promise = request.apply(this, arguments)
51
58
  if (promise && typeof promise.then === 'function') {
52
- promise.then(() => finish(params), e => finish(params, e))
59
+ const onResolve = asyncResource.bind(() => finish(params))
60
+ const onReject = asyncResource.bind(e => finish(params, e))
61
+
62
+ promise.then(onResolve, onReject)
53
63
  } else {
54
64
  finish(params)
55
65
  }
@@ -0,0 +1,48 @@
1
+ 'use strict'
2
+
3
+ const { addHook, AsyncResource } = require('./helpers/instrument')
4
+ const shimmer = require('../../datadog-shimmer')
5
+
6
+ function createWrapAcquire () {
7
+ return function wrapAcquire (acquire) {
8
+ return function acquireWithTrace (callback, priority) {
9
+ if (typeof callback === 'function') {
10
+ arguments[0] = AsyncResource.bind(callback)
11
+ }
12
+
13
+ return acquire.apply(this, arguments)
14
+ }
15
+ }
16
+ }
17
+
18
+ function createWrapPool () {
19
+ return function wrapPool (Pool) {
20
+ if (typeof Pool !== 'function') return Pool
21
+
22
+ return function PoolWithTrace (factory) {
23
+ const pool = Pool.apply(this, arguments)
24
+
25
+ if (pool && typeof pool.acquire === 'function') {
26
+ shimmer.wrap(pool, 'acquire', createWrapAcquire())
27
+ }
28
+
29
+ return pool
30
+ }
31
+ }
32
+ }
33
+
34
+ addHook({
35
+ name: 'generic-pool',
36
+ versions: ['^2.4']
37
+ }, genericPool => {
38
+ shimmer.wrap(genericPool.Pool.prototype, 'acquire', createWrapAcquire())
39
+ return genericPool
40
+ })
41
+
42
+ addHook({
43
+ name: 'generic-pool',
44
+ versions: ['2 - 2.3']
45
+ }, genericPool => {
46
+ shimmer.wrap(genericPool, 'Pool', createWrapPool())
47
+ return genericPool
48
+ })
@@ -0,0 +1,52 @@
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 startCh = channel('apm:ioredis:command:start')
11
+ const asyncEndCh = channel('apm:ioredis:command:async-end')
12
+ const endCh = channel('apm:ioredis:command:end')
13
+ const errorCh = channel('apm:ioredis:command:error')
14
+
15
+ addHook({ name: 'ioredis', versions: ['>=2'] }, Redis => {
16
+ shimmer.wrap(Redis.prototype, 'sendCommand', sendCommand => function (command, stream) {
17
+ if (!startCh.hasSubscribers) return sendCommand.apply(this, arguments)
18
+
19
+ if (!command || !command.promise) return sendCommand.apply(this, arguments)
20
+
21
+ const options = this.options || {}
22
+ const connectionName = options.connectionName
23
+ const db = options.db
24
+ const connectionOptions = { host: options.host, port: options.port }
25
+
26
+ startCh.publish({ db, command: command.name, args: command.args, connectionOptions, connectionName })
27
+
28
+ 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))
31
+
32
+ command.promise.then(onResolve, onReject)
33
+
34
+ try {
35
+ return sendCommand.apply(this, arguments)
36
+ } catch (err) {
37
+ errorCh.publish(err)
38
+
39
+ throw err
40
+ } finally {
41
+ endCh.publish(undefined)
42
+ }
43
+ })
44
+ return Redis
45
+ })
46
+
47
+ function finish (asyncEndCh, errorCh, error) {
48
+ if (error) {
49
+ errorCh.publish(error)
50
+ }
51
+ asyncEndCh.publish(undefined)
52
+ }
@@ -0,0 +1,30 @@
1
+ 'use strict'
2
+
3
+ const { addHook } = require('./helpers/instrument')
4
+ const { wrapThen } = require('./helpers/promise')
5
+ const { AsyncResource } = require('./helpers/instrument')
6
+ const shimmer = require('../../datadog-shimmer')
7
+
8
+ function wrapAddQueue (addQueue) {
9
+ return function addQueueWithTrace (name) {
10
+ if (typeof name === 'function') {
11
+ arguments[0] = AsyncResource.bind(name)
12
+ } else if (typeof this[name] === 'function') {
13
+ arguments[0] = AsyncResource.bind((...args) => this[name](...args))
14
+ }
15
+
16
+ return addQueue.apply(this, arguments)
17
+ }
18
+ }
19
+
20
+ addHook({
21
+ name: 'mongoose',
22
+ versions: ['>=4.6.4']
23
+ }, mongoose => {
24
+ if (mongoose.Promise !== global.Promise) {
25
+ shimmer.wrap(mongoose.Promise.prototype, 'then', wrapThen)
26
+ }
27
+
28
+ shimmer.wrap(mongoose.Collection.prototype, 'addQueue', wrapAddQueue)
29
+ return mongoose
30
+ })
@@ -0,0 +1,105 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ channel,
5
+ addHook
6
+ } = require('./helpers/instrument')
7
+ const shimmer = require('../../datadog-shimmer')
8
+
9
+ function wrapPino (symbol, wrapper, pino) {
10
+ return function pinoWithTrace () {
11
+ const instance = pino.apply(this, arguments)
12
+
13
+ Object.defineProperty(instance, symbol, {
14
+ configurable: true,
15
+ enumerable: true,
16
+ writable: true,
17
+ value: wrapper(instance[symbol])
18
+ })
19
+
20
+ return instance
21
+ }
22
+ }
23
+
24
+ function wrapAsJson (asJson) {
25
+ const ch = channel('apm:pino:log')
26
+ return function asJsonWithTrace (obj, msg, num, time) {
27
+ obj = arguments[0] = obj || {}
28
+
29
+ const payload = { message: obj }
30
+ ch.publish(payload)
31
+ arguments[0] = payload.message
32
+
33
+ return asJson.apply(this, arguments)
34
+ }
35
+ }
36
+
37
+ function wrapMixin (mixin) {
38
+ const ch = channel('apm:pino:log')
39
+ return function mixinWithTrace () {
40
+ let obj = {}
41
+
42
+ if (mixin) {
43
+ obj = mixin.apply(this, arguments)
44
+ }
45
+
46
+ const payload = { message: obj }
47
+ ch.publish(payload)
48
+
49
+ return payload.message
50
+ }
51
+ }
52
+
53
+ function wrapPrettifyObject (prettifyObject) {
54
+ const ch = channel('apm:pino:log')
55
+ return function prettifyObjectWithTrace (input) {
56
+ const payload = { message: input.input }
57
+ ch.publish(payload)
58
+ input.input = payload.message
59
+ return prettifyObject.apply(this, arguments)
60
+ }
61
+ }
62
+
63
+ function wrapPrettyFactory (prettyFactory) {
64
+ const ch = channel('apm:pino:log')
65
+ return function prettyFactoryWithTrace () {
66
+ const pretty = prettyFactory.apply(this, arguments)
67
+ return function prettyWithTrace (obj) {
68
+ const payload = { message: obj }
69
+ ch.publish(payload)
70
+ arguments[0] = payload.message
71
+ return pretty.apply(this, arguments)
72
+ }
73
+ }
74
+ }
75
+
76
+ addHook({ name: 'pino', versions: ['2 - 3', '4', '>=5 <5.14.0'] }, pino => {
77
+ const asJsonSym = (pino.symbols && pino.symbols.asJsonSym) || 'asJson'
78
+
79
+ return shimmer.wrap(pino, wrapPino(asJsonSym, wrapAsJson, pino))
80
+ })
81
+
82
+ addHook({ name: 'pino', versions: ['>=5.14.0 <6.8.0'] }, pino => {
83
+ const mixinSym = pino.symbols.mixinSym
84
+
85
+ return shimmer.wrap(pino, wrapPino(mixinSym, wrapMixin, pino))
86
+ })
87
+
88
+ addHook({ name: 'pino', versions: ['>=6.8.0'] }, pino => {
89
+ const mixinSym = pino.symbols.mixinSym
90
+
91
+ const wrapped = shimmer.wrap(pino, wrapPino(mixinSym, wrapMixin, pino))
92
+ wrapped.pino = wrapped
93
+ wrapped.default = wrapped
94
+
95
+ return wrapped
96
+ })
97
+
98
+ addHook({ name: 'pino-pretty', file: 'lib/utils.js', versions: ['>=3'] }, utils => {
99
+ shimmer.wrap(utils, 'prettifyObject', wrapPrettifyObject)
100
+ return utils
101
+ })
102
+
103
+ addHook({ name: 'pino-pretty', versions: ['1 - 2'] }, prettyFactory => {
104
+ return shimmer.wrap(prettyFactory, wrapPrettyFactory(prettyFactory))
105
+ })
@@ -0,0 +1,118 @@
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 startCh = channel('apm:redis:command:start')
11
+ const asyncEndCh = channel('apm:redis:command:async-end')
12
+ const endCh = channel('apm:redis:command:end')
13
+ const errorCh = channel('apm:redis:command:error')
14
+
15
+ addHook({ name: '@node-redis/client', file: 'dist/lib/client/commands-queue.js', versions: ['>=1'] }, redis => {
16
+ shimmer.wrap(redis.default.prototype, 'addCommand', addCommand => function (command) {
17
+ if (!startCh.hasSubscribers) {
18
+ return addCommand.apply(this, arguments)
19
+ }
20
+
21
+ const name = command[0]
22
+ const args = command.slice(1)
23
+
24
+ start(this, name, args)
25
+
26
+ const res = addCommand.apply(this, arguments)
27
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
28
+ const onResolve = asyncResource.bind(() => finish(asyncEndCh, errorCh))
29
+ const onReject = asyncResource.bind(err => finish(asyncEndCh, errorCh, err))
30
+
31
+ res.then(onResolve, onReject)
32
+ endCh.publish(undefined)
33
+ return res
34
+ })
35
+ return redis
36
+ })
37
+
38
+ addHook({ name: 'redis', versions: ['>=2.6 <4'] }, redis => {
39
+ shimmer.wrap(redis.RedisClient.prototype, 'internal_send_command', internalSendCommand => function (options) {
40
+ if (!startCh.hasSubscribers) return internalSendCommand.apply(this, arguments)
41
+
42
+ if (!options.callback) return internalSendCommand.apply(this, arguments)
43
+
44
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
45
+
46
+ const cb = asyncResource.bind(options.callback)
47
+
48
+ start(this, options.command, options.args)
49
+
50
+ options.callback = AsyncResource.bind(wrapCallback(asyncEndCh, errorCh, cb))
51
+
52
+ try {
53
+ return internalSendCommand.apply(this, arguments)
54
+ } catch (err) {
55
+ errorCh.publish(err)
56
+
57
+ throw err
58
+ } finally {
59
+ endCh.publish(undefined)
60
+ }
61
+ })
62
+ return redis
63
+ })
64
+
65
+ addHook({ name: 'redis', versions: ['>=0.12 <2.6'] }, redis => {
66
+ shimmer.wrap(redis.RedisClient.prototype, 'send_command', sendCommand => function (command, args, callback) {
67
+ if (!startCh.hasSubscribers) {
68
+ return sendCommand.apply(this, arguments)
69
+ }
70
+
71
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
72
+
73
+ start(this, command, args)
74
+
75
+ if (typeof callback === 'function') {
76
+ const cb = asyncResource.bind(callback)
77
+ arguments[2] = AsyncResource.bind(wrapCallback(asyncEndCh, errorCh, cb))
78
+ } else if (Array.isArray(args) && typeof args[args.length - 1] === 'function') {
79
+ const cb = asyncResource.bind(args[args.length - 1])
80
+ args[args.length - 1] = AsyncResource.bind(wrapCallback(asyncEndCh, errorCh, cb))
81
+ } else {
82
+ arguments[2] = AsyncResource.bind(wrapCallback(asyncEndCh, errorCh))
83
+ }
84
+
85
+ try {
86
+ return sendCommand.apply(this, arguments)
87
+ } catch (err) {
88
+ errorCh.publish(err)
89
+
90
+ throw err
91
+ } finally {
92
+ endCh.publish(undefined)
93
+ }
94
+ })
95
+ return redis
96
+ })
97
+
98
+ function start (client, command, args) {
99
+ const db = client.selected_db
100
+ const connectionOptions = client.connection_options || client.connection_option || client.connectionOption || {}
101
+ startCh.publish({ db, command, args, connectionOptions })
102
+ }
103
+
104
+ function wrapCallback (asyncEndCh, errorCh, callback) {
105
+ return function (err) {
106
+ finish(asyncEndCh, errorCh, err)
107
+ if (callback) {
108
+ return callback.apply(this, arguments)
109
+ }
110
+ }
111
+ }
112
+
113
+ function finish (asyncEndCh, errorCh, error) {
114
+ if (error) {
115
+ errorCh.publish(error)
116
+ }
117
+ asyncEndCh.publish(undefined)
118
+ }