Skip to content

Commit 6312629

Browse files
feat: Add llm attribute to all transactions that contain llm spans for openai (#1946)
1 parent cefce24 commit 6312629

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/instrumentation/openai.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
AI: { OPENAI }
2020
} = require('../../lib/metrics/names')
2121
const semver = require('semver')
22+
const { DESTINATIONS } = require('../config/attribute-filter')
2223

2324
let TRACKING_METRIC = OPENAI.TRACKING_PREFIX
2425

@@ -282,6 +283,8 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
282283
err
283284
})
284285
}
286+
287+
segment.transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true)
285288
}
286289
}
287290
}
@@ -301,6 +304,8 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
301304
promise: true,
302305
// eslint-disable-next-line max-params
303306
after(_shim, _fn, _name, err, response, segment) {
307+
segment.transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true)
308+
304309
if (!response) {
305310
// If we get an error, it is possible that `response = null`.
306311
// In that case, we define it to be an empty object.

test/versioned/openai/chat-completions.tap.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const fs = require('fs')
2525
const { version: pkgVersion } = JSON.parse(
2626
fs.readFileSync(`${__dirname}/node_modules/openai/package.json`)
2727
)
28+
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
2829

2930
tap.test('OpenAI instrumentation - chat completions', (t) => {
3031
t.autoend()
@@ -343,4 +344,19 @@ tap.test('OpenAI instrumentation - chat completions', (t) => {
343344
test.end()
344345
})
345346
})
347+
348+
t.test('should add llm attribute to transaction', (test) => {
349+
const { client, agent } = t.context
350+
helper.runInTransaction(agent, async (tx) => {
351+
await client.chat.completions.create({
352+
messages: [{ role: 'user', content: 'You are a mathematician.' }]
353+
})
354+
355+
const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)
356+
t.equal(attributes.llm, true)
357+
358+
tx.end()
359+
test.end()
360+
})
361+
})
346362
})

test/versioned/openai/embeddings.tap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const fs = require('fs')
2323
const { version: pkgVersion } = JSON.parse(
2424
fs.readFileSync(`${__dirname}/node_modules/openai/package.json`)
2525
)
26+
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
2627

2728
tap.test('OpenAI instrumentation - embedding', (t) => {
2829
t.autoend()
@@ -153,4 +154,20 @@ tap.test('OpenAI instrumentation - embedding', (t) => {
153154
test.end()
154155
})
155156
})
157+
158+
t.test('should add llm attribute to transaction', (test) => {
159+
const { client, agent } = t.context
160+
helper.runInTransaction(agent, async (tx) => {
161+
await client.embeddings.create({
162+
input: 'This is an embedding test.',
163+
model: 'text-embedding-ada-002'
164+
})
165+
166+
const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)
167+
t.equal(attributes.llm, true)
168+
169+
tx.end()
170+
test.end()
171+
})
172+
})
156173
})

0 commit comments

Comments
 (0)