-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add new v7 Fetch Transport #4765
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 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
74a1d81
feat(browser): Add new v7 Fetch Transport
AbhiPrasad e48fe10
fix imports
AbhiPrasad acdfa7e
initial set of tests
AbhiPrasad bafdc2f
Merge branch 'master' into abhi-new-fetch-transport
AbhiPrasad ed97fdd
Create new transport in browser backend
AbhiPrasad 78b26bb
make sure we apply sdk logic
AbhiPrasad 2bcbbf5
add integration tests
AbhiPrasad 0423410
yarn fix
AbhiPrasad 0f39885
Merge branch 'master' into abhi-new-fetch-transport
AbhiPrasad 8f9e4b5
Merge branch 'master' into abhi-new-fetch-transport
AbhiPrasad b294707
update suite
AbhiPrasad c0ef13d
Merge branch 'master' into abhi-new-fetch-transport
AbhiPrasad 05c8c2a
console.log
AbhiPrasad 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { BaseTransport } from './base'; | ||
export { FetchTransport } from './fetch'; | ||
export { XHRTransport } from './xhr'; | ||
|
||
export { makeNewFetchTransport } from './new-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
BaseTransportOptions, | ||
createTransport, | ||
NewTransport, | ||
TransportMakeRequestResponse, | ||
TransportRequest, | ||
} from '@sentry/core'; | ||
|
||
import { FetchImpl, getNativeFetchImplementation } from './utils'; | ||
|
||
export interface FetchTransportOptions extends BaseTransportOptions { | ||
requestOptions?: RequestInit; | ||
} | ||
|
||
/** | ||
* Creates a Transport that uses the Fetch API to send events to Sentry. | ||
*/ | ||
export function makeNewFetchTransport( | ||
options: FetchTransportOptions, | ||
nativeFetch: FetchImpl = getNativeFetchImplementation(), | ||
): NewTransport { | ||
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> { | ||
const requestOptions: RequestInit = { | ||
body: request.body, | ||
method: 'POST', | ||
referrerPolicy: 'origin', | ||
...options.requestOptions, | ||
}; | ||
|
||
return nativeFetch(options.url, requestOptions).then(response => { | ||
return response.text().then(body => ({ | ||
body, | ||
headers: { | ||
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'), | ||
'retry-after': response.headers.get('Retry-After'), | ||
}, | ||
reason: response.statusText, | ||
statusCode: response.status, | ||
})); | ||
}); | ||
} | ||
|
||
return createTransport({ bufferSize: options.bufferSize }, makeRequest); | ||
} |
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,97 @@ | ||
import { makeNewFetchTransport, FetchTransportOptions } from '../../../src/transports/new-fetch'; | ||
import { createEnvelope, serializeEnvelope } from '@sentry/utils'; | ||
import { EventEnvelope, EventItem } from '@sentry/types'; | ||
import { FetchImpl } from '../../../src/transports/utils'; | ||
|
||
const DEFAULT_FETCH_TRANSPORT_OPTIONS: FetchTransportOptions = { | ||
url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', | ||
}; | ||
|
||
const ERROR_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ | ||
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, | ||
]); | ||
|
||
class Headers { | ||
headers: { [key: string]: string } = {}; | ||
get(key: string) { | ||
return this.headers[key] || null; | ||
} | ||
set(key: string, value: string) { | ||
this.headers[key] = value; | ||
} | ||
} | ||
|
||
describe('NewFetchTransport', () => { | ||
it('calls fetch with the given URL', async () => { | ||
const mockFetch = jest.fn(() => | ||
Promise.resolve({ | ||
headers: new Headers(), | ||
status: 200, | ||
text: () => Promise.resolve({}), | ||
}), | ||
) as unknown as FetchImpl; | ||
const transport = makeNewFetchTransport(DEFAULT_FETCH_TRANSPORT_OPTIONS, mockFetch); | ||
|
||
expect(mockFetch).toHaveBeenCalledTimes(0); | ||
const res = await transport.send(ERROR_ENVELOPE); | ||
expect(mockFetch).toHaveBeenCalledTimes(1); | ||
|
||
expect(res.status).toBe('success'); | ||
|
||
expect(mockFetch).toHaveBeenLastCalledWith(DEFAULT_FETCH_TRANSPORT_OPTIONS.url, { | ||
body: serializeEnvelope(ERROR_ENVELOPE), | ||
method: 'POST', | ||
referrerPolicy: 'origin', | ||
}); | ||
}); | ||
|
||
it('sets rate limit headers', async () => { | ||
const headers = { | ||
get: jest.fn(), | ||
}; | ||
|
||
const mockFetch = jest.fn(() => | ||
Promise.resolve({ | ||
headers, | ||
status: 200, | ||
text: () => Promise.resolve({}), | ||
}), | ||
) as unknown as FetchImpl; | ||
const transport = makeNewFetchTransport(DEFAULT_FETCH_TRANSPORT_OPTIONS, mockFetch); | ||
|
||
expect(headers.get).toHaveBeenCalledTimes(0); | ||
await transport.send(ERROR_ENVELOPE); | ||
|
||
expect(headers.get).toHaveBeenCalledTimes(2); | ||
expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
expect(headers.get).toHaveBeenCalledWith('Retry-After'); | ||
}); | ||
|
||
it('allows for custom options to be passed in', async () => { | ||
const mockFetch = jest.fn(() => | ||
Promise.resolve({ | ||
headers: new Headers(), | ||
status: 200, | ||
text: () => Promise.resolve({}), | ||
}), | ||
) as unknown as FetchImpl; | ||
|
||
const REQUEST_OPTIONS: RequestInit = { | ||
referrerPolicy: 'strict-origin', | ||
keepalive: true, | ||
referrer: 'http://example.org', | ||
}; | ||
|
||
const transport = makeNewFetchTransport( | ||
{ ...DEFAULT_FETCH_TRANSPORT_OPTIONS, requestOptions: REQUEST_OPTIONS }, | ||
mockFetch, | ||
); | ||
|
||
await transport.send(ERROR_ENVELOPE); | ||
expect(mockFetch).toHaveBeenLastCalledWith(DEFAULT_FETCH_TRANSPORT_OPTIONS.url, { | ||
body: serializeEnvelope(ERROR_ENVELOPE), | ||
method: 'POST', | ||
...REQUEST_OPTIONS, | ||
}); | ||
}); | ||
}); |
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
1 change: 1 addition & 0 deletions
1
packages/integration-tests/suites/new-transports/fetch-captureException/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 @@ | ||
Sentry.captureException({}); |
21 changes: 21 additions & 0 deletions
21
packages/integration-tests/suites/new-transports/fetch-captureException/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,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; | ||
|
||
sentryTest('should capture an empty object with the new fetch transport', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'Non-Error exception captured with keys: [object has no keys]', | ||
mechanism: { | ||
type: 'generic', | ||
handled: true, | ||
}, | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
packages/integration-tests/suites/new-transports/fetch-startTransaction/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,2 @@ | ||
const transaction = Sentry.startTransaction({ name: 'test_transaction_1' }); | ||
transaction.finish(); |
13 changes: 13 additions & 0 deletions
13
packages/integration-tests/suites/new-transports/fetch-startTransaction/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,13 @@ | ||
import { expect } from '@playwright/test'; | ||
import { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; | ||
|
||
sentryTest('should report a transaction with the new fetch transport', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
const transaction = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(transaction.transaction).toBe('test_transaction_1'); | ||
expect(transaction.spans).toBeDefined(); | ||
}); |
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,13 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
// eslint-disable-next-line no-unused-vars | ||
import '@sentry/tracing'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
_experiments: { | ||
newTransport: true, | ||
}, | ||
tracesSampleRate: 1.0, | ||
}); |
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.
not nice to duplicate all of this, but considering we are deleting stuff very soon, I think duplicating this code is fine.