-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/metrics #36
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
Feature/metrics #36
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
94d67b7
URL parsers
konraddysput 9ca101c
Command line attribute provider
konraddysput 71245a0
Code review suggestions
konraddysput d384c5e
Merge branch 'feature/submission-urls' into feature/command-line-attr…
konraddysput 86f8262
Metrics support
konraddysput 8c92345
Nullable token
konraddysput d66cd60
Merge branch 'feature/submission-urls' into feature/command-line-attr…
konraddysput 9489faa
Merge branch 'feature/command-line-attribute-provider' into feature/m…
konraddysput 0488a4f
Accept null | undefined
konraddysput 04ed4ee
Correct import
konraddysput e960bd3
Determines typo
konraddysput 90aa99c
Code review adjustements
konraddysput 74f8659
Code review adjustements, test based on the variable and not random d…
konraddysput 9b3592d
Recursive to iterative + delay helper error on reject
konraddysput 994ce10
Added one more test case
konraddysput f130439
Rollback submission url in the test
konraddysput 837b743
Merge branch 'dev' into feature/metrics
konraddysput 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { BacktraceSessionProvider, IdGenerator } from '@backtrace/sdk-core'; | ||
| import { TimeHelper } from '@backtrace/sdk-core/lib/common/TimeHelper'; | ||
|
|
||
| export class BacktraceBrowserSessionProvider implements BacktraceSessionProvider { | ||
| /** | ||
| * Session persistence interval. If no event was send in the persistence interval | ||
| * the session is treaten as an old session. | ||
| */ | ||
| public static readonly PERSISTENCE_INTERVAL = TimeHelper.convertSecondsToMilliseconds(30 * 60); | ||
| private readonly SESSION_LAST_ACTIVE = 'backtrace-last-active'; | ||
| private readonly SESSION_GUID = 'backtrace-guid'; | ||
|
|
||
| get lastActive(): number { | ||
| return this._lastActive; | ||
| } | ||
|
|
||
| public readonly newSession: boolean = true; | ||
|
|
||
| public readonly sessionId: string = IdGenerator.uuid(); | ||
|
|
||
| private _lastActive = 0; | ||
|
|
||
| constructor() { | ||
| if (!window.localStorage) { | ||
| return; | ||
| } | ||
|
|
||
| const lastActive = this.readLastActiveTimestamp(); | ||
| if (!lastActive || TimeHelper.now() - lastActive > BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL) { | ||
| this.updateLastActiveTimestamp(); | ||
| localStorage.setItem(this.SESSION_GUID, this.sessionId); | ||
| return; | ||
| } | ||
| this._lastActive = lastActive; | ||
| this.newSession = false; | ||
| this.sessionId = localStorage.getItem(this.SESSION_GUID) as string; | ||
| } | ||
|
|
||
| public afterMetricsSubmission(): void { | ||
| this.updateLastActiveTimestamp(); | ||
| } | ||
|
|
||
| public shouldSend(): boolean { | ||
| // if the document is hidden, we shouldn't send metrics, because the open document | ||
| // is the one who is being used by the user. This condition makes sure two or more web | ||
| // browser tabs of the same app won't report the same metrics or false positive metrics. | ||
| return document.hidden === false; | ||
| } | ||
|
|
||
| private readLastActiveTimestamp(): number | undefined { | ||
| const lastActiveStringTimestamp = localStorage.getItem(this.SESSION_LAST_ACTIVE); | ||
| if (!lastActiveStringTimestamp) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const lastActive = parseInt(lastActiveStringTimestamp, 10); | ||
| if (isNaN(lastActive)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return lastActive; | ||
| } | ||
|
|
||
| public updateLastActiveTimestamp() { | ||
| this._lastActive = TimeHelper.now(); | ||
| localStorage.setItem(this.SESSION_LAST_ACTIVE, this._lastActive.toString(10)); | ||
| } | ||
| } | ||
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
51 changes: 51 additions & 0 deletions
51
packages/browser/tests/metrics/persistentSessionProviderTests.spec.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,51 @@ | ||
| import { TimeHelper } from '@backtrace/sdk-core/lib/common/TimeHelper'; | ||
| import { BacktraceBrowserSessionProvider } from '../../src/BacktraceBrowserSessionProvider'; | ||
| describe('Session provider tests', () => { | ||
| it('Should generate a new uuid on new session', () => { | ||
| const sessionProvider = new BacktraceBrowserSessionProvider(); | ||
|
|
||
| expect(sessionProvider.sessionId).toBeDefined(); | ||
| }); | ||
|
|
||
| it('Should reuse the same sessionId', () => { | ||
| const sessionProvider1 = new BacktraceBrowserSessionProvider(); | ||
| const sessionProvider2 = new BacktraceBrowserSessionProvider(); | ||
| expect(sessionProvider1.sessionId).toEqual(sessionProvider2.sessionId); | ||
| }); | ||
|
|
||
| it('Should generate a new sessionId if the lastActive timestamp is greater than persistence interval time', () => { | ||
| const fakeId = 'test'; | ||
| const lastSessionActiveDate = new Date(Date.now() - BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL - 1); | ||
| localStorage.setItem('backtrace-last-active', lastSessionActiveDate.getTime().toString(10)); | ||
| localStorage.setItem('backtrace-guid', fakeId); | ||
|
|
||
| const sessionProvider = new BacktraceBrowserSessionProvider(); | ||
| expect(sessionProvider.sessionId).toBeDefined(); | ||
| expect(sessionProvider.sessionId).not.toEqual(fakeId); | ||
| }); | ||
|
|
||
| it('Should not generate a new sessionId if the lastActive timestamp is lower than persistence interval time', () => { | ||
| const fakeId = 'test'; | ||
| const lastSessionActiveDate = new Date(Date.now() - BacktraceBrowserSessionProvider.PERSISTENCE_INTERVAL + 1); | ||
| localStorage.setItem('backtrace-last-active', lastSessionActiveDate.getTime().toString(10)); | ||
| localStorage.setItem('backtrace-guid', fakeId); | ||
|
|
||
| const sessionProvider = new BacktraceBrowserSessionProvider(); | ||
| expect(sessionProvider.sessionId).toBeDefined(); | ||
| expect(sessionProvider.sessionId).toEqual(fakeId); | ||
| }); | ||
|
|
||
| it('Should update timestamp', () => { | ||
| const timestamp = Date.now(); | ||
| jest.spyOn(TimeHelper, 'now').mockImplementation(() => { | ||
| return timestamp; | ||
| }); | ||
|
|
||
| localStorage.setItem('backtrace-last-active', new Date(2010, 1, 1, 1, 1, 1, 1).getTime().toString(10)); | ||
| const sessionProvider = new BacktraceBrowserSessionProvider(); | ||
|
|
||
| sessionProvider.afterMetricsSubmission(); | ||
|
|
||
| expect(sessionProvider.lastActive).toEqual(timestamp); | ||
| }); | ||
| }); |
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.