Repository navigation
test(e2e): Move OpenAI and Anthropic send-to-sentry cases into gen-ai-libraries #25841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mydea
wants to merge
1
commit into
develop
Choose a base branch
from
test/e2e-merge-ai-send-to-sentry-into-gen-ai-libraries
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−966
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
dev-packages/e2e-tests/test-applications/gen-ai-libraries/tests/extras.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import type { SerializedStreamedSpan } from '@sentry-internal/test-utils'; | ||
| import { collectStreamedSpans, getSpanOp } from '@sentry-internal/test-utils'; | ||
| import { APP } from './constants'; | ||
| import { attr, describeTree, expectCommonChatAttributes, LIBRARIES } from './utils'; | ||
|
|
||
| const OPENAI = LIBRARIES.find(library => library.id === 'openai')!; | ||
| const ANTHROPIC = LIBRARIES.find(library => library.id === 'anthropic')!; | ||
|
|
||
| /** | ||
| * Calls `/:lib/:extra` and returns its trace once the request span (the segment) has arrived. The | ||
| * segment ends last, so every model-call span of the request is in by then. | ||
| */ | ||
| async function requestExtra(baseURL: string | undefined, path: string): Promise<SerializedStreamedSpan[]> { | ||
| const spansPromise = collectStreamedSpans( | ||
| APP, | ||
| spansOfTrace => spansOfTrace.some(span => span.name === path) && spansOfTrace.some(span => span.is_segment), | ||
| ); | ||
|
|
||
| const response = await fetch(`${baseURL}/${path}`); | ||
| expect(response.status, await response.text()).toBe(200); | ||
|
|
||
| return spansPromise; | ||
| } | ||
|
|
||
| /** The only span of the trace with `op`; finding exactly one also catches a call instrumented twice. */ | ||
| function onlySpanWithOp(spans: SerializedStreamedSpan[], op: string): SerializedStreamedSpan { | ||
| const matching = spans.filter(span => getSpanOp(span) === op); | ||
| expect(matching, `expected exactly one ${op} span in:\n${describeTree(spans)}`).toHaveLength(1); | ||
| return matching[0]!; | ||
| } | ||
|
|
||
| /** `gen_ai.response.finish_reasons` is a JSON array; a streamed answer can repeat the reason per chunk. */ | ||
| function finishReasons(span: SerializedStreamedSpan): string[] { | ||
| const raw = attr(span, 'gen_ai.response.finish_reasons'); | ||
| expect(typeof raw, 'gen_ai.response.finish_reasons').toBe('string'); | ||
| return JSON.parse(raw as string); | ||
| } | ||
|
|
||
| test('openai: a streamed chat completion records its token usage', async ({ baseURL }) => { | ||
| const spans = await requestExtra(baseURL, 'openai/chat-stream'); | ||
| const span = onlySpanWithOp(spans, 'gen_ai.chat'); | ||
|
|
||
| // The usage only arrives in the final chunk, which the streaming instrumentation has to pick up. | ||
| expectCommonChatAttributes(span, OPENAI); | ||
| expect(attr(span, 'gen_ai.response.streaming')).toBe(true); | ||
| expect(finishReasons(span)).toContain('stop'); | ||
| }); | ||
|
|
||
| test('openai: a Responses API call emits a gen_ai.chat span', async ({ baseURL }) => { | ||
| const spans = await requestExtra(baseURL, 'openai/responses'); | ||
| const span = onlySpanWithOp(spans, 'gen_ai.chat'); | ||
|
|
||
| expectCommonChatAttributes(span, OPENAI); | ||
| expect(finishReasons(span)).toEqual(['completed']); | ||
| expect(attr(span, 'gen_ai.input.messages')).toContain('capital of France'); | ||
| }); | ||
|
|
||
| test('openai: an embeddings call emits a gen_ai.embeddings span', async ({ baseURL }) => { | ||
| const spans = await requestExtra(baseURL, 'openai/embeddings'); | ||
| const span = onlySpanWithOp(spans, 'gen_ai.embeddings'); | ||
|
|
||
| expect(span.name).toBe('embeddings openai/text-embedding-3-small'); | ||
| expect(span.status).toBe('ok'); | ||
| expect(attr(span, 'sentry.origin')).toBe('auto.ai.openai'); | ||
| expect(attr(span, 'gen_ai.operation.name')).toBe('embeddings'); | ||
| expect(attr(span, 'gen_ai.provider.name')).toBe('openai'); | ||
| expect(attr(span, 'gen_ai.embeddings.input')).toContain('Paris'); | ||
| expect(attr(span, 'gen_ai.usage.input_tokens')).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('anthropic: a streamed message records its output usage', async ({ baseURL }) => { | ||
| const spans = await requestExtra(baseURL, 'anthropic/chat-stream'); | ||
| const span = onlySpanWithOp(spans, 'gen_ai.chat'); | ||
|
|
||
| expect(attr(span, 'gen_ai.response.streaming')).toBe(true); | ||
| expect(finishReasons(span)).toContain('end_turn'); | ||
| expect(attr(span, 'gen_ai.usage.output_tokens')).toBeGreaterThan(0); | ||
| // The input usage is not asserted: the instrumentation takes it from `message_start`, where | ||
| // OpenRouter reports 0, and ignores the real count OpenRouter (and the Anthropic API) put in | ||
| // `message_delta`. | ||
| expect(attr(span, 'gen_ai.provider.name')).toBe(ANTHROPIC.provider); | ||
| expect(attr(span, 'sentry.origin')).toBe(ANTHROPIC.origin); | ||
| }); | ||
|
|
||
| test('anthropic: the stream helper emits a single gen_ai.chat span', async ({ baseURL }) => { | ||
| const spans = await requestExtra(baseURL, 'anthropic/stream-helper'); | ||
| // `messages.stream()` calls `create` underneath, which must not produce a second span. | ||
| const span = onlySpanWithOp(spans, 'gen_ai.chat'); | ||
|
|
||
| expect(attr(span, 'gen_ai.response.streaming')).toBe(true); | ||
| expect(finishReasons(span)).toContain('end_turn'); | ||
| expect(attr(span, 'gen_ai.usage.output_tokens')).toBeGreaterThan(0); | ||
| expect(attr(span, 'gen_ai.provider.name')).toBe(ANTHROPIC.provider); | ||
| expect(attr(span, 'sentry.origin')).toBe(ANTHROPIC.origin); | ||
| }); | ||
5 changes: 0 additions & 5 deletions
5
dev-packages/e2e-tests/test-applications/node-anthropic-send-to-sentry/.gitignore
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
dev-packages/e2e-tests/test-applications/node-anthropic-send-to-sentry/package.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded span attribute names in tests
Low Severity
New extra-route assertions compare span ops and attributes with string literals such as
gen_ai.chatandsentry.origin. This was flagged because the review rules require those values to come from@sentry/conventions. The rest of this app's tests use the same local style.Additional Locations (2)
dev-packages/e2e-tests/test-applications/gen-ai-libraries/tests/extras.test.ts#L71-L84dev-packages/e2e-tests/test-applications/gen-ai-libraries/tests/extras.test.ts#L85-L96Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit a22efbc. Configure here.