dd-trace 2.5.0 → 2.7.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 (84) hide show
  1. package/LICENSE-3rdparty.csv +0 -2
  2. package/index.d.ts +17 -8
  3. package/package.json +19 -23
  4. package/packages/datadog-instrumentations/index.js +15 -0
  5. package/packages/datadog-instrumentations/src/aws-sdk.js +104 -0
  6. package/packages/datadog-instrumentations/src/connect.js +111 -0
  7. package/packages/datadog-instrumentations/src/express.js +27 -0
  8. package/packages/datadog-instrumentations/src/fastify.js +187 -0
  9. package/packages/datadog-instrumentations/src/find-my-way.js +30 -0
  10. package/packages/datadog-instrumentations/src/google-cloud-pubsub.js +100 -0
  11. package/packages/datadog-instrumentations/src/http/server.js +1 -1
  12. package/packages/datadog-instrumentations/src/jest.js +175 -0
  13. package/packages/datadog-instrumentations/src/kafkajs.js +112 -0
  14. package/packages/datadog-instrumentations/src/knex.js +20 -0
  15. package/packages/datadog-instrumentations/src/koa.js +159 -0
  16. package/packages/datadog-instrumentations/src/limitd-client.js +21 -0
  17. package/packages/datadog-instrumentations/src/oracledb.js +128 -0
  18. package/packages/datadog-instrumentations/src/paperplane.js +77 -0
  19. package/packages/datadog-instrumentations/src/pg.js +2 -2
  20. package/packages/datadog-instrumentations/src/restify.js +58 -0
  21. package/packages/datadog-instrumentations/src/rhea.js +1 -1
  22. package/packages/datadog-instrumentations/src/router.js +177 -0
  23. package/packages/datadog-plugin-aws-sdk/src/base.js +146 -0
  24. package/packages/datadog-plugin-aws-sdk/src/index.js +16 -106
  25. package/packages/datadog-plugin-aws-sdk/src/services/cloudwatchlogs.js +4 -2
  26. package/packages/datadog-plugin-aws-sdk/src/services/default.js +7 -0
  27. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +3 -1
  28. package/packages/datadog-plugin-aws-sdk/src/services/eventbridge.js +5 -4
  29. package/packages/datadog-plugin-aws-sdk/src/services/index.js +12 -0
  30. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +4 -3
  31. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +4 -3
  32. package/packages/datadog-plugin-aws-sdk/src/services/redshift.js +3 -1
  33. package/packages/datadog-plugin-aws-sdk/src/services/s3.js +3 -1
  34. package/packages/datadog-plugin-aws-sdk/src/services/sns.js +4 -3
  35. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +45 -6
  36. package/packages/datadog-plugin-connect/src/index.js +10 -114
  37. package/packages/datadog-plugin-cucumber/src/index.js +9 -21
  38. package/packages/datadog-plugin-cypress/src/plugin.js +6 -17
  39. package/packages/datadog-plugin-dns/src/index.js +12 -1
  40. package/packages/datadog-plugin-express/src/index.js +11 -25
  41. package/packages/datadog-plugin-fastify/src/index.js +17 -4
  42. package/packages/datadog-plugin-find-my-way/src/index.js +20 -0
  43. package/packages/datadog-plugin-fs/src/index.js +2 -0
  44. package/packages/datadog-plugin-google-cloud-pubsub/src/index.js +56 -111
  45. package/packages/datadog-plugin-http/src/server.js +2 -10
  46. package/packages/datadog-plugin-jest/src/index.js +101 -3
  47. package/packages/datadog-plugin-jest/src/util.js +1 -29
  48. package/packages/datadog-plugin-kafkajs/src/index.js +64 -90
  49. package/packages/datadog-plugin-koa/src/index.js +12 -164
  50. package/packages/datadog-plugin-mocha/src/index.js +5 -15
  51. package/packages/datadog-plugin-oracledb/src/index.js +34 -100
  52. package/packages/datadog-plugin-paperplane/src/index.js +14 -100
  53. package/packages/datadog-plugin-paperplane/src/logger.js +11 -0
  54. package/packages/datadog-plugin-paperplane/src/server.js +24 -0
  55. package/packages/datadog-plugin-restify/src/index.js +13 -75
  56. package/packages/datadog-plugin-router/src/index.js +67 -164
  57. package/packages/datadog-plugin-web/src/index.js +20 -0
  58. package/packages/dd-trace/lib/version.js +1 -1
  59. package/packages/dd-trace/src/appsec/callbacks/ddwaf.js +31 -11
  60. package/packages/dd-trace/src/appsec/index.js +7 -3
  61. package/packages/dd-trace/src/appsec/reporter.js +33 -3
  62. package/packages/dd-trace/src/appsec/rule_manager.js +2 -2
  63. package/packages/dd-trace/src/config.js +25 -3
  64. package/packages/dd-trace/src/format.js +9 -5
  65. package/packages/dd-trace/src/plugin_manager.js +13 -7
  66. package/packages/dd-trace/src/plugins/index.js +1 -2
  67. package/packages/dd-trace/src/plugins/log_plugin.js +8 -4
  68. package/packages/dd-trace/src/plugins/plugin.js +8 -0
  69. package/packages/dd-trace/src/plugins/util/test.js +20 -1
  70. package/packages/dd-trace/src/plugins/util/web.js +41 -12
  71. package/packages/dd-trace/src/profiling/config.js +8 -8
  72. package/packages/dd-trace/src/profiling/index.js +4 -4
  73. package/packages/dd-trace/src/profiling/profilers/{heap.js → space.js} +2 -2
  74. package/packages/dd-trace/src/profiling/profilers/{cpu.js → wall.js} +3 -3
  75. package/packages/dd-trace/src/span_processor.js +4 -1
  76. package/scripts/install_plugin_modules.js +1 -0
  77. package/packages/datadog-plugin-aws-sdk/src/helpers.js +0 -103
  78. package/packages/datadog-plugin-fastify/src/fastify.js +0 -198
  79. package/packages/datadog-plugin-fastify/src/find-my-way.js +0 -37
  80. package/packages/datadog-plugin-jest/src/jest-environment.js +0 -272
  81. package/packages/datadog-plugin-jest/src/jest-jasmine2.js +0 -185
  82. package/packages/datadog-plugin-knex/src/index.js +0 -23
  83. package/packages/datadog-plugin-limitd-client/src/index.js +0 -30
  84. package/scripts/postpublish.js +0 -24
@@ -1,33 +1,19 @@
1
1
  'use strict'
2
2
 
3
- const web = require('../../dd-trace/src/plugins/util/web')
4
- const routerPlugin = require('../../datadog-plugin-router/src')
3
+ const RouterPlugin = require('../../datadog-plugin-router/src')
5
4
 
6
- function createWrapHandle (tracer, config) {
7
- config = web.normalizeConfig(config)
8
-
9
- return function wrapHandle (handle) {
10
- return function handleWithTrace (req, res) {
11
- web.instrument(tracer, config, req, res, 'express.request')
12
-
13
- return handle.apply(this, arguments)
14
- }
5
+ class ExpressPlugin extends RouterPlugin {
6
+ static get name () {
7
+ return 'express'
15
8
  }
16
- }
17
9
 
18
- function patch (express, tracer, config) {
19
- this.wrap(express.application, 'handle', createWrapHandle(tracer, config))
20
- routerPlugin.patch.call(this, { prototype: express.Router }, tracer, config)
21
- }
10
+ constructor (...args) {
11
+ super(...args)
22
12
 
23
- function unpatch (express) {
24
- this.unwrap(express.application, 'handle')
25
- routerPlugin.unpatch.call(this, { prototype: express.Router })
13
+ this.addSub('apm:express:request:handle', ({ req }) => {
14
+ this.setFramework(req, 'express', this.config)
15
+ })
16
+ }
26
17
  }
27
18
 
28
- module.exports = {
29
- name: 'express',
30
- versions: ['>=4'],
31
- patch,
32
- unpatch
33
- }
19
+ module.exports = ExpressPlugin
@@ -1,6 +1,19 @@
1
1
  'use strict'
2
2
 
3
- module.exports = [].concat(
4
- require('./fastify'),
5
- require('./find-my-way') // TODO make this its own plugin, since restify uses it too
6
- )
3
+ const RouterPlugin = require('../../datadog-plugin-router/src')
4
+
5
+ class FastifyPlugin extends RouterPlugin {
6
+ static get name () {
7
+ return 'fastify'
8
+ }
9
+
10
+ constructor (...args) {
11
+ super(...args)
12
+
13
+ this.addSub('apm:fastify:request:handle', ({ req }) => {
14
+ this.setFramework(req, 'fastify', this.config)
15
+ })
16
+ }
17
+ }
18
+
19
+ module.exports = FastifyPlugin
@@ -0,0 +1,20 @@
1
+ 'use strict'
2
+
3
+ const Plugin = require('../../dd-trace/src/plugins/plugin')
4
+ const web = require('../../dd-trace/src/plugins/util/web')
5
+
6
+ class FindMyWayPlugin extends Plugin {
7
+ static get name () {
8
+ return 'find-my-way'
9
+ }
10
+
11
+ constructor (...args) {
12
+ super(...args)
13
+
14
+ this.addSub('apm:find-my-way:request:route', ({ req, route }) => {
15
+ web.setRoute(req, route)
16
+ })
17
+ }
18
+ }
19
+
20
+ module.exports = FindMyWayPlugin
@@ -60,6 +60,7 @@ function createWrapCreateReadStream (config, tracer) {
60
60
  const tags = makeFSFlagTags('ReadStream', path, options, 'r', config, tracer)
61
61
  return tracer.trace('fs.operation', { tags, orphanable }, (span, done) => {
62
62
  const stream = createReadStream.apply(this, arguments)
63
+ stream.once('close', done)
63
64
  stream.once('end', done)
64
65
  stream.once('error', done)
65
66
  return stream
@@ -74,6 +75,7 @@ function createWrapCreateWriteStream (config, tracer) {
74
75
  const tags = makeFSFlagTags('WriteStream', path, options, 'w', config, tracer)
75
76
  return tracer.trace('fs.operation', { tags, orphanable }, (span, done) => {
76
77
  const stream = createWriteStream.apply(this, arguments)
78
+ stream.once('close', done)
77
79
  stream.once('finish', done)
78
80
  stream.once('error', done)
79
81
  return stream
@@ -1,116 +1,98 @@
1
1
  'use strict'
2
2
 
3
+ const Plugin = require('../../dd-trace/src/plugins/plugin')
4
+ const { storage } = require('../../datadog-core')
3
5
  const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
4
6
 
5
7
  const messageSpans = new WeakMap()
8
+ class GoogleCloudPubsubPlugin extends Plugin {
9
+ static get name () {
10
+ return 'google-cloud-pubsub'
11
+ }
12
+
13
+ constructor (...args) {
14
+ super(...args)
6
15
 
7
- function createWrapRequest (tracer, config) {
8
- return function wrapRequest (request) {
9
- return function requestWithTrace (cfg = { reqOpts: {} }, cb) {
16
+ this.addSub(`apm:google-cloud-pubsub:request:start`, ({ cfg, projectId, messages }) => {
17
+ const store = storage.getStore()
18
+ const childOf = store ? store.span : store
10
19
  const topic = getTopic(cfg)
11
20
  const tags = {
12
21
  component: '@google-cloud/pubsub',
13
22
  'resource.name': [cfg.method, topic].filter(x => x).join(' '),
14
- 'service.name': config.service || `${tracer._service}-pubsub`,
23
+ 'service.name': this.config.service || `${this.tracer._service}-pubsub`,
15
24
  'span.kind': 'client',
16
25
  'pubsub.method': cfg.method,
17
- 'gcloud.project_id': this.projectId,
26
+ 'gcloud.project_id': projectId,
18
27
  'pubsub.topic': topic
19
28
  }
20
29
  if (cfg.method === 'publish') {
21
30
  tags['span.kind'] = 'producer'
22
31
  }
23
- cb = tracer.scope().bind(cb)
24
- return tracer.trace('pubsub.request', { tags }, (span, done) => {
25
- analyticsSampler.sample(span, config.measured)
26
-
27
- if (cfg.reqOpts && cfg.method === 'publish') {
28
- for (const msg of cfg.reqOpts.messages) {
29
- if (!msg.attributes) {
30
- msg.attributes = {}
31
- }
32
- tracer.inject(span, 'text_map', msg.attributes)
33
- }
34
- }
35
-
36
- arguments[1] = function (err) {
37
- done(err)
38
- return cb.apply(this, arguments)
39
- }
40
-
41
- return request.apply(this, arguments)
32
+ const span = this.tracer.startSpan('pubsub.request', {
33
+ childOf,
34
+ tags
42
35
  })
43
- }
44
- }
45
- }
46
36
 
47
- function createWrapSubscriptionEmit (tracer, config) {
48
- return function wrapSubscriptionEmit (emit) {
49
- return function emitWithTrace (eventName, message) {
50
- if (eventName !== 'message' || !message) return emit.apply(this, arguments)
37
+ analyticsSampler.sample(span, this.config.measured)
38
+ this.enter(span, store)
51
39
 
52
- const span = messageSpans.get(message)
53
-
54
- if (!span) return emit.apply(this, arguments)
55
-
56
- return tracer.scope().activate(span, () => {
57
- try {
58
- return emit.apply(this, arguments)
59
- } catch (e) {
60
- span.setTag('error', e)
61
- throw e
40
+ for (const msg of messages) {
41
+ if (!msg.attributes) {
42
+ msg.attributes = {}
62
43
  }
63
- })
64
- }
65
- }
66
- }
44
+ this.tracer.inject(span, 'text_map', msg.attributes)
45
+ }
46
+ })
67
47
 
68
- function createWrapLeaseDispense (tracer, config) {
69
- return function wrapDispense (dispense) {
70
- return function dispenseWithTrace (message) {
48
+ this.addSub(`apm:google-cloud-pubsub:receive:start`, ({ message }) => {
49
+ const store = storage.getStore()
71
50
  const subscription = message._subscriber._subscription
72
51
  const topic = subscription.metadata && subscription.metadata.topic
52
+ const childOf = this.tracer.extract('text_map', message.attributes)
73
53
  const tags = {
74
54
  component: '@google-cloud/pubsub',
75
55
  'resource.name': topic,
76
- 'service.name': config.service || tracer._service,
56
+ 'service.name': this.config.service || this.tracer._service,
77
57
  'gcloud.project_id': subscription.pubsub.projectId,
78
58
  'pubsub.topic': topic,
79
59
  'span.kind': 'consumer',
80
60
  'span.type': 'worker'
81
61
  }
82
62
 
83
- const childOf = tracer.extract('text_map', message.attributes)
84
- const span = tracer.startSpan('pubsub.receive', { tags, childOf })
63
+ const span = this.tracer.startSpan('pubsub.receive', {
64
+ childOf,
65
+ tags
66
+ })
85
67
 
86
- analyticsSampler.sample(span, config.measured, true)
68
+ analyticsSampler.sample(span, this.config.measured, true)
69
+ this.enter(span, store)
87
70
 
88
71
  messageSpans.set(message, span)
72
+ })
89
73
 
90
- return dispense.apply(this, arguments)
91
- }
92
- }
93
- }
74
+ this.addSub(`apm:google-cloud-pubsub:request:error`, err => {
75
+ const span = storage.getStore().span
76
+ span.setTag('error', err)
77
+ })
94
78
 
95
- function createWrapLeaseRemove (tracer, config) {
96
- return function wrapRemove (remove) {
97
- return function removeWithTrace (message) {
98
- finish(message)
79
+ this.addSub(`apm:google-cloud-pubsub:request:finish`, () => {
80
+ const span = storage.getStore().span
81
+ span.finish()
82
+ })
99
83
 
100
- return remove.apply(this, arguments)
101
- }
102
- }
103
- }
104
-
105
- function createWrapLeaseClear (tracer, config) {
106
- return function wrapClear (clear) {
107
- return function clearWithTrace () {
108
- for (const message of this._messages) {
109
- finish(message)
110
- }
84
+ this.addSub(`apm:google-cloud-pubsub:receive:error`, ({ err, message }) => {
85
+ const span = messageSpans.get(message)
86
+ if (!span) return undefined
87
+ span.setTag('error', err)
88
+ })
111
89
 
112
- return clear.apply(this, arguments)
113
- }
90
+ this.addSub(`apm:google-cloud-pubsub:receive:finish`, ({ message }) => {
91
+ const span = messageSpans.get(message)
92
+ if (!span) return
93
+ span.setTag('pubsub.ack', message._handled ? 1 : 0)
94
+ span.finish()
95
+ })
114
96
  }
115
97
  }
116
98
 
@@ -120,41 +102,4 @@ function getTopic (cfg) {
120
102
  }
121
103
  }
122
104
 
123
- function finish (message) {
124
- const span = messageSpans.get(message)
125
-
126
- if (!span) return
127
-
128
- span.setTag('pubsub.ack', message._handled ? 1 : 0)
129
- span.finish()
130
- }
131
-
132
- module.exports = [
133
- {
134
- name: '@google-cloud/pubsub',
135
- versions: ['>=1.2'],
136
- patch ({ PubSub, Subscription }, tracer, config) {
137
- this.wrap(PubSub.prototype, 'request', createWrapRequest(tracer, config))
138
- this.wrap(Subscription.prototype, 'emit', createWrapSubscriptionEmit(tracer, config))
139
- },
140
- unpatch ({ PubSub, Subscription }) {
141
- this.unwrap(PubSub.prototype, 'request')
142
- this.unwrap(Subscription.prototype, 'emit')
143
- }
144
- },
145
- {
146
- name: '@google-cloud/pubsub',
147
- versions: ['>=1.2'],
148
- file: 'build/src/lease-manager.js',
149
- patch ({ LeaseManager }, tracer, config) {
150
- this.wrap(LeaseManager.prototype, '_dispense', createWrapLeaseDispense(tracer, config))
151
- this.wrap(LeaseManager.prototype, 'remove', createWrapLeaseRemove(tracer, config))
152
- this.wrap(LeaseManager.prototype, 'clear', createWrapLeaseClear(tracer, config))
153
- },
154
- unpatch ({ LeaseManager }) {
155
- this.unwrap(LeaseManager.prototype, '_dispense')
156
- this.unwrap(LeaseManager.prototype, 'remove')
157
- this.unwrap(LeaseManager.prototype, 'clear')
158
- }
159
- }
160
- ]
105
+ module.exports = GoogleCloudPubsubPlugin
@@ -4,9 +4,6 @@ const Plugin = require('../../dd-trace/src/plugins/plugin')
4
4
  const { storage } = require('../../datadog-core')
5
5
  const web = require('../../dd-trace/src/plugins/util/web')
6
6
  const { incomingHttpRequestStart } = require('../../dd-trace/src/appsec/gateway/channels')
7
- const tags = require('../../../ext/tags')
8
- const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
9
- const SERVICE_NAME = tags.SERVICE_NAME
10
7
 
11
8
  class HttpServerPlugin extends Plugin {
12
9
  static get name () {
@@ -20,11 +17,6 @@ class HttpServerPlugin extends Plugin {
20
17
  const store = storage.getStore()
21
18
  const span = web.startSpan(this.tracer, this.config, req, res, 'http.request')
22
19
 
23
- if (this.config.service) {
24
- span.setTag(SERVICE_NAME, this.config.service)
25
- }
26
-
27
- analyticsSampler.sample(span, this.config.measured, true)
28
20
  this.enter(span, store)
29
21
 
30
22
  const context = web.getContext(req)
@@ -52,10 +44,10 @@ class HttpServerPlugin extends Plugin {
52
44
  })
53
45
  })
54
46
 
55
- this.addSub('apm:http:server:request:async-end', ({ req }) => {
47
+ this.addSub('apm:http:server:request:finish', ({ req }) => {
56
48
  const context = web.getContext(req)
57
49
 
58
- if (!context) return // Not created by a http.Server instance.
50
+ if (!context || !context.res) return // Not created by a http.Server instance.
59
51
 
60
52
  web.wrapRes(context, context.req, context.res, context.res.end)()
61
53
  })
@@ -1,4 +1,102 @@
1
- const jestEnvironment = require('./jest-environment')
2
- const jestJasmine2 = require('./jest-jasmine2')
1
+ const Plugin = require('../../dd-trace/src/plugins/plugin')
2
+ const { storage } = require('../../datadog-core')
3
3
 
4
- module.exports = [].concat(jestEnvironment, jestJasmine2)
4
+ const {
5
+ CI_APP_ORIGIN,
6
+ TEST_STATUS,
7
+ JEST_TEST_RUNNER,
8
+ finishAllTraceSpans,
9
+ getTestEnvironmentMetadata,
10
+ getTestParentSpan,
11
+ getTestCommonTags,
12
+ TEST_PARAMETERS,
13
+ getCodeOwnersFileEntries,
14
+ getCodeOwnersForFilename,
15
+ TEST_CODE_OWNERS
16
+ } = require('../../dd-trace/src/plugins/util/test')
17
+
18
+ function getTestSpanMetadata (tracer, test) {
19
+ const childOf = getTestParentSpan(tracer)
20
+
21
+ const { suite, name, runner, testParameters } = test
22
+
23
+ const commonTags = getTestCommonTags(name, suite, tracer._version)
24
+
25
+ return {
26
+ childOf,
27
+ ...commonTags,
28
+ [JEST_TEST_RUNNER]: runner,
29
+ [TEST_PARAMETERS]: testParameters
30
+ }
31
+ }
32
+
33
+ class JestPlugin extends Plugin {
34
+ static get name () {
35
+ return 'jest'
36
+ }
37
+
38
+ constructor (...args) {
39
+ super(...args)
40
+
41
+ this.testEnvironmentMetadata = getTestEnvironmentMetadata('jest', this.config)
42
+ this.codeOwnersEntries = getCodeOwnersFileEntries()
43
+
44
+ this.addSub('ci:jest:test:start', (test) => {
45
+ const store = storage.getStore()
46
+ const span = this.startTestSpan(test)
47
+
48
+ this.enter(span, store)
49
+ })
50
+
51
+ this.addSub('ci:jest:test:end', (status) => {
52
+ const span = storage.getStore().span
53
+ span.setTag(TEST_STATUS, status)
54
+ span.finish()
55
+ finishAllTraceSpans(span)
56
+ this.exit()
57
+ })
58
+
59
+ this.addSub('ci:jest:test-suite:end', () => {
60
+ this.tracer._exporter._writer.flush()
61
+ })
62
+
63
+ this.addSub('ci:jest:test:err', (error) => {
64
+ if (error) {
65
+ const span = storage.getStore().span
66
+ span.setTag(TEST_STATUS, 'fail')
67
+ span.setTag('error', error)
68
+ }
69
+ })
70
+
71
+ this.addSub('ci:jest:test:skip', (test) => {
72
+ const span = this.startTestSpan(test)
73
+ span.setTag(TEST_STATUS, 'skip')
74
+ span.finish()
75
+ })
76
+ }
77
+
78
+ startTestSpan (test) {
79
+ const { childOf, ...testSpanMetadata } = getTestSpanMetadata(this.tracer, test)
80
+
81
+ const codeOwners = getCodeOwnersForFilename(test.suite, this.codeOwnersEntries)
82
+
83
+ if (codeOwners) {
84
+ testSpanMetadata[TEST_CODE_OWNERS] = codeOwners
85
+ }
86
+
87
+ const testSpan = this.tracer
88
+ .startSpan('jest.test', {
89
+ childOf,
90
+ tags: {
91
+ ...this.testEnvironmentMetadata,
92
+ ...testSpanMetadata
93
+ }
94
+ })
95
+
96
+ testSpan.context()._trace.origin = CI_APP_ORIGIN
97
+
98
+ return testSpan
99
+ }
100
+ }
101
+
102
+ module.exports = JestPlugin
@@ -1,8 +1,3 @@
1
- const { SAMPLING_RULE_DECISION } = require('../../dd-trace/src/constants')
2
- const { SAMPLING_PRIORITY, SPAN_TYPE } = require('../../../ext/tags')
3
- const { AUTO_KEEP } = require('../../../ext/priority')
4
- const { TEST_TYPE, TEST_STATUS, getTestParentSpan } = require('../../dd-trace/src/plugins/util/test')
5
-
6
1
  /**
7
2
  * There are two ways to call `test.each` in `jest`:
8
3
  * 1. With an array of arrays: https://jestjs.io/docs/api#1-testeachtablename-fn-timeout
@@ -38,27 +33,4 @@ function getFormattedJestTestParameters (testParameters) {
38
33
  return formattedParameters
39
34
  }
40
35
 
41
- function getTestSpanTags (tracer, testEnvironmentMetadata) {
42
- const childOf = getTestParentSpan(tracer)
43
-
44
- const commonSpanTags = {
45
- [TEST_TYPE]: 'test',
46
- [SAMPLING_RULE_DECISION]: 1,
47
- [SAMPLING_PRIORITY]: AUTO_KEEP,
48
- [SPAN_TYPE]: 'test',
49
- ...testEnvironmentMetadata
50
- }
51
- return {
52
- childOf,
53
- commonSpanTags
54
- }
55
- }
56
-
57
- function setSuppressedErrors (suppressedErrors, testSpan) {
58
- if (suppressedErrors && suppressedErrors.length) {
59
- testSpan.setTag('error', suppressedErrors[0])
60
- testSpan.setTag(TEST_STATUS, 'fail')
61
- }
62
- }
63
-
64
- module.exports = { getFormattedJestTestParameters, getTestSpanTags, setSuppressedErrors }
36
+ module.exports = { getFormattedJestTestParameters }
@@ -1,90 +1,85 @@
1
1
  'use strict'
2
2
 
3
+ const Plugin = require('../../dd-trace/src/plugins/plugin')
4
+ const { storage } = require('../../datadog-core')
3
5
  const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
4
6
 
5
- function createWrapProducer (tracer, config) {
6
- return function wrapProducer (createProducer) {
7
- return function producerWithTrace () {
8
- const serviceName = config.service || `${tracer._service}-kafka`
9
- const producer = createProducer.apply(this, arguments)
10
-
11
- const send = producer.send
12
-
13
- const tags = {
14
- 'service.name': serviceName,
15
- 'span.kind': 'producer',
16
- 'component': 'kafkajs'
17
- }
7
+ class KafkajsPlugin extends Plugin {
8
+ static get name () {
9
+ return 'kafkajs'
10
+ }
18
11
 
19
- producer.send = tracer.wrap('kafka.produce', { tags }, function (...args) {
20
- const { topic, messages = [] } = args[0]
21
- const currentSpan = tracer.scope().active()
12
+ constructor (...args) {
13
+ super(...args)
14
+
15
+ this.addSub(`apm:kafkajs:produce:start`, ({ topic, messages }) => {
16
+ const store = storage.getStore()
17
+ const childOf = store ? store.span : store
18
+ const span = this.tracer.startSpan('kafka.produce', {
19
+ childOf,
20
+ tags: {
21
+ 'service.name': this.config.service || `${this.tracer._service}-kafka`,
22
+ 'span.kind': 'producer',
23
+ 'component': 'kafkajs'
24
+ }
25
+ })
22
26
 
23
- analyticsSampler.sample(currentSpan, config.measured)
27
+ analyticsSampler.sample(span, this.config.measured)
28
+ this.enter(span, store)
24
29
 
25
- currentSpan.addTags({
30
+ span.addTags({
31
+ 'resource.name': topic,
32
+ 'kafka.topic': topic,
33
+ 'kafka.batch_size': messages.length
34
+ })
35
+ for (const message of messages) {
36
+ if (typeof message === 'object') {
37
+ this.tracer.inject(span, 'text_map', message.headers)
38
+ }
39
+ }
40
+ })
41
+
42
+ this.addSub(`apm:kafkajs:consume:start`, ({ topic, partition, message }) => {
43
+ const store = storage.getStore()
44
+ const childOf = extract(this.tracer, message.headers)
45
+ const span = this.tracer.startSpan('kafka.consume', {
46
+ childOf,
47
+ tags: {
48
+ 'service.name': this.config.service || `${this.tracer._service}-kafka`,
49
+ 'span.kind': 'consumer',
50
+ 'span.type': 'worker',
51
+ 'component': 'kafkajs',
26
52
  'resource.name': topic,
27
53
  'kafka.topic': topic,
28
- 'kafka.batch_size': messages.length
29
- })
30
-
31
- for (const message of messages) {
32
- message.headers = message.headers || {}
33
- tracer.inject(currentSpan, 'text_map', message.headers)
54
+ 'kafka.partition': partition,
55
+ 'kafka.message.offset': message.offset
34
56
  }
35
-
36
- return send.apply(this, args)
37
57
  })
38
58
 
39
- return producer
40
- }
41
- }
42
- }
59
+ analyticsSampler.sample(span, this.config.measured, true)
60
+ this.enter(span, store)
61
+ })
43
62
 
44
- function createWrapConsumer (tracer, config) {
45
- return function wrapConsumer (createConsumer) {
46
- return function consumerWithTrace () {
47
- const serviceName = config.service || `${tracer._service}-kafka`
48
- const consumer = createConsumer.apply(this, arguments)
49
- const run = consumer.run
50
-
51
- const tags = {
52
- 'service.name': serviceName,
53
- 'span.kind': 'consumer',
54
- 'span.type': 'worker',
55
- 'component': 'kafkajs'
56
- }
63
+ this.addSub(`apm:kafkajs:end`, () => {
64
+ this.exit()
65
+ })
57
66
 
58
- consumer.run = function ({ eachMessage, ...runArgs }) {
59
- if (typeof eachMessage !== 'function') return run({ eachMessage, ...runArgs })
67
+ this.addSub(`apm:kafkajs:consume:error`, errorHandler)
60
68
 
61
- return run({
62
- eachMessage: function (...eachMessageArgs) {
63
- const { topic, partition, message } = eachMessageArgs[0]
64
- const childOf = extract(tracer, message.headers)
69
+ this.addSub(`apm:kafkajs:consume:finish`, finishHandler)
65
70
 
66
- return tracer.trace('kafka.consume', { childOf, tags }, () => {
67
- const currentSpan = tracer.scope().active()
71
+ this.addSub(`apm:kafkajs:produce:error`, errorHandler)
68
72
 
69
- analyticsSampler.sample(currentSpan, config.measured, true)
70
-
71
- currentSpan.addTags({
72
- 'resource.name': topic,
73
- 'kafka.topic': topic,
74
- 'kafka.partition': partition,
75
- 'kafka.message.offset': message.offset
76
- })
73
+ this.addSub(`apm:kafkajs:produce:finish`, finishHandler)
74
+ }
75
+ }
77
76
 
78
- return eachMessage.apply(this, eachMessageArgs)
79
- })
80
- },
81
- ...runArgs
82
- })
83
- }
77
+ function finishHandler () {
78
+ storage.getStore().span.finish()
79
+ }
84
80
 
85
- return consumer
86
- }
87
- }
81
+ function errorHandler (error) {
82
+ storage.getStore().span.setTag('error', error)
88
83
  }
89
84
 
90
85
  function extract (tracer, bufferMap) {
@@ -99,25 +94,4 @@ function extract (tracer, bufferMap) {
99
94
  return tracer.extract('text_map', textMap)
100
95
  }
101
96
 
102
- module.exports = [
103
- {
104
- name: 'kafkajs',
105
- versions: ['>=1.4'],
106
- patch ({ Kafka }, tracer, config) {
107
- this.wrap(
108
- Kafka.prototype,
109
- 'producer',
110
- createWrapProducer(tracer, config)
111
- )
112
- this.wrap(
113
- Kafka.prototype,
114
- 'consumer',
115
- createWrapConsumer(tracer, config)
116
- )
117
- },
118
- unpatch ({ Kafka }) {
119
- this.unwrap(Kafka.prototype, 'producer')
120
- this.unwrap(Kafka.prototype, 'consumer')
121
- }
122
- }
123
- ]
97
+ module.exports = KafkajsPlugin