Skip to content

Commit 2fea9b5

Browse files
authoredFeb 14, 2025··
fix(openai): apply span char limit truncation to chat completion input tags (#5276)
1 parent 5a08ad9 commit 2fea9b5

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
 

‎packages/datadog-plugin-openai/src/tracing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ function truncateApiKey (apiKey) {
795795

796796
function tagChatCompletionRequestContent (contents, messageIdx, tags) {
797797
if (typeof contents === 'string') {
798-
tags[`openai.request.messages.${messageIdx}.content`] = contents
798+
tags[`openai.request.messages.${messageIdx}.content`] = normalize(contents)
799799
} else if (Array.isArray(contents)) {
800800
// content can also be an array of objects
801801
// which represent text input or image url

‎packages/datadog-plugin-openai/test/index.spec.js

+65-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const semver = require('semver')
77
const nock = require('nock')
88
const sinon = require('sinon')
99
const { spawn } = require('child_process')
10+
const { useEnv } = require('../../../integration-tests/helpers')
1011

1112
const agent = require('../../dd-trace/test/plugins/agent')
1213
const { DogStatsDClient } = require('../../dd-trace/src/dogstatsd')
@@ -31,12 +32,12 @@ describe('Plugin', () => {
3132
tracer = require(tracerRequirePath)
3233
})
3334

34-
before(() => {
35+
beforeEach(() => {
3536
return agent.load('openai')
3637
})
3738

38-
after(() => {
39-
return agent.close({ ritmReset: false })
39+
afterEach(() => {
40+
return agent.close({ ritmReset: false, wipe: true })
4041
})
4142

4243
beforeEach(() => {
@@ -72,6 +73,67 @@ describe('Plugin', () => {
7273
sinon.restore()
7374
})
7475

76+
describe('with configuration', () => {
77+
useEnv({
78+
DD_OPENAI_SPAN_CHAR_LIMIT: 0
79+
})
80+
81+
it('should truncate both inputs and outputs', async () => {
82+
if (version === '3.0.0') return
83+
nock('https://api.openai.com:443')
84+
.post('/v1/chat/completions')
85+
.reply(200, {
86+
model: 'gpt-3.5-turbo-0301',
87+
choices: [{
88+
message: {
89+
role: 'assistant',
90+
content: "In that case, it's best to avoid peanut"
91+
}
92+
}]
93+
})
94+
95+
const checkTraces = agent
96+
.use(traces => {
97+
expect(traces[0][0].meta).to.have.property('openai.request.messages.0.content',
98+
'...')
99+
expect(traces[0][0].meta).to.have.property('openai.request.messages.1.content',
100+
'...')
101+
expect(traces[0][0].meta).to.have.property('openai.request.messages.2.content', '...')
102+
expect(traces[0][0].meta).to.have.property('openai.response.choices.0.message.content',
103+
'...')
104+
})
105+
106+
const params = {
107+
model: 'gpt-3.5-turbo',
108+
messages: [
109+
{
110+
role: 'user',
111+
content: 'Peanut Butter or Jelly?',
112+
name: 'hunter2'
113+
},
114+
{
115+
role: 'assistant',
116+
content: 'Are you allergic to peanuts?',
117+
name: 'hal'
118+
},
119+
{
120+
role: 'user',
121+
content: 'Deathly allergic!',
122+
name: 'hunter2'
123+
}
124+
]
125+
}
126+
127+
if (semver.satisfies(realVersion, '>=4.0.0')) {
128+
await openai.chat.completions.create(params)
129+
} else {
130+
await openai.createChatCompletion(params)
131+
}
132+
133+
await checkTraces
134+
})
135+
})
136+
75137
describe('without initialization', () => {
76138
it('should not error', (done) => {
77139
spawn('node', ['no-init'], {

0 commit comments

Comments
 (0)
Please sign in to comment.