Skip to content

Commit

Permalink
feat: Add llm attribute to all transactions that contain llm spans fo…
Browse files Browse the repository at this point in the history
…r openai (#1946)
  • Loading branch information
svetlanabrennan committed Jan 12, 2024
1 parent cefce24 commit 6312629
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/instrumentation/openai.js
Expand Up @@ -19,6 +19,7 @@ const {
AI: { OPENAI }
} = require('../../lib/metrics/names')
const semver = require('semver')
const { DESTINATIONS } = require('../config/attribute-filter')

let TRACKING_METRIC = OPENAI.TRACKING_PREFIX

Expand Down Expand Up @@ -282,6 +283,8 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
err
})
}

segment.transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true)
}
}
}
Expand All @@ -301,6 +304,8 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
promise: true,
// eslint-disable-next-line max-params
after(_shim, _fn, _name, err, response, segment) {
segment.transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true)

if (!response) {
// If we get an error, it is possible that `response = null`.
// In that case, we define it to be an empty object.
Expand Down
16 changes: 16 additions & 0 deletions test/versioned/openai/chat-completions.tap.js
Expand Up @@ -25,6 +25,7 @@ const fs = require('fs')
const { version: pkgVersion } = JSON.parse(
fs.readFileSync(`${__dirname}/node_modules/openai/package.json`)
)
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')

tap.test('OpenAI instrumentation - chat completions', (t) => {
t.autoend()
Expand Down Expand Up @@ -343,4 +344,19 @@ tap.test('OpenAI instrumentation - chat completions', (t) => {
test.end()
})
})

t.test('should add llm attribute to transaction', (test) => {
const { client, agent } = t.context
helper.runInTransaction(agent, async (tx) => {
await client.chat.completions.create({
messages: [{ role: 'user', content: 'You are a mathematician.' }]
})

const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)
t.equal(attributes.llm, true)

tx.end()
test.end()
})
})
})
17 changes: 17 additions & 0 deletions test/versioned/openai/embeddings.tap.js
Expand Up @@ -23,6 +23,7 @@ const fs = require('fs')
const { version: pkgVersion } = JSON.parse(
fs.readFileSync(`${__dirname}/node_modules/openai/package.json`)
)
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')

tap.test('OpenAI instrumentation - embedding', (t) => {
t.autoend()
Expand Down Expand Up @@ -153,4 +154,20 @@ tap.test('OpenAI instrumentation - embedding', (t) => {
test.end()
})
})

t.test('should add llm attribute to transaction', (test) => {
const { client, agent } = t.context
helper.runInTransaction(agent, async (tx) => {
await client.embeddings.create({
input: 'This is an embedding test.',
model: 'text-embedding-ada-002'
})

const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)
t.equal(attributes.llm, true)

tx.end()
test.end()
})
})
})

0 comments on commit 6312629

Please sign in to comment.