-
Notifications
You must be signed in to change notification settings - Fork 166
Replace NewRelic browser instrumentation with custom error handler #8950
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
Changes from all commits
7812b78
6f46a45
575e2b4
2b4d20e
914adef
01ad0e1
6470b0b
223a63d
164cb8a
106fd69
7ff899a
da0edb2
625de7e
e59f074
ff757a4
f2f1da4
c45092f
80d18c6
51f3e01
33f4f68
36cb9d6
c9d605c
13d961e
1f7ee55
cffb342
4431b94
4bdf111
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,37 @@ | ||
| import isTrackableErrorEvent from './is-trackable-error-event'; | ||
|
|
||
| describe('isTrackableErrorEvent', () => { | ||
| context('with filename not present on event', () => { | ||
| const event = new ErrorEvent('error'); | ||
|
|
||
| it('returns false', () => { | ||
| expect(isTrackableErrorEvent(event)).to.be.false(); | ||
| }); | ||
| }); | ||
|
|
||
| context('with filename as an invalid url', () => { | ||
| const event = new ErrorEvent('error', { filename: '.' }); | ||
|
|
||
| it('returns false', () => { | ||
| expect(isTrackableErrorEvent(event)).to.be.false(); | ||
| }); | ||
| }); | ||
|
|
||
| context('with filename from a different host', () => { | ||
| const event = new ErrorEvent('error', { filename: 'http://different.example.com/foo.js' }); | ||
|
|
||
| it('returns false', () => { | ||
| expect(isTrackableErrorEvent(event)).to.be.false(); | ||
| }); | ||
| }); | ||
|
|
||
| context('with filename from the same host', () => { | ||
| const event = new ErrorEvent('error', { | ||
| filename: new URL('foo.js', window.location.origin).toString(), | ||
| }); | ||
|
|
||
| it('returns true', () => { | ||
| expect(isTrackableErrorEvent(event)).to.be.true(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| function isTrackableErrorEvent(event: ErrorEvent): boolean { | ||
| try { | ||
| return new URL(event.filename).host === window.location.host; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export default isTrackableErrorEvent; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { trackError, isTrackableErrorEvent } from '@18f/identity-analytics'; | ||
|
|
||
| export interface WindowWithInitialErrors extends Window { | ||
| _e: ErrorEvent[]; | ||
| } | ||
|
|
||
| declare let window: WindowWithInitialErrors; | ||
|
|
||
| const { _e: initialErrors } = window; | ||
|
|
||
| const handleErrorEvent = (event: ErrorEvent) => | ||
| isTrackableErrorEvent(event) && trackError(event.error); | ||
| initialErrors.forEach(handleErrorEvent); | ||
| window.addEventListener('error', handleErrorEvent); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class FrontendErrorLogger | ||
| class FrontendError < StandardError; end | ||
|
|
||
| def self.track_error(name:, message:, stack:) | ||
| NewRelic::Agent.notice_error( | ||
| FrontendError.new, | ||
| expected: true, | ||
| custom_params: { frontend_error: { name:, message:, stack: } }, | ||
| ) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,8 +55,9 @@ | |
| ) %> | ||
| <meta content="#ffffff" name="theme-color" /> | ||
|
|
||
| <% if BrowserSupport.supported?(request.user_agent) && IdentityConfig.store.newrelic_browser_key.present? && IdentityConfig.store.newrelic_browser_app_id.present? %> | ||
| <%= render 'shared/newrelic/browser_instrumentation' %> | ||
| <%# Prelude script for error tracking (see `track-errors`) %> | ||
| <%= javascript_tag(nonce: true) do %> | ||
| _e=[],addEventListener("error",(e)=>_e.push(e)); | ||
| <% end %> | ||
|
|
||
| <%= yield(:head) if content_for?(:head) %> | ||
|
|
@@ -109,6 +110,7 @@ | |
| false, | ||
| ) %> | ||
| <%= javascript_packs_tag_once('application', prepend: true) %> | ||
| <%= javascript_packs_tag_once('track-errors') if BrowserSupport.supported?(request.user_agent) %> | ||
|
Contributor
Author
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. Question: Can we defer this?
Contributor
Author
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.
I think we can and should make this I still need to get some clarification about I'll plan to explore this as a follow-on task. |
||
| <%= render_javascript_pack_once_tags %> | ||
|
|
||
| <%= render 'shared/dap_analytics' if IdentityConfig.store.participate_in_dap && !session_with_trust? %> | ||
|
|
||
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.
Follow-up task: Would like to move
trackEventandtrackErrorto dedicated filestrack-event.tsandtrack-error.tsso thisindex.tscan actually be an index.