|
| 1 | +import { GEN_AI_PROVIDER_NAME } from '@sentry/conventions/attributes'; |
| 2 | +import { expect, it } from 'vitest'; |
| 3 | +import { |
| 4 | + GEN_AI_OPERATION_NAME_ATTRIBUTE, |
| 5 | + GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE, |
| 6 | + GEN_AI_REQUEST_MODEL_ATTRIBUTE, |
| 7 | + GEN_AI_REQUEST_STREAM_ATTRIBUTE, |
| 8 | + GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE, |
| 9 | + GEN_AI_RESPONSE_STREAMING_ATTRIBUTE, |
| 10 | + GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, |
| 11 | + GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, |
| 12 | + GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, |
| 13 | +} from '../../../../../packages/core/src/tracing/ai/gen-ai-attributes'; |
| 14 | +import { createRunner } from '../../../runner'; |
| 15 | + |
| 16 | +// These tests are not exhaustive because the instrumentation is |
| 17 | +// already tested in the core unit tests and we merely want to test |
| 18 | +// that the instrumentation does not break in our cloudflare SDK. |
| 19 | + |
| 20 | +it('traces a basic Workers AI text generation request', async ({ signal }) => { |
| 21 | + const runner = createRunner(__dirname) |
| 22 | + .ignore('event') |
| 23 | + .expect(envelope => { |
| 24 | + const transactionEvent = envelope[1]?.[0]?.[1] as any; |
| 25 | + |
| 26 | + // The transaction event is framework-generated and carries non-deterministic fields |
| 27 | + // (random ports, ids, timestamps, sdk version), so we assert the stable subset. |
| 28 | + expect(transactionEvent).toEqual( |
| 29 | + expect.objectContaining({ |
| 30 | + type: 'transaction', |
| 31 | + transaction: 'GET /', |
| 32 | + transaction_info: { source: 'route' }, |
| 33 | + contexts: expect.objectContaining({ |
| 34 | + trace: expect.objectContaining({ |
| 35 | + op: 'http.server', |
| 36 | + origin: 'auto.http.cloudflare', |
| 37 | + status: 'ok', |
| 38 | + }), |
| 39 | + }), |
| 40 | + spans: [ |
| 41 | + expect.objectContaining({ |
| 42 | + description: 'chat @cf/meta/llama-3.1-8b-instruct', |
| 43 | + op: 'gen_ai.chat', |
| 44 | + origin: 'auto.ai.cloudflare.workers_ai', |
| 45 | + data: { |
| 46 | + 'sentry.origin': 'auto.ai.cloudflare.workers_ai', |
| 47 | + 'sentry.op': 'gen_ai.chat', |
| 48 | + [GEN_AI_PROVIDER_NAME]: 'cloudflare.workers_ai', |
| 49 | + [GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'chat', |
| 50 | + [GEN_AI_REQUEST_MODEL_ATTRIBUTE]: '@cf/meta/llama-3.1-8b-instruct', |
| 51 | + [GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE]: 0.7, |
| 52 | + [GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE]: 100, |
| 53 | + [GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 12, |
| 54 | + [GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE]: 7, |
| 55 | + [GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 19, |
| 56 | + }, |
| 57 | + }), |
| 58 | + ], |
| 59 | + }), |
| 60 | + ); |
| 61 | + }) |
| 62 | + .start(signal); |
| 63 | + await runner.makeRequest('get', '/'); |
| 64 | + await runner.completed(); |
| 65 | +}); |
| 66 | + |
| 67 | +it('traces a streaming Workers AI text generation request', async ({ signal }) => { |
| 68 | + const runner = createRunner(__dirname) |
| 69 | + .ignore('event') |
| 70 | + .expect(envelope => { |
| 71 | + const transactionEvent = envelope[1]?.[0]?.[1] as any; |
| 72 | + |
| 73 | + expect(transactionEvent).toEqual( |
| 74 | + expect.objectContaining({ |
| 75 | + type: 'transaction', |
| 76 | + transaction: 'GET /stream', |
| 77 | + transaction_info: { source: 'url' }, |
| 78 | + contexts: expect.objectContaining({ |
| 79 | + trace: expect.objectContaining({ |
| 80 | + op: 'http.server', |
| 81 | + origin: 'auto.http.cloudflare', |
| 82 | + status: 'ok', |
| 83 | + }), |
| 84 | + }), |
| 85 | + spans: [ |
| 86 | + expect.objectContaining({ |
| 87 | + description: 'chat @cf/meta/llama-3.1-8b-instruct', |
| 88 | + op: 'gen_ai.chat', |
| 89 | + origin: 'auto.ai.cloudflare.workers_ai', |
| 90 | + data: { |
| 91 | + 'sentry.origin': 'auto.ai.cloudflare.workers_ai', |
| 92 | + 'sentry.op': 'gen_ai.chat', |
| 93 | + [GEN_AI_PROVIDER_NAME]: 'cloudflare.workers_ai', |
| 94 | + [GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'chat', |
| 95 | + [GEN_AI_REQUEST_MODEL_ATTRIBUTE]: '@cf/meta/llama-3.1-8b-instruct', |
| 96 | + [GEN_AI_REQUEST_STREAM_ATTRIBUTE]: true, |
| 97 | + [GEN_AI_RESPONSE_STREAMING_ATTRIBUTE]: true, |
| 98 | + [GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 12, |
| 99 | + [GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE]: 7, |
| 100 | + [GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 19, |
| 101 | + }, |
| 102 | + }), |
| 103 | + ], |
| 104 | + }), |
| 105 | + ); |
| 106 | + }) |
| 107 | + .start(signal); |
| 108 | + await runner.makeRequest('get', '/stream'); |
| 109 | + await runner.completed(); |
| 110 | +}); |
| 111 | + |
| 112 | +// The Workers AI integration deliberately does not call `captureException` itself. |
| 113 | +// When a `run` call fails, the error must bubble up out of the fetch handler and be |
| 114 | +// reported by the top-level Cloudflare instrumentation instead — so it shows up in |
| 115 | +// Sentry exactly once, with the `auto.http.cloudflare` mechanism. |
| 116 | +it('bubbles up Workers AI errors to be captured by the top-level handler', async ({ signal }) => { |
| 117 | + const runner = createRunner(__dirname) |
| 118 | + // A failing run still produces a (sampled) transaction; we only care about the error event here. |
| 119 | + .ignore('transaction') |
| 120 | + .expect(envelope => { |
| 121 | + const errorEvent = envelope[1]?.[0]?.[1] as any; |
| 122 | + |
| 123 | + expect(errorEvent).toEqual( |
| 124 | + expect.objectContaining({ |
| 125 | + level: 'error', |
| 126 | + exception: { |
| 127 | + values: [ |
| 128 | + expect.objectContaining({ |
| 129 | + type: 'Error', |
| 130 | + value: 'Model not found', |
| 131 | + stacktrace: { |
| 132 | + frames: expect.any(Array), |
| 133 | + }, |
| 134 | + mechanism: { type: 'auto.http.cloudflare', handled: false }, |
| 135 | + }), |
| 136 | + ], |
| 137 | + }, |
| 138 | + request: expect.objectContaining({ |
| 139 | + method: 'GET', |
| 140 | + url: expect.any(String), |
| 141 | + }), |
| 142 | + }), |
| 143 | + ); |
| 144 | + }) |
| 145 | + .start(signal); |
| 146 | + await runner.makeRequest('get', '/error', { expectError: true }); |
| 147 | + await runner.completed(); |
| 148 | +}); |
0 commit comments