dd-trace 5.80.0 → 5.81.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 (213) hide show
  1. package/LICENSE-3rdparty.csv +79 -88
  2. package/ext/tags.d.ts +1 -0
  3. package/ext/tags.js +1 -0
  4. package/index.d.ts +35 -35
  5. package/loader-hook.mjs +10 -3
  6. package/package.json +22 -40
  7. package/packages/datadog-esbuild/index.js +36 -19
  8. package/packages/datadog-instrumentations/index.js +1 -0
  9. package/packages/datadog-instrumentations/src/anthropic.js +12 -0
  10. package/packages/datadog-instrumentations/src/aws-sdk.js +5 -1
  11. package/packages/datadog-instrumentations/src/cucumber.js +2 -2
  12. package/packages/datadog-instrumentations/src/find-my-way.js +6 -5
  13. package/packages/datadog-instrumentations/src/google-genai.js +120 -0
  14. package/packages/datadog-instrumentations/src/graphql.js +20 -0
  15. package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -0
  16. package/packages/datadog-instrumentations/src/helpers/instrument.js +10 -0
  17. package/packages/datadog-instrumentations/src/helpers/register.js +6 -1
  18. package/packages/datadog-instrumentations/src/helpers/rewriter/compiler.js +27 -0
  19. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +152 -0
  20. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/index.js +5 -0
  21. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/langchain.js +237 -0
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/loader.js +9 -0
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/loader.mjs +11 -0
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +139 -0
  25. package/packages/datadog-instrumentations/src/langchain.js +3 -109
  26. package/packages/datadog-instrumentations/src/mocha/main.js +1 -1
  27. package/packages/datadog-instrumentations/src/mysql2.js +1 -1
  28. package/packages/datadog-instrumentations/src/playwright.js +45 -16
  29. package/packages/datadog-instrumentations/src/router.js +1 -1
  30. package/packages/datadog-instrumentations/src/selenium.js +3 -1
  31. package/packages/datadog-instrumentations/src/ws.js +35 -17
  32. package/packages/datadog-plugin-child_process/src/scrub-cmd-params.js +1 -1
  33. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +23 -2
  34. package/packages/datadog-plugin-cypress/src/plugin.js +1 -1
  35. package/packages/datadog-plugin-cypress/src/support.js +73 -31
  36. package/packages/datadog-plugin-google-genai/src/index.js +17 -0
  37. package/packages/datadog-plugin-google-genai/src/tracing.js +41 -0
  38. package/packages/datadog-plugin-graphql/src/tools/transforms.js +5 -4
  39. package/packages/datadog-plugin-jest/src/util.js +1 -1
  40. package/packages/datadog-plugin-langchain/src/tracing.js +7 -3
  41. package/packages/datadog-plugin-next/src/index.js +11 -3
  42. package/packages/dd-trace/src/aiguard/sdk.js +18 -10
  43. package/packages/dd-trace/src/appsec/api_security_sampler.js +1 -1
  44. package/packages/dd-trace/src/appsec/iast/overhead-controller.js +1 -1
  45. package/packages/dd-trace/src/appsec/iast/taint-tracking/rewriter-esm.mjs +1 -1
  46. package/packages/dd-trace/src/appsec/iast/taint-tracking/rewriter.js +1 -2
  47. package/packages/dd-trace/src/appsec/iast/vulnerability-reporter.js +1 -1
  48. package/packages/dd-trace/src/appsec/reporter.js +0 -4
  49. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +4 -8
  50. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +4 -2
  51. package/packages/dd-trace/src/config.js +81 -7
  52. package/packages/dd-trace/src/config_defaults.js +14 -2
  53. package/packages/dd-trace/src/datastreams/encoding.js +23 -6
  54. package/packages/dd-trace/src/datastreams/pathway.js +40 -1
  55. package/packages/dd-trace/src/datastreams/processor.js +1 -1
  56. package/packages/dd-trace/src/datastreams/schemas/schema_builder.js +1 -1
  57. package/packages/dd-trace/src/debugger/devtools_client/breakpoints.js +15 -5
  58. package/packages/dd-trace/src/debugger/devtools_client/condition.js +1 -1
  59. package/packages/dd-trace/src/debugger/devtools_client/config.js +2 -0
  60. package/packages/dd-trace/src/debugger/devtools_client/index.js +30 -15
  61. package/packages/dd-trace/src/debugger/devtools_client/inspector_promises_polyfill.js +2 -0
  62. package/packages/dd-trace/src/debugger/devtools_client/json-buffer.js +24 -18
  63. package/packages/dd-trace/src/debugger/devtools_client/send.js +18 -8
  64. package/packages/dd-trace/src/debugger/devtools_client/snapshot/collector.js +103 -15
  65. package/packages/dd-trace/src/debugger/devtools_client/snapshot/constants.js +25 -0
  66. package/packages/dd-trace/src/debugger/devtools_client/snapshot/index.js +56 -25
  67. package/packages/dd-trace/src/debugger/devtools_client/snapshot/processor.js +64 -23
  68. package/packages/dd-trace/src/debugger/devtools_client/snapshot/symbols.js +3 -1
  69. package/packages/dd-trace/src/debugger/devtools_client/snapshot-pruner.js +404 -0
  70. package/packages/dd-trace/src/debugger/devtools_client/source-maps.js +1 -1
  71. package/packages/dd-trace/src/debugger/devtools_client/state.js +7 -2
  72. package/packages/dd-trace/src/debugger/devtools_client/status.js +1 -1
  73. package/packages/dd-trace/src/debugger/index.js +1 -1
  74. package/packages/dd-trace/src/encode/span-stats.js +7 -1
  75. package/packages/dd-trace/src/histogram.js +1 -1
  76. package/packages/dd-trace/src/id.js +60 -0
  77. package/packages/dd-trace/src/lambda/runtime/ritm.js +1 -1
  78. package/packages/dd-trace/src/llmobs/constants/tags.js +1 -0
  79. package/packages/dd-trace/src/llmobs/plugins/genai/index.js +104 -0
  80. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +486 -0
  81. package/packages/dd-trace/src/llmobs/plugins/langchain/index.js +2 -2
  82. package/packages/dd-trace/src/llmobs/plugins/{openai.js → openai/index.js} +48 -6
  83. package/packages/dd-trace/src/llmobs/plugins/openai/utils.js +114 -0
  84. package/packages/dd-trace/src/llmobs/sdk.js +5 -0
  85. package/packages/dd-trace/src/llmobs/span_processor.js +6 -1
  86. package/packages/dd-trace/src/llmobs/tagger.js +4 -0
  87. package/packages/dd-trace/src/opentelemetry/logs/index.js +2 -2
  88. package/packages/dd-trace/src/opentelemetry/logs/logger.js +3 -2
  89. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +5 -3
  90. package/packages/dd-trace/src/opentelemetry/logs/otlp_transformer.js +8 -8
  91. package/packages/dd-trace/src/opentelemetry/metrics/constants.js +34 -0
  92. package/packages/dd-trace/src/opentelemetry/metrics/index.js +81 -0
  93. package/packages/dd-trace/src/opentelemetry/metrics/instruments.js +225 -0
  94. package/packages/dd-trace/src/opentelemetry/metrics/meter.js +171 -0
  95. package/packages/dd-trace/src/opentelemetry/metrics/meter_provider.js +54 -0
  96. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +62 -0
  97. package/packages/dd-trace/src/opentelemetry/metrics/otlp_transformer.js +251 -0
  98. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +532 -0
  99. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +10 -18
  100. package/packages/dd-trace/src/opentelemetry/otlp/otlp_transformer_base.js +36 -22
  101. package/packages/dd-trace/src/opentelemetry/otlp/protobuf_loader.js +1 -1
  102. package/packages/dd-trace/src/opentelemetry/span.js +1 -1
  103. package/packages/dd-trace/src/opentelemetry/tracer.js +1 -1
  104. package/packages/dd-trace/src/opentelemetry/tracer_provider.js +1 -1
  105. package/packages/dd-trace/src/payload-tagging/index.js +2 -2
  106. package/packages/dd-trace/src/plugin_manager.js +4 -2
  107. package/packages/dd-trace/src/plugins/index.js +1 -0
  108. package/packages/dd-trace/src/plugins/util/test.js +3 -3
  109. package/packages/dd-trace/src/plugins/util/url.js +119 -1
  110. package/packages/dd-trace/src/plugins/util/web.js +10 -41
  111. package/packages/dd-trace/src/process-tags/index.js +81 -0
  112. package/packages/dd-trace/src/profiling/config.js +1 -1
  113. package/packages/dd-trace/src/profiling/exporters/agent.js +1 -1
  114. package/packages/dd-trace/src/profiling/profilers/events.js +10 -1
  115. package/packages/dd-trace/src/proxy.js +5 -0
  116. package/packages/dd-trace/src/rate_limiter.js +1 -1
  117. package/packages/dd-trace/src/remote_config/manager.js +1 -1
  118. package/packages/dd-trace/src/ritm.js +1 -1
  119. package/packages/dd-trace/src/service-naming/schemas/v0/web.js +4 -0
  120. package/packages/dd-trace/src/service-naming/schemas/v1/web.js +4 -0
  121. package/packages/dd-trace/src/span_format.js +9 -4
  122. package/packages/dd-trace/src/span_processor.js +8 -3
  123. package/packages/dd-trace/src/span_stats.js +15 -4
  124. package/packages/dd-trace/src/spanleak.js +1 -1
  125. package/packages/dd-trace/src/supported-configurations.json +13 -0
  126. package/packages/dd-trace/src/telemetry/dependencies.js +1 -1
  127. package/packages/dd-trace/src/telemetry/telemetry.js +11 -2
  128. package/vendor/dist/@datadog/sketches-js/LICENSE +39 -0
  129. package/vendor/dist/@datadog/sketches-js/index.js +1 -0
  130. package/vendor/dist/@datadog/source-map/LICENSE +28 -0
  131. package/vendor/dist/@datadog/source-map/index.js +1 -0
  132. package/vendor/dist/@isaacs/ttlcache/LICENSE +55 -0
  133. package/vendor/dist/@isaacs/ttlcache/index.js +1 -0
  134. package/vendor/dist/@opentelemetry/core/LICENSE +201 -0
  135. package/vendor/dist/@opentelemetry/core/index.js +1 -0
  136. package/vendor/dist/@opentelemetry/resources/LICENSE +201 -0
  137. package/vendor/dist/@opentelemetry/resources/index.js +1 -0
  138. package/vendor/dist/astring/LICENSE +19 -0
  139. package/vendor/dist/astring/index.js +1 -0
  140. package/vendor/dist/crypto-randomuuid/index.js +1 -0
  141. package/vendor/dist/escape-string-regexp/LICENSE +9 -0
  142. package/vendor/dist/escape-string-regexp/index.js +1 -0
  143. package/vendor/dist/esquery/LICENSE +24 -0
  144. package/vendor/dist/esquery/index.js +1 -0
  145. package/vendor/dist/ignore/LICENSE +21 -0
  146. package/vendor/dist/ignore/index.js +1 -0
  147. package/vendor/dist/istanbul-lib-coverage/LICENSE +24 -0
  148. package/vendor/dist/istanbul-lib-coverage/index.js +1 -0
  149. package/vendor/dist/jest-docblock/LICENSE +21 -0
  150. package/vendor/dist/jest-docblock/index.js +1 -0
  151. package/vendor/dist/jsonpath-plus/LICENSE +22 -0
  152. package/vendor/dist/jsonpath-plus/index.js +1 -0
  153. package/vendor/dist/limiter/LICENSE +19 -0
  154. package/vendor/dist/limiter/index.js +1 -0
  155. package/vendor/dist/lodash.sortby/LICENSE +47 -0
  156. package/vendor/dist/lodash.sortby/index.js +1 -0
  157. package/vendor/dist/lru-cache/LICENSE +15 -0
  158. package/vendor/dist/lru-cache/index.js +1 -0
  159. package/vendor/dist/meriyah/LICENSE +7 -0
  160. package/vendor/dist/meriyah/index.js +1 -0
  161. package/vendor/dist/module-details-from-path/LICENSE +21 -0
  162. package/vendor/dist/module-details-from-path/index.js +1 -0
  163. package/vendor/dist/mutexify/promise/LICENSE +21 -0
  164. package/vendor/dist/mutexify/promise/index.js +1 -0
  165. package/vendor/dist/opentracing/LICENSE +201 -0
  166. package/vendor/dist/opentracing/binary_carrier.d.ts +11 -0
  167. package/vendor/dist/opentracing/constants.d.ts +61 -0
  168. package/vendor/dist/opentracing/examples/demo/demo.d.ts +2 -0
  169. package/vendor/dist/opentracing/ext/tags.d.ts +90 -0
  170. package/vendor/dist/opentracing/functions.d.ts +20 -0
  171. package/vendor/dist/opentracing/global_tracer.d.ts +14 -0
  172. package/vendor/dist/opentracing/index.d.ts +12 -0
  173. package/vendor/dist/opentracing/index.js +1 -0
  174. package/vendor/dist/opentracing/mock_tracer/index.d.ts +5 -0
  175. package/vendor/dist/opentracing/mock_tracer/mock_context.d.ts +13 -0
  176. package/vendor/dist/opentracing/mock_tracer/mock_report.d.ts +16 -0
  177. package/vendor/dist/opentracing/mock_tracer/mock_span.d.ts +50 -0
  178. package/vendor/dist/opentracing/mock_tracer/mock_tracer.d.ts +26 -0
  179. package/vendor/dist/opentracing/noop.d.ts +8 -0
  180. package/vendor/dist/opentracing/reference.d.ts +33 -0
  181. package/vendor/dist/opentracing/span.d.ts +147 -0
  182. package/vendor/dist/opentracing/span_context.d.ts +26 -0
  183. package/vendor/dist/opentracing/test/api_compatibility.d.ts +16 -0
  184. package/vendor/dist/opentracing/test/mocktracer_implemenation.d.ts +3 -0
  185. package/vendor/dist/opentracing/test/noop_implementation.d.ts +4 -0
  186. package/vendor/dist/opentracing/test/opentracing_api.d.ts +3 -0
  187. package/vendor/dist/opentracing/test/unittest.d.ts +2 -0
  188. package/vendor/dist/opentracing/tracer.d.ts +127 -0
  189. package/vendor/dist/path-to-regexp/LICENSE +21 -0
  190. package/vendor/dist/path-to-regexp/index.js +1 -0
  191. package/vendor/dist/pprof-format/LICENSE +8 -0
  192. package/vendor/dist/pprof-format/index.js +1 -0
  193. package/vendor/dist/protobufjs/LICENSE +39 -0
  194. package/vendor/dist/protobufjs/index.js +1 -0
  195. package/vendor/dist/protobufjs/minimal/LICENSE +39 -0
  196. package/vendor/dist/protobufjs/minimal/index.js +1 -0
  197. package/vendor/dist/retry/LICENSE +21 -0
  198. package/vendor/dist/retry/index.js +1 -0
  199. package/vendor/dist/rfdc/LICENSE +15 -0
  200. package/vendor/dist/rfdc/index.js +1 -0
  201. package/vendor/dist/semifies/LICENSE +201 -0
  202. package/vendor/dist/semifies/index.js +1 -0
  203. package/vendor/dist/shell-quote/LICENSE +24 -0
  204. package/vendor/dist/shell-quote/index.js +1 -0
  205. package/vendor/dist/source-map/LICENSE +28 -0
  206. package/vendor/dist/source-map/index.js +1 -0
  207. package/vendor/dist/source-map/lib/util/LICENSE +28 -0
  208. package/vendor/dist/source-map/lib/util/index.js +1 -0
  209. package/vendor/dist/source-map/mappings.wasm +0 -0
  210. package/vendor/dist/tlhunter-sorted-set/LICENSE +21 -0
  211. package/vendor/dist/tlhunter-sorted-set/index.js +1 -0
  212. package/vendor/dist/ttl-set/LICENSE +21 -0
  213. package/vendor/dist/ttl-set/index.js +1 -0
@@ -1,5 +1,5 @@
1
1
  'use strict'
2
- const { createCoverageMap } = require('istanbul-lib-coverage')
2
+ const { createCoverageMap } = require('../../../vendor/dist/istanbul-lib-coverage')
3
3
 
4
4
  const { addHook, channel } = require('./helpers/instrument')
5
5
  const shimmer = require('../../datadog-shimmer')
@@ -43,7 +43,7 @@ const {
43
43
  CUCUMBER_WORKER_TRACE_PAYLOAD_CODE,
44
44
  getIsFaultyEarlyFlakeDetection
45
45
  } = require('../../dd-trace/src/plugins/util/test')
46
- const satisfies = require('semifies')
46
+ const satisfies = require('../../../vendor/dist/semifies')
47
47
 
48
48
  const isMarkedAsUnskippable = (pickle) => {
49
49
  return pickle.tags.some(tag => tag.name === '@datadog:unskippable')
@@ -9,13 +9,13 @@ function wrapOn (on) {
9
9
  return function onWithTrace (method, path, opts) {
10
10
  const index = typeof opts === 'function' ? 2 : 3
11
11
  const handler = arguments[index]
12
- const wrapper = shimmer.wrapFunction(handler, handler => function (req) {
13
- routeChannel.publish({ req, route: path })
14
-
15
- return handler.apply(this, arguments)
16
- })
17
12
 
18
13
  if (typeof handler === 'function') {
14
+ const wrapper = shimmer.wrapFunction(handler, handler => function (req) {
15
+ routeChannel.publish({ req, route: path })
16
+
17
+ return handler.apply(this, arguments)
18
+ })
19
19
  arguments[index] = wrapper
20
20
  }
21
21
 
@@ -24,6 +24,7 @@ function wrapOn (on) {
24
24
  }
25
25
 
26
26
  addHook({ name: 'find-my-way', versions: ['>=1'] }, Router => {
27
+ // No need to wrap the off method as it would not contain the handler function.
27
28
  shimmer.wrap(Router.prototype, 'on', wrapOn)
28
29
 
29
30
  return Router
@@ -0,0 +1,120 @@
1
+ 'use strict'
2
+
3
+ const { addHook } = require('./helpers/instrument')
4
+ const shimmer = require('../../datadog-shimmer')
5
+ const tracingChannel = require('dc-polyfill').tracingChannel
6
+ const channel = require('dc-polyfill').channel
7
+
8
+ const genaiTracingChannel = tracingChannel('apm:google:genai:request')
9
+ const onStreamedChunkCh = channel('apm:google:genai:request:chunk')
10
+
11
+ function wrapGenerateContent (method) {
12
+ return function wrappedGenerateContent (original) {
13
+ return function (...args) {
14
+ if (!genaiTracingChannel.start.hasSubscribers) {
15
+ return original.apply(this, args)
16
+ }
17
+
18
+ const normalizedName = normalizeMethodName(method)
19
+
20
+ const ctx = { args, methodName: normalizedName }
21
+
22
+ return genaiTracingChannel.start.runStores(ctx, () => {
23
+ let result
24
+ try {
25
+ result = original.apply(this, arguments)
26
+ } catch (error) {
27
+ finish(ctx, null, error)
28
+ throw error
29
+ } finally {
30
+ genaiTracingChannel.end.publish(ctx)
31
+ }
32
+ return result.then(response => {
33
+ if (response[Symbol.asyncIterator]) {
34
+ shimmer.wrap(response, Symbol.asyncIterator, iterator => wrapStreamIterator(iterator, ctx))
35
+ } else {
36
+ finish(ctx, response, null)
37
+ }
38
+ return response
39
+ }).catch(error => {
40
+ finish(ctx, null, error)
41
+ throw error
42
+ })
43
+ })
44
+ }
45
+ }
46
+ }
47
+
48
+ function wrapStreamIterator (iterator, ctx) {
49
+ return function () {
50
+ const itr = iterator.apply(this, arguments)
51
+ shimmer.wrap(itr, 'next', next => function () {
52
+ return next.apply(this, arguments)
53
+ .then(res => {
54
+ const { done, value: chunk } = res
55
+ onStreamedChunkCh.publish({ ctx, chunk, done })
56
+
57
+ if (done) {
58
+ finish(ctx)
59
+ }
60
+
61
+ return res
62
+ })
63
+ .catch(error => {
64
+ finish(ctx, null, error)
65
+ throw error
66
+ })
67
+ })
68
+
69
+ return itr
70
+ }
71
+ }
72
+ function finish (ctx, result, error) {
73
+ if (error) {
74
+ ctx.error = error
75
+ genaiTracingChannel.error.publish(ctx)
76
+ }
77
+
78
+ // streamed responses are handled and set separately
79
+ ctx.result ??= result
80
+
81
+ genaiTracingChannel.asyncEnd.publish(ctx)
82
+ }
83
+ // Hook the main package entry point
84
+ addHook({
85
+ name: '@google/genai',
86
+ versions: ['>=1.19.0']
87
+ }, exports => {
88
+ // Wrap GoogleGenAI to intercept when it creates Models instances
89
+ if (!exports.GoogleGenAI) return exports
90
+
91
+ shimmer.wrap(exports, 'GoogleGenAI', GoogleGenAI => {
92
+ return class extends GoogleGenAI {
93
+ constructor (...args) {
94
+ super(...args)
95
+
96
+ // We are patching the instance instead of the prototype because when it is compiled from
97
+ // typescript, the models property is not available on the prototype.
98
+ if (this.models) {
99
+ if (this.models.generateContent) {
100
+ shimmer.wrap(this.models, 'generateContent', wrapGenerateContent('generateContent'))
101
+ }
102
+ if (this.models.generateContentStream) {
103
+ shimmer.wrap(this.models, 'generateContentStream', wrapGenerateContent('generateContentStream'))
104
+ }
105
+ if (this.models.embedContent) {
106
+ shimmer.wrap(this.models, 'embedContent', wrapGenerateContent('embedContent'))
107
+ }
108
+ }
109
+ }
110
+ }
111
+ })
112
+ return exports
113
+ })
114
+
115
+ function normalizeMethodName (methodName) {
116
+ // Convert camelCase to snake_case and add Models prefix
117
+ return 'Models.' + methodName
118
+ .replaceAll(/([a-z0-9])([A-Z])/g, '$1_$2')
119
+ .toLowerCase()
120
+ }
@@ -6,6 +6,8 @@ const {
6
6
  } = require('./helpers/instrument')
7
7
  const shimmer = require('../../datadog-shimmer')
8
8
 
9
+ const ddGlobal = globalThis[Symbol.for('dd-trace')] ??= {}
10
+
9
11
  /** cached objects */
10
12
 
11
13
  const contexts = new WeakMap()
@@ -360,3 +362,21 @@ addHook({ name: 'graphql', file: 'validation/validate.js', versions: ['>=0.10']
360
362
 
361
363
  return validate
362
364
  })
365
+
366
+ addHook({ name: 'graphql', file: 'language/printer.js', versions: ['>=0.10'] }, printer => {
367
+ // HACK: It's possible `graphql` is loaded before `@apollo/gateway` so we
368
+ // can't use a channel as the latter plugin would load after the publish
369
+ // happened. Not sure how to handle this so for now use a global.
370
+ ddGlobal.graphql_printer = printer
371
+ return printer
372
+ })
373
+
374
+ addHook({ name: 'graphql', file: 'language/visitor.js', versions: ['>=0.10'] }, visitor => {
375
+ ddGlobal.graphql_visitor = visitor
376
+ return visitor
377
+ })
378
+
379
+ addHook({ name: 'graphql', file: 'utilities/index.js', versions: ['>=0.10'] }, utilities => {
380
+ ddGlobal.graphql_utilities = utilities
381
+ return utilities
382
+ })
@@ -13,6 +13,7 @@ module.exports = {
13
13
  '@playwright/test': () => require('../playwright'),
14
14
  '@elastic/elasticsearch': () => require('../elasticsearch'),
15
15
  '@elastic/transport': () => require('../elasticsearch'),
16
+ '@google/genai': () => require('../google-genai'),
16
17
  '@google-cloud/pubsub': () => require('../google-cloud-pubsub'),
17
18
  '@google-cloud/vertexai': () => require('../google-cloud-vertexai'),
18
19
  '@graphql-tools/executor': () => require('../graphql'),
@@ -2,6 +2,7 @@
2
2
 
3
3
  const dc = require('dc-polyfill')
4
4
  const instrumentations = require('./instrumentations')
5
+ const rewriterInstrumentations = require('./rewriter/instrumentations')
5
6
  const { AsyncResource } = require('async_hooks')
6
7
 
7
8
  const channelMap = {}
@@ -22,6 +23,15 @@ exports.tracingChannel = function (name) {
22
23
  return tc
23
24
  }
24
25
 
26
+ exports.getHooks = function getHooks (names) {
27
+ names = [names].flat()
28
+
29
+ return rewriterInstrumentations
30
+ .map(inst => inst.module)
31
+ .filter(({ name }) => names.includes(name))
32
+ .map(({ name, versionRange, filePath }) => ({ name, versions: [versionRange], file: filePath }))
33
+ }
34
+
25
35
  /**
26
36
  * @param {object} args
27
37
  * @param {string|string[]} args.name module name
@@ -2,7 +2,7 @@
2
2
 
3
3
  const { channel } = require('dc-polyfill')
4
4
  const path = require('path')
5
- const satisfies = require('semifies')
5
+ const satisfies = require('../../../../vendor/dist/semifies')
6
6
  const Hook = require('./hook')
7
7
  const requirePackageJson = require('../../../dd-trace/src/require-package-json')
8
8
  const log = require('../../../dd-trace/src/log')
@@ -10,6 +10,7 @@ const checkRequireCache = require('./check-require-cache')
10
10
  const telemetry = require('../../../dd-trace/src/guardrails/telemetry')
11
11
  const { isInServerlessEnvironment } = require('../../../dd-trace/src/serverless')
12
12
  const { getEnvironmentVariables } = require('../../../dd-trace/src/config-helper')
13
+ const rewriter = require('./rewriter')
13
14
 
14
15
  const envs = getEnvironmentVariables()
15
16
 
@@ -48,6 +49,10 @@ if (DD_TRACE_DEBUG && DD_TRACE_DEBUG.toLowerCase() !== 'false') {
48
49
  const seenCombo = new Set()
49
50
  const allInstrumentations = {}
50
51
 
52
+ for (const inst of disabledInstrumentations) {
53
+ rewriter.disable(inst)
54
+ }
55
+
51
56
  // TODO: make this more efficient
52
57
  for (const packageName of names) {
53
58
  if (disabledInstrumentations.has(packageName)) continue
@@ -0,0 +1,27 @@
1
+ 'use strict'
2
+
3
+ let meriyah
4
+ let astring
5
+ let esquery
6
+
7
+ module.exports = {
8
+ parse: (...args) => {
9
+ meriyah ??= require('../../../../../vendor/dist/meriyah')
10
+
11
+ return meriyah.parse(...args)
12
+ },
13
+
14
+ generate: (...args) => {
15
+ astring ??= require('../../../../../vendor/dist/astring')
16
+
17
+ return astring.generate(...args)
18
+ },
19
+
20
+ traverse: (ast, query, visitor) => {
21
+ esquery ??= require('../../../../../vendor/dist/esquery').default
22
+
23
+ const selector = esquery.parse(query)
24
+
25
+ return esquery.traverse(ast, selector, visitor)
26
+ },
27
+ }
@@ -0,0 +1,152 @@
1
+ 'use strict'
2
+
3
+ /*
4
+ This rewriter is basically a JavaScript version of Orchestrion-JS. The goal is
5
+ not to replace Orchestrion-JS, but rather to make it easier and faster to write
6
+ new integrations in the short-term, especially as many changes to the rewriter
7
+ will be needed as all the patterns we need have not been identified yet. This
8
+ will avoid the back and forth of having to make Rust changes to an external
9
+ library for every integration change or addition that requires something new.
10
+
11
+ In the meantime, we'll work concurrently on a change to Orchestrion-JS that
12
+ adds an "arbitrary transform" or "plugin" system that can be used from
13
+ JavaScript, in order to enable quick iteration while still using Orchestrion-JS.
14
+ Once that's done we'll use that, so that we can remove this JS approach and
15
+ return to using Orchestrion-JS.
16
+
17
+ The long term goal is to backport any additional features we add to the JS
18
+ rewriter (or using the plugin system in Orchestrion-JS once we're using that)
19
+ to Orchestrion-JS once we're confident that the implementation is fairly
20
+ complete and has all features we need.
21
+
22
+ Here is a list of the additions and changes in this rewriter compared to
23
+ Orchestrion-JS that will need to be backported:
24
+
25
+ (NOTE: Please keep this list up-to-date whenever new features are added)
26
+
27
+ - Supports an `astQuery` field to filter AST nodes with an esquery query. This
28
+ is mostly meant to be used when experimenting or if what needs to be queried
29
+ is not a function. We'll see over time if something like this is needed to be
30
+ backported or if it can be replaced by simpler queries.
31
+ - Supports replacing methods of child class instances in the base constructor.
32
+ */
33
+
34
+ const { readFileSync } = require('fs')
35
+ const { join } = require('path')
36
+ const semifies = require('../../../../../vendor/dist/semifies')
37
+ const transforms = require('./transforms')
38
+ const { generate, parse, traverse } = require('./compiler')
39
+ const log = require('../../../../dd-trace/src/log')
40
+ const instrumentations = require('./instrumentations')
41
+ const { getEnvironmentVariable } = require('../../../../dd-trace/src/config-helper')
42
+
43
+ const NODE_OPTIONS = getEnvironmentVariable('NODE_OPTIONS')
44
+
45
+ const supported = {}
46
+ const disabled = new Set()
47
+
48
+ // TODO: Source maps without `--enable-source-maps`.
49
+ const enableSourceMaps = NODE_OPTIONS?.includes('--enable-source-maps') ||
50
+ process.execArgv?.some(arg => arg.includes('--enable-source-maps'))
51
+
52
+ let SourceMapGenerator
53
+
54
+ function rewrite (content, filename, format) {
55
+ if (!content) return content
56
+
57
+ try {
58
+ let ast
59
+
60
+ filename = filename.replace('file://', '')
61
+
62
+ for (const inst of instrumentations) {
63
+ const { astQuery, functionQuery = {}, module: { name, versionRange, filePath } } = inst
64
+ const { kind } = functionQuery
65
+ const operator = kind === 'Async' ? 'tracePromise' : kind === 'Callback' ? 'traceCallback' : 'traceSync'
66
+ const transform = transforms[operator]
67
+
68
+ if (disabled.has(name)) continue
69
+ if (!filename.endsWith(`${name}/${filePath}`)) continue
70
+ if (!transform) continue
71
+ if (!satisfies(filename, filePath, versionRange)) continue
72
+
73
+ ast ??= parse(content.toString(), { loc: true, ranges: true, module: format === 'module' })
74
+
75
+ const query = astQuery || fromFunctionQuery(functionQuery)
76
+ const state = { ...inst, format, functionQuery, operator }
77
+
78
+ traverse(ast, query, (...args) => transform(state, ...args))
79
+ }
80
+
81
+ if (ast) {
82
+ if (!enableSourceMaps) return generate(ast)
83
+
84
+ // TODO: Can we use the same version of `source-map` that DI uses?
85
+ SourceMapGenerator ??= require('../../../../../vendor/dist/@datadog/source-map').SourceMapGenerator
86
+
87
+ const sourceMap = new SourceMapGenerator({ file: filename })
88
+ const code = generate(ast, { sourceMap })
89
+ const map = Buffer.from(sourceMap.toString()).toString('base64')
90
+
91
+ return code + '\n' + `//# sourceMappingURL=data:application/json;base64,${map}`
92
+ }
93
+ } catch (e) {
94
+ log.error(e)
95
+ }
96
+
97
+ return content
98
+ }
99
+
100
+ function disable (instrumentation) {
101
+ disabled.add(instrumentation)
102
+ }
103
+
104
+ function satisfies (filename, filePath, versions) {
105
+ const [basename] = filename.split(filePath)
106
+
107
+ if (supported[basename] === undefined) {
108
+ try {
109
+ const pkg = JSON.parse(readFileSync(
110
+ join(basename, 'package.json'), 'utf8'
111
+ ))
112
+
113
+ supported[basename] = semifies(pkg.version, versions)
114
+ } catch {
115
+ supported[basename] = false
116
+ }
117
+ }
118
+
119
+ return supported[basename]
120
+ }
121
+
122
+ // TODO: Support index
123
+ function fromFunctionQuery (functionQuery) {
124
+ const { methodName, functionName, expressionName, className } = functionQuery
125
+ const queries = []
126
+
127
+ if (className) {
128
+ queries.push(
129
+ `[id.name="${className}"]`,
130
+ `[id.name="${className}"] > ClassBody > [key.name="${methodName}"] > [async]`,
131
+ `[id.name="${className}"] > ClassExpression > ClassBody > [key.name="${methodName}"] > [async]`
132
+ )
133
+ } else if (methodName) {
134
+ queries.push(
135
+ `ClassBody > [key.name="${methodName}"] > [async]`,
136
+ `Property[key.name="${methodName}"] > [async]`
137
+ )
138
+ }
139
+
140
+ if (functionName) {
141
+ queries.push(`FunctionDeclaration[id.name="${functionName}"][async]`)
142
+ } else if (expressionName) {
143
+ queries.push(
144
+ `FunctionExpression[id.name="${expressionName}"][async]`,
145
+ `ArrowFunctionExpression[id.name="${expressionName}"][async]`
146
+ )
147
+ }
148
+
149
+ return queries.join(', ')
150
+ }
151
+
152
+ module.exports = { rewrite, disable }
@@ -0,0 +1,5 @@
1
+ 'use strict'
2
+
3
+ module.exports = [
4
+ ...require('./langchain')
5
+ ]
@@ -0,0 +1,237 @@
1
+ 'use strict'
2
+
3
+ module.exports = [
4
+ {
5
+ module: {
6
+ name: '@langchain/core',
7
+ versionRange: '>=0.1',
8
+ filePath: 'dist/runnables/base.cjs'
9
+ },
10
+ functionQuery: {
11
+ methodName: 'invoke',
12
+ kind: 'Async',
13
+ className: 'RunnableSequence'
14
+ },
15
+ channelName: 'RunnableSequence_invoke'
16
+ },
17
+ {
18
+ module: {
19
+ name: '@langchain/core',
20
+ versionRange: '>=0.1',
21
+ filePath: 'dist/runnables/base.cjs'
22
+ },
23
+ functionQuery: {
24
+ methodName: 'batch',
25
+ kind: 'Async',
26
+ className: 'RunnableSequence'
27
+ },
28
+ channelName: 'RunnableSequence_batch'
29
+ },
30
+ {
31
+ module: {
32
+ name: '@langchain/core',
33
+ versionRange: '>=0.1',
34
+ filePath: 'dist/language_models/chat_models.cjs'
35
+ },
36
+ functionQuery: {
37
+ methodName: 'generate',
38
+ kind: 'Async',
39
+ className: 'BaseChatModel'
40
+ },
41
+ channelName: 'BaseChatModel_generate'
42
+ },
43
+ {
44
+ module: {
45
+ name: '@langchain/core',
46
+ versionRange: '>=0.1',
47
+ filePath: 'dist/language_models/llms.cjs'
48
+ },
49
+ functionQuery: {
50
+ methodName: 'generate',
51
+ kind: 'Async'
52
+ },
53
+ channelName: 'BaseLLM_generate'
54
+ },
55
+ {
56
+ module: {
57
+ name: '@langchain/core',
58
+ versionRange: '>=0.1',
59
+ filePath: 'dist/embeddings.cjs'
60
+ },
61
+ functionQuery: {
62
+ methodName: 'embedQuery',
63
+ kind: 'Async',
64
+ className: 'Embeddings'
65
+ },
66
+ channelName: 'Embeddings_embedQuery'
67
+ },
68
+ {
69
+ module: {
70
+ name: '@langchain/core',
71
+ versionRange: '>=0.1',
72
+ filePath: 'dist/embeddings.cjs'
73
+ },
74
+ functionQuery: {
75
+ methodName: 'embedDocuments',
76
+ kind: 'Async',
77
+ className: 'Embeddings'
78
+ },
79
+ channelName: 'Embeddings_embedDocuments'
80
+ },
81
+ {
82
+ module: {
83
+ name: '@langchain/core',
84
+ versionRange: '>=0.1',
85
+ filePath: 'dist/tools/index.cjs'
86
+ },
87
+ functionQuery: {
88
+ methodName: 'invoke',
89
+ kind: 'Async',
90
+ className: 'StructuredTool'
91
+ },
92
+ channelName: 'Tool_invoke'
93
+ },
94
+ {
95
+ module: {
96
+ name: '@langchain/core',
97
+ versionRange: '>=0.1',
98
+ filePath: 'dist/vectorstores.cjs'
99
+ },
100
+ functionQuery: {
101
+ methodName: 'similaritySearch',
102
+ kind: 'Async',
103
+ className: 'VectorStore'
104
+ },
105
+ channelName: 'VectorStore_similaritySearch'
106
+ },
107
+ {
108
+ module: {
109
+ name: '@langchain/core',
110
+ versionRange: '>=0.1',
111
+ filePath: 'dist/vectorstores.cjs'
112
+ },
113
+ functionQuery: {
114
+ methodName: 'similaritySearchWithScore',
115
+ kind: 'Async',
116
+ className: 'VectorStore'
117
+ },
118
+ channelName: 'VectorStore_similaritySearchWithScore'
119
+ },
120
+ {
121
+ module: {
122
+ name: '@langchain/core',
123
+ versionRange: '>=0.1',
124
+ filePath: 'dist/runnables/base.js'
125
+ },
126
+ functionQuery: {
127
+ methodName: 'invoke',
128
+ kind: 'Async',
129
+ className: 'RunnableSequence'
130
+ },
131
+ channelName: 'RunnableSequence_invoke'
132
+ },
133
+ {
134
+ module: {
135
+ name: '@langchain/core',
136
+ versionRange: '>=0.1',
137
+ filePath: 'dist/runnables/base.js'
138
+ },
139
+ functionQuery: {
140
+ methodName: 'batch',
141
+ kind: 'Async',
142
+ className: 'RunnableSequence'
143
+ },
144
+ channelName: 'RunnableSequence_batch'
145
+ },
146
+ {
147
+ module: {
148
+ name: '@langchain/core',
149
+ versionRange: '>=0.1',
150
+ filePath: 'dist/language_models/chat_models.js'
151
+ },
152
+ functionQuery: {
153
+ methodName: 'generate',
154
+ kind: 'Async',
155
+ className: 'BaseChatModel'
156
+ },
157
+ channelName: 'BaseChatModel_generate'
158
+ },
159
+ {
160
+ module: {
161
+ name: '@langchain/core',
162
+ versionRange: '>=0.1',
163
+ filePath: 'dist/language_models/llms.js'
164
+ },
165
+ functionQuery: {
166
+ methodName: 'generate',
167
+ kind: 'Async'
168
+ },
169
+ channelName: 'BaseLLM_generate'
170
+ },
171
+ {
172
+ module: {
173
+ name: '@langchain/core',
174
+ versionRange: '>=0.1',
175
+ filePath: 'dist/embeddings.js'
176
+ },
177
+ functionQuery: {
178
+ methodName: 'embedQuery',
179
+ kind: 'Async',
180
+ className: 'Embeddings'
181
+ },
182
+ channelName: 'Embeddings_embedQuery'
183
+ },
184
+ {
185
+ module: {
186
+ name: '@langchain/core',
187
+ versionRange: '>=0.1',
188
+ filePath: 'dist/embeddings.js'
189
+ },
190
+ functionQuery: {
191
+ methodName: 'embedDocuments',
192
+ kind: 'Async',
193
+ className: 'Embeddings'
194
+ },
195
+ channelName: 'Embeddings_embedDocuments'
196
+ },
197
+ {
198
+ module: {
199
+ name: '@langchain/core',
200
+ versionRange: '>=0.1',
201
+ filePath: 'dist/tools/index.js'
202
+ },
203
+ functionQuery: {
204
+ methodName: 'invoke',
205
+ kind: 'Async',
206
+ className: 'StructuredTool'
207
+ },
208
+ channelName: 'Tool_invoke'
209
+ },
210
+ {
211
+ module: {
212
+ name: '@langchain/core',
213
+ versionRange: '>=0.1',
214
+ filePath: 'dist/vectorstores.js'
215
+ },
216
+ functionQuery: {
217
+ methodName: 'similaritySearch',
218
+ kind: 'Async',
219
+ className: 'VectorStore'
220
+ },
221
+ channelName: 'VectorStore_similaritySearch'
222
+ },
223
+ {
224
+
225
+ module: {
226
+ name: '@langchain/core',
227
+ versionRange: '>=0.1',
228
+ filePath: 'dist/vectorstores.js'
229
+ },
230
+ functionQuery: {
231
+ methodName: 'similaritySearchWithScore',
232
+ kind: 'Async',
233
+ className: 'VectorStore'
234
+ },
235
+ channelName: 'VectorStore_similaritySearchWithScore'
236
+ }
237
+ ]
@@ -0,0 +1,9 @@
1
+ 'use strict'
2
+
3
+ const Module = require('module')
4
+ const shimmer = require('../../../../datadog-shimmer')
5
+ const { rewrite } = require('./')
6
+
7
+ shimmer.wrap(Module.prototype, '_compile', compile => function (content, filename, format) {
8
+ return compile.call(this, rewrite(content, filename, format), filename, format)
9
+ })