-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(e2e): Look up events via the organization trace endpoint #23371
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
39 changes: 8 additions & 31 deletions
39
...ages/e2e-tests/test-applications/node-express-send-to-sentry/tests/send-to-sentry.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 |
|---|---|---|
| @@ -1,45 +1,22 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| const EVENT_POLLING_TIMEOUT = 90_000; | ||
|
|
||
| const authToken = process.env.E2E_TEST_AUTH_TOKEN; | ||
| const sentryTestOrgSlug = process.env.E2E_TEST_SENTRY_ORG_SLUG; | ||
| const sentryTestProject = process.env.E2E_TEST_SENTRY_PROJECT; | ||
| import { EVENT_POLLING_OPTIONS, findErrorInTrace, findTransactionInTrace } from './utils/sentry-api'; | ||
|
|
||
| test('Sends exception to Sentry', async ({ baseURL }) => { | ||
| const response = await fetch(`${baseURL}/test-error`); | ||
| const { exceptionId } = await response.json(); | ||
|
|
||
| const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${exceptionId}/`; | ||
| const { exceptionId, traceId } = await response.json(); | ||
|
|
||
| console.log(`Polling for error eventId: ${exceptionId}`); | ||
| console.log(`Polling for error eventId: ${exceptionId} in trace: ${traceId}`); | ||
|
|
||
| await expect | ||
| .poll( | ||
| async () => { | ||
| const response = await fetch(url, { headers: { Authorization: `Bearer ${authToken}` } }); | ||
| return response.status; | ||
| }, | ||
| { timeout: EVENT_POLLING_TIMEOUT }, | ||
| ) | ||
| .toBe(200); | ||
| await expect.poll(() => findErrorInTrace(traceId, exceptionId), EVENT_POLLING_OPTIONS).toBeDefined(); | ||
| }); | ||
|
|
||
| test('Sends transaction to Sentry', async ({ baseURL }) => { | ||
| const response = await fetch(`${baseURL}/test-transaction`); | ||
| const { transactionId } = await response.json(); | ||
|
|
||
| const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${transactionId}/`; | ||
| const { transactionId, traceId } = await response.json(); | ||
|
|
||
| console.log(`Polling for transaction eventId: ${transactionId}`); | ||
| console.log(`Polling for transaction eventId: ${transactionId} in trace: ${traceId}`); | ||
|
|
||
| await expect | ||
| .poll( | ||
| async () => { | ||
| const response = await fetch(url, { headers: { Authorization: `Bearer ${authToken}` } }); | ||
| return response.status; | ||
| }, | ||
| { timeout: EVENT_POLLING_TIMEOUT }, | ||
| ) | ||
| .toBe(200); | ||
| .poll(() => findTransactionInTrace(traceId, transactionId), EVENT_POLLING_OPTIONS) | ||
| .toMatchObject({ op: 'e2e-test' }); | ||
| }); |
72 changes: 72 additions & 0 deletions
72
...ackages/e2e-tests/test-applications/node-express-send-to-sentry/tests/utils/sentry-api.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,72 @@ | ||
| const authToken = process.env.E2E_TEST_AUTH_TOKEN; | ||
| const sentryTestOrgSlug = process.env.E2E_TEST_SENTRY_ORG_SLUG; | ||
|
|
||
| /** | ||
| * Spans only become queryable once they have made it through to EAP, which takes | ||
| * noticeably longer than the error pipeline (~2min vs ~20s when this was measured). | ||
| */ | ||
| export const EVENT_POLLING_OPTIONS = { timeout: 180_000, intervals: [5_000] }; | ||
|
|
||
| /** | ||
| * A node of the span tree returned by the organization trace endpoint. Spans, errors and | ||
| * occurrences all share this shape and are discriminated by `event_type`. | ||
| */ | ||
| export interface TraceItem { | ||
| event_id?: string; | ||
| /** On spans this is the event id of the transaction the span belongs to. */ | ||
| transaction_id?: string; | ||
| event_type?: 'span' | 'error' | 'occurrence' | 'uptime_check'; | ||
| op?: string; | ||
| is_transaction?: boolean; | ||
| children?: TraceItem[]; | ||
| errors?: TraceItem[]; | ||
| occurrences?: TraceItem[]; | ||
| } | ||
|
|
||
| export async function fetchTrace(traceId: string): Promise<TraceItem[]> { | ||
| const response = await fetch( | ||
| `https://sentry.io/api/0/organizations/${sentryTestOrgSlug}/trace/${traceId}/?statsPeriod=1h`, | ||
| { headers: { Authorization: `Bearer ${authToken}` } }, | ||
| ); | ||
|
|
||
| // The trace endpoint is org scoped, so the auth token needs `org:read` on top of the | ||
| // project scopes the other assertions rely on. That never resolves by waiting, so fail | ||
| // loudly instead of polling until the timeout and reporting it as a missing event. | ||
| if (response.status === 401 || response.status === 403) { | ||
| throw new Error( | ||
| `Trace lookup for ${traceId} was rejected with ${response.status}: ${await response.text()}. ` + | ||
| 'E2E_TEST_AUTH_TOKEN needs the `org:read` scope.', | ||
| ); | ||
| } | ||
|
|
||
| // Empty traces and the occasional rate limit are expected while polling, so treat anything | ||
| // else that is not a success as "not there yet" -- but log it, since a rejected request and | ||
| // a trace that has not landed are otherwise indistinguishable. | ||
| if (!response.ok) { | ||
| console.log(`Trace lookup for ${traceId} returned ${response.status}: ${await response.text()}`); | ||
| return []; | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
|
||
| /** | ||
| * Errors attach to whichever span was active when they were captured, and relocate from the | ||
| * top level into that span once it lands, so a given event can surface at any depth. | ||
| */ | ||
| export function flattenTrace(items: TraceItem[]): TraceItem[] { | ||
| return items.flatMap(item => [ | ||
| item, | ||
| ...flattenTrace(item.children ?? []), | ||
| ...flattenTrace(item.errors ?? []), | ||
| ...flattenTrace(item.occurrences ?? []), | ||
| ]); | ||
| } | ||
|
|
||
| export async function findErrorInTrace(traceId: string, eventId: string): Promise<TraceItem | undefined> { | ||
| return flattenTrace(await fetchTrace(traceId)).find(item => item.event_type === 'error' && item.event_id === eventId); | ||
| } | ||
|
|
||
| export async function findTransactionInTrace(traceId: string, eventId: string): Promise<TraceItem | undefined> { | ||
| return flattenTrace(await fetchTrace(traceId)).find(item => item.is_transaction && item.transaction_id === eventId); | ||
| } | ||
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
10 changes: 8 additions & 2 deletions
10
dev-packages/e2e-tests/test-applications/react-send-to-sentry/src/globals.d.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 |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| interface RecordedEvent { | ||
| eventId: string; | ||
| traceId: string; | ||
| op?: string; | ||
| } | ||
|
|
||
| interface Window { | ||
| recordedTransactions?: string[]; | ||
| capturedExceptionId?: string; | ||
| recordedTransactions?: RecordedEvent[]; | ||
| capturedException?: RecordedEvent; | ||
| sentryReplayId?: string; | ||
| } |
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
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.
Bug: The
findTransactionInTracefunction incorrectly usestransaction_idinstead ofevent_idto find a transaction node, which will cause the lookup to always fail.Severity: MEDIUM
Suggested Fix
In the
findTransactionInTracefunction, change the condition to find the transaction by itsevent_id. Modify thefindcall fromitem => item.is_transaction && item.transaction_id === eventIdtoitem => item.is_transaction && item.event_id === eventId.Prompt for AI Agent
Also affects:
dev-packages/e2e-tests/test-applications/react-send-to-sentry/tests/utils/sentry-api.ts:70~72Did we get this right? 👍 / 👎 to inform future reviews.