dd-trace 5.34.0 → 5.35.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.34.0",
3
+ "version": "5.35.0",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -40,6 +40,18 @@ function wrapRequest (send) {
40
40
  }
41
41
  }
42
42
 
43
+ function wrapDeserialize (deserialize, channelSuffix) {
44
+ const headersCh = channel(`apm:aws:response:deserialize:${channelSuffix}`)
45
+
46
+ return function (response) {
47
+ if (headersCh.hasSubscribers) {
48
+ headersCh.publish({ headers: response.headers })
49
+ }
50
+
51
+ return deserialize.apply(this, arguments)
52
+ }
53
+ }
54
+
43
55
  function wrapSmithySend (send) {
44
56
  return function (command, ...args) {
45
57
  const cb = args[args.length - 1]
@@ -61,6 +73,10 @@ function wrapSmithySend (send) {
61
73
  const responseStartChannel = channel(`apm:aws:response:start:${channelSuffix}`)
62
74
  const responseFinishChannel = channel(`apm:aws:response:finish:${channelSuffix}`)
63
75
 
76
+ if (typeof command.deserialize === 'function') {
77
+ shimmer.wrap(command, 'deserialize', deserialize => wrapDeserialize(deserialize, channelSuffix))
78
+ }
79
+
64
80
  return innerAr.runInAsyncScope(() => {
65
81
  startCh.publish({
66
82
  serviceIdentifier,
@@ -102,6 +102,7 @@ module.exports = {
102
102
  oracledb: () => require('../oracledb'),
103
103
  openai: () => require('../openai'),
104
104
  paperplane: () => require('../paperplane'),
105
+ passport: () => require('../passport'),
105
106
  'passport-http': () => require('../passport-http'),
106
107
  'passport-local': () => require('../passport-local'),
107
108
  pg: () => require('../pg'),
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ const shimmer = require('../../datadog-shimmer')
4
+ const { channel, addHook } = require('./helpers/instrument')
5
+
6
+ const onPassportDeserializeUserChannel = channel('datadog:passport:deserializeUser:finish')
7
+
8
+ function wrapDone (done) {
9
+ return function wrappedDone (err, user) {
10
+ if (!err && user) {
11
+ const abortController = new AbortController()
12
+
13
+ onPassportDeserializeUserChannel.publish({ user, abortController })
14
+
15
+ if (abortController.signal.aborted) return
16
+ }
17
+
18
+ return done.apply(this, arguments)
19
+ }
20
+ }
21
+
22
+ function wrapDeserializeUser (deserializeUser) {
23
+ return function wrappedDeserializeUser (fn, req, done) {
24
+ if (typeof fn === 'function') return deserializeUser.apply(this, arguments)
25
+
26
+ if (typeof req === 'function') {
27
+ done = req
28
+ arguments[1] = wrapDone(done)
29
+ } else {
30
+ arguments[2] = wrapDone(done)
31
+ }
32
+
33
+ return deserializeUser.apply(this, arguments)
34
+ }
35
+ }
36
+
37
+ addHook({
38
+ name: 'passport',
39
+ file: 'lib/authenticator.js',
40
+ versions: ['>=0.3.0']
41
+ }, Authenticator => {
42
+ shimmer.wrap(Authenticator.prototype, 'deserializeUser', wrapDeserializeUser)
43
+
44
+ return Authenticator
45
+ })
@@ -24,11 +24,23 @@ const PROVIDER = {
24
24
  }
25
25
 
26
26
  class Generation {
27
- constructor ({ message = '', finishReason = '', choiceId = '' } = {}) {
27
+ constructor ({
28
+ message = '',
29
+ finishReason = '',
30
+ choiceId = '',
31
+ role,
32
+ inputTokens,
33
+ outputTokens
34
+ } = {}) {
28
35
  // stringify message as it could be a single generated message as well as a list of embeddings
29
36
  this.message = typeof message === 'string' ? message : JSON.stringify(message) || ''
30
37
  this.finishReason = finishReason || ''
31
38
  this.choiceId = choiceId || undefined
39
+ this.role = role
40
+ this.usage = {
41
+ inputTokens,
42
+ outputTokens
43
+ }
32
44
  }
33
45
  }
34
46
 
@@ -202,9 +214,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
202
214
  if (generations.length > 0) {
203
215
  const generation = generations[0]
204
216
  return new Generation({
205
- message: generation.message,
217
+ message: generation.message.content,
206
218
  finishReason: generation.finish_reason,
207
- choiceId: shouldSetChoiceIds ? generation.id : undefined
219
+ choiceId: shouldSetChoiceIds ? generation.id : undefined,
220
+ role: generation.message.role,
221
+ inputTokens: body.usage?.prompt_tokens,
222
+ outputTokens: body.usage?.completion_tokens
208
223
  })
209
224
  }
210
225
  }
@@ -214,7 +229,9 @@ function extractTextAndResponseReason (response, provider, modelName) {
214
229
  return new Generation({
215
230
  message: completion.data?.text,
216
231
  finishReason: completion?.finishReason,
217
- choiceId: shouldSetChoiceIds ? completion?.id : undefined
232
+ choiceId: shouldSetChoiceIds ? completion?.id : undefined,
233
+ inputTokens: body.usage?.prompt_tokens,
234
+ outputTokens: body.usage?.completion_tokens
218
235
  })
219
236
  }
220
237
  return new Generation()
@@ -226,7 +243,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
226
243
  const results = body.results || []
227
244
  if (results.length > 0) {
228
245
  const result = results[0]
229
- return new Generation({ message: result.outputText, finishReason: result.completionReason })
246
+ return new Generation({
247
+ message: result.outputText,
248
+ finishReason: result.completionReason,
249
+ inputTokens: body.inputTextTokenCount,
250
+ outputTokens: result.tokenCount
251
+ })
230
252
  }
231
253
  break
232
254
  }
@@ -252,7 +274,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
252
274
  break
253
275
  }
254
276
  case PROVIDER.META: {
255
- return new Generation({ message: body.generation, finishReason: body.stop_reason })
277
+ return new Generation({
278
+ message: body.generation,
279
+ finishReason: body.stop_reason,
280
+ inputTokens: body.prompt_token_count,
281
+ outputTokens: body.generation_token_count
282
+ })
256
283
  }
257
284
  case PROVIDER.MISTRAL: {
258
285
  const mistralGenerations = body.outputs || []
@@ -0,0 +1,120 @@
1
+ 'use strict'
2
+
3
+ const Plugin = require('../../dd-trace/src/plugins/plugin')
4
+ const telemetryMetrics = require('../../dd-trace/src/telemetry/metrics')
5
+ const apiMetrics = telemetryMetrics.manager.namespace('tracers')
6
+
7
+ // api ==> here
8
+ const objectMap = new WeakMap()
9
+
10
+ const injectionEnabledTag =
11
+ `injection_enabled:${process.env.DD_INJECTION_ENABLED ? 'yes' : 'no'}`
12
+
13
+ module.exports = class DdTraceApiPlugin extends Plugin {
14
+ static get id () {
15
+ return 'dd-trace-api'
16
+ }
17
+
18
+ constructor (...args) {
19
+ super(...args)
20
+
21
+ const tracer = this._tracer
22
+
23
+ this.addSub('datadog-api:v1:tracerinit', ({ proxy }) => {
24
+ const proxyVal = proxy()
25
+ objectMap.set(proxyVal, tracer)
26
+ objectMap.set(proxyVal.appsec, tracer.appsec)
27
+ objectMap.set(proxyVal.dogstatsd, tracer.dogstatsd)
28
+ })
29
+
30
+ const handleEvent = (name) => {
31
+ const counter = apiMetrics.count('dd_trace_api.called', [
32
+ `name:${name.replaceAll(':', '.')}`,
33
+ 'api_version:v1',
34
+ injectionEnabledTag
35
+ ])
36
+
37
+ // For v1, APIs are 1:1 with their internal equivalents, so we can just
38
+ // call the internal method directly. That's what we do here unless we
39
+ // want to override. As the API evolves, this may change.
40
+ this.addSub(`datadog-api:v1:${name}`, ({ self, args, ret, proxy, revProxy }) => {
41
+ counter.inc()
42
+
43
+ if (name.includes(':')) {
44
+ name = name.split(':').pop()
45
+ }
46
+
47
+ if (objectMap.has(self)) {
48
+ self = objectMap.get(self)
49
+ }
50
+
51
+ for (let i = 0; i < args.length; i++) {
52
+ if (objectMap.has(args[i])) {
53
+ args[i] = objectMap.get(args[i])
54
+ }
55
+ if (typeof args[i] === 'function') {
56
+ const orig = args[i]
57
+ args[i] = (...fnArgs) => {
58
+ for (let j = 0; j < fnArgs.length; j++) {
59
+ if (revProxy && revProxy[j]) {
60
+ const proxyVal = revProxy[j]()
61
+ objectMap.set(proxyVal, fnArgs[j])
62
+ fnArgs[j] = proxyVal
63
+ }
64
+ }
65
+ // TODO do we need to apply(this, ...) here?
66
+ return orig(...fnArgs)
67
+ }
68
+ }
69
+ }
70
+
71
+ try {
72
+ ret.value = self[name](...args)
73
+ if (proxy) {
74
+ const proxyVal = proxy()
75
+ objectMap.set(proxyVal, ret.value)
76
+ ret.value = proxyVal
77
+ } else if (ret.value && typeof ret.value === 'object') {
78
+ throw new TypeError(`Objects need proxies when returned via API (${name})`)
79
+ }
80
+ } catch (e) {
81
+ ret.error = e
82
+ }
83
+ })
84
+ }
85
+
86
+ // handleEvent('configure')
87
+ handleEvent('startSpan')
88
+ handleEvent('wrap')
89
+ handleEvent('trace')
90
+ handleEvent('inject')
91
+ handleEvent('extract')
92
+ handleEvent('getRumData')
93
+ handleEvent('profilerStarted')
94
+ handleEvent('context:toTraceId')
95
+ handleEvent('context:toSpanId')
96
+ handleEvent('context:toTraceparent')
97
+ handleEvent('span:context')
98
+ handleEvent('span:setTag')
99
+ handleEvent('span:addTags')
100
+ handleEvent('span:finish')
101
+ handleEvent('span:addLink')
102
+ handleEvent('scope')
103
+ handleEvent('scope:activate')
104
+ handleEvent('scope:active')
105
+ handleEvent('scope:bind')
106
+ handleEvent('appsec:blockRequest')
107
+ handleEvent('appsec:isUserBlocked')
108
+ handleEvent('appsec:setUser')
109
+ handleEvent('appsec:trackCustomEvent')
110
+ handleEvent('appsec:trackUserLoginFailureEvent')
111
+ handleEvent('appsec:trackUserLoginSuccessEvent')
112
+ handleEvent('dogstatsd:decrement')
113
+ handleEvent('dogstatsd:distribution')
114
+ handleEvent('dogstatsd:flush')
115
+ handleEvent('dogstatsd:gauge')
116
+ handleEvent('dogstatsd:histogram')
117
+ handleEvent('dogstatsd:increment')
118
+ handleEvent('use')
119
+ }
120
+ }
@@ -115,7 +115,10 @@ function block (req, res, rootSpan, abortController, actionParameters = defaultB
115
115
  res.removeHeader(headerName)
116
116
  }
117
117
 
118
- res.writeHead(statusCode, headers).end(body)
118
+ res.writeHead(statusCode, headers)
119
+
120
+ // this is needed to call the original end method, since express-session replaces it
121
+ res.constructor.prototype.end.call(res, body)
119
122
 
120
123
  responseBlockedSet.add(res)
121
124
 
@@ -14,6 +14,7 @@ module.exports = {
14
14
  incomingHttpRequestStart: dc.channel('dd-trace:incomingHttpRequestStart'),
15
15
  incomingHttpRequestEnd: dc.channel('dd-trace:incomingHttpRequestEnd'),
16
16
  passportVerify: dc.channel('datadog:passport:verify:finish'),
17
+ passportUser: dc.channel('datadog:passport:deserializeUser:finish'),
17
18
  queryParser: dc.channel('datadog:query:read:finish'),
18
19
  setCookieChannel: dc.channel('datadog:iast:set-cookie'),
19
20
  nextBodyParsed: dc.channel('apm:next:body-parsed'),
@@ -22,7 +22,8 @@ const EXCLUDED_LOCATIONS = getNodeModulesPaths(
22
22
  'sqreen/lib/package-reader/index.js',
23
23
  'ws/lib/websocket-server.js',
24
24
  'google-gax/build/src/grpc.js',
25
- 'cookie-signature/index.js'
25
+ 'cookie-signature/index.js',
26
+ 'express-session/index.js'
26
27
  )
27
28
 
28
29
  const EXCLUDED_PATHS_FROM_STACK = [
@@ -10,6 +10,7 @@ const {
10
10
  incomingHttpRequestStart,
11
11
  incomingHttpRequestEnd,
12
12
  passportVerify,
13
+ passportUser,
13
14
  queryParser,
14
15
  nextBodyParsed,
15
16
  nextQueryParsed,
@@ -67,6 +68,7 @@ function enable (_config) {
67
68
  incomingHttpRequestStart.subscribe(incomingHttpStartTranslator)
68
69
  incomingHttpRequestEnd.subscribe(incomingHttpEndTranslator)
69
70
  passportVerify.subscribe(onPassportVerify) // possible optimization: only subscribe if collection mode is enabled
71
+ passportUser.subscribe(onPassportDeserializeUser)
70
72
  queryParser.subscribe(onRequestQueryParsed)
71
73
  nextBodyParsed.subscribe(onRequestBodyParsed)
72
74
  nextQueryParsed.subscribe(onRequestQueryParsed)
@@ -197,6 +199,20 @@ function onPassportVerify ({ framework, login, user, success, abortController })
197
199
  handleResults(results, store.req, store.req.res, rootSpan, abortController)
198
200
  }
199
201
 
202
+ function onPassportDeserializeUser ({ user, abortController }) {
203
+ const store = storage.getStore()
204
+ const rootSpan = store?.req && web.root(store.req)
205
+
206
+ if (!rootSpan) {
207
+ log.warn('[ASM] No rootSpan found in onPassportDeserializeUser')
208
+ return
209
+ }
210
+
211
+ const results = UserTracking.trackUser(user, rootSpan)
212
+
213
+ handleResults(results, store.req, store.req.res, rootSpan, abortController)
214
+ }
215
+
200
216
  function onRequestQueryParsed ({ req, res, query, abortController }) {
201
217
  if (!query || typeof query !== 'object') return
202
218
 
@@ -310,6 +326,7 @@ function disable () {
310
326
  if (incomingHttpRequestStart.hasSubscribers) incomingHttpRequestStart.unsubscribe(incomingHttpStartTranslator)
311
327
  if (incomingHttpRequestEnd.hasSubscribers) incomingHttpRequestEnd.unsubscribe(incomingHttpEndTranslator)
312
328
  if (passportVerify.hasSubscribers) passportVerify.unsubscribe(onPassportVerify)
329
+ if (passportUser.hasSubscribers) passportUser.unsubscribe(onPassportDeserializeUser)
313
330
  if (queryParser.hasSubscribers) queryParser.unsubscribe(onRequestQueryParsed)
314
331
  if (nextBodyParsed.hasSubscribers) nextBodyParsed.unsubscribe(onRequestBodyParsed)
315
332
  if (nextQueryParsed.hasSubscribers) nextQueryParsed.unsubscribe(onRequestQueryParsed)
@@ -2,6 +2,8 @@
2
2
 
3
3
  const { getRootSpan } = require('./utils')
4
4
  const log = require('../../log')
5
+ const waf = require('../waf')
6
+ const addresses = require('../addresses')
5
7
 
6
8
  function setUserTags (user, rootSpan) {
7
9
  for (const k of Object.keys(user)) {
@@ -22,6 +24,13 @@ function setUser (tracer, user) {
22
24
  }
23
25
 
24
26
  setUserTags(user, rootSpan)
27
+ rootSpan.setTag('_dd.appsec.user.collection_mode', 'sdk')
28
+
29
+ waf.run({
30
+ persistent: {
31
+ [addresses.USER_ID]: '' + user.id
32
+ }
33
+ })
25
34
  }
26
35
 
27
36
  module.exports = {
@@ -23,6 +23,7 @@ function checkUserAndSetUser (tracer, user) {
23
23
  if (rootSpan) {
24
24
  if (!rootSpan.context()._tags['usr.id']) {
25
25
  setUserTags(user, rootSpan)
26
+ rootSpan.setTag('_dd.appsec.user.collection_mode', 'sdk')
26
27
  }
27
28
  } else {
28
29
  log.warn('[ASM] Root span not available in isUserBlocked')
@@ -186,6 +186,15 @@ function incrementMissingUserLoginMetric (framework, eventType) {
186
186
  }).inc()
187
187
  }
188
188
 
189
+ function incrementMissingUserIdMetric (framework, eventType) {
190
+ if (!enabled) return
191
+
192
+ appsecMetrics.count('instrum.user_auth.missing_user_id', {
193
+ framework,
194
+ event_type: eventType
195
+ }).inc()
196
+ }
197
+
189
198
  function getRequestMetrics (req) {
190
199
  if (req) {
191
200
  const store = getStore(req)
@@ -203,6 +212,7 @@ module.exports = {
203
212
  incrementWafUpdatesMetric,
204
213
  incrementWafRequestsMetric,
205
214
  incrementMissingUserLoginMetric,
215
+ incrementMissingUserIdMetric,
206
216
 
207
217
  getRequestMetrics
208
218
  }
@@ -53,6 +53,8 @@ function obfuscateIfNeeded (str) {
53
53
  function getUserId (user) {
54
54
  if (!user) return
55
55
 
56
+ // should we iterate on user keys instead to be case insensitive ?
57
+ // but if we iterate over user then we're missing the inherited props ?
56
58
  for (const field of USER_ID_FIELDS) {
57
59
  let id = user[field]
58
60
 
@@ -73,11 +75,6 @@ function getUserId (user) {
73
75
  function trackLogin (framework, login, user, success, rootSpan) {
74
76
  if (!collectionMode || collectionMode === 'disabled') return
75
77
 
76
- if (!rootSpan) {
77
- log.error('[ASM] No rootSpan found in AppSec trackLogin')
78
- return
79
- }
80
-
81
78
  if (typeof login !== 'string') {
82
79
  log.error('[ASM] Invalid login provided to AppSec trackLogin')
83
80
 
@@ -162,7 +159,36 @@ function trackLogin (framework, login, user, success, rootSpan) {
162
159
  return waf.run({ persistent })
163
160
  }
164
161
 
162
+ function trackUser (user, rootSpan) {
163
+ if (!collectionMode || collectionMode === 'disabled') return
164
+
165
+ const userId = getUserId(user)
166
+ if (!userId) {
167
+ log.error('[ASM] No valid user ID found in AppSec trackUser')
168
+ telemetry.incrementMissingUserIdMetric('passport', 'authenticated_request')
169
+ return
170
+ }
171
+
172
+ rootSpan.setTag('_dd.appsec.usr.id', userId)
173
+
174
+ const isSdkCalled = rootSpan.context()._tags['_dd.appsec.user.collection_mode'] === 'sdk'
175
+ // do not override SDK
176
+ if (!isSdkCalled) {
177
+ rootSpan.addTags({
178
+ 'usr.id': userId,
179
+ '_dd.appsec.user.collection_mode': collectionMode
180
+ })
181
+
182
+ return waf.run({
183
+ persistent: {
184
+ [addresses.USER_ID]: userId
185
+ }
186
+ })
187
+ }
188
+ }
189
+
165
190
  module.exports = {
166
191
  setCollectionMode,
167
- trackLogin
192
+ trackLogin,
193
+ trackUser
168
194
  }
@@ -21,6 +21,8 @@ module.exports = {
21
21
  findScriptFromPartialPath (path) {
22
22
  if (!path) return null // This shouldn't happen, but better safe than sorry
23
23
 
24
+ path = path.toLowerCase()
25
+
24
26
  const bestMatch = new Array(3)
25
27
  let maxMatchLength = -1
26
28
 
@@ -33,7 +35,7 @@ module.exports = {
33
35
 
34
36
  // Compare characters from the end
35
37
  while (i >= 0 && j >= 0) {
36
- const urlChar = url[i]
38
+ const urlChar = url[i].toLowerCase()
37
39
  const pathChar = path[j]
38
40
 
39
41
  // Check if both characters is a path boundary
@@ -43,8 +45,8 @@ module.exports = {
43
45
  // If both are boundaries, or if characters match exactly
44
46
  if (isBoundary || urlChar === pathChar) {
45
47
  if (isBoundary) {
46
- lastBoundaryPos = matchLength
47
48
  atBoundary = true
49
+ lastBoundaryPos = matchLength
48
50
  } else {
49
51
  atBoundary = false
50
52
  }
@@ -71,7 +73,10 @@ module.exports = {
71
73
  }
72
74
 
73
75
  // If we found a valid match and it's better than our previous best
74
- if (atBoundary && lastBoundaryPos !== -1 && lastBoundaryPos > maxMatchLength) {
76
+ if (atBoundary && (
77
+ lastBoundaryPos > maxMatchLength ||
78
+ (lastBoundaryPos === maxMatchLength && url.length < bestMatch[0].length) // Prefer shorter paths
79
+ )) {
75
80
  maxMatchLength = lastBoundaryPos
76
81
  bestMatch[0] = url
77
82
  bestMatch[1] = scriptId
@@ -8,7 +8,9 @@ const {
8
8
  parseModelId
9
9
  } = require('../../../../datadog-plugin-aws-sdk/src/services/bedrockruntime/utils')
10
10
 
11
- const enabledOperations = ['invokeModel']
11
+ const ENABLED_OPERATIONS = ['invokeModel']
12
+
13
+ const requestIdsToTokens = {}
12
14
 
13
15
  class BedrockRuntimeLLMObsPlugin extends BaseLLMObsPlugin {
14
16
  constructor () {
@@ -18,7 +20,7 @@ class BedrockRuntimeLLMObsPlugin extends BaseLLMObsPlugin {
18
20
  const request = response.request
19
21
  const operation = request.operation
20
22
  // avoids instrumenting other non supported runtime operations
21
- if (!enabledOperations.includes(operation)) {
23
+ if (!ENABLED_OPERATIONS.includes(operation)) {
22
24
  return
23
25
  }
24
26
  const { modelProvider, modelName } = parseModelId(request.params.modelId)
@@ -30,6 +32,17 @@ class BedrockRuntimeLLMObsPlugin extends BaseLLMObsPlugin {
30
32
  const span = storage.getStore()?.span
31
33
  this.setLLMObsTags({ request, span, response, modelProvider, modelName })
32
34
  })
35
+
36
+ this.addSub('apm:aws:response:deserialize:bedrockruntime', ({ headers }) => {
37
+ const requestId = headers['x-amzn-requestid']
38
+ const inputTokenCount = headers['x-amzn-bedrock-input-token-count']
39
+ const outputTokenCount = headers['x-amzn-bedrock-output-token-count']
40
+
41
+ requestIdsToTokens[requestId] = {
42
+ inputTokensFromHeaders: inputTokenCount && parseInt(inputTokenCount),
43
+ outputTokensFromHeaders: outputTokenCount && parseInt(outputTokenCount)
44
+ }
45
+ })
33
46
  }
34
47
 
35
48
  setLLMObsTags ({ request, span, response, modelProvider, modelName }) {
@@ -52,7 +65,39 @@ class BedrockRuntimeLLMObsPlugin extends BaseLLMObsPlugin {
52
65
  })
53
66
 
54
67
  // add I/O tags
55
- this._tagger.tagLLMIO(span, requestParams.prompt, textAndResponseReason.message)
68
+ this._tagger.tagLLMIO(
69
+ span,
70
+ requestParams.prompt,
71
+ [{ content: textAndResponseReason.message, role: textAndResponseReason.role }]
72
+ )
73
+
74
+ // add token metrics
75
+ const { inputTokens, outputTokens, totalTokens } = extractTokens({
76
+ requestId: response.$metadata.requestId,
77
+ usage: textAndResponseReason.usage
78
+ })
79
+ this._tagger.tagMetrics(span, {
80
+ inputTokens,
81
+ outputTokens,
82
+ totalTokens
83
+ })
84
+ }
85
+ }
86
+
87
+ function extractTokens ({ requestId, usage }) {
88
+ const {
89
+ inputTokensFromHeaders,
90
+ outputTokensFromHeaders
91
+ } = requestIdsToTokens[requestId] || {}
92
+ delete requestIdsToTokens[requestId]
93
+
94
+ const inputTokens = usage.inputTokens || inputTokensFromHeaders || 0
95
+ const outputTokens = usage.outputTokens || outputTokensFromHeaders || 0
96
+
97
+ return {
98
+ inputTokens,
99
+ outputTokens,
100
+ totalTokens: inputTokens + outputTokens
56
101
  }
57
102
  }
58
103
 
@@ -31,6 +31,9 @@ loadChannel.subscribe(({ name }) => {
31
31
  // Globals
32
32
  maybeEnable(require('../../datadog-plugin-fetch/src'))
33
33
 
34
+ // Always enabled
35
+ maybeEnable(require('../../datadog-plugin-dd-trace-api/src'))
36
+
34
37
  function maybeEnable (Plugin) {
35
38
  if (!Plugin || typeof Plugin !== 'function') return
36
39
  if (!pluginClasses[Plugin.id]) {
@@ -34,6 +34,7 @@ module.exports = {
34
34
  get couchbase () { return require('../../../datadog-plugin-couchbase/src') },
35
35
  get cypress () { return require('../../../datadog-plugin-cypress/src') },
36
36
  get dns () { return require('../../../datadog-plugin-dns/src') },
37
+ get 'dd-trace-api' () { return require('../../../datadog-plugin-dd-trace-api/src') },
37
38
  get elasticsearch () { return require('../../../datadog-plugin-elasticsearch/src') },
38
39
  get express () { return require('../../../datadog-plugin-express/src') },
39
40
  get fastify () { return require('../../../datadog-plugin-fastify/src') },