dd-trace 3.55.0 → 3.57.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/index.d.ts +15 -0
  2. package/package.json +2 -1
  3. package/packages/datadog-instrumentations/src/fetch.js +6 -45
  4. package/packages/datadog-instrumentations/src/helpers/fetch.js +22 -0
  5. package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -1
  6. package/packages/datadog-instrumentations/src/jest.js +77 -10
  7. package/packages/datadog-instrumentations/src/mongoose.js +2 -1
  8. package/packages/datadog-instrumentations/src/openai.js +149 -0
  9. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +6 -1
  10. package/packages/datadog-instrumentations/src/selenium.js +69 -0
  11. package/packages/datadog-plugin-cucumber/src/index.js +2 -2
  12. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +2 -2
  13. package/packages/datadog-plugin-cypress/src/support.js +19 -3
  14. package/packages/datadog-plugin-fetch/src/index.js +20 -11
  15. package/packages/datadog-plugin-jest/src/index.js +7 -2
  16. package/packages/datadog-plugin-mocha/src/index.js +4 -5
  17. package/packages/datadog-plugin-openai/src/index.js +159 -32
  18. package/packages/datadog-plugin-openai/src/services.js +2 -1
  19. package/packages/datadog-plugin-playwright/src/index.js +2 -2
  20. package/packages/datadog-plugin-selenium/src/index.js +71 -0
  21. package/packages/dd-trace/src/appsec/iast/analyzers/analyzers.js +1 -0
  22. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-base-analyzer.js +70 -0
  23. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-password-analyzer.js +14 -0
  24. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-password-rules.js +12 -0
  25. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-rule-type.js +6 -0
  26. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-secret-analyzer.js +5 -50
  27. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-secret-rules.js +742 -0
  28. package/packages/dd-trace/src/appsec/iast/analyzers/hardcoded-secrets-rules.js +539 -66
  29. package/packages/dd-trace/src/appsec/iast/taint-tracking/operations-taint-object.js +1 -9
  30. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +4 -2
  31. package/packages/dd-trace/src/appsec/iast/vulnerabilities.js +1 -0
  32. package/packages/dd-trace/src/appsec/remote_config/index.js +5 -5
  33. package/packages/dd-trace/src/appsec/reporter.js +11 -10
  34. package/packages/dd-trace/src/appsec/telemetry.js +36 -7
  35. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +4 -2
  36. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +4 -1
  37. package/packages/dd-trace/src/config.js +94 -9
  38. package/packages/dd-trace/src/dogstatsd.js +13 -11
  39. package/packages/dd-trace/src/index.js +5 -1
  40. package/packages/dd-trace/src/noop/dogstatsd.js +11 -0
  41. package/packages/dd-trace/src/noop/proxy.js +3 -0
  42. package/packages/dd-trace/src/opentracing/propagation/text_map.js +10 -4
  43. package/packages/dd-trace/src/opentracing/span.js +2 -0
  44. package/packages/dd-trace/src/plugins/index.js +2 -0
  45. package/packages/dd-trace/src/plugins/util/test.js +34 -3
  46. package/packages/dd-trace/src/profiling/config.js +8 -4
  47. package/packages/dd-trace/src/profiling/profiler.js +4 -0
  48. package/packages/dd-trace/src/profiling/ssi-telemetry-mock-profiler.js +33 -0
  49. package/packages/dd-trace/src/profiling/ssi-telemetry.js +167 -0
  50. package/packages/dd-trace/src/proxy.js +33 -7
  51. package/packages/dd-trace/src/tagger.js +13 -3
  52. package/packages/dd-trace/src/telemetry/index.js +5 -4
  53. package/packages/dd-trace/src/telemetry/metrics.js +2 -2
@@ -21,6 +21,7 @@ class Config {
21
21
  DD_AGENT_HOST,
22
22
  DD_ENV,
23
23
  DD_PROFILING_CODEHOTSPOTS_ENABLED,
24
+ DD_PROFILING_CPU_ENABLED,
24
25
  DD_PROFILING_DEBUG_SOURCE_MAPS,
25
26
  DD_PROFILING_ENABLED,
26
27
  DD_PROFILING_ENDPOINT_COLLECTION_ENABLED,
@@ -165,7 +166,7 @@ class Config {
165
166
 
166
167
  this.timelineEnabled = isTrue(coalesce(options.timelineEnabled,
167
168
  DD_PROFILING_TIMELINE_ENABLED,
168
- DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED, false))
169
+ DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED, samplingContextsAvailable))
169
170
  logExperimentalVarDeprecation('TIMELINE_ENABLED')
170
171
  checkOptionWithSamplingContextAllowed(this.timelineEnabled, 'Timeline view')
171
172
 
@@ -176,7 +177,9 @@ class Config {
176
177
  checkOptionWithSamplingContextAllowed(this.codeHotspotsEnabled, 'Code hotspots')
177
178
 
178
179
  this.cpuProfilingEnabled = isTrue(coalesce(options.cpuProfilingEnabled,
179
- DD_PROFILING_EXPERIMENTAL_CPU_ENABLED, false))
180
+ DD_PROFILING_CPU_ENABLED,
181
+ DD_PROFILING_EXPERIMENTAL_CPU_ENABLED, samplingContextsAvailable))
182
+ logExperimentalVarDeprecation('CPU_ENABLED')
180
183
  checkOptionWithSamplingContextAllowed(this.cpuProfilingEnabled, 'CPU profiling')
181
184
 
182
185
  this.profilers = ensureProfilers(profilers, this)
@@ -288,8 +291,9 @@ function ensureProfilers (profilers, options) {
288
291
  }
289
292
  }
290
293
 
291
- // Events profiler is a profiler for timeline events
292
- if (options.timelineEnabled) {
294
+ // Events profiler is a profiler that produces timeline events. It is only
295
+ // added if timeline is enabled and there's a wall profiler.
296
+ if (options.timelineEnabled && profilers.some(p => p instanceof WallProfiler)) {
293
297
  profilers.push(new EventsProfiler(options))
294
298
  }
295
299
 
@@ -4,6 +4,9 @@ const { EventEmitter } = require('events')
4
4
  const { Config } = require('./config')
5
5
  const { snapshotKinds } = require('./constants')
6
6
  const { threadNamePrefix } = require('./profilers/shared')
7
+ const dc = require('dc-polyfill')
8
+
9
+ const profileSubmittedChannel = dc.channel('datadog:profiling:profile-submitted')
7
10
 
8
11
  function maybeSourceMap (sourceMap, SourceMapper, debug) {
9
12
  if (!sourceMap) return
@@ -161,6 +164,7 @@ class Profiler extends EventEmitter {
161
164
  this._capture(this._timeoutInterval, endDate)
162
165
  }
163
166
  await this._submit(encodedProfiles, startDate, endDate, snapshotKind)
167
+ profileSubmittedChannel.publish()
164
168
  this._logger.debug('Submitted profiles')
165
169
  } catch (err) {
166
170
  this._logger.error(err)
@@ -0,0 +1,33 @@
1
+ 'use strict'
2
+
3
+ const dc = require('dc-polyfill')
4
+ const coalesce = require('koalas')
5
+ const profileSubmittedChannel = dc.channel('datadog:profiling:mock-profile-submitted')
6
+ const { DD_PROFILING_UPLOAD_PERIOD } = process.env
7
+
8
+ let timerId
9
+
10
+ module.exports = {
11
+ start: config => {
12
+ // Copied from packages/dd-trace/src/profiler.js
13
+ const flushInterval = coalesce(config.interval, Number(DD_PROFILING_UPLOAD_PERIOD) * 1000, 65 * 1000)
14
+
15
+ function scheduleProfileSubmit () {
16
+ timerId = setTimeout(emitProfileSubmit, flushInterval)
17
+ }
18
+
19
+ function emitProfileSubmit () {
20
+ profileSubmittedChannel.publish()
21
+ scheduleProfileSubmit()
22
+ }
23
+
24
+ scheduleProfileSubmit()
25
+ },
26
+
27
+ stop: () => {
28
+ if (timerId !== undefined) {
29
+ clearTimeout(timerId)
30
+ timerId = undefined
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,167 @@
1
+ 'use strict'
2
+
3
+ const telemetryMetrics = require('../telemetry/metrics')
4
+ const profilersNamespace = telemetryMetrics.manager.namespace('profilers')
5
+ const performance = require('perf_hooks').performance
6
+ const dc = require('dc-polyfill')
7
+ const { isTrue, isFalse } = require('../util')
8
+
9
+ // If the process lived for less than 30 seconds, it's considered short-lived
10
+ const DEFAULT_SHORT_LIVED_THRESHOLD = 30000
11
+
12
+ const EnablementChoice = {
13
+ MANUALLY_ENABLED: Symbol('SSITelemetry.EnablementChoice.MANUALLY_ENABLED'),
14
+ SSI_ENABLED: Symbol('SSITelemetry.EnablementChoice.SSI_ENABLED'),
15
+ SSI_NOT_ENABLED: Symbol('SSITelemetry.EnablementChoice.SSI_NOT_ENABLED'),
16
+ DISABLED: Symbol('SSITelemetry.EnablementChoice.MANUALLY_DISABLED')
17
+ }
18
+ Object.freeze(EnablementChoice)
19
+
20
+ function getEnablementChoiceFromEnv () {
21
+ const { DD_PROFILING_ENABLED, DD_INJECTION_ENABLED } = process.env
22
+ if (DD_INJECTION_ENABLED === undefined || isFalse(DD_PROFILING_ENABLED)) {
23
+ return EnablementChoice.DISABLED
24
+ } else if (DD_INJECTION_ENABLED.split(',').includes('profiling')) {
25
+ return EnablementChoice.SSI_ENABLED
26
+ } else if (isTrue(DD_PROFILING_ENABLED)) {
27
+ return EnablementChoice.MANUALLY_ENABLED
28
+ } else {
29
+ return EnablementChoice.SSI_NOT_ENABLED
30
+ }
31
+ }
32
+
33
+ function enablementChoiceToTagValue (enablementChoice) {
34
+ switch (enablementChoice) {
35
+ case EnablementChoice.MANUALLY_ENABLED:
36
+ return 'manually_enabled'
37
+ case EnablementChoice.SSI_ENABLED:
38
+ return 'ssi_enabled'
39
+ case EnablementChoice.SSI_NOT_ENABLED:
40
+ return 'not_enabled'
41
+ case EnablementChoice.MANUALLY_DISABLED:
42
+ // Can't emit this one as a tag
43
+ throw new Error('Invalid enablement choice')
44
+ }
45
+ }
46
+
47
+ /**
48
+ * This class emits telemetry metrics about the profiler behavior under SSI. It will only emit metrics
49
+ * when the application closes, and will emit the following metrics:
50
+ * - `number_of_profiles`: The number of profiles that were submitted
51
+ * - `number_of_runtime_id`: The number of runtime IDs in the app (always 1 for Node.js)
52
+ * It will also add tags describing the state of heuristics triggers, the enablement choice, and whether
53
+ * actual profiles were sent (as opposed to mock profiles). There is a mock profiler that is activated
54
+ * when the profiler is not enabled, and it will emit mock profile submission events at the same cadence
55
+ * the profiler would, providing insight into how many profiles would've been emitted if SSI enabled
56
+ * profiling. Note that telemetry is per tracer instance, and each worker thread will have its own instance.
57
+ */
58
+ class SSITelemetry {
59
+ constructor ({
60
+ enablementChoice = getEnablementChoiceFromEnv(),
61
+ shortLivedThreshold = DEFAULT_SHORT_LIVED_THRESHOLD
62
+ } = {}) {
63
+ if (!Object.values(EnablementChoice).includes(enablementChoice)) {
64
+ throw new Error('Invalid enablement choice')
65
+ }
66
+ if (typeof shortLivedThreshold !== 'number' || shortLivedThreshold <= 0) {
67
+ throw new Error('Short-lived threshold must be a positive number')
68
+ }
69
+ this.enablementChoice = enablementChoice
70
+ this.shortLivedThreshold = shortLivedThreshold
71
+
72
+ this.hasSentProfiles = false
73
+ this.noSpan = true
74
+ }
75
+
76
+ enabled () {
77
+ return this.enablementChoice !== EnablementChoice.DISABLED
78
+ }
79
+
80
+ start () {
81
+ if (this.enabled()) {
82
+ // Used to determine short-livedness of the process. We could use the process start time as the
83
+ // reference point, but the tracer initialization point is more relevant, as we couldn't be
84
+ // collecting profiles earlier anyway. The difference is not particularly significant if the
85
+ // tracer is initialized early in the process lifetime.
86
+ this.startTime = performance.now()
87
+
88
+ this._onSpanCreated = this._onSpanCreated.bind(this)
89
+ this._onProfileSubmitted = this._onProfileSubmitted.bind(this)
90
+ this._onMockProfileSubmitted = this._onMockProfileSubmitted.bind(this)
91
+ this._onAppClosing = this._onAppClosing.bind(this)
92
+
93
+ dc.subscribe('dd-trace:span:start', this._onSpanCreated)
94
+ dc.subscribe('datadog:profiling:profile-submitted', this._onProfileSubmitted)
95
+ dc.subscribe('datadog:profiling:mock-profile-submitted', this._onMockProfileSubmitted)
96
+ dc.subscribe('datadog:telemetry:app-closing', this._onAppClosing)
97
+ }
98
+ }
99
+
100
+ _onSpanCreated () {
101
+ this.noSpan = false
102
+ dc.unsubscribe('dd-trace:span:start', this._onSpanCreated)
103
+ }
104
+
105
+ _onProfileSubmitted () {
106
+ this.hasSentProfiles = true
107
+ this._incProfileCount()
108
+ }
109
+
110
+ _onMockProfileSubmitted () {
111
+ this._incProfileCount()
112
+ }
113
+
114
+ _incProfileCount () {
115
+ this._ensureProfileMetrics()
116
+ this._profileCount.inc()
117
+ }
118
+
119
+ _ensureProfileMetrics () {
120
+ const decision = []
121
+ if (this.noSpan) {
122
+ decision.push('no_span')
123
+ }
124
+ if (performance.now() - this.startTime < this.shortLivedThreshold) {
125
+ decision.push('short_lived')
126
+ }
127
+ if (decision.length === 0) {
128
+ decision.push('triggered')
129
+ }
130
+
131
+ const tags = [
132
+ 'installation:ssi',
133
+ `enablement_choice:${enablementChoiceToTagValue(this.enablementChoice)}`,
134
+ `has_sent_profiles:${this.hasSentProfiles}`,
135
+ `heuristic_hypothetical_decision:${decision.join('_')}`
136
+ ]
137
+
138
+ this._profileCount = profilersNamespace.count('ssi_heuristic.number_of_profiles', tags)
139
+ this._runtimeIdCount = profilersNamespace.count('ssi_heuristic.number_of_runtime_id', tags)
140
+
141
+ if (!this._emittedRuntimeId && decision[0] === 'triggered') {
142
+ // Tags won't change anymore, so we can emit the runtime ID metric now
143
+ this._emittedRuntimeId = true
144
+ this._runtimeIdCount.inc()
145
+ }
146
+ }
147
+
148
+ _onAppClosing () {
149
+ this._ensureProfileMetrics()
150
+ // Last ditch effort to emit a runtime ID count metric
151
+ if (!this._emittedRuntimeId) {
152
+ this._emittedRuntimeId = true
153
+ this._runtimeIdCount.inc()
154
+ }
155
+ // So we have the metrics in the final state
156
+ this._profileCount.inc(0)
157
+
158
+ dc.unsubscribe('datadog:profiling:profile-submitted', this._onProfileSubmitted)
159
+ dc.unsubscribe('datadog:profiling:mock-profile-submitted', this._onMockProfileSubmitted)
160
+ dc.unsubscribe('datadog:telemetry:app-closing', this._onAppClosing)
161
+ if (this.noSpan) {
162
+ dc.unsubscribe('dd-trace:span:start', this._onSpanCreated)
163
+ }
164
+ }
165
+ }
166
+
167
+ module.exports = { SSITelemetry, EnablementChoice }
@@ -11,7 +11,24 @@ const PluginManager = require('./plugin_manager')
11
11
  const remoteConfig = require('./appsec/remote_config')
12
12
  const AppsecSdk = require('./appsec/sdk')
13
13
  const dogstatsd = require('./dogstatsd')
14
+ const NoopDogStatsDClient = require('./noop/dogstatsd')
14
15
  const spanleak = require('./spanleak')
16
+ const { SSITelemetry } = require('./profiling/ssi-telemetry')
17
+
18
+ class LazyModule {
19
+ constructor (provider) {
20
+ this.provider = provider
21
+ }
22
+
23
+ enable (...args) {
24
+ this.module = this.provider()
25
+ this.module.enable(...args)
26
+ }
27
+
28
+ disable () {
29
+ this.module?.disable()
30
+ }
31
+ }
15
32
 
16
33
  class Tracer extends NoopProxy {
17
34
  constructor () {
@@ -20,8 +37,14 @@ class Tracer extends NoopProxy {
20
37
  this._initialized = false
21
38
  this._nomenclature = nomenclature
22
39
  this._pluginManager = new PluginManager(this)
23
- this.dogstatsd = new dogstatsd.NoopDogStatsDClient()
40
+ this.dogstatsd = new NoopDogStatsDClient()
24
41
  this._tracingInitialized = false
42
+
43
+ // these requires must work with esm bundler
44
+ this._modules = {
45
+ appsec: new LazyModule(() => require('./appsec')),
46
+ iast: new LazyModule(() => require('./appsec/iast'))
47
+ }
25
48
  }
26
49
 
27
50
  init (options) {
@@ -56,7 +79,7 @@ class Tracer extends NoopProxy {
56
79
  }
57
80
 
58
81
  if (config.remoteConfig.enabled && !config.isCiVisibility) {
59
- const rc = remoteConfig.enable(config)
82
+ const rc = remoteConfig.enable(config, this._modules.appsec)
60
83
 
61
84
  rc.on('APM_TRACING', (action, conf) => {
62
85
  if (action === 'unapply') {
@@ -72,6 +95,8 @@ class Tracer extends NoopProxy {
72
95
  require('./serverless').maybeStartServerlessMiniAgent(config)
73
96
  }
74
97
 
98
+ const ssiTelemetry = new SSITelemetry()
99
+ ssiTelemetry.start()
75
100
  if (config.profiling.enabled) {
76
101
  // do not stop tracer initialization if the profiler fails to be imported
77
102
  try {
@@ -80,6 +105,8 @@ class Tracer extends NoopProxy {
80
105
  } catch (e) {
81
106
  log.error(e)
82
107
  }
108
+ } else if (ssiTelemetry.enabled()) {
109
+ require('./profiling/ssi-telemetry-mock-profiler').start(config)
83
110
  }
84
111
  if (!this._profilerStarted) {
85
112
  this._profilerStarted = Promise.resolve(false)
@@ -107,9 +134,8 @@ class Tracer extends NoopProxy {
107
134
 
108
135
  _enableOrDisableTracing (config) {
109
136
  if (config.tracing !== false) {
110
- // dirty require for now so zero appsec code is executed unless explicitly enabled
111
137
  if (config.appsec.enabled) {
112
- require('./appsec').enable(config)
138
+ this._modules.appsec.enable(config)
113
139
  }
114
140
  if (!this._tracingInitialized) {
115
141
  this._tracer = new DatadogTracer(config)
@@ -117,11 +143,11 @@ class Tracer extends NoopProxy {
117
143
  this._tracingInitialized = true
118
144
  }
119
145
  if (config.iast.enabled) {
120
- require('./appsec/iast').enable(config, this._tracer)
146
+ this._modules.iast.enable(config, this._tracer)
121
147
  }
122
148
  } else if (this._tracingInitialized) {
123
- require('./appsec').disable()
124
- require('./appsec/iast').disable()
149
+ this._modules.appsec.disable()
150
+ this._modules.iast.disable()
125
151
  }
126
152
 
127
153
  if (this._tracingInitialized) {
@@ -2,7 +2,13 @@
2
2
 
3
3
  const log = require('./log')
4
4
 
5
- function add (carrier, keyValuePairs) {
5
+ const otelTagMap = {
6
+ 'deployment.environment': 'env',
7
+ 'service.name': 'service',
8
+ 'service.version': 'version'
9
+ }
10
+
11
+ function add (carrier, keyValuePairs, parseOtelTags = false) {
6
12
  if (!carrier || !keyValuePairs) return
7
13
 
8
14
  if (Array.isArray(keyValuePairs)) {
@@ -13,12 +19,16 @@ function add (carrier, keyValuePairs) {
13
19
  if (typeof keyValuePairs === 'string') {
14
20
  const segments = keyValuePairs.split(',')
15
21
  for (const segment of segments) {
16
- const separatorIndex = segment.indexOf(':')
22
+ const separatorIndex = parseOtelTags ? segment.indexOf('=') : segment.indexOf(':')
17
23
  if (separatorIndex === -1) continue
18
24
 
19
- const key = segment.slice(0, separatorIndex)
25
+ let key = segment.slice(0, separatorIndex)
20
26
  const value = segment.slice(separatorIndex + 1)
21
27
 
28
+ if (parseOtelTags && key in otelTagMap) {
29
+ key = otelTagMap[key]
30
+ }
31
+
22
32
  carrier[key.trim()] = value.trim()
23
33
  }
24
34
  } else {
@@ -10,6 +10,7 @@ const logs = require('./logs')
10
10
 
11
11
  const telemetryStartChannel = dc.channel('datadog:telemetry:start')
12
12
  const telemetryStopChannel = dc.channel('datadog:telemetry:stop')
13
+ const telemetryAppClosingChannel = dc.channel('datadog:telemetry:app-closing')
13
14
 
14
15
  let config
15
16
  let pluginManager
@@ -129,12 +130,12 @@ function appClosing () {
129
130
  if (!config?.telemetry?.enabled) {
130
131
  return
131
132
  }
133
+ // Give chance to listeners to update metrics before shutting down.
134
+ telemetryAppClosingChannel.publish()
132
135
  const { reqType, payload } = createPayload('app-closing')
133
136
  sendData(config, application, host, reqType, payload)
134
- // we flush before shutting down. Only in CI Visibility
135
- if (config.isCiVisibility) {
136
- metricsManager.send(config, application, host)
137
- }
137
+ // We flush before shutting down.
138
+ metricsManager.send(config, application, host)
138
139
  }
139
140
 
140
141
  function onBeforeExit () {
@@ -75,8 +75,8 @@ class CountMetric extends Metric {
75
75
  return this.track(value)
76
76
  }
77
77
 
78
- dec (value = -1) {
79
- return this.track(value)
78
+ dec (value = 1) {
79
+ return this.track(-value)
80
80
  }
81
81
 
82
82
  track (value = 1) {