dd-trace 5.44.0 → 5.45.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "5.44.0",
3
+ "version": "5.45.0",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -7,6 +7,7 @@ const waf = require('../waf')
7
7
  const { keepTrace } = require('../../priority_sampler')
8
8
  const addresses = require('../addresses')
9
9
  const { ASM } = require('../../standalone/product')
10
+ const { incrementSdkEventMetric } = require('../telemetry')
10
11
 
11
12
  function trackUserLoginSuccessEvent (tracer, user, metadata) {
12
13
  // TODO: better user check here and in _setUser() ?
@@ -15,6 +16,8 @@ function trackUserLoginSuccessEvent (tracer, user, metadata) {
15
16
  return
16
17
  }
17
18
 
19
+ incrementSdkEventMetric('login_success')
20
+
18
21
  const rootSpan = getRootSpan(tracer)
19
22
  if (!rootSpan) {
20
23
  log.warn('[ASM] Root span not available in trackUserLoginSuccessEvent')
@@ -48,6 +51,8 @@ function trackUserLoginFailureEvent (tracer, userId, exists, metadata) {
48
51
  trackEvent('users.login.failure', fields, 'trackUserLoginFailureEvent', getRootSpan(tracer))
49
52
 
50
53
  runWaf('users.login.failure', { login: userId })
54
+
55
+ incrementSdkEventMetric('login_failure')
51
56
  }
52
57
 
53
58
  function trackCustomEvent (tracer, eventName, metadata) {
@@ -57,6 +62,8 @@ function trackCustomEvent (tracer, eventName, metadata) {
57
62
  }
58
63
 
59
64
  trackEvent(eventName, metadata, 'trackCustomEvent', getRootSpan(tracer))
65
+
66
+ incrementSdkEventMetric('custom')
60
67
  }
61
68
 
62
69
  function trackEvent (eventName, fields, sdkMethodName, rootSpan) {
@@ -2,7 +2,7 @@
2
2
 
3
3
  const { DD_TELEMETRY_REQUEST_METRICS } = require('./common')
4
4
  const { addRaspRequestMetrics, trackRaspMetrics } = require('./rasp')
5
- const { incrementMissingUserId, incrementMissingUserLogin } = require('./user')
5
+ const { incrementMissingUserId, incrementMissingUserLogin, incrementSdkEvent } = require('./user')
6
6
  const {
7
7
  addWafRequestMetrics,
8
8
  trackWafMetrics,
@@ -121,6 +121,12 @@ function incrementMissingUserIdMetric (framework, eventType) {
121
121
  incrementMissingUserId(framework, eventType)
122
122
  }
123
123
 
124
+ function incrementSdkEventMetric (framework, eventType) {
125
+ if (!enabled) return
126
+
127
+ incrementSdkEvent(framework, eventType)
128
+ }
129
+
124
130
  function getRequestMetrics (req) {
125
131
  if (req) {
126
132
  const store = getStore(req)
@@ -141,6 +147,7 @@ module.exports = {
141
147
  incrementWafRequestsMetric,
142
148
  incrementMissingUserLoginMetric,
143
149
  incrementMissingUserIdMetric,
150
+ incrementSdkEventMetric,
144
151
 
145
152
  getRequestMetrics
146
153
  }
@@ -18,7 +18,15 @@ function incrementMissingUserId (framework, eventType) {
18
18
  }).inc()
19
19
  }
20
20
 
21
+ function incrementSdkEvent (eventType) {
22
+ appsecMetrics.count('sdk.event', {
23
+ event_type: eventType,
24
+ sdk_version: 'v1'
25
+ }).inc()
26
+ }
27
+
21
28
  module.exports = {
22
29
  incrementMissingUserLogin,
23
- incrementMissingUserId
30
+ incrementMissingUserId,
31
+ incrementSdkEvent
24
32
  }
@@ -194,7 +194,6 @@ class DogStatsDClient {
194
194
  }
195
195
  }
196
196
 
197
- // TODO: Handle arrays of tags and tags translation.
198
197
  class MetricsAggregationClient {
199
198
  constructor (client) {
200
199
  this._client = client
@@ -211,98 +210,132 @@ class MetricsAggregationClient {
211
210
  }
212
211
 
213
212
  reset () {
214
- this._counters = {}
215
- this._gauges = {}
216
- this._histograms = {}
213
+ this._counters = new Map()
214
+ this._gauges = new Map()
215
+ this._histograms = new Map()
217
216
  }
218
217
 
219
- distribution (name, value, tag) {
220
- this._client.distribution(name, value, tag && [tag])
218
+ // TODO: Aggerate with a histogram and send the buckets to the client.
219
+ distribution (name, value, tags) {
220
+ this._client.distribution(name, value, tags)
221
221
  }
222
222
 
223
- boolean (name, value, tag) {
224
- this.gauge(name, value ? 1 : 0, tag)
223
+ boolean (name, value, tags) {
224
+ this.gauge(name, value ? 1 : 0, tags)
225
225
  }
226
226
 
227
- histogram (name, value, tag) {
228
- this._histograms[name] = this._histograms[name] || new Map()
227
+ histogram (name, value, tags) {
228
+ const node = this._ensureTree(this._histograms, name, tags, null)
229
229
 
230
- if (!this._histograms[name].has(tag)) {
231
- this._histograms[name].set(tag, new Histogram())
230
+ if (!node.value) {
231
+ node.value = new Histogram()
232
232
  }
233
233
 
234
- this._histograms[name].get(tag).record(value)
234
+ node.value.record(value)
235
235
  }
236
236
 
237
- count (name, count, tag, monotonic = true) {
238
- if (typeof tag === 'boolean') {
239
- monotonic = tag
240
- tag = undefined
237
+ count (name, count, tags = [], monotonic = true) {
238
+ if (typeof tags === 'boolean') {
239
+ monotonic = tags
240
+ tags = []
241
241
  }
242
242
 
243
- const map = monotonic ? this._counters : this._gauges
243
+ const container = monotonic ? this._counters : this._gauges
244
+ const node = this._ensureTree(container, name, tags, 0)
244
245
 
245
- map[name] = map[name] || new Map()
246
-
247
- const value = map[name].get(tag) || 0
248
-
249
- map[name].set(tag, value + count)
246
+ node.value = node.value + count
250
247
  }
251
248
 
252
- gauge (name, value, tag) {
253
- this._gauges[name] = this._gauges[name] || new Map()
254
- this._gauges[name].set(tag, value)
249
+ gauge (name, value, tags) {
250
+ const node = this._ensureTree(this._gauges, name, tags, 0)
251
+
252
+ node.value = value
255
253
  }
256
254
 
257
- increment (name, count = 1, tag) {
258
- this.count(name, count, tag)
255
+ increment (name, count = 1, tags) {
256
+ this.count(name, count, tags)
259
257
  }
260
258
 
261
- decrement (name, count = 1, tag) {
262
- this.count(name, -count, tag)
259
+ decrement (name, count = 1, tags) {
260
+ this.count(name, -count, tags)
263
261
  }
264
262
 
265
263
  _captureGauges () {
266
- Object.keys(this._gauges).forEach(name => {
267
- this._gauges[name].forEach((value, tag) => {
268
- this._client.gauge(name, value, tag && [tag])
269
- })
264
+ this._captureTree(this._gauges, (node, name, tags) => {
265
+ this._client.gauge(name, node.value, tags)
270
266
  })
271
267
  }
272
268
 
273
269
  _captureCounters () {
274
- Object.keys(this._counters).forEach(name => {
275
- this._counters[name].forEach((value, tag) => {
276
- this._client.increment(name, value, tag && [tag])
277
- })
270
+ this._captureTree(this._counters, (node, name, tags) => {
271
+ this._client.increment(name, node.value, tags)
278
272
  })
279
273
 
280
- this._counters = {}
274
+ this._counters.clear()
281
275
  }
282
276
 
283
277
  _captureHistograms () {
284
- Object.keys(this._histograms).forEach(name => {
285
- this._histograms[name].forEach((stats, tag) => {
286
- const tags = tag && [tag]
278
+ this._captureTree(this._histograms, (node, name, tags) => {
279
+ let stats = node.value
287
280
 
288
- // Stats can contain garbage data when a value was never recorded.
289
- if (stats.count === 0) {
290
- stats = { max: 0, min: 0, sum: 0, avg: 0, median: 0, p95: 0, count: 0, reset: stats.reset }
291
- }
281
+ // Stats can contain garbage data when a value was never recorded.
282
+ if (stats.count === 0) {
283
+ stats = { max: 0, min: 0, sum: 0, avg: 0, median: 0, p95: 0, count: 0 }
284
+ }
292
285
 
293
- this._client.gauge(`${name}.min`, stats.min, tags)
294
- this._client.gauge(`${name}.max`, stats.max, tags)
295
- this._client.increment(`${name}.sum`, stats.sum, tags)
296
- this._client.increment(`${name}.total`, stats.sum, tags)
297
- this._client.gauge(`${name}.avg`, stats.avg, tags)
298
- this._client.increment(`${name}.count`, stats.count, tags)
299
- this._client.gauge(`${name}.median`, stats.median, tags)
300
- this._client.gauge(`${name}.95percentile`, stats.p95, tags)
286
+ this._client.gauge(`${name}.min`, stats.min, tags)
287
+ this._client.gauge(`${name}.max`, stats.max, tags)
288
+ this._client.increment(`${name}.sum`, stats.sum, tags)
289
+ this._client.increment(`${name}.total`, stats.sum, tags)
290
+ this._client.gauge(`${name}.avg`, stats.avg, tags)
291
+ this._client.increment(`${name}.count`, stats.count, tags)
292
+ this._client.gauge(`${name}.median`, stats.median, tags)
293
+ this._client.gauge(`${name}.95percentile`, stats.p95, tags)
301
294
 
302
- stats.reset()
303
- })
295
+ node.value.reset()
304
296
  })
305
297
  }
298
+
299
+ _captureTree (tree, fn) {
300
+ for (const [name, root] of tree) {
301
+ this._captureNode(root, name, [], fn)
302
+ }
303
+ }
304
+
305
+ _captureNode (node, name, tags, fn) {
306
+ if (node.touched) {
307
+ fn(node, name, tags)
308
+ }
309
+
310
+ for (const [tag, next] of node.nodes) {
311
+ this._captureNode(next, name, tags.concat(tag), fn)
312
+ }
313
+ }
314
+
315
+ _ensureTree (tree, name, tags, value) {
316
+ tags = tags ? [].concat(tags) : []
317
+
318
+ let node = this._ensureNode(tree, name, value)
319
+
320
+ for (const tag of tags) {
321
+ node = this._ensureNode(node.nodes, tag, value)
322
+ }
323
+
324
+ node.touched = true
325
+
326
+ return node
327
+ }
328
+
329
+ _ensureNode (container, key, value) {
330
+ let node = container.get(key)
331
+
332
+ if (!node) {
333
+ node = { nodes: new Map(), touched: false, value }
334
+ container.set(key, node)
335
+ }
336
+
337
+ return node
338
+ }
306
339
  }
307
340
 
308
341
  /**
@@ -324,45 +357,29 @@ class CustomMetrics {
324
357
  }
325
358
 
326
359
  increment (stat, value = 1, tags) {
327
- for (const tag of this._normalizeTags(tags)) {
328
- this._client.increment(stat, value, tag)
329
- }
360
+ this._client.increment(stat, value, CustomMetrics.tagTranslator(tags))
330
361
  }
331
362
 
332
363
  decrement (stat, value = 1, tags) {
333
- for (const tag of this._normalizeTags(tags)) {
334
- this._client.decrement(stat, value, tag)
335
- }
364
+ this._client.decrement(stat, value, CustomMetrics.tagTranslator(tags))
336
365
  }
337
366
 
338
367
  gauge (stat, value, tags) {
339
- for (const tag of this._normalizeTags(tags)) {
340
- this._client.gauge(stat, value, tag)
341
- }
368
+ this._client.gauge(stat, value, CustomMetrics.tagTranslator(tags))
342
369
  }
343
370
 
344
371
  distribution (stat, value, tags) {
345
- for (const tag of this._normalizeTags(tags)) {
346
- this._client.distribution(stat, value, tag)
347
- }
372
+ this._client.distribution(stat, value, CustomMetrics.tagTranslator(tags))
348
373
  }
349
374
 
350
375
  histogram (stat, value, tags) {
351
- for (const tag of this._normalizeTags(tags)) {
352
- this._client.histogram(stat, value, tag)
353
- }
376
+ this._client.histogram(stat, value, CustomMetrics.tagTranslator(tags))
354
377
  }
355
378
 
356
379
  flush () {
357
380
  return this._client.flush()
358
381
  }
359
382
 
360
- _normalizeTags (tags) {
361
- tags = CustomMetrics.tagTranslator(tags)
362
-
363
- return tags.length === 0 ? [undefined] : tags
364
- }
365
-
366
383
  /**
367
384
  * Exposing { tagName: 'tagValue' } to the end user
368
385
  * These are translated into [ 'tagName:tagValue' ] for internal use
@@ -7,39 +7,28 @@ class Histogram {
7
7
  this.reset()
8
8
  }
9
9
 
10
- get min () { return this._min }
11
- get max () { return this._max }
12
- get avg () { return this._count === 0 ? 0 : this._sum / this._count }
13
- get sum () { return this._sum }
14
- get count () { return this._count }
10
+ get min () { return this._sketch.count === 0 ? 0 : this._sketch.min }
11
+ get max () { return this._sketch.count === 0 ? 0 : this._sketch.max }
12
+ get avg () { return this._sketch.count === 0 ? 0 : this._sketch.sum / this._sketch.count }
13
+ get sum () { return this._sketch.sum }
14
+ get count () { return this._sketch.count }
15
15
  get median () { return this.percentile(50) }
16
16
  get p95 () { return this.percentile(95) }
17
17
 
18
18
  percentile (percentile) {
19
- return this._histogram.getValueAtQuantile(percentile / 100) || 0
19
+ return this._sketch.getValueAtQuantile(percentile / 100) || 0
20
20
  }
21
21
 
22
- record (value) {
23
- if (this._count === 0) {
24
- this._min = this._max = value
25
- } else {
26
- this._min = Math.min(this._min, value)
27
- this._max = Math.max(this._max, value)
28
- }
29
-
30
- this._count++
31
- this._sum += value
22
+ merge (histogram) {
23
+ return this._sketch.merge(histogram._sketch)
24
+ }
32
25
 
33
- this._histogram.accept(value)
26
+ record (value) {
27
+ this._sketch.accept(value)
34
28
  }
35
29
 
36
30
  reset () {
37
- this._min = 0
38
- this._max = 0
39
- this._sum = 0
40
- this._count = 0
41
-
42
- this._histogram = new DDSketch()
31
+ this._sketch = new DDSketch()
43
32
  }
44
33
  }
45
34
 
@@ -6,6 +6,7 @@ const { storage } = require('./storage')
6
6
 
7
7
  const LLMObsSpanProcessor = require('./span_processor')
8
8
 
9
+ const telemetry = require('./telemetry')
9
10
  const { channel } = require('dc-polyfill')
10
11
  const spanProcessCh = channel('dd-trace:span:process')
11
12
  const evalMetricAppendCh = channel('llmobs:eval-metric:append')
@@ -29,6 +30,7 @@ let spanWriter
29
30
  let evalWriter
30
31
 
31
32
  function enable (config) {
33
+ const startTime = performance.now()
32
34
  // create writers and eval writer append and flush channels
33
35
  // span writer append is handled by the span processor
34
36
  evalWriter = new LLMObsEvalMetricsWriter(config)
@@ -44,6 +46,7 @@ function enable (config) {
44
46
 
45
47
  // distributed tracing for llmobs
46
48
  injectCh.subscribe(handleLLMObsParentIdInjection)
49
+ telemetry.recordLLMObsEnabled(startTime, config)
47
50
  }
48
51
 
49
52
  function disable () {
@@ -74,6 +74,23 @@ function incrementLLMObsSpanFinishedCount (span, value = 1) {
74
74
  llmobsMetrics.count('span.finished', tags).inc(value)
75
75
  }
76
76
 
77
+ function recordLLMObsEnabled (startTime, config, value = 1) {
78
+ const initTimeMs = performance.now() - startTime
79
+ // There isn't an easy way to determine if a user automatically enabled LLMObs via
80
+ // in-code or command line setup. We'll use the presence of DD_LLMOBS_ENABLED env var
81
+ // as a rough heuristic, but note that this isn't perfect since
82
+ // a user may have env vars but enable manually in code.
83
+ const autoEnabled = !!config._env?.['llmobs.enabled']
84
+ const tags = {
85
+ error: 0,
86
+ agentless: Number(config.llmobs.agentlessEnabled),
87
+ site: config.site,
88
+ auto: Number(autoEnabled)
89
+ }
90
+ llmobsMetrics.count('product_enabled', tags).inc(value)
91
+ llmobsMetrics.distribution('init_time', tags).track(initTimeMs)
92
+ }
93
+
77
94
  function recordLLMObsRawSpanSize (event, rawEventSize) {
78
95
  const tags = extractTagsFromSpanEvent(event)
79
96
  llmobsMetrics.distribution('span.raw_size', tags).track(rawEventSize)
@@ -85,9 +102,18 @@ function recordLLMObsSpanSize (event, eventSize, shouldTruncate) {
85
102
  llmobsMetrics.distribution('span.size', tags).track(eventSize)
86
103
  }
87
104
 
105
+ function recordDroppedPayload (numEvents, eventType, error) {
106
+ if (eventType !== 'span' && eventType !== 'evaluation_metric') return
107
+ const metricName = eventType === 'span' ? 'dropped_span_event' : 'dropped_eval_event'
108
+ const tags = { error }
109
+ llmobsMetrics.count(metricName, tags).inc(numEvents)
110
+ }
111
+
88
112
  module.exports = {
113
+ recordLLMObsEnabled,
89
114
  incrementLLMObsSpanStartCount,
90
115
  incrementLLMObsSpanFinishedCount,
91
116
  recordLLMObsRawSpanSize,
92
- recordLLMObsSpanSize
117
+ recordLLMObsSpanSize,
118
+ recordDroppedPayload
93
119
  }
@@ -6,6 +6,7 @@ const { URL, format } = require('url')
6
6
  const logger = require('../../log')
7
7
 
8
8
  const { encodeUnicode } = require('../util')
9
+ const telemetry = require('../telemetry')
9
10
  const log = require('../../log')
10
11
 
11
12
  class BaseLLMObsWriter {
@@ -45,6 +46,7 @@ class BaseLLMObsWriter {
45
46
  append (event, byteLength) {
46
47
  if (this._buffer.length >= this._bufferLimit) {
47
48
  logger.warn(`${this.constructor.name} event buffer full (limit is ${this._bufferLimit}), dropping event`)
49
+ telemetry.recordDroppedPayload(1, this._eventType, 'buffer_full')
48
50
  return
49
51
  }
50
52
 
@@ -76,10 +78,12 @@ class BaseLLMObsWriter {
76
78
  logger.error(
77
79
  'Error sending %d LLMObs %s events to %s: %s', events.length, this._eventType, this._url, err.message, err
78
80
  )
81
+ telemetry.recordDroppedPayload(events.length, this._eventType, 'request_error')
79
82
  } else if (code >= 300) {
80
83
  logger.error(
81
84
  'Error sending %d LLMObs %s events to %s: %s', events.length, this._eventType, this._url, code
82
85
  )
86
+ telemetry.recordDroppedPayload(events.length, this._eventType, 'http_error')
83
87
  } else {
84
88
  logger.debug(`Sent ${events.length} LLMObs ${this._eventType} events to ${this._url}`)
85
89
  }
@@ -10,6 +10,7 @@ const { getExtraServices } = require('../service-naming/extra-services')
10
10
  const { UNACKNOWLEDGED, ACKNOWLEDGED, ERROR } = require('./apply_states')
11
11
  const Scheduler = require('./scheduler')
12
12
  const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA } = require('../plugins/util/tags')
13
+ const tagger = require('../tagger')
13
14
 
14
15
  const clientId = uuid()
15
16
 
@@ -34,6 +35,10 @@ class RemoteConfigManager extends EventEmitter {
34
35
  port: config.port
35
36
  }))
36
37
 
38
+ tagger.add(config.tags, {
39
+ '_dd.rc.client_id': clientId
40
+ })
41
+
37
42
  const tags = config.repositoryUrl
38
43
  ? {
39
44
  ...config.tags,