Skip to content

Commit

Permalink
feat: Add version tracking metric on OpenAI events (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 28, 2023
1 parent 9d38faa commit d11d100
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/instrumentation/openai.js
Expand Up @@ -18,6 +18,8 @@ const MIN_STREAM_VERSION = '4.12.2'
const { AI } = require('../../lib/metrics/names')
const semver = require('semver')

let TRACKING_METRIC = AI.TRACKING_PREFIX

/**
* Checks if we should skip instrumentation.
* Currently it checks if `ai_monitoring.enabled` is true
Expand Down Expand Up @@ -46,6 +48,11 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
return
}

// Update the tracking metric name with the version of the library
// being instrumented. We do not have access to the version when
// initially declaring the variable.
TRACKING_METRIC = `${TRACKING_METRIC}OpenAI/${shim.pkgVersion}`

/**
* Adds apiKey and response headers to the active segment
* on symbols
Expand Down Expand Up @@ -76,6 +83,7 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
* @param {object} msg LLM event
*/
function recordEvent(type, msg) {
agent.metrics.getOrCreateMetric(TRACKING_METRIC).incrementCallCount()
msg = agent?.llm?.metadata ? { ...agent.llm.metadata, ...msg } : msg
agent.customEventAggregator.add([{ type, timestamp: Date.now() }, msg])
}
Expand Down
1 change: 1 addition & 0 deletions lib/metrics/names.js
Expand Up @@ -165,6 +165,7 @@ const EXPRESS = {
}

const AI = {
TRACKING_PREFIX: 'Nodejs/ML/',
OPEN_AI: 'AI/OpenAI'
}

Expand Down
16 changes: 16 additions & 0 deletions test/versioned/openai/chat-completions.tap.js
Expand Up @@ -13,6 +13,7 @@
const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const { AI } = require('../../../lib/metrics/names')
const responses = require('./mock-responses')
const {
beforeHook,
Expand Down Expand Up @@ -63,6 +64,21 @@ tap.test('OpenAI instrumentation - chat completions', (t) => {
})
})

t.test('should increment tracking metric for each chat completion event', (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 metrics = agent.metrics.getOrCreateMetric(`${AI.TRACKING_PREFIX}OpenAI/${pkgVersion}`)
t.equal(metrics.callCount > 0, true)

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

t.test('should create chat completion message and summary for every message sent', (test) => {
const { client, agent } = t.context
helper.runInTransaction(agent, async (tx) => {
Expand Down
23 changes: 23 additions & 0 deletions test/versioned/openai/embeddings.tap.js
Expand Up @@ -14,6 +14,13 @@ const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const { beforeHook, afterEachHook, afterHook } = require('./common')
const { AI } = require('../../../lib/metrics/names')

const fs = require('fs')
// have to read and not require because openai does not export the package.json
const { version: pkgVersion } = JSON.parse(
fs.readFileSync(`${__dirname}/node_modules/openai/package.json`)
)

tap.test('OpenAI instrumentation - embedding', (t) => {
t.autoend()
Expand Down Expand Up @@ -48,6 +55,22 @@ tap.test('OpenAI instrumentation - embedding', (t) => {
})
})

t.test('should increment tracking metric for each embedding event', (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 metrics = agent.metrics.getOrCreateMetric(`${AI.TRACKING_PREFIX}OpenAI/${pkgVersion}`)
t.equal(metrics.callCount > 0, true)

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

t.test('should create an embedding message', (test) => {
const { client, agent } = t.context
helper.runInTransaction(agent, async (tx) => {
Expand Down

0 comments on commit d11d100

Please sign in to comment.