-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Trace continuation from server-timing headers #18673
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const errorBtn = document.getElementById('errorBtn'); | ||
| errorBtn.addEventListener('click', () => { | ||
| throw new Error(`Sentry Test Error ${Math.random()}`); | ||
| }); | ||
|
|
||
| const fetchBtn = document.getElementById('fetchBtn'); | ||
| fetchBtn.addEventListener('click', async () => { | ||
| await fetch('http://sentry-test-site.example'); | ||
| }); | ||
|
|
||
| const xhrBtn = document.getElementById('xhrBtn'); | ||
| xhrBtn.addEventListener('click', () => { | ||
| const xhr = new XMLHttpRequest(); | ||
| xhr.open('GET', 'http://sentry-test-site.example'); | ||
| xhr.send(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="errorBtn">Throw Error</button> | ||
| <button id="fetchBtn">Fetch Request</button> | ||
| <button id="xhrBtn">XHR Request</button> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import type { EventAndTraceHeader } from '../../../../utils/helpers'; | ||
| import { | ||
| eventAndTraceHeaderRequestParser, | ||
| getFirstSentryEnvelopeRequest, | ||
| shouldSkipTracingTest, | ||
| } from '../../../../utils/helpers'; | ||
|
|
||
| const META_TAG_TRACE_ID = '12345678901234567890123456789012'; | ||
| const META_TAG_PARENT_SPAN_ID = '1234567890123456'; | ||
| const META_TAG_BAGGAGE = | ||
| 'sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-sampled=true,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod,sentry-sample_rand=0.42'; | ||
|
|
||
| sentryTest( | ||
| 'create a new trace for a navigation after the server timing headers', | ||
| async ({ getLocalTestUrl, page, enableConsole }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| enableConsole(); | ||
|
|
||
| const url = await getLocalTestUrl({ | ||
| testDir: __dirname, | ||
| responseHeaders: { | ||
| 'Server-Timing': `sentry-trace;desc=${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}-1, baggage;desc="${META_TAG_BAGGAGE}"`, | ||
| }, | ||
| }); | ||
|
|
||
| const [pageloadEvent, pageloadTraceHeader] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>( | ||
| page, | ||
| url, | ||
| eventAndTraceHeaderRequestParser, | ||
| ); | ||
| const [navigationEvent, navigationTraceHeader] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>( | ||
| page, | ||
| `${url}#foo`, | ||
| eventAndTraceHeaderRequestParser, | ||
| ); | ||
|
|
||
| const pageloadTraceContext = pageloadEvent.contexts?.trace; | ||
| const navigationTraceContext = navigationEvent.contexts?.trace; | ||
|
|
||
| expect(pageloadEvent.type).toEqual('transaction'); | ||
| expect(pageloadTraceContext).toMatchObject({ | ||
| op: 'pageload', | ||
| trace_id: META_TAG_TRACE_ID, | ||
| parent_span_id: META_TAG_PARENT_SPAN_ID, | ||
| span_id: expect.stringMatching(/^[\da-f]{16}$/), | ||
| }); | ||
|
|
||
| expect(pageloadTraceHeader).toEqual({ | ||
| environment: 'prod', | ||
| release: '1.0.0', | ||
| sample_rate: '0.2', | ||
| sampled: 'true', | ||
| transaction: 'my-transaction', | ||
| public_key: 'public', | ||
| trace_id: META_TAG_TRACE_ID, | ||
| sample_rand: '0.42', | ||
| }); | ||
|
|
||
| expect(navigationEvent.type).toEqual('transaction'); | ||
| expect(navigationTraceContext).toMatchObject({ | ||
| op: 'navigation', | ||
| trace_id: expect.stringMatching(/^[\da-f]{32}$/), | ||
| span_id: expect.stringMatching(/^[\da-f]{16}$/), | ||
| }); | ||
| // navigation span is head of trace, so there's no parent span: | ||
| expect(navigationTraceContext).not.toHaveProperty('parent_span_id'); | ||
|
|
||
| expect(navigationTraceHeader).toEqual({ | ||
| environment: 'production', | ||
| public_key: 'public', | ||
| sample_rate: '1', | ||
| sampled: 'true', | ||
| trace_id: navigationTraceContext?.trace_id, | ||
| sample_rand: expect.any(String), | ||
| }); | ||
|
|
||
| expect(pageloadTraceContext?.trace_id).not.toEqual(navigationTraceContext?.trace_id); | ||
| expect(pageloadTraceHeader?.sample_rand).not.toEqual(navigationTraceHeader?.sample_rand); | ||
| }, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| // in browser TwP means not setting tracesSampleRate but adding browserTracingIntegration, | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [Sentry.browserTracingIntegration()], | ||
| tracePropagationTargets: ['http://sentry-test-site.example'], | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="errorBtn">Throw Error</button> | ||
| </body> | ||
| </html> | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import type { EventAndTraceHeader } from '../../../../utils/helpers'; | ||
| import { | ||
| eventAndTraceHeaderRequestParser, | ||
| getFirstSentryEnvelopeRequest, | ||
| shouldSkipTracingTest, | ||
| } from '../../../../utils/helpers'; | ||
|
|
||
| const META_TAG_TRACE_ID = '12345678901234567890123456789012'; | ||
| const META_TAG_PARENT_SPAN_ID = '1234567890123456'; | ||
| const META_TAG_BAGGAGE = | ||
| 'sentry-trace_id=12345678901234567890123456789012,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod,sentry-sample_rand=0.42'; | ||
|
|
||
| sentryTest('error on initial page has traceId from server timing headers', async ({ getLocalTestUrl, page }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ | ||
| testDir: __dirname, | ||
| responseHeaders: { | ||
| 'Server-Timing': `sentry-trace;desc=${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}, baggage;desc="${META_TAG_BAGGAGE}"`, | ||
| }, | ||
| }); | ||
| await page.goto(url); | ||
|
|
||
| const errorEventPromise = getFirstSentryEnvelopeRequest<EventAndTraceHeader>( | ||
| page, | ||
| undefined, | ||
| eventAndTraceHeaderRequestParser, | ||
| ); | ||
|
|
||
| await page.locator('#errorBtn').click(); | ||
| const [errorEvent, errorTraceHeader] = await errorEventPromise; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing subject.js causes test to failMedium Severity · Bugbot Rules The Additional Locations (1) |
||
|
|
||
| expect(errorEvent.type).toEqual(undefined); | ||
| expect(errorEvent.contexts?.trace).toEqual({ | ||
| trace_id: META_TAG_TRACE_ID, | ||
| parent_span_id: META_TAG_PARENT_SPAN_ID, | ||
| span_id: expect.stringMatching(/^[\da-f]{16}$/), | ||
| }); | ||
|
|
||
| expect(errorTraceHeader).toEqual({ | ||
| environment: 'prod', | ||
| public_key: 'public', | ||
| release: '1.0.0', | ||
| trace_id: META_TAG_TRACE_ID, | ||
| sample_rand: '0.42', | ||
| }); | ||
| }); | ||
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.
Cursor pointed out that the type/logic was wrong in this expection...