-
Notifications
You must be signed in to change notification settings - Fork 4
Browser breadcrumbs support #64
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
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
170 changes: 170 additions & 0 deletions
170
packages/browser/src/breadcrumbs/DocumentEventSubscriber.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,170 @@ | ||
| import { | ||
| BreadcrumbLogLevel, | ||
| BreadcrumbsEventSubscriber, | ||
| BreadcrumbsManager, | ||
| BreadcrumbType, | ||
| } from '@backtrace/sdk-core'; | ||
|
|
||
| export class DocumentEventSubscriber implements BreadcrumbsEventSubscriber { | ||
| private readonly _controller: AbortController = new AbortController(); | ||
|
|
||
| public start(breadcrumbsManager: BreadcrumbsManager): void { | ||
| const signal = this._controller.signal; | ||
| document.addEventListener( | ||
| 'click', | ||
| (mouseEvent: MouseEvent) => { | ||
| const target = mouseEvent.target as Element; | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| breadcrumbsManager.addBreadcrumb( | ||
| `Clicked ${target.id} ${target.tagName}`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.User, | ||
| { | ||
| id: target.id, | ||
| class: target.className, | ||
| name: target.tagName, | ||
| text: (target as { text?: string })?.text, | ||
| }, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
|
|
||
| document.addEventListener( | ||
| 'dblclick', | ||
| (mouseEvent: MouseEvent) => { | ||
| const target = mouseEvent.target as Element; | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| breadcrumbsManager.addBreadcrumb( | ||
| `Double clicked ${target.id} ${target.tagName}`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.User, | ||
| { | ||
| id: target.id, | ||
| class: target.className, | ||
| name: target.tagName, | ||
| text: (target as { text?: string })?.text, | ||
| }, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
|
|
||
| document.addEventListener( | ||
| 'drag', | ||
| (dragEvent: DragEvent) => { | ||
| const target = dragEvent.target as Element; | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| breadcrumbsManager.addBreadcrumb( | ||
| `An element ${target.id} ${target.tagName} is being dragged`, | ||
| BreadcrumbLogLevel.Debug, | ||
| BreadcrumbType.User, | ||
| { | ||
| id: target.id, | ||
| class: target.className, | ||
| name: target.tagName, | ||
| }, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
|
|
||
| document.addEventListener( | ||
| 'drop', | ||
| (dragEvent: DragEvent) => { | ||
| const target = dragEvent.target as Element; | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| breadcrumbsManager.addBreadcrumb( | ||
| `A dragged element is dropped on the target ${target.id} ${target.tagName}`, | ||
| BreadcrumbLogLevel.Debug, | ||
| BreadcrumbType.User, | ||
| { | ||
| id: target.id, | ||
| class: target.className, | ||
| name: target.tagName, | ||
| }, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
|
|
||
| window.addEventListener('load', () => { | ||
| breadcrumbsManager.addBreadcrumb(`The page has loaded`, BreadcrumbLogLevel.Info, BreadcrumbType.System); | ||
| }); | ||
|
|
||
| window.addEventListener('unload', () => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `The page started unloading`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.System, | ||
| ); | ||
| }); | ||
|
|
||
| window.addEventListener('pagehide', () => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| 'User navigates away from a webpage', | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.User, | ||
| ); | ||
| }); | ||
|
|
||
| window.addEventListener('pageshow', () => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| 'User navigates to a webpage', | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.User, | ||
| ); | ||
| }); | ||
|
|
||
| window.addEventListener( | ||
| 'online', | ||
| () => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `The browser starts working online`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.System, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
|
|
||
| window.addEventListener( | ||
| 'offline', | ||
| () => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `The browser starts working offline `, | ||
| BreadcrumbLogLevel.Warning, | ||
| BreadcrumbType.System, | ||
| ); | ||
| }, | ||
| { | ||
| signal, | ||
| }, | ||
| ); | ||
| } | ||
| public dispose(): void { | ||
| this._controller.abort(); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
packages/browser/src/breadcrumbs/HistoryEventSubscriber.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,58 @@ | ||
| import { | ||
| BreadcrumbLogLevel, | ||
| BreadcrumbsEventSubscriber, | ||
| BreadcrumbsManager, | ||
| BreadcrumbType, | ||
| } from '@backtrace/sdk-core'; | ||
|
|
||
| export class HistoryEventSubscriber implements BreadcrumbsEventSubscriber { | ||
| private _abortController = new AbortController(); | ||
| private _originalHistoryPushStateMethod?: typeof history.pushState; | ||
| public start(breadcrumbsManager: BreadcrumbsManager): void { | ||
| if ((breadcrumbsManager.breadcrumbsType & BreadcrumbType.Navigation) !== BreadcrumbType.Navigation) { | ||
| return; | ||
| } | ||
| window.addEventListener( | ||
| 'popstate', | ||
| (event: PopStateEvent) => { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `Navigating back to ${document.location}`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.Navigation, | ||
| { | ||
| location: document.location.toString(), | ||
| state: event.state, | ||
| }, | ||
| ); | ||
| }, | ||
| { | ||
| signal: this._abortController.signal, | ||
| }, | ||
| ); | ||
|
|
||
| const originalHistoryPushStateMethod = history.pushState; | ||
| history.pushState = (...args) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const [data, _, url] = args; | ||
| originalHistoryPushStateMethod.apply(history, args); | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `Navigating to ${document.location}`, | ||
| BreadcrumbLogLevel.Info, | ||
| BreadcrumbType.Navigation, | ||
| { | ||
| url: url?.toString(), | ||
| data, | ||
| location: document.location.toString(), | ||
| }, | ||
| ); | ||
| }; | ||
| this._originalHistoryPushStateMethod = originalHistoryPushStateMethod; | ||
| } | ||
|
|
||
| public dispose(): void { | ||
| this._abortController.abort(); | ||
| if (this._originalHistoryPushStateMethod) { | ||
| history.pushState = this._originalHistoryPushStateMethod; | ||
| } | ||
| } | ||
| } |
96 changes: 96 additions & 0 deletions
96
packages/browser/src/breadcrumbs/WebRequestEventSubscriber.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 { | ||
| BreadcrumbLogLevel, | ||
| BreadcrumbsEventSubscriber, | ||
| BreadcrumbsManager, | ||
| BreadcrumbType, | ||
| } from '@backtrace/sdk-core'; | ||
|
|
||
| export class WebRequestEventSubscriber implements BreadcrumbsEventSubscriber { | ||
| private _xmlHttpRequestOriginalOpenMethod?: typeof XMLHttpRequest.prototype.open; | ||
| private _fetchOriginalMethod?: typeof window.fetch; | ||
|
|
||
| public start(breadcrumbsManager: BreadcrumbsManager): void { | ||
| if ((breadcrumbsManager.breadcrumbsType & BreadcrumbType.Http) !== BreadcrumbType.Http) { | ||
| return; | ||
| } | ||
| const xmlHttpRequestOriginalOpenMethod = XMLHttpRequest.prototype.open; | ||
|
|
||
| XMLHttpRequest.prototype.open = function ( | ||
| method: string, | ||
| url: string | URL, | ||
| async?: boolean, | ||
| username?: string | null, | ||
| password?: string | null, | ||
| ) { | ||
| const readyStateChangeCallback = this.onreadystatechange; | ||
| this.onreadystatechange = (event: Event) => { | ||
| if (this.readyState === XMLHttpRequest.DONE) { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `Sent an HTTP ${method} request to ${url}. Response status code: ${this.status}`, | ||
| BreadcrumbLogLevel.Debug, | ||
| BreadcrumbType.Http, | ||
| { | ||
| method, | ||
| url: url.toString(), | ||
| statusCode: this.status, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| readyStateChangeCallback?.apply(this, [event]); | ||
| }; | ||
|
|
||
| xmlHttpRequestOriginalOpenMethod.call(this, method, url, async || true, username, password); | ||
| }; | ||
|
|
||
| this._xmlHttpRequestOriginalOpenMethod = xmlHttpRequestOriginalOpenMethod; | ||
|
|
||
| const fetchOriginalMethod = window.fetch; | ||
|
|
||
| window.fetch = async function (resource, config) { | ||
| const method = config?.method ?? 'GET'; | ||
| const attributes = { | ||
| url: resource.toString(), | ||
| method: method, | ||
| referrer: config?.referrer, | ||
| }; | ||
|
|
||
| try { | ||
| const result = await fetchOriginalMethod(resource, config); | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `Sent an HTTP ${method} request to ${resource}. Response status code: ${result.status}`, | ||
| BreadcrumbLogLevel.Debug, | ||
| BreadcrumbType.Http, | ||
| { | ||
| ...attributes, | ||
| statusCode: result.status, | ||
| }, | ||
| ); | ||
|
|
||
| return result; | ||
| } catch (e) { | ||
| breadcrumbsManager.addBreadcrumb( | ||
| `HTTP ${method} failure on request to ${resource}. Reason: ${ | ||
| e instanceof Error ? e.message : e?.toString() ?? 'unknown' | ||
| }`, | ||
| BreadcrumbLogLevel.Warning, | ||
| BreadcrumbType.Http, | ||
| attributes, | ||
| ); | ||
| throw e; | ||
| } | ||
| }; | ||
|
|
||
| this._fetchOriginalMethod = fetchOriginalMethod; | ||
| } | ||
|
|
||
| public dispose(): void { | ||
| if (this._fetchOriginalMethod) { | ||
| window.fetch = this._fetchOriginalMethod; | ||
| } | ||
|
|
||
| if (this._xmlHttpRequestOriginalOpenMethod) { | ||
| XMLHttpRequest.prototype.open = this._xmlHttpRequestOriginalOpenMethod; | ||
| } | ||
| } | ||
| } | ||
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.