dd-trace 2.4.2 → 2.6.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 (53) hide show
  1. package/LICENSE-3rdparty.csv +1 -0
  2. package/ci/init.js +6 -0
  3. package/ci/jest/env.js +16 -3
  4. package/ext/exporters.d.ts +2 -1
  5. package/ext/exporters.js +2 -1
  6. package/index.d.ts +17 -1
  7. package/package.json +5 -4
  8. package/packages/datadog-instrumentations/index.js +1 -0
  9. package/packages/datadog-instrumentations/src/cypress.js +8 -0
  10. package/packages/datadog-instrumentations/src/jest.js +170 -0
  11. package/packages/datadog-plugin-aws-sdk/src/helpers.js +4 -4
  12. package/packages/datadog-plugin-aws-sdk/src/index.js +1 -1
  13. package/packages/datadog-plugin-cucumber/src/index.js +16 -16
  14. package/packages/datadog-plugin-cypress/src/index.js +10 -5
  15. package/packages/datadog-plugin-cypress/src/plugin.js +18 -17
  16. package/packages/datadog-plugin-fs/src/index.js +2 -0
  17. package/packages/datadog-plugin-http/src/server.js +0 -8
  18. package/packages/datadog-plugin-jest/src/index.js +101 -3
  19. package/packages/datadog-plugin-jest/src/util.js +1 -29
  20. package/packages/datadog-plugin-mocha/src/index.js +14 -15
  21. package/packages/dd-trace/lib/version.js +1 -1
  22. package/packages/dd-trace/src/appsec/callbacks/ddwaf.js +29 -12
  23. package/packages/dd-trace/src/appsec/index.js +7 -3
  24. package/packages/dd-trace/src/appsec/recommended.json +15 -5
  25. package/packages/dd-trace/src/appsec/reporter.js +29 -3
  26. package/packages/dd-trace/src/appsec/rule_manager.js +2 -2
  27. package/packages/dd-trace/src/ci-visibility/exporters/agentless/index.js +32 -0
  28. package/packages/dd-trace/src/ci-visibility/exporters/agentless/writer.js +51 -0
  29. package/packages/dd-trace/src/config.js +33 -4
  30. package/packages/dd-trace/src/encode/0.4.js +0 -1
  31. package/packages/dd-trace/src/encode/agentless-ci-visibility.js +193 -0
  32. package/packages/dd-trace/src/encode/tags-processors.js +116 -0
  33. package/packages/dd-trace/src/exporter.js +3 -0
  34. package/packages/dd-trace/src/exporters/agent/index.js +1 -1
  35. package/packages/dd-trace/src/exporters/agent/writer.js +7 -32
  36. package/packages/dd-trace/src/exporters/{agent → common}/docker.js +0 -0
  37. package/packages/dd-trace/src/exporters/common/request.js +83 -0
  38. package/packages/dd-trace/src/exporters/common/writer.js +36 -0
  39. package/packages/dd-trace/src/exporters/{agent/scheduler.js → scheduler.js} +0 -0
  40. package/packages/dd-trace/src/format.js +9 -5
  41. package/packages/dd-trace/src/instrumenter.js +3 -0
  42. package/packages/dd-trace/src/pkg.js +11 -6
  43. package/packages/dd-trace/src/plugins/util/test.js +79 -1
  44. package/packages/dd-trace/src/plugins/util/web.js +11 -10
  45. package/packages/dd-trace/src/profiling/exporters/agent.js +1 -1
  46. package/packages/dd-trace/src/profiling/profilers/cpu.js +1 -1
  47. package/packages/dd-trace/src/proxy.js +2 -0
  48. package/packages/dd-trace/src/span_processor.js +4 -1
  49. package/packages/dd-trace/src/telemetry.js +187 -0
  50. package/scripts/install_plugin_modules.js +1 -0
  51. package/packages/datadog-plugin-jest/src/jest-environment.js +0 -272
  52. package/packages/datadog-plugin-jest/src/jest-jasmine2.js +0 -185
  53. package/packages/dd-trace/src/exporters/agent/request.js +0 -86
@@ -1,185 +0,0 @@
1
- const { promisify } = require('util')
2
-
3
- const { RESOURCE_NAME } = require('../../../ext/tags')
4
- const {
5
- TEST_NAME,
6
- TEST_SUITE,
7
- TEST_STATUS,
8
- TEST_FRAMEWORK_VERSION,
9
- JEST_TEST_RUNNER,
10
- CI_APP_ORIGIN,
11
- getTestEnvironmentMetadata,
12
- finishAllTraceSpans,
13
- getTestSuitePath
14
- } = require('../../dd-trace/src/plugins/util/test')
15
- const { getTestSpanTags, setSuppressedErrors } = require('./util')
16
-
17
- function createWrapIt (tracer, globalConfig, globalInput, testEnvironmentMetadata) {
18
- return function wrapIt (it) {
19
- return function itWithTrace (description, specFunction, timeout) {
20
- let oldSpecFunction = specFunction
21
- if (specFunction.length) {
22
- oldSpecFunction = promisify(oldSpecFunction)
23
- }
24
-
25
- const { childOf, commonSpanTags } = getTestSpanTags(tracer, testEnvironmentMetadata)
26
-
27
- const testSuite = getTestSuitePath(globalInput.jasmine.testPath, globalConfig.rootDir)
28
-
29
- const newSpecFunction = tracer.wrap(
30
- 'jest.test',
31
- {
32
- type: 'test',
33
- childOf,
34
- tags: {
35
- ...commonSpanTags,
36
- [TEST_SUITE]: testSuite,
37
- [TEST_FRAMEWORK_VERSION]: tracer._version,
38
- [JEST_TEST_RUNNER]: 'jest-jasmine2'
39
- }
40
- },
41
- async (done) => {
42
- const testSpan = tracer.scope().active()
43
- const { currentTestName } = globalInput.expect.getState()
44
- const resource = `${testSuite}.${currentTestName}`
45
- testSpan.setTag(TEST_NAME, currentTestName)
46
- testSpan.setTag(RESOURCE_NAME, resource)
47
- testSpan.context()._trace.origin = CI_APP_ORIGIN
48
- let result
49
- globalInput.jasmine.testSpanByTestName[currentTestName] = testSpan
50
-
51
- try {
52
- result = await oldSpecFunction()
53
- const suppressedErrors = globalInput.expect.getState().suppressedErrors
54
- setSuppressedErrors(suppressedErrors, testSpan)
55
- if (!testSpan._spanContext._tags[TEST_STATUS]) {
56
- testSpan.setTag(TEST_STATUS, 'pass')
57
- }
58
- } catch (error) {
59
- testSpan.setTag(TEST_STATUS, 'fail')
60
- testSpan.setTag('error', error)
61
- if (done) {
62
- done(error)
63
- }
64
- throw error
65
- } finally {
66
- finishAllTraceSpans(testSpan)
67
- }
68
- if (done) {
69
- done(result)
70
- }
71
- }
72
- )
73
- return it(description, newSpecFunction, timeout)
74
- }
75
- }
76
- }
77
-
78
- function createWrapOnException (tracer, globalInput) {
79
- return function wrapOnException (onException) {
80
- return function onExceptionWithTrace (err) {
81
- let activeTestSpan = tracer.scope().active()
82
- if (!activeTestSpan) {
83
- activeTestSpan = globalInput.jasmine.testSpanByTestName[this.getFullName()]
84
- }
85
- if (!activeTestSpan) {
86
- return onException.apply(this, arguments)
87
- }
88
- const {
89
- [TEST_NAME]: testName,
90
- [TEST_SUITE]: testSuite,
91
- [TEST_STATUS]: testStatus
92
- } = activeTestSpan._spanContext._tags
93
-
94
- const isActiveSpanFailing = this.getFullName() === testName &&
95
- this.result.testPath.endsWith(testSuite)
96
-
97
- if (isActiveSpanFailing && !testStatus) {
98
- activeTestSpan.setTag(TEST_STATUS, 'fail')
99
- // If we don't do this, jest will show this file on its error message
100
- if (err.stack) {
101
- const stackFrames = err.stack.split('\n')
102
- const filteredStackFrames = stackFrames.filter(frame => !frame.includes(__dirname)).join('\n')
103
- err.stack = filteredStackFrames
104
- }
105
- activeTestSpan.setTag('error', err)
106
- // need to manually finish, as it will not be caught in `itWithTrace`
107
- activeTestSpan.finish()
108
- }
109
-
110
- return onException.apply(this, arguments)
111
- }
112
- }
113
- }
114
-
115
- function createWrapItSkip (tracer, globalConfig, globalInput, testEnvironmentMetadata) {
116
- return function wrapItSkip (it) {
117
- return function itSkipWithTrace () {
118
- const { childOf, commonSpanTags } = getTestSpanTags(tracer, testEnvironmentMetadata)
119
-
120
- const testSuite = getTestSuitePath(globalInput.jasmine.testPath, globalConfig.rootDir)
121
-
122
- const spec = it.apply(this, arguments)
123
-
124
- const testName = spec.getFullName()
125
- const resource = `${testSuite}.${testName}`
126
-
127
- const testSpan = tracer.startSpan(
128
- 'jest.test',
129
- {
130
- childOf,
131
- tags: {
132
- ...commonSpanTags,
133
- [RESOURCE_NAME]: resource,
134
- [TEST_NAME]: testName,
135
- [TEST_SUITE]: testSuite,
136
- [TEST_STATUS]: 'skip',
137
- [TEST_FRAMEWORK_VERSION]: tracer._version,
138
- [JEST_TEST_RUNNER]: 'jest-jasmine2'
139
- }
140
- }
141
- )
142
- testSpan.context()._trace.origin = CI_APP_ORIGIN
143
- testSpan.finish()
144
-
145
- return spec
146
- }
147
- }
148
- }
149
-
150
- function createWrapJasmineAsyncInstall (tracer, instrumenter, testEnvironmentMetadata) {
151
- return function jasmineAsyncInstallWithTrace (jasmineAsyncInstall) {
152
- return function (globalConfig, globalInput) {
153
- globalInput.jasmine.testSpanByTestName = {}
154
- instrumenter.wrap(globalInput.jasmine.Spec.prototype, 'onException', createWrapOnException(tracer, globalInput))
155
- instrumenter.wrap(globalInput, 'it', createWrapIt(tracer, globalConfig, globalInput, testEnvironmentMetadata))
156
- // instruments 'it.only'
157
- instrumenter.wrap(globalInput, 'fit', createWrapIt(tracer, globalConfig, globalInput, testEnvironmentMetadata))
158
- // instruments 'it.skip'
159
- instrumenter.wrap(
160
- globalInput,
161
- 'xit',
162
- createWrapItSkip(tracer, globalConfig, globalInput, testEnvironmentMetadata)
163
- )
164
- return jasmineAsyncInstall(globalConfig, globalInput)
165
- }
166
- }
167
- }
168
-
169
- module.exports = [
170
- {
171
- name: 'jest-jasmine2',
172
- versions: ['>=24.8.0'],
173
- file: 'build/jasmineAsyncInstall.js',
174
- patch: function (jasmineAsyncInstallExport, tracer, config) {
175
- const testEnvironmentMetadata = getTestEnvironmentMetadata('jest', config)
176
- return this.wrapExport(
177
- jasmineAsyncInstallExport.default,
178
- createWrapJasmineAsyncInstall(tracer, this, testEnvironmentMetadata)(jasmineAsyncInstallExport.default)
179
- )
180
- },
181
- unpatch: function (jasmineAsyncInstallExport) {
182
- this.unwrapExport(jasmineAsyncInstallExport.default)
183
- }
184
- }
185
- ]
@@ -1,86 +0,0 @@
1
- 'use strict'
2
-
3
- const http = require('http')
4
- const https = require('https')
5
- const docker = require('./docker')
6
- const log = require('../../log')
7
- const { storage } = require('../../../../datadog-core')
8
-
9
- const httpAgent = new http.Agent({ keepAlive: true })
10
- const httpsAgent = new https.Agent({ keepAlive: true })
11
- const containerId = docker.id()
12
-
13
- function retriableRequest (options, callback, client, data) {
14
- const store = storage.getStore()
15
-
16
- storage.enterWith({ noop: true })
17
-
18
- const req = client.request(options, res => {
19
- let data = ''
20
-
21
- res.setTimeout(options.timeout)
22
-
23
- res.on('data', chunk => { data += chunk })
24
- res.on('end', () => {
25
- if (res.statusCode >= 200 && res.statusCode <= 299) {
26
- callback(null, data, res.statusCode)
27
- } else {
28
- const error = new Error(`Error from the agent: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}`)
29
- error.status = res.statusCode
30
-
31
- callback(error, null, res.statusCode)
32
- }
33
- })
34
- })
35
- req.setTimeout(options.timeout, req.abort)
36
- data.forEach(buffer => req.write(buffer))
37
-
38
- storage.enterWith(store)
39
-
40
- return req
41
- }
42
-
43
- function request (options, callback) {
44
- options = Object.assign({
45
- headers: {},
46
- data: [],
47
- timeout: 2000
48
- }, options)
49
-
50
- const data = [].concat(options.data)
51
- const isSecure = options.protocol === 'https:'
52
- const client = isSecure ? https : http
53
- const agent = isSecure ? httpsAgent : httpAgent
54
-
55
- options.agent = agent
56
- options.headers['Content-Length'] = byteLength(data)
57
-
58
- if (containerId) {
59
- options.headers['Datadog-Container-ID'] = containerId
60
- }
61
- const firstRequest = retriableRequest(options, callback, client, data)
62
-
63
- // The first request will be retried if it fails due to a socket connection close
64
- const firstRequestErrorHandler = error => {
65
- if (firstRequest.reusedSocket && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
66
- log.debug('Retrying request due to socket connection error')
67
- const retriedReq = retriableRequest(options, callback, client, data)
68
- // The retried request will fail normally
69
- retriedReq.on('error', e => callback(new Error(`Network error trying to reach the agent: ${e.message}`)))
70
- retriedReq.end()
71
- } else {
72
- callback(new Error(`Network error trying to reach the agent: ${error.message}`))
73
- }
74
- }
75
-
76
- firstRequest.on('error', firstRequestErrorHandler)
77
- firstRequest.end()
78
-
79
- return firstRequest
80
- }
81
-
82
- function byteLength (data) {
83
- return data.length > 0 ? data.reduce((prev, next) => prev + next.length, 0) : 0
84
- }
85
-
86
- module.exports = request