dd-trace 2.3.1 → 2.4.2

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 (53) hide show
  1. package/ci/init.js +26 -2
  2. package/index.d.ts +51 -0
  3. package/package.json +2 -2
  4. package/packages/datadog-instrumentations/index.js +10 -0
  5. package/packages/datadog-instrumentations/src/amqp10.js +70 -0
  6. package/packages/datadog-instrumentations/src/amqplib.js +58 -0
  7. package/packages/datadog-instrumentations/src/cassandra-driver.js +191 -0
  8. package/packages/datadog-instrumentations/src/cucumber.js +27 -12
  9. package/packages/datadog-instrumentations/src/helpers/hook.js +44 -0
  10. package/packages/datadog-instrumentations/src/helpers/instrument.js +31 -58
  11. package/packages/datadog-instrumentations/src/http/client.js +170 -0
  12. package/packages/datadog-instrumentations/src/http/server.js +61 -0
  13. package/packages/datadog-instrumentations/src/http.js +4 -0
  14. package/packages/datadog-instrumentations/src/mocha.js +139 -0
  15. package/packages/datadog-instrumentations/src/mongodb-core.js +179 -0
  16. package/packages/datadog-instrumentations/src/net.js +117 -0
  17. package/packages/datadog-instrumentations/src/pg.js +75 -0
  18. package/packages/datadog-instrumentations/src/rhea.js +224 -0
  19. package/packages/datadog-instrumentations/src/tedious.js +66 -0
  20. package/packages/datadog-plugin-amqp10/src/index.js +79 -122
  21. package/packages/datadog-plugin-amqplib/src/index.js +77 -142
  22. package/packages/datadog-plugin-cassandra-driver/src/index.js +52 -224
  23. package/packages/datadog-plugin-cucumber/src/index.js +3 -1
  24. package/packages/datadog-plugin-elasticsearch/src/index.js +4 -2
  25. package/packages/datadog-plugin-http/src/client.js +112 -252
  26. package/packages/datadog-plugin-http/src/index.js +29 -3
  27. package/packages/datadog-plugin-http/src/server.js +54 -32
  28. package/packages/datadog-plugin-jest/src/jest-environment.js +3 -3
  29. package/packages/datadog-plugin-jest/src/jest-jasmine2.js +5 -3
  30. package/packages/datadog-plugin-mocha/src/index.js +96 -207
  31. package/packages/datadog-plugin-mongodb-core/src/index.js +119 -3
  32. package/packages/datadog-plugin-net/src/index.js +65 -121
  33. package/packages/datadog-plugin-next/src/index.js +10 -10
  34. package/packages/datadog-plugin-pg/src/index.js +32 -69
  35. package/packages/datadog-plugin-rhea/src/index.js +59 -225
  36. package/packages/datadog-plugin-tedious/src/index.js +38 -86
  37. package/packages/dd-trace/lib/version.js +1 -1
  38. package/packages/dd-trace/src/appsec/recommended.json +235 -315
  39. package/packages/dd-trace/src/config.js +6 -0
  40. package/packages/dd-trace/src/iitm.js +5 -1
  41. package/packages/dd-trace/src/loader.js +6 -4
  42. package/packages/dd-trace/src/noop/tracer.js +4 -0
  43. package/packages/dd-trace/src/opentracing/propagation/text_map.js +34 -1
  44. package/packages/dd-trace/src/opentracing/span.js +34 -0
  45. package/packages/dd-trace/src/plugin_manager.js +4 -0
  46. package/packages/dd-trace/src/plugins/plugin.js +3 -1
  47. package/packages/dd-trace/src/plugins/util/web.js +99 -93
  48. package/packages/dd-trace/src/proxy.js +4 -0
  49. package/packages/dd-trace/src/ritm.js +60 -25
  50. package/packages/dd-trace/src/tracer.js +16 -0
  51. package/packages/datadog-plugin-mongodb-core/src/legacy.js +0 -59
  52. package/packages/datadog-plugin-mongodb-core/src/unified.js +0 -138
  53. package/packages/datadog-plugin-mongodb-core/src/util.js +0 -143
@@ -1,181 +1,75 @@
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
- const tx = require('../../dd-trace/src/plugins/util/tx')
5
6
 
6
- function createWrapInnerExecute (tracer, config) {
7
- const isValid = (args) => {
8
- return args.length === 4 || typeof args[3] === 'function'
7
+ class CassandraDriverPlugin extends Plugin {
8
+ static get name () {
9
+ return 'cassandra-driver'
9
10
  }
11
+ constructor (...args) {
12
+ super(...args)
10
13
 
11
- return function wrapInnerExecute (_innerExecute) {
12
- return function _innerExecuteWithTrace (query, params, execOptions, callback) {
13
- if (!isValid(arguments)) {
14
- return _innerExecute.apply(this, arguments)
15
- }
16
-
17
- const scope = tracer.scope()
18
- const span = start(tracer, config, this, query)
19
-
20
- return scope.bind(_innerExecute, span).apply(this, tx.wrap(span, arguments))
21
- }
22
- }
23
- }
24
-
25
- function createWrapExecute (tracer, config) {
26
- return function wrapExecute (_execute) {
27
- return function _executeWithTrace (query, params, execOptions, callback) {
28
- const span = start(tracer, config, this, query)
29
- const promise = tracer.scope().bind(_execute, span).apply(this, arguments)
30
-
31
- return tx.wrap(span, promise)
32
- }
33
- }
34
- }
14
+ this.addSub(`apm:cassandra:query:start`, ({ keyspace, query, connectionOptions }) => {
15
+ const store = storage.getStore()
16
+ const childOf = store ? store.span : store
35
17
 
36
- function createWrapExecutionStart (tracer, config) {
37
- return function wrapExecutionStart (start) {
38
- return function startWithTrace (getHostCallback) {
39
- const span = tracer.scope().active()
40
- const execution = this
41
-
42
- if (!isRequestValid(this, arguments, 1, span)) {
43
- return start.apply(this, arguments)
18
+ if (Array.isArray(query)) {
19
+ query = combine(query)
44
20
  }
45
21
 
46
- arguments[0] = function () {
47
- addHost(span, execution._connection)
48
- return getHostCallback.apply(this, arguments)
22
+ const span = this.tracer.startSpan('cassandra.query', {
23
+ childOf,
24
+ tags: {
25
+ 'service.name': this.config.service || `${this.tracer._service}-cassandra`,
26
+ 'resource.name': trim(query, 5000),
27
+ 'span.type': 'cassandra',
28
+ 'span.kind': 'client',
29
+ 'db.type': 'cassandra',
30
+ 'cassandra.query': query,
31
+ 'cassandra.keyspace': keyspace
32
+ }
33
+ })
34
+
35
+ if (connectionOptions) {
36
+ span.addTags({
37
+ 'out.host': connectionOptions.host,
38
+ 'out.port': connectionOptions.port
39
+ })
49
40
  }
50
41
 
51
- return start.apply(this, arguments)
52
- }
53
- }
54
- }
55
-
56
- function createWrapSendOnConnection (tracer, config) {
57
- return function wrapSendOnConnection (_sendOnConnection) {
58
- return function _sendOnConnectionWithTrace () {
59
- const span = tracer.scope().active()
60
-
61
- addHost(span, this._connection)
62
-
63
- return _sendOnConnection.apply(this, arguments)
64
- }
65
- }
66
- }
67
-
68
- function createWrapSend (tracer, config) {
69
- return function wrapSend (send) {
70
- return function sendWithTrace (request, options, callback) {
71
- const span = tracer.scope().active()
72
- const handler = this
73
-
74
- if (!isRequestValid(this, arguments, 3, span)) {
75
- return send.apply(this, arguments)
76
- }
42
+ analyticsSampler.sample(span, this.config.measured)
43
+ this.enter(span, store)
44
+ })
77
45
 
78
- arguments[2] = function () {
79
- addHost(span, handler.connection)
80
- return callback.apply(this, arguments)
81
- }
46
+ this.addSub(`apm:cassandra:query:end`, () => {
47
+ this.exit()
48
+ })
82
49
 
83
- return send.apply(this, arguments)
84
- }
85
- }
86
- }
50
+ this.addSub(`apm:cassandra:query:error`, err => {
51
+ storage.getStore().span.setTag('error', err)
52
+ })
87
53
 
88
- function createWrapBatch (tracer, config) {
89
- return function wrapBatch (batch) {
90
- return function batchWithTrace (queries, options, callback) {
91
- const query = combine(queries)
92
- const span = start(tracer, config, this, query)
93
- const scope = tracer.scope()
94
- const fn = scope.bind(batch, span)
54
+ this.addSub(`apm:cassandra:query:async-end`, () => {
55
+ storage.getStore().span.finish()
56
+ })
95
57
 
96
- try {
97
- return tx.wrap(span, fn.apply(this, tx.wrap(span, arguments)))
98
- } catch (e) {
99
- finish(span, e)
100
- throw e
58
+ this.addSub(`apm:cassandra:query:addConnection`, connectionOptions => {
59
+ const store = storage.getStore()
60
+ if (!store) {
61
+ return
101
62
  }
102
- }
103
- }
104
- }
105
-
106
- function createWrapStream (tracer, config) {
107
- return function wrapStream (stream) {
108
- return function streamWithTrace (query, params, options, callback) {
109
- return tracer.scope().bind(stream.apply(this, arguments))
110
- }
111
- }
112
- }
113
-
114
- function start (tracer, config, client = {}, query) {
115
- const scope = tracer.scope()
116
- const childOf = scope.active()
117
- const span = tracer.startSpan('cassandra.query', {
118
- childOf,
119
- tags: {
120
- 'service.name': config.service || `${tracer._service}-cassandra`,
121
- 'resource.name': trim(query, 5000),
122
- 'span.type': 'cassandra',
123
- 'span.kind': 'client',
124
- 'db.type': 'cassandra',
125
- 'cassandra.query': query,
126
- 'cassandra.keyspace': client.keyspace
127
- }
128
- })
129
-
130
- analyticsSampler.sample(span, config.measured)
131
-
132
- return span
133
- }
134
-
135
- function finish (span, error) {
136
- addError(span, error)
137
-
138
- span.finish()
139
-
140
- return error
141
- }
142
-
143
- function addTag (span, key, value) {
144
- if (value) {
145
- span.setTag(key, value)
146
- }
147
- }
148
-
149
- function addHost (span, connection) {
150
- if (span && connection) {
151
- addTag(span, 'out.host', connection.address)
152
- addTag(span, 'out.port', connection.port)
153
- }
154
- }
155
-
156
- function addError (span, error) {
157
- if (error && error instanceof Error) {
158
- span.addTags({
159
- 'error.type': error.name,
160
- 'error.msg': error.message,
161
- 'error.stack': error.stack
63
+ const span = store.span
64
+ span.addTags({
65
+ 'out.host': connectionOptions.address,
66
+ 'out.port': connectionOptions.port
67
+ })
162
68
  })
163
69
  }
164
-
165
- return error
166
- }
167
-
168
- function isRequestValid (exec, args, length, span) {
169
- if (!exec) return false
170
- if (args.length !== length || typeof args[length - 1] !== 'function') return false
171
- if (!span || span.context()._name !== 'cassandra.query') return false
172
-
173
- return true
174
70
  }
175
71
 
176
72
  function combine (queries) {
177
- if (!Array.isArray(queries)) return []
178
-
179
73
  return queries
180
74
  .map(query => (query.query || query).replace(/;?$/, ';'))
181
75
  .join(' ')
@@ -187,70 +81,4 @@ function trim (str, size) {
187
81
  return `${str.substr(0, size - 3)}...`
188
82
  }
189
83
 
190
- module.exports = [
191
- {
192
- name: 'cassandra-driver',
193
- versions: ['>=3.0.0'],
194
- patch (cassandra, tracer, config) {
195
- this.wrap(cassandra.Client.prototype, 'batch', createWrapBatch(tracer, config))
196
- this.wrap(cassandra.Client.prototype, 'stream', createWrapStream(tracer, config))
197
- },
198
- unpatch (cassandra) {
199
- this.unwrap(cassandra.Client.prototype, 'batch')
200
- this.unwrap(cassandra.Client.prototype, 'stream')
201
- }
202
- },
203
- {
204
- name: 'cassandra-driver',
205
- versions: ['>=4.4'],
206
- patch (cassandra, tracer, config) {
207
- this.wrap(cassandra.Client.prototype, '_execute', createWrapExecute(tracer, config))
208
- },
209
- unpatch (cassandra) {
210
- this.unwrap(cassandra.Client.prototype, '_execute')
211
- }
212
- },
213
- {
214
- name: 'cassandra-driver',
215
- versions: ['3 - 4.3'],
216
- patch (cassandra, tracer, config) {
217
- this.wrap(cassandra.Client.prototype, '_innerExecute', createWrapInnerExecute(tracer, config))
218
- },
219
- unpatch (cassandra) {
220
- this.unwrap(cassandra.Client.prototype, '_innerExecute')
221
- }
222
- },
223
- {
224
- name: 'cassandra-driver',
225
- versions: ['>=3.3'],
226
- file: 'lib/request-execution.js',
227
- patch (RequestExecution, tracer, config) {
228
- this.wrap(RequestExecution.prototype, '_sendOnConnection', createWrapSendOnConnection(tracer, config))
229
- },
230
- unpatch (RequestExecution) {
231
- this.unwrap(RequestExecution.prototype, '_sendOnConnection')
232
- }
233
- },
234
- {
235
- name: 'cassandra-driver',
236
- versions: ['3.3 - 4.3'],
237
- file: 'lib/request-execution.js',
238
- patch (RequestExecution, tracer, config) {
239
- this.wrap(RequestExecution.prototype, 'start', createWrapExecutionStart(tracer, config))
240
- },
241
- unpatch (RequestExecution) {
242
- this.unwrap(RequestExecution.prototype, 'start')
243
- }
244
- },
245
- {
246
- name: 'cassandra-driver',
247
- versions: ['3 - 3.2'],
248
- file: 'lib/request-handler.js',
249
- patch (RequestHandler, tracer, config) {
250
- this.wrap(RequestHandler.prototype, 'send', createWrapSend(tracer, config))
251
- },
252
- unpatch (RequestHandler) {
253
- this.unwrap(RequestHandler.prototype, 'send')
254
- }
255
- }
256
- ]
84
+ module.exports = CassandraDriverPlugin
@@ -16,8 +16,9 @@ const {
16
16
  getTestEnvironmentMetadata,
17
17
  getTestSuitePath
18
18
  } = require('../../dd-trace/src/plugins/util/test')
19
- const { SPAN_TYPE, RESOURCE_NAME } = require('../../../ext/tags')
19
+ const { SPAN_TYPE, RESOURCE_NAME, SAMPLING_PRIORITY } = require('../../../ext/tags')
20
20
  const { SAMPLING_RULE_DECISION } = require('../../dd-trace/src/constants')
21
+ const { AUTO_KEEP } = require('../../../ext/priority')
21
22
 
22
23
  class CucumberPlugin extends Plugin {
23
24
  static get name () {
@@ -44,6 +45,7 @@ class CucumberPlugin extends Plugin {
44
45
  [TEST_NAME]: pickleName,
45
46
  [TEST_SUITE]: testSuite,
46
47
  [SAMPLING_RULE_DECISION]: 1,
48
+ [SAMPLING_PRIORITY]: AUTO_KEEP,
47
49
  [TEST_FRAMEWORK_VERSION]: this.tracer._version,
48
50
  ...testEnvironmentMetadata
49
51
  }
@@ -13,8 +13,6 @@ class ElasticsearchPlugin extends Plugin {
13
13
  super(...args)
14
14
 
15
15
  this.addSub('apm:elasticsearch:query:start', ({ params }) => {
16
- this.config = normalizeConfig(this.config)
17
-
18
16
  const store = storage.getStore()
19
17
  const childOf = store ? store.span : store
20
18
  const body = getBody(params.body || params.bulkBody)
@@ -51,6 +49,10 @@ class ElasticsearchPlugin extends Plugin {
51
49
  span.finish()
52
50
  })
53
51
  }
52
+
53
+ configure (config) {
54
+ return super.configure(normalizeConfig(config))
55
+ }
54
56
  }
55
57
 
56
58
  function normalizeConfig (config) {