-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
664 lines (638 loc) · 25.3 KB
/
test.ts
File metadata and controls
664 lines (638 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';
describe('Anthropic integration', () => {
afterAll(() => {
cleanupChildProcesses();
});
const EXPECTED_TRANSACTION_DEFAULT_PII_FALSE = {
transaction: 'main',
spans: expect.arrayContaining([
// First span - basic message completion without PII
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.temperature': 0.7,
'gen_ai.request.max_tokens': 100,
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'msg_mock123',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
// Second span - error handling
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'error-model',
}),
description: 'messages error-model',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'internal_error',
}),
// Third span - token counting (no response.text because recordOutputs=false by default)
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
// Fourth span - models.retrieve
expect.objectContaining({
data: expect.objectContaining({
'anthropic.response.timestamp': '2024-05-08T05:20:00.000Z',
'gen_ai.operation.name': 'models',
'sentry.op': 'gen_ai.models',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'claude-3-haiku-20240307',
'gen_ai.response.model': 'claude-3-haiku-20240307',
}),
description: 'models claude-3-haiku-20240307',
op: 'gen_ai.models',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
]),
};
const EXPECTED_TRANSACTION_DEFAULT_PII_TRUE = {
transaction: 'main',
spans: expect.arrayContaining([
// First span - basic message completion with PII
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.max_tokens': 100,
'gen_ai.request.messages':
'[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What is the capital of France?"}]',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.temperature': 0.7,
'gen_ai.response.id': 'msg_mock123',
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.text': 'Hello from Anthropic mock!',
'gen_ai.system': 'anthropic',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
expect.objectContaining({
data: expect.objectContaining({
'http.request.method': 'POST',
'http.request.method_original': 'POST',
'http.response.header.content-length': 247,
'http.response.status_code': 200,
'otel.kind': 'CLIENT',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.node_fetch',
'url.path': '/anthropic/v1/messages',
'url.query': '',
'url.scheme': 'http',
}),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'ok',
}),
// Second - error handling with PII
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.messages': '[{"role":"user","content":"This will fail"}]',
'gen_ai.request.model': 'error-model',
'gen_ai.system': 'anthropic',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
}),
description: 'messages error-model',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'internal_error',
}),
expect.objectContaining({
data: expect.objectContaining({
'http.request.method': 'POST',
'http.request.method_original': 'POST',
'http.response.header.content-length': 15,
'http.response.status_code': 404,
'otel.kind': 'CLIENT',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.node_fetch',
'url.path': '/anthropic/v1/messages',
'url.query': '',
'url.scheme': 'http',
}),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'not_found',
}),
// Third - token counting with PII (response.text is present because sendDefaultPii=true enables recordOutputs)
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.messages': '[{"role":"user","content":"What is the capital of France?"}]',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.text': '15',
'gen_ai.system': 'anthropic',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
expect.objectContaining({
data: expect.objectContaining({
'http.request.method': 'POST',
'http.request.method_original': 'POST',
'http.response.header.content-length': 19,
'http.response.status_code': 200,
'otel.kind': 'CLIENT',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.node_fetch',
'url.path': '/anthropic/v1/messages/count_tokens',
'url.query': '',
'url.scheme': 'http',
}),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'ok',
}),
// Fourth - models.retrieve with PII
expect.objectContaining({
data: expect.objectContaining({
'anthropic.response.timestamp': '2024-05-08T05:20:00.000Z',
'gen_ai.operation.name': 'models',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'claude-3-haiku-20240307',
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.system': 'anthropic',
'sentry.op': 'gen_ai.models',
'sentry.origin': 'auto.ai.anthropic',
}),
description: 'models claude-3-haiku-20240307',
op: 'gen_ai.models',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
expect.objectContaining({
data: expect.objectContaining({
'http.request.method': 'GET',
'http.request.method_original': 'GET',
'http.response.header.content-length': 123,
'http.response.status_code': 200,
'otel.kind': 'CLIENT',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.node_fetch',
'url.path': '/anthropic/v1/models/claude-3-haiku-20240307',
'url.query': '',
'url.scheme': 'http',
'user_agent.original': 'Anthropic/JS 0.63.0',
}),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'ok',
}),
// Fifth - messages.create with stream: true
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.messages': '[{"role":"user","content":"What is the capital of France?"}]',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.stream': true,
'gen_ai.response.id': 'msg_stream123',
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.streaming': true,
'gen_ai.response.text': 'Hello from stream!',
'gen_ai.system': 'anthropic',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
}),
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
expect.objectContaining({
data: expect.objectContaining({
'http.request.method': 'POST',
'http.request.method_original': 'POST',
'http.response.status_code': 200,
'otel.kind': 'CLIENT',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.otel.node_fetch',
'url.path': '/anthropic/v1/messages',
'url.query': '',
'url.scheme': 'http',
'user_agent.original': 'Anthropic/JS 0.63.0',
}),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'ok',
}),
// Sixth - messages.stream
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.stream': true,
}),
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
]),
};
const EXPECTED_TRANSACTION_WITH_OPTIONS = {
transaction: 'main',
spans: expect.arrayContaining([
// Check that custom options are respected
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.request.messages': expect.any(String), // Should include messages when recordInputs: true
'gen_ai.response.text': expect.any(String), // Should include response text when recordOutputs: true
}),
}),
// Check token counting with options
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'gen_ai.request.messages': expect.any(String), // Should include messages when recordInputs: true
'gen_ai.response.text': '15', // Present because recordOutputs=true is set in options
}),
op: 'gen_ai.messages',
}),
// Check models.retrieve with options
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'models',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'claude-3-haiku-20240307',
'gen_ai.response.model': 'claude-3-haiku-20240307',
}),
op: 'gen_ai.models',
description: 'models claude-3-haiku-20240307',
}),
]),
};
const EXPECTED_MODEL_ERROR = {
exception: {
values: [
{
type: 'Error',
value: '404 Model not found',
},
],
},
};
const EXPECTED_STREAM_EVENT_HANDLER_MESSAGE = {
message: 'stream event from user-added event listener captured',
};
createEsmAndCjsTests(__dirname, 'scenario-manual-client.mjs', 'instrument.mjs', (createRunner, test) => {
test('creates anthropic related spans when manually insturmenting client', async () => {
await createRunner()
.ignore('event')
.expect({ transaction: EXPECTED_TRANSACTION_DEFAULT_PII_FALSE })
.start()
.completed();
});
});
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
test('creates anthropic related spans with sendDefaultPii: false', async () => {
await createRunner()
.expect({ event: EXPECTED_MODEL_ERROR })
.expect({ transaction: EXPECTED_TRANSACTION_DEFAULT_PII_FALSE })
.expect({ event: EXPECTED_STREAM_EVENT_HANDLER_MESSAGE })
.start()
.completed();
});
});
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('creates anthropic related spans with sendDefaultPii: true', async () => {
await createRunner()
.expect({ event: EXPECTED_MODEL_ERROR })
.expect({ transaction: EXPECTED_TRANSACTION_DEFAULT_PII_TRUE })
.expect({ event: EXPECTED_STREAM_EVENT_HANDLER_MESSAGE })
.start()
.completed();
});
});
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument-with-options.mjs', (createRunner, test) => {
test('creates anthropic related spans with custom options', async () => {
await createRunner()
.expect({ event: EXPECTED_MODEL_ERROR })
.expect({ transaction: EXPECTED_TRANSACTION_WITH_OPTIONS })
.expect({ event: EXPECTED_STREAM_EVENT_HANDLER_MESSAGE })
.start()
.completed();
});
});
const EXPECTED_STREAM_SPANS_PII_FALSE = {
transaction: 'main',
spans: expect.arrayContaining([
// messages.create with stream: true
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.system': 'anthropic',
'gen_ai.operation.name': 'messages',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.stream': true,
'gen_ai.response.streaming': true,
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'msg_stream_1',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
'gen_ai.response.finish_reasons': '["end_turn"]',
}),
}),
// messages.stream
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.system': 'anthropic',
'gen_ai.operation.name': 'messages',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.streaming': true,
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'msg_stream_1',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
}),
}),
// messages.stream with redundant stream: true param
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.system': 'anthropic',
'gen_ai.operation.name': 'messages',
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.request.stream': true,
'gen_ai.response.streaming': true,
'gen_ai.response.model': 'claude-3-haiku-20240307',
'gen_ai.response.id': 'msg_stream_1',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
}),
}),
]),
};
const EXPECTED_STREAM_SPANS_PII_TRUE = {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.response.streaming': true,
// streamed text concatenated
'gen_ai.response.text': 'Hello from stream!',
}),
}),
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.response.streaming': true,
'gen_ai.response.text': 'Hello from stream!',
}),
}),
expect.objectContaining({
description: 'messages claude-3-haiku-20240307 stream-response',
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.response.streaming': true,
'gen_ai.response.text': 'Hello from stream!',
}),
}),
]),
};
createEsmAndCjsTests(__dirname, 'scenario-stream.mjs', 'instrument.mjs', (createRunner, test) => {
test('streams produce spans with token usage and metadata (PII false)', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_STREAM_SPANS_PII_FALSE }).start().completed();
});
});
createEsmAndCjsTests(__dirname, 'scenario-stream.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('streams record response text when PII true', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_STREAM_SPANS_PII_TRUE }).start().completed();
});
});
// Non-streaming tool calls + available tools (PII true)
createEsmAndCjsTests(__dirname, 'scenario-tools.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('non-streaming sets available tools and tool calls with PII', async () => {
const EXPECTED_TOOLS_JSON =
'[{"name":"weather","description":"Get the weather by city","input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}]';
const EXPECTED_TOOL_CALLS_JSON =
'[{"type":"tool_use","id":"tool_weather_1","name":"weather","input":{"city":"Paris"}}]';
await createRunner()
.ignore('event')
.expect({
transaction: {
spans: expect.arrayContaining([
expect.objectContaining({
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.request.available_tools': EXPECTED_TOOLS_JSON,
'gen_ai.response.tool_calls': EXPECTED_TOOL_CALLS_JSON,
}),
}),
]),
},
})
.start()
.completed();
});
});
// Streaming tool calls + available tools (PII true)
createEsmAndCjsTests(__dirname, 'scenario-stream-tools.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('streaming sets available tools and tool calls with PII', async () => {
const EXPECTED_TOOLS_JSON =
'[{"name":"weather","description":"Get weather","input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}]';
const EXPECTED_TOOL_CALLS_JSON =
'[{"type":"tool_use","id":"tool_weather_2","name":"weather","input":{"city":"Paris"}}]';
await createRunner()
.ignore('event')
.expect({
transaction: {
spans: expect.arrayContaining([
expect.objectContaining({
description: expect.stringContaining('stream-response'),
op: 'gen_ai.messages',
data: expect.objectContaining({
'gen_ai.request.available_tools': EXPECTED_TOOLS_JSON,
'gen_ai.response.tool_calls': EXPECTED_TOOL_CALLS_JSON,
}),
}),
]),
},
})
.start()
.completed();
});
});
// Additional error scenarios - Streaming errors
const EXPECTED_STREAM_ERROR_SPANS = {
transaction: 'main',
spans: expect.arrayContaining([
// Error with messages.create on stream initialization
expect.objectContaining({
description: 'messages error-stream-init stream-response',
op: 'gen_ai.messages',
status: 'internal_error', // Actual status coming from the instrumentation
data: expect.objectContaining({
'gen_ai.request.model': 'error-stream-init',
'gen_ai.request.stream': true,
}),
}),
// Error with messages.stream on stream initialization
expect.objectContaining({
description: 'messages error-stream-init stream-response',
op: 'gen_ai.messages',
status: 'internal_error', // Actual status coming from the instrumentation
data: expect.objectContaining({
'gen_ai.request.model': 'error-stream-init',
}),
}),
// Error midway with messages.create on streaming - note: The stream is started successfully
// so we get a successful span with the content that was streamed before the error
expect.objectContaining({
description: 'messages error-stream-midway stream-response',
op: 'gen_ai.messages',
status: 'ok',
data: expect.objectContaining({
'gen_ai.request.model': 'error-stream-midway',
'gen_ai.request.stream': true,
'gen_ai.response.streaming': true,
'gen_ai.response.text': 'This stream will ', // We received some data before error
}),
}),
// Error midway with messages.stream - same behavior, we get a span with the streamed data
expect.objectContaining({
description: 'messages error-stream-midway stream-response',
op: 'gen_ai.messages',
status: 'ok',
data: expect.objectContaining({
'gen_ai.request.model': 'error-stream-midway',
'gen_ai.response.streaming': true,
'gen_ai.response.text': 'This stream will ', // We received some data before error
}),
}),
]),
};
createEsmAndCjsTests(__dirname, 'scenario-stream-errors.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('handles streaming errors correctly', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_STREAM_ERROR_SPANS }).start().completed();
});
});
// Additional error scenarios - Tool errors and model retrieval errors
const EXPECTED_ERROR_SPANS = {
transaction: 'main',
spans: expect.arrayContaining([
// Invalid tool format error
expect.objectContaining({
description: 'messages invalid-format',
op: 'gen_ai.messages',
status: 'internal_error',
data: expect.objectContaining({
'gen_ai.request.model': 'invalid-format',
}),
}),
// Model retrieval error
expect.objectContaining({
description: 'models nonexistent-model',
op: 'gen_ai.models',
status: 'internal_error',
data: expect.objectContaining({
'gen_ai.request.model': 'nonexistent-model',
}),
}),
// Successful tool usage (for comparison)
expect.objectContaining({
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
status: 'ok',
data: expect.objectContaining({
'gen_ai.request.model': 'claude-3-haiku-20240307',
'gen_ai.response.tool_calls': expect.stringContaining('tool_ok_1'),
}),
}),
]),
};
createEsmAndCjsTests(__dirname, 'scenario-errors.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('handles tool errors and model retrieval errors correctly', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_ERROR_SPANS }).start().completed();
});
});
createEsmAndCjsTests(
__dirname,
'scenario-message-truncation.mjs',
'instrument-with-pii.mjs',
(createRunner, test) => {
test('truncates messages when they exceed byte limit - keeps only last message and crops it', async () => {
await createRunner()
.ignore('event')
.expect({
transaction: {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
// Messages should be present (truncation happened) and should be a JSON array
'gen_ai.request.messages': expect.stringMatching(/^\[\{"role":"user","content":"C+"\}\]$/),
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
]),
},
})
.start()
.completed();
});
},
);
});