-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add onRequestSpanEnd
hook to browser tracing integration
#17884
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
logaretm
wants to merge
7
commits into
develop
Choose a base branch
from
awad/js-56-add-on-response-span-end-hook
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.
+307
−72
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b4e333
feat: added hook to browser integration settings
logaretm b019c76
refactor: hoist replay header parsing code to browser utils
logaretm ea046d1
fix: parse XHR response with extracted util
logaretm a7f6321
tests: added browser integration tests for the hook
logaretm bdccf52
fix: make xhr header parsing safer
logaretm 6d86701
fix: lint issue
logaretm ae30de5
chore: bump size limit
logaretm 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
18 changes: 18 additions & 0 deletions
18
...er-integration-tests/suites/tracing/browserTracingIntegration/on-request-span-end/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,18 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [ | ||
Sentry.browserTracingIntegration({ | ||
idleTimeout: 1000, | ||
onRequestSpanEnd(span, { headers }) { | ||
if (headers) { | ||
span.setAttribute('hook.called.response-type', headers.get('x-response-type')); | ||
} | ||
}, | ||
}), | ||
], | ||
tracesSampleRate: 1, | ||
}); |
11 changes: 11 additions & 0 deletions
11
...integration-tests/suites/tracing/browserTracingIntegration/on-request-span-end/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,11 @@ | ||
fetch('http://sentry-test.io/fetch', { | ||
headers: { | ||
foo: 'fetch', | ||
}, | ||
}); | ||
|
||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.open('GET', 'http://sentry-test.io/xhr'); | ||
xhr.setRequestHeader('foo', 'xhr'); | ||
xhr.send(); |
61 changes: 61 additions & 0 deletions
61
...er-integration-tests/suites/tracing/browserTracingIntegration/on-request-span-end/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,61 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/core'; | ||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers'; | ||
|
||
sentryTest('should call onRequestSpanEnd hook', async ({ browserName, getLocalTestUrl, page }) => { | ||
const supportedBrowsers = ['chromium', 'firefox']; | ||
|
||
if (shouldSkipTracingTest() || !supportedBrowsers.includes(browserName)) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('http://sentry-test.io/fetch', async route => { | ||
await route.fulfill({ | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Response-Type': 'fetch', | ||
'access-control-expose-headers': '*', | ||
}, | ||
body: '', | ||
}); | ||
}); | ||
await page.route('http://sentry-test.io/xhr', async route => { | ||
await route.fulfill({ | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Response-Type': 'xhr', | ||
'access-control-expose-headers': '*', | ||
}, | ||
body: '', | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const envelopes = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url, timeout: 10000 }); | ||
|
||
const tracingEvent = envelopes[envelopes.length - 1]; // last envelope contains tracing data on all browsers | ||
|
||
expect(tracingEvent.spans).toContainEqual( | ||
expect.objectContaining({ | ||
op: 'http.client', | ||
data: expect.objectContaining({ | ||
type: 'xhr', | ||
'hook.called.response-type': 'xhr', | ||
}), | ||
}), | ||
); | ||
|
||
expect(tracingEvent.spans).toContainEqual( | ||
expect.objectContaining({ | ||
op: 'http.client', | ||
data: expect.objectContaining({ | ||
type: 'fetch', | ||
'hook.called.response-type': 'fetch', | ||
}), | ||
}), | ||
); | ||
}); |
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
Oops, something went wrong.
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.
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.
Bug: XHR Memory Leak and Header Parsing Errors
In
xhrCallback
, XHR spans may leak memory because cleanup and theonRequestSpanEnd
callback are skipped ifsentryXhrData.status_code
is undefined. Additionally,parseXhrResponseHeaders
receives a wrapped XHR object, which could lead to runtime errors when parsing response headers for theonRequestSpanEnd
callback.