@@ -7,6 +7,7 @@ const semver = require('semver')
7
7
const nock = require ( 'nock' )
8
8
const sinon = require ( 'sinon' )
9
9
const { spawn } = require ( 'child_process' )
10
+ const { useEnv } = require ( '../../../integration-tests/helpers' )
10
11
11
12
const agent = require ( '../../dd-trace/test/plugins/agent' )
12
13
const { DogStatsDClient } = require ( '../../dd-trace/src/dogstatsd' )
@@ -31,12 +32,12 @@ describe('Plugin', () => {
31
32
tracer = require ( tracerRequirePath )
32
33
} )
33
34
34
- before ( ( ) => {
35
+ beforeEach ( ( ) => {
35
36
return agent . load ( 'openai' )
36
37
} )
37
38
38
- after ( ( ) => {
39
- return agent . close ( { ritmReset : false } )
39
+ afterEach ( ( ) => {
40
+ return agent . close ( { ritmReset : false , wipe : true } )
40
41
} )
41
42
42
43
beforeEach ( ( ) => {
@@ -72,6 +73,67 @@ describe('Plugin', () => {
72
73
sinon . restore ( )
73
74
} )
74
75
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
+
75
137
describe ( 'without initialization' , ( ) => {
76
138
it ( 'should not error' , ( done ) => {
77
139
spawn ( 'node' , [ 'no-init' ] , {
0 commit comments