-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(browser): Add mode option for the browser session integration #18997
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
Open
JPeer264
wants to merge
5
commits into
develop
Choose a base branch
from
jp/browser-session-mode
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
38d6a15
feat(browser): Add mode option for the browser session integration
JPeer264 e32b2e4
feat: BrowserSessionIntegration available in CDN/bundle
JPeer264 f300ff8
test: Verify if only one session exists
JPeer264 2484c0f
ref: mode -> lifecycle
JPeer264 f7824f4
chore: update size-limit
JPeer264 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
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/init.js
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,9 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '0.1', | ||
| integrations: [Sentry.browserSessionIntegration({ lifecycle: 'page' })], | ||
| }); |
12 changes: 12 additions & 0 deletions
12
dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/subject.js
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,12 @@ | ||
| let clickCount = 0; | ||
|
|
||
| document.getElementById('navigate').addEventListener('click', () => { | ||
| clickCount++; | ||
| // Each click navigates to a different page | ||
| history.pushState({}, '', `/page-${clickCount}`); | ||
| }); | ||
|
|
||
| document.getElementById('manual-session').addEventListener('click', () => { | ||
| Sentry.startSession(); | ||
| Sentry.captureException('Test error from manual session'); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/template.html
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,10 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="navigate">Navigate via pushState</button> | ||
| <button id="manual-session">Manual session</button> | ||
| </body> | ||
| </html> |
72 changes: 72 additions & 0 deletions
72
dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/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,72 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { SessionContext } from '@sentry/core'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers'; | ||
|
|
||
| sentryTest('should start a session on pageload with page lifecycle.', async ({ getLocalTestUrl, page }) => { | ||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const sessions = await getMultipleSentryEnvelopeRequests<SessionContext>(page, 1, { | ||
| url, | ||
| envelopeType: 'session', | ||
| timeout: 2000, | ||
| }); | ||
|
|
||
| expect(sessions.length).toBeGreaterThanOrEqual(1); | ||
| const session = sessions[0]; | ||
| expect(session).toBeDefined(); | ||
| expect(session.init).toBe(true); | ||
| expect(session.errors).toBe(0); | ||
| expect(session.status).toBe('ok'); | ||
| }); | ||
|
|
||
| sentryTest( | ||
| 'should NOT start a new session on pushState navigation with page lifecycle.', | ||
| async ({ getLocalTestUrl, page }) => { | ||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const sessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, { | ||
| url, | ||
| envelopeType: 'session', | ||
| timeout: 4000, | ||
| }); | ||
|
|
||
| const manualSessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, { | ||
| envelopeType: 'session', | ||
| timeout: 4000, | ||
| }); | ||
|
|
||
| const eventsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, { | ||
| envelopeType: 'event', | ||
| timeout: 4000, | ||
| }); | ||
|
|
||
| await page.waitForSelector('#navigate'); | ||
|
|
||
| await page.locator('#navigate').click(); | ||
| await page.locator('#navigate').click(); | ||
| await page.locator('#navigate').click(); | ||
|
|
||
| const sessions = (await sessionsPromise).filter(session => session.init); | ||
|
|
||
| expect(sessions.length).toBe(1); | ||
| expect(sessions[0].init).toBe(true); | ||
|
|
||
| // teardown and verify if nothing else got sent | ||
| await page.locator('#manual-session').click(); | ||
|
|
||
| const newSessions = (await manualSessionsPromise).filter(session => session.init); | ||
| const events = await eventsPromise; | ||
|
|
||
| expect(newSessions.length).toBe(2); | ||
| expect(newSessions[0].init).toBe(true); | ||
| expect(newSessions[1].init).toBe(true); | ||
| expect(newSessions[1].sid).not.toBe(newSessions[0].sid); | ||
| expect(events).toEqual([ | ||
| expect.objectContaining({ | ||
| level: 'error', | ||
| message: 'Test error from manual session', | ||
| }), | ||
| ]); | ||
| }, | ||
| ); | ||
8 changes: 8 additions & 0 deletions
8
dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/init.js
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,8 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '0.1', | ||
| }); |
6 changes: 6 additions & 0 deletions
6
dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/subject.js
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,6 @@ | ||
| let clickCount = 0; | ||
|
|
||
| document.getElementById('navigate').addEventListener('click', () => { | ||
| clickCount++; | ||
| history.pushState({}, '', `/page-${clickCount}`); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/template.html
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,9 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="navigate">Navigate via pushState</button> | ||
| </body> | ||
| </html> |
27 changes: 27 additions & 0 deletions
27
dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/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,27 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { SessionContext } from '@sentry/core'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers'; | ||
|
|
||
| sentryTest( | ||
| 'should start new sessions on pushState navigation with route lifecycle (default).', | ||
| async ({ getLocalTestUrl, page }) => { | ||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const sessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, { | ||
| url, | ||
| envelopeType: 'session', | ||
| timeout: 4000, | ||
| }); | ||
|
|
||
| await page.waitForSelector('#navigate'); | ||
|
|
||
| await page.locator('#navigate').click(); | ||
| await page.locator('#navigate').click(); | ||
| await page.locator('#navigate').click(); | ||
|
|
||
| const sessions = (await sessionsPromise).filter(session => session.init); | ||
|
|
||
| expect(sessions.length).toBe(3); | ||
| }, | ||
| ); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
l: What I like to do when I test that something is not sent, is to await an event after navigating so that we have some time before ending the tests. I guess in this case, we could wait for an error or a transaction event. There's still an assumption here that the session would have been sent before that event but I think that's reasonable and better than not waiting at all or waiting for a a specific time.
We could even make it more deterministic by:
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.
Those are very valid points. I'll try to implement these