@@ -134,6 +134,39 @@ it('tracks OpenAI usage', async () => {
134
134
) ;
135
135
} ) ;
136
136
137
+ it ( 'tracks error when OpenAI metrics function throws' , async ( ) => {
138
+ const tracker = new LDAIConfigTrackerImpl ( mockLdClient , configKey , variationKey , testContext ) ;
139
+ jest . spyOn ( global . Date , 'now' ) . mockReturnValueOnce ( 1000 ) . mockReturnValueOnce ( 2000 ) ;
140
+
141
+ const error = new Error ( 'OpenAI API error' ) ;
142
+ await expect (
143
+ tracker . trackOpenAIMetrics ( async ( ) => {
144
+ throw error ;
145
+ } ) ,
146
+ ) . rejects . toThrow ( error ) ;
147
+
148
+ expect ( mockTrack ) . toHaveBeenCalledWith (
149
+ '$ld:ai:duration:total' ,
150
+ testContext ,
151
+ { configKey, variationKey } ,
152
+ 1000 ,
153
+ ) ;
154
+
155
+ expect ( mockTrack ) . toHaveBeenCalledWith (
156
+ '$ld:ai:generation' ,
157
+ testContext ,
158
+ { configKey, variationKey } ,
159
+ 1 ,
160
+ ) ;
161
+
162
+ expect ( mockTrack ) . toHaveBeenCalledWith (
163
+ '$ld:ai:generation:error' ,
164
+ testContext ,
165
+ { configKey, variationKey } ,
166
+ 1 ,
167
+ ) ;
168
+ } ) ;
169
+
137
170
it ( 'tracks Bedrock conversation with successful response' , ( ) => {
138
171
const tracker = new LDAIConfigTrackerImpl ( mockLdClient , configKey , variationKey , testContext ) ;
139
172
@@ -196,11 +229,22 @@ it('tracks Bedrock conversation with error response', () => {
196
229
$metadata : { httpStatusCode : 400 } ,
197
230
} ;
198
231
199
- // TODO: We may want a track failure.
200
-
201
232
tracker . trackBedrockConverseMetrics ( response ) ;
202
233
203
- expect ( mockTrack ) . not . toHaveBeenCalled ( ) ;
234
+ expect ( mockTrack ) . toHaveBeenCalledTimes ( 2 ) ;
235
+ expect ( mockTrack ) . toHaveBeenCalledWith (
236
+ '$ld:ai:generation' ,
237
+ testContext ,
238
+ { configKey, variationKey } ,
239
+ 1 ,
240
+ ) ;
241
+
242
+ expect ( mockTrack ) . toHaveBeenCalledWith (
243
+ '$ld:ai:generation:error' ,
244
+ testContext ,
245
+ { configKey, variationKey } ,
246
+ 1 ,
247
+ ) ;
204
248
} ) ;
205
249
206
250
it ( 'tracks tokens' , ( ) => {
@@ -304,3 +348,41 @@ it('summarizes tracked metrics', () => {
304
348
success : true ,
305
349
} ) ;
306
350
} ) ;
351
+
352
+ it ( 'tracks duration when async function throws' , async ( ) => {
353
+ const tracker = new LDAIConfigTrackerImpl ( mockLdClient , configKey , variationKey , testContext ) ;
354
+ jest . spyOn ( global . Date , 'now' ) . mockReturnValueOnce ( 1000 ) . mockReturnValueOnce ( 2000 ) ;
355
+
356
+ const error = new Error ( 'test error' ) ;
357
+ await expect (
358
+ tracker . trackDurationOf ( async ( ) => {
359
+ throw error ;
360
+ } ) ,
361
+ ) . rejects . toThrow ( error ) ;
362
+
363
+ expect ( mockTrack ) . toHaveBeenCalledWith (
364
+ '$ld:ai:duration:total' ,
365
+ testContext ,
366
+ { configKey, variationKey } ,
367
+ 1000 ,
368
+ ) ;
369
+ } ) ;
370
+
371
+ it ( 'tracks error' , ( ) => {
372
+ const tracker = new LDAIConfigTrackerImpl ( mockLdClient , configKey , variationKey , testContext ) ;
373
+ tracker . trackError ( ) ;
374
+
375
+ expect ( mockTrack ) . toHaveBeenCalledWith (
376
+ '$ld:ai:generation' ,
377
+ testContext ,
378
+ { configKey, variationKey } ,
379
+ 1 ,
380
+ ) ;
381
+
382
+ expect ( mockTrack ) . toHaveBeenCalledWith (
383
+ '$ld:ai:generation:error' ,
384
+ testContext ,
385
+ { configKey, variationKey } ,
386
+ 1 ,
387
+ ) ;
388
+ } ) ;
0 commit comments