dd-trace 6.0.0-pre-c879030 → 6.0.0-pre-0f36e48

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.
package/index.d.ts CHANGED
@@ -187,6 +187,7 @@ interface Plugins {
187
187
  "restify": tracer.plugins.restify;
188
188
  "rhea": tracer.plugins.rhea;
189
189
  "router": tracer.plugins.router;
190
+ "selenium": tracer.plugins.selenium;
190
191
  "sharedb": tracer.plugins.sharedb;
191
192
  "tedious": tracer.plugins.tedious;
192
193
  "winston": tracer.plugins.winston;
@@ -1736,6 +1737,12 @@ declare namespace tracer {
1736
1737
  */
1737
1738
  interface router extends Integration {}
1738
1739
 
1740
+ /**
1741
+ * This plugin automatically instruments the
1742
+ * [selenium-webdriver](https://www.npmjs.com/package/selenium-webdriver) module.
1743
+ */
1744
+ interface selenium extends Integration {}
1745
+
1739
1746
  /**
1740
1747
  * This plugin automatically instruments the
1741
1748
  * [sharedb](https://github.com/share/sharedb) module.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "6.0.0-pre-c879030",
3
+ "version": "6.0.0-pre-0f36e48",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -36,6 +36,7 @@
36
36
  "test:integration:cucumber": "mocha --colors --timeout 30000 \"integration-tests/cucumber/*.spec.js\"",
37
37
  "test:integration:cypress": "mocha --colors --timeout 30000 \"integration-tests/cypress/*.spec.js\"",
38
38
  "test:integration:playwright": "mocha --colors --timeout 30000 \"integration-tests/playwright/*.spec.js\"",
39
+ "test:integration:selenium": "mocha --colors --timeout 30000 \"integration-tests/selenium/*.spec.js\"",
39
40
  "test:integration:profiler": "mocha --colors --timeout 90000 \"integration-tests/profiler/*.spec.js\"",
40
41
  "test:integration:serverless": "mocha --colors --timeout 30000 \"integration-tests/serverless/*.spec.js\"",
41
42
  "test:integration:plugins": "mocha --colors --exit -r \"packages/dd-trace/test/setup/mocha.js\" \"packages/datadog-plugin-@($(echo $PLUGINS))/test/integration-test/**/*.spec.js\"",
@@ -7,11 +7,16 @@ exports.createWrapFetch = function createWrapFetch (Request, ch) {
7
7
  return function (input, init) {
8
8
  if (!ch.start.hasSubscribers) return fetch.apply(this, arguments)
9
9
 
10
- const req = new Request(input, init)
11
- const headers = req.headers
12
- const ctx = { req, headers }
10
+ if (input instanceof Request) {
11
+ const ctx = { req: input }
13
12
 
14
- return ch.tracePromise(() => fetch.call(this, req, { headers: ctx.headers }), ctx)
13
+ return ch.tracePromise(() => fetch.call(this, input, init), ctx)
14
+ } else {
15
+ const req = new Request(input, init)
16
+ const ctx = { req }
17
+
18
+ return ch.tracePromise(() => fetch.call(this, req), ctx)
19
+ }
15
20
  }
16
21
  }
17
22
  }
@@ -58,6 +58,7 @@ module.exports = {
58
58
  'jest-environment-node': () => require('../jest'),
59
59
  'jest-environment-jsdom': () => require('../jest'),
60
60
  'jest-jasmine2': () => require('../jest'),
61
+ 'jest-runtime': () => require('../jest'),
61
62
  'jest-worker': () => require('../jest'),
62
63
  knex: () => require('../knex'),
63
64
  koa: () => require('../koa'),
@@ -103,8 +104,9 @@ module.exports = {
103
104
  restify: () => require('../restify'),
104
105
  rhea: () => require('../rhea'),
105
106
  router: () => require('../router'),
106
- sharedb: () => require('../sharedb'),
107
+ 'selenium-webdriver': () => require('../selenium'),
107
108
  sequelize: () => require('../sequelize'),
109
+ sharedb: () => require('../sharedb'),
108
110
  tedious: () => require('../tedious'),
109
111
  when: () => require('../when'),
110
112
  winston: () => require('../winston')
@@ -876,6 +876,34 @@ if (DD_MAJOR < 4) {
876
876
  }, jasmineAsyncInstallWraper)
877
877
  }
878
878
 
879
+ const LIBRARIES_BYPASSING_JEST_REQUIRE_ENGINE = [
880
+ 'selenium-webdriver'
881
+ ]
882
+
883
+ function shouldBypassJestRequireEngine (moduleName) {
884
+ return (
885
+ LIBRARIES_BYPASSING_JEST_REQUIRE_ENGINE.some(library => moduleName.includes(library))
886
+ )
887
+ }
888
+
889
+ addHook({
890
+ name: 'jest-runtime',
891
+ versions: ['>=24.8.0']
892
+ }, (runtimePackage) => {
893
+ const Runtime = runtimePackage.default ? runtimePackage.default : runtimePackage
894
+
895
+ shimmer.wrap(Runtime.prototype, 'requireModuleOrMock', requireModuleOrMock => function (from, moduleName) {
896
+ // TODO: do this for every library that we instrument
897
+ if (shouldBypassJestRequireEngine(moduleName)) {
898
+ // To bypass jest's own require engine
899
+ return this._requireCoreModule(moduleName)
900
+ }
901
+ return requireModuleOrMock.apply(this, arguments)
902
+ })
903
+
904
+ return runtimePackage
905
+ })
906
+
879
907
  addHook({
880
908
  name: 'jest-worker',
881
909
  versions: ['>=24.9.0'],
@@ -10,6 +10,98 @@ const startCh = channel('apm:openai:request:start')
10
10
  const finishCh = channel('apm:openai:request:finish')
11
11
  const errorCh = channel('apm:openai:request:error')
12
12
 
13
+ const V4_PACKAGE_SHIMS = [
14
+ {
15
+ file: 'resources/chat/completions.js',
16
+ targetClass: 'Completions',
17
+ baseResource: 'chat.completions',
18
+ methods: ['create']
19
+ },
20
+ {
21
+ file: 'resources/completions.js',
22
+ targetClass: 'Completions',
23
+ baseResource: 'completions',
24
+ methods: ['create']
25
+ },
26
+ {
27
+ file: 'resources/embeddings.js',
28
+ targetClass: 'Embeddings',
29
+ baseResource: 'embeddings',
30
+ methods: ['create']
31
+ },
32
+ {
33
+ file: 'resources/files.js',
34
+ targetClass: 'Files',
35
+ baseResource: 'files',
36
+ methods: ['create', 'del', 'list', 'retrieve']
37
+ },
38
+ {
39
+ file: 'resources/files.js',
40
+ targetClass: 'Files',
41
+ baseResource: 'files',
42
+ methods: ['retrieveContent'],
43
+ versions: ['>=4.0.0 <4.17.1']
44
+ },
45
+ {
46
+ file: 'resources/files.js',
47
+ targetClass: 'Files',
48
+ baseResource: 'files',
49
+ methods: ['content'], // replaced `retrieveContent` in v4.17.1
50
+ versions: ['>=4.17.1']
51
+ },
52
+ {
53
+ file: 'resources/images.js',
54
+ targetClass: 'Images',
55
+ baseResource: 'images',
56
+ methods: ['createVariation', 'edit', 'generate']
57
+ },
58
+ {
59
+ file: 'resources/fine-tuning/jobs/jobs.js',
60
+ targetClass: 'Jobs',
61
+ baseResource: 'fine_tuning.jobs',
62
+ methods: ['cancel', 'create', 'list', 'listEvents', 'retrieve'],
63
+ versions: ['>=4.34.0'] // file location changed in 4.34.0
64
+ },
65
+ {
66
+ file: 'resources/fine-tuning/jobs.js',
67
+ targetClass: 'Jobs',
68
+ baseResource: 'fine_tuning.jobs',
69
+ methods: ['cancel', 'create', 'list', 'listEvents', 'retrieve'],
70
+ versions: ['>=4.1.0 <4.34.0']
71
+ },
72
+ {
73
+ file: 'resources/fine-tunes.js', // deprecated after 4.1.0
74
+ targetClass: 'FineTunes',
75
+ baseResource: 'fine-tune',
76
+ methods: ['cancel', 'create', 'list', 'listEvents', 'retrieve'],
77
+ versions: ['>=4.0.0 <4.1.0']
78
+ },
79
+ {
80
+ file: 'resources/models.js',
81
+ targetClass: 'Models',
82
+ baseResource: 'models',
83
+ methods: ['del', 'list', 'retrieve']
84
+ },
85
+ {
86
+ file: 'resources/moderations.js',
87
+ targetClass: 'Moderations',
88
+ baseResource: 'moderations',
89
+ methods: ['create']
90
+ },
91
+ {
92
+ file: 'resources/audio/transcriptions.js',
93
+ targetClass: 'Transcriptions',
94
+ baseResource: 'audio.transcriptions',
95
+ methods: ['create']
96
+ },
97
+ {
98
+ file: 'resources/audio/translations.js',
99
+ targetClass: 'Translations',
100
+ baseResource: 'audio.translations',
101
+ methods: ['create']
102
+ }
103
+ ]
104
+
13
105
  addHook({ name: 'openai', file: 'dist/api.js', versions: ['>=3.0.0 <4'] }, exports => {
14
106
  const methodNames = Object.getOwnPropertyNames(exports.OpenAIApi.prototype)
15
107
  methodNames.shift() // remove leading 'constructor' method
@@ -48,3 +140,60 @@ addHook({ name: 'openai', file: 'dist/api.js', versions: ['>=3.0.0 <4'] }, expor
48
140
 
49
141
  return exports
50
142
  })
143
+
144
+ for (const shim of V4_PACKAGE_SHIMS) {
145
+ const { file, targetClass, baseResource, methods } = shim
146
+ addHook({ name: 'openai', file, versions: shim.versions || ['>=4'] }, exports => {
147
+ const targetPrototype = exports[targetClass].prototype
148
+
149
+ for (const methodName of methods) {
150
+ shimmer.wrap(targetPrototype, methodName, methodFn => function () {
151
+ if (!startCh.hasSubscribers) {
152
+ return methodFn.apply(this, arguments)
153
+ }
154
+
155
+ const client = this._client || this.client
156
+
157
+ startCh.publish({
158
+ methodName: `${baseResource}.${methodName}`,
159
+ args: arguments,
160
+ basePath: client.baseURL,
161
+ apiKey: client.apiKey
162
+ })
163
+
164
+ const apiProm = methodFn.apply(this, arguments)
165
+
166
+ // wrapping `parse` avoids problematic wrapping of `then` when trying to call
167
+ // `withResponse` in userland code after. This way, we can return the whole `APIPromise`
168
+ shimmer.wrap(apiProm, 'parse', origApiPromParse => function () {
169
+ return origApiPromParse.apply(this, arguments)
170
+ // the original response is wrapped in a promise, so we need to unwrap it
171
+ .then(body => Promise.all([this.responsePromise, body]))
172
+ .then(([{ response, options }, body]) => {
173
+ finishCh.publish({
174
+ headers: response.headers,
175
+ body,
176
+ path: response.url,
177
+ method: options.method
178
+ })
179
+
180
+ return body
181
+ })
182
+ .catch(err => {
183
+ errorCh.publish({ err })
184
+
185
+ throw err
186
+ })
187
+ .finally(() => {
188
+ // maybe we don't want to unwrap here in case the promise is re-used?
189
+ // other hand: we want to avoid resource leakage
190
+ shimmer.unwrap(apiProm, 'parse')
191
+ })
192
+ })
193
+
194
+ return apiProm
195
+ })
196
+ }
197
+ return exports
198
+ })
199
+ }
@@ -4,7 +4,12 @@ const { addHook } = require('./helpers/instrument')
4
4
  const shimmer = require('../../datadog-shimmer')
5
5
  const tracer = require('../../dd-trace')
6
6
 
7
- if (process.env.DD_TRACE_OTEL_ENABLED) {
7
+ const otelSdkEnabled = process.env.DD_TRACE_OTEL_ENABLED ||
8
+ process.env.OTEL_SDK_DISABLED
9
+ ? !process.env.OTEL_SDK_DISABLED
10
+ : undefined
11
+
12
+ if (otelSdkEnabled) {
8
13
  addHook({
9
14
  name: '@opentelemetry/sdk-trace-node',
10
15
  file: 'build/src/NodeTracerProvider.js',
@@ -0,0 +1,69 @@
1
+ const { addHook, channel } = require('./helpers/instrument')
2
+ const shimmer = require('../../datadog-shimmer')
3
+
4
+ const ciSeleniumDriverGetStartCh = channel('ci:selenium:driver:get')
5
+
6
+ const RUM_STOP_SESSION_SCRIPT = `
7
+ if (window.DD_RUM && window.DD_RUM.stopSession) {
8
+ window.DD_RUM.stopSession();
9
+ return true;
10
+ } else {
11
+ return false;
12
+ }
13
+ `
14
+ const IS_RUM_ACTIVE_SCRIPT = 'return !!window.DD_RUM'
15
+
16
+ const DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS = 500
17
+ const DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME = 'datadog-ci-visibility-test-execution-id'
18
+
19
+ // TODO: can we increase the supported version range?
20
+ addHook({
21
+ name: 'selenium-webdriver',
22
+ versions: ['>=4.11.0']
23
+ }, (seleniumPackage, seleniumVersion) => {
24
+ // TODO: do not turn this into async. Use promises
25
+ shimmer.wrap(seleniumPackage.WebDriver.prototype, 'get', get => async function () {
26
+ let traceId
27
+ const setTraceId = (inputTraceId) => {
28
+ traceId = inputTraceId
29
+ }
30
+ const getResult = await get.apply(this, arguments)
31
+
32
+ const isRumActive = await this.executeScript(IS_RUM_ACTIVE_SCRIPT)
33
+ const capabilities = await this.getCapabilities()
34
+
35
+ ciSeleniumDriverGetStartCh.publish({
36
+ setTraceId,
37
+ seleniumVersion,
38
+ browserName: capabilities.getBrowserName(),
39
+ browserVersion: capabilities.getBrowserVersion(),
40
+ isRumActive
41
+ })
42
+
43
+ await this.manage().addCookie({
44
+ name: DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME,
45
+ value: traceId
46
+ })
47
+
48
+ return getResult
49
+ })
50
+
51
+ shimmer.wrap(seleniumPackage.WebDriver.prototype, 'quit', quit => async function () {
52
+ const isRumActive = await this.executeScript(RUM_STOP_SESSION_SCRIPT)
53
+
54
+ if (isRumActive) {
55
+ // We'll have time for RUM to flush the events (there's no callback to know when it's done)
56
+ await new Promise(resolve => {
57
+ setTimeout(() => {
58
+ resolve()
59
+ }, DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS)
60
+ })
61
+ }
62
+
63
+ await this.manage().deleteCookie(DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME)
64
+
65
+ return quit.apply(this, arguments)
66
+ })
67
+
68
+ return seleniumPackage
69
+ })
@@ -17,8 +17,11 @@ class FetchPlugin extends HttpClientPlugin {
17
17
 
18
18
  const store = super.bindStart(ctx)
19
19
 
20
- ctx.headers = headers
21
- ctx.req = new globalThis.Request(req, { headers })
20
+ for (const name in headers) {
21
+ if (!req.headers.has(name)) {
22
+ req.headers.set(name, headers[name])
23
+ }
24
+ }
22
25
 
23
26
  return store
24
27
  }
@@ -144,10 +144,9 @@ class MochaPlugin extends CiPlugin {
144
144
 
145
145
  this.addSub('ci:mocha:test:finish', (status) => {
146
146
  const store = storage.getStore()
147
+ const span = store?.span
147
148
 
148
- if (store && store.span) {
149
- const span = store.span
150
-
149
+ if (span) {
151
150
  span.setTag(TEST_STATUS, status)
152
151
 
153
152
  span.finish()