dd-trace 6.9.0 → 6.10.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 (59) hide show
  1. package/ci/vitest-no-worker-init-setup.mjs +112 -10
  2. package/index.d.ts +42 -0
  3. package/package.json +4 -3
  4. package/packages/datadog-instrumentations/src/confluentinc-kafka-javascript.js +111 -6
  5. package/packages/datadog-instrumentations/src/helpers/pool-acquire.js +629 -0
  6. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +2 -1
  7. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/webdriverio.js +81 -0
  8. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +1 -0
  9. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +83 -1
  10. package/packages/datadog-instrumentations/src/jest.js +1 -1
  11. package/packages/datadog-instrumentations/src/mocha/main.js +1 -1
  12. package/packages/datadog-instrumentations/src/mocha/utils.js +2 -8
  13. package/packages/datadog-instrumentations/src/mysql.js +45 -15
  14. package/packages/datadog-instrumentations/src/mysql2.js +119 -18
  15. package/packages/datadog-instrumentations/src/path-to-regexp.js +67 -1
  16. package/packages/datadog-instrumentations/src/pg.js +116 -92
  17. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +43 -4
  18. package/packages/datadog-instrumentations/src/vitest-main.js +169 -17
  19. package/packages/datadog-instrumentations/src/vitest-util.js +64 -5
  20. package/packages/datadog-instrumentations/src/vitest-worker.js +150 -25
  21. package/packages/datadog-instrumentations/src/webdriverio.js +31 -17
  22. package/packages/datadog-plugin-aws-durable-execution-sdk-js/src/trace-checkpoint.js +12 -6
  23. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +2 -5
  24. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +1 -5
  25. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +1 -1
  26. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +31 -17
  27. package/packages/datadog-plugin-jest/src/index.js +5 -4
  28. package/packages/datadog-plugin-kafkajs/src/batch-consumer.js +2 -3
  29. package/packages/datadog-plugin-kafkajs/src/consumer.js +2 -3
  30. package/packages/datadog-plugin-kafkajs/src/producer.js +3 -2
  31. package/packages/datadog-plugin-kafkajs/src/utils.js +65 -7
  32. package/packages/datadog-plugin-mocha/src/index.js +550 -39
  33. package/packages/datadog-plugin-mysql/src/index.js +37 -0
  34. package/packages/datadog-plugin-next/src/index.js +1 -19
  35. package/packages/datadog-plugin-openai-agents/src/integration.js +278 -69
  36. package/packages/datadog-plugin-openai-agents/src/processor.js +16 -4
  37. package/packages/datadog-plugin-pg/src/index.js +1 -0
  38. package/packages/datadog-plugin-vitest/src/index.js +8 -20
  39. package/packages/datadog-plugin-ws/src/util.js +2 -1
  40. package/packages/dd-trace/src/appsec/api_security/normalized-route.js +453 -0
  41. package/packages/dd-trace/src/appsec/handlers/http-request.js +13 -0
  42. package/packages/dd-trace/src/carrier.js +356 -0
  43. package/packages/dd-trace/src/config/generated-config-types.d.ts +3 -0
  44. package/packages/dd-trace/src/config/supported-configurations.json +10 -0
  45. package/packages/dd-trace/src/datastreams/context.js +6 -1
  46. package/packages/dd-trace/src/datastreams/manager.js +5 -1
  47. package/packages/dd-trace/src/datastreams/pathway.js +23 -22
  48. package/packages/dd-trace/src/datastreams/processor.js +3 -2
  49. package/packages/dd-trace/src/llmobs/constants/tags.js +5 -0
  50. package/packages/dd-trace/src/llmobs/index.js +34 -2
  51. package/packages/dd-trace/src/llmobs/span_processor.js +16 -0
  52. package/packages/dd-trace/src/llmobs/tagger.js +96 -0
  53. package/packages/dd-trace/src/llmobs/util.js +78 -0
  54. package/packages/dd-trace/src/opentracing/propagation/text_map.js +126 -120
  55. package/packages/dd-trace/src/opentracing/propagation/text_map_dsm.js +4 -8
  56. package/packages/dd-trace/src/plugin_manager.js +2 -0
  57. package/packages/{datadog-plugin-jest/src/util.js → dd-trace/src/plugins/util/jest.js} +1 -62
  58. package/packages/dd-trace/src/plugins/util/test.js +84 -5
  59. package/packages/dd-trace/src/plugins/util/web.js +53 -2
@@ -23,7 +23,11 @@ const {
23
23
  OUTPUT_MESSAGES,
24
24
  TAGS,
25
25
  NAME,
26
+ PARENT_AGENT_NAME,
27
+ PARENT_AGENT_SPAN_ID,
26
28
  PROPAGATED_PARENT_ID_KEY,
29
+ PROPAGATED_PARENT_AGENT_ID_KEY,
30
+ PROPAGATED_PARENT_AGENT_NAME_KEY,
27
31
  ROOT_PARENT_ID,
28
32
  CACHE_READ_INPUT_TOKENS_METRIC_KEY,
29
33
  CACHE_WRITE_INPUT_TOKENS_METRIC_KEY,
@@ -57,6 +61,7 @@ const {
57
61
  const { storage } = require('./storage')
58
62
  const {
59
63
  findGenAIAncestorSpanId,
64
+ resolveAgentAttribution,
60
65
  validateCostTags,
61
66
  writeBridgeTags,
62
67
  validateToolDefinitions,
@@ -173,6 +178,7 @@ class LLMObsTagger {
173
178
  this._setTag(span, PARENT_ID_KEY, parentId)
174
179
 
175
180
  this.#tagSamplingDecision(span, parent)
181
+ this.#tagAgentAttribution(span, parent)
176
182
 
177
183
  // apply annotation context
178
184
  const annotationContext = storage.getStore()?.annotationContext
@@ -229,6 +235,34 @@ class LLMObsTagger {
229
235
  if (samplingDecision != null) this._setTag(span, SAMPLING_DECISION, samplingDecision)
230
236
  }
231
237
 
238
+ /**
239
+ * Store the nearest agent ancestor on the span so it can be surfaced as
240
+ * `meta.agent_attribution` at finish. Resolved once here, at registration, so downstream
241
+ * children inherit it with a single lookup rather than walking the ancestor chain.
242
+ *
243
+ * @param {import('../opentracing/span')} span
244
+ * @param {import('../opentracing/span')} [parent] the LLMObs parent span, if any
245
+ */
246
+ // TODO: spans whose kind changes after registration (e.g. claude-agent-sdk tools promoted to
247
+ // sub-agents) will not retroactively update already-finished children's attribution. Follow up.
248
+ #tagAgentAttribution (span, parent) {
249
+ let name, spanId
250
+ if (registry.has(parent)) {
251
+ // Local LLMObs parent: attribute to it if it is an agent, else inherit its resolution.
252
+ ({ name, spanId } = resolveAgentAttribution(registry.get(parent), parent))
253
+ } else if (span.context()._trace.tags[PROPAGATED_PARENT_ID_KEY]) {
254
+ // Distributed LLMObs parent: inherit the nearest agent propagated from upstream. The
255
+ // name may be absent when the upstream hop ran an older SDK or its name was not
256
+ // wire-safe; the id-only case is expected and the backend resolves the name by span id.
257
+ const traceTags = span.context()._trace.tags
258
+ name = traceTags[PROPAGATED_PARENT_AGENT_NAME_KEY]
259
+ spanId = traceTags[PROPAGATED_PARENT_AGENT_ID_KEY]
260
+ }
261
+
262
+ if (name != null) this._setTag(span, PARENT_AGENT_NAME, name)
263
+ if (spanId != null) this._setTag(span, PARENT_AGENT_SPAN_ID, spanId)
264
+ }
265
+
232
266
  // TODO: similarly for the following `tag` methods,
233
267
  // how can we transition from a span weakmap to core API functionality
234
268
  tagLLMIO (span, inputData, outputData) {
@@ -696,6 +730,59 @@ class LLMObsTagger {
696
730
  return filteredAudioParts
697
731
  }
698
732
 
733
+ // Image counterpart of #filterAudioParts, with the same wire shape and validation.
734
+ #filterImageParts (imageParts) {
735
+ if (!Array.isArray(imageParts)) {
736
+ imageParts = [imageParts]
737
+ }
738
+
739
+ const filteredImageParts = []
740
+ for (const imagePart of imageParts) {
741
+ if (imagePart == null || typeof imagePart !== 'object') {
742
+ this.#handleFailure('Image part must be an object.', 'invalid_io_messages')
743
+ continue
744
+ }
745
+
746
+ const { mimeType, content, attachmentKey } = imagePart
747
+
748
+ if (typeof mimeType !== 'string' || !mimeType) {
749
+ this.#handleFailure('Image part mimeType must be a non-empty string.', 'invalid_io_messages')
750
+ continue
751
+ }
752
+
753
+ if (content == null && attachmentKey == null) {
754
+ this.#handleFailure("Image part must have either 'content' or 'attachmentKey'.", 'invalid_io_messages')
755
+ continue
756
+ }
757
+
758
+ if (content != null && attachmentKey != null) {
759
+ this.#handleFailure(
760
+ "Image part must have only one of 'content' or 'attachmentKey', not both.", 'invalid_io_messages'
761
+ )
762
+ continue
763
+ }
764
+
765
+ const imagePartObj = { mime_type: mimeType }
766
+
767
+ if (content == null) {
768
+ if (typeof attachmentKey !== 'string') {
769
+ this.#handleFailure('Image part attachmentKey must be a string.', 'invalid_io_messages')
770
+ continue
771
+ }
772
+ imagePartObj.attachment_key = attachmentKey
773
+ } else {
774
+ if (typeof content !== 'string') {
775
+ this.#handleFailure('Image part content must be a base64-encoded string.', 'invalid_io_messages')
776
+ continue
777
+ }
778
+ imagePartObj.content = content
779
+ }
780
+
781
+ filteredImageParts.push(imagePartObj)
782
+ }
783
+ return filteredImageParts
784
+ }
785
+
699
786
  #tagMessages (span, data, key) {
700
787
  if (!data) {
701
788
  return
@@ -723,6 +810,7 @@ class LLMObsTagger {
723
810
  toolResults,
724
811
  toolId,
725
812
  audioParts,
813
+ imageParts,
726
814
  } = message
727
815
  const messageObj = {}
728
816
 
@@ -764,6 +852,14 @@ class LLMObsTagger {
764
852
  }
765
853
  }
766
854
 
855
+ if (imageParts != null) {
856
+ const filteredImageParts = this.#filterImageParts(imageParts)
857
+
858
+ if (filteredImageParts.length) {
859
+ messageObj.image_parts = filteredImageParts
860
+ }
861
+ }
862
+
767
863
  if (toolId) {
768
864
  if (role === 'tool') {
769
865
  condition = this.#tagConditionalString(toolId, 'Tool ID', messageObj, 'tool_id') && condition
@@ -6,6 +6,10 @@ const {
6
6
  LLMOBS_PARENT_ID_BRIDGE_KEY,
7
7
  LLMOBS_TRACE_ID_BRIDGE_KEY,
8
8
  SPAN_KINDS,
9
+ SPAN_KIND,
10
+ NAME,
11
+ PARENT_AGENT_NAME,
12
+ PARENT_AGENT_SPAN_ID,
9
13
  } = require('./constants/tags')
10
14
 
11
15
  const DECIMAL_TRACE_ID_REGEX = /^\d+$/
@@ -294,6 +298,76 @@ function getFunctionArguments (fn, args = []) {
294
298
  }
295
299
  }
296
300
 
301
+ // The `x-datadog-tags` tagset encoder rejects commas (the entry delimiter) and any byte
302
+ // outside 0x20-0x7E, and a raise there drops the ENTIRE header (taking ml_app,
303
+ // llmobs_trace_id, parent_id, sampling with it). An agent name is arbitrary user text, so it
304
+ // must be skipped rather than sanitized when unsafe: only the digit-safe id then propagates and
305
+ // the backend resolves the real name by span id. `=` is legal in tagset values (illegal only in
306
+ // keys), so a name containing `=` is safe and must not be dropped.
307
+ /**
308
+ * @param {string} name
309
+ * @returns {boolean}
310
+ */
311
+ function agentNameWireSafe (name) {
312
+ // Conservative slice of the 512B shared tagset budget, mirroring dd-trace-py.
313
+ if (Buffer.byteLength(name, 'utf8') > 256) return false
314
+ for (let index = 0; index < name.length; index++) {
315
+ const code = name.charCodeAt(index)
316
+ if (code < 0x20 || code > 0x7E || code === 0x2C /* comma */) return false
317
+ }
318
+ return true
319
+ }
320
+
321
+ /**
322
+ * Resolve the nearest agent that a *child* of `span` should be attributed to.
323
+ *
324
+ * If `span` is itself an agent, the child attributes directly to `span`. Otherwise `span`
325
+ * already resolved its own nearest agent at registration, so the child inherits that with a
326
+ * single lookup rather than walking the ancestor chain. An agent never attributes itself.
327
+ *
328
+ * @param {Record<string, unknown> | undefined} tags - Registry entry for the parent span.
329
+ * @param {import('../opentracing/span')} [span] - The parent span itself (needed for span id / name).
330
+ * @returns {{ name: string | undefined, spanId: string | undefined }}
331
+ */
332
+ function resolveAgentAttribution (tags, span) {
333
+ if (!tags) return { name: undefined, spanId: undefined }
334
+ if (tags[SPAN_KIND] === 'agent') {
335
+ return { name: tags[NAME] || span._name, spanId: span.context().toSpanId() }
336
+ }
337
+ return { name: tags[PARENT_AGENT_NAME], spanId: tags[PARENT_AGENT_SPAN_ID] }
338
+ }
339
+
340
+ /**
341
+ * Removes all `key=value` entries for the given key from a comma-separated tagset string.
342
+ *
343
+ * @param {string} tags - Existing tagset string (may be empty).
344
+ * @param {string} key
345
+ * @returns {string}
346
+ */
347
+ function stripTagsetEntry (tags, key) {
348
+ if (!tags.includes(key)) return tags
349
+ return tags.split(',').filter(entry => !entry.startsWith(`${key}=`)).join(',')
350
+ }
351
+
352
+ /**
353
+ * Appends `key=value` to the tagset string with a comma separator, but only when `value` is
354
+ * truthy, passes the optional `safeguard` predicate, and fits within `maxTagSetLength`. Returns
355
+ * the original `tags` unchanged when the value is absent, unsafe, or would overflow the budget.
356
+ *
357
+ * @param {string} tags - Existing tagset string (may be empty).
358
+ * @param {string} key
359
+ * @param {string | undefined} value
360
+ * @param {((v: string) => boolean) | null} [safeguard]
361
+ * @param {number} [maxTagSetLength]
362
+ * @returns {string}
363
+ */
364
+ function appendOptionalPropagatedTag (tags, key, value, safeguard, maxTagSetLength) {
365
+ if (!value || (safeguard && !safeguard(value))) return tags
366
+ const entry = `${tags ? ',' : ''}${key}=${value}`
367
+ if (maxTagSetLength != null && tags.length + entry.length > maxTagSetLength) return tags
368
+ return `${tags}${entry}`
369
+ }
370
+
297
371
  function spanHasError (span) {
298
372
  const spanContext = span.context()
299
373
  return !!(spanContext.getTag('error') || spanContext.getTag('error.type'))
@@ -443,6 +517,8 @@ function formatAudioPart (data, mimeType) {
443
517
  }
444
518
 
445
519
  module.exports = {
520
+ agentNameWireSafe,
521
+ appendOptionalPropagatedTag,
446
522
  audioMimeTypeFromFormat,
447
523
  encodeUnicode,
448
524
  findGenAIAncestorSpanId,
@@ -450,6 +526,8 @@ module.exports = {
450
526
  llmObsTraceIdToWire,
451
527
  normalizeLlmObsTraceId,
452
528
  formatAudioPart,
529
+ resolveAgentAttribution,
530
+ stripTagsetEntry,
453
531
  validateCostTags,
454
532
  validateKind,
455
533
  getFunctionArguments,