Repository navigation
feat: Accept tunnel: true in init to enable the managed tunnel
#25200
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
+504
−21
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
42cb087
feat: Accept `tunnel: true` in `init` to enable the managed tunnel
logaretm 2f9fbee
feat: Explain a managed tunnel that nothing on the server answers
logaretm b7759aa
fix(core): Keep the managed tunnel warning out of production bundles
logaretm de55542
fix(aws-serverless): Keep the managed tunnel when the Lambda extensio…
logaretm 4466c2d
fix(core): Point Next.js users to `tunnelRoute` when the managed tunn…
logaretm ac8a68c
feat(browser): Send directly to Sentry when nothing answers the manag…
logaretm 8d07f1e
fix(browser): Normalize the managed tunnel's base path like the serve…
logaretm afaaa0c
fix(browser): Keep the managed tunnel when Sentry answers 404 or 405 …
logaretm a387492
fix(browser): Send directly when anything but the tunnel answers a ma…
logaretm 51664fc
ref(browser): Warn about a rejected DSN from the managed tunnel trans…
logaretm a61789b
ref(browser): Switch the managed tunnel to direct sends in one place
logaretm 4ee4dd0
test(browser): Drop the server-side tunnel match from the browser tun…
logaretm a8699a4
ref(browser): Trim the managed tunnel comments
logaretm 92f5265
fix(aws-serverless): Keep the Lambda extension for a relative tunnel …
logaretm c68139a
ref: Import the internal tunnel helpers under their `_INTERNAL_` names
logaretm 02cfc41
ref: Narrow `tunnel` to the browser or server tunnel options per SDK
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
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
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,105 @@ | ||
| import type { BrowserTunnelOptions, Envelope, Transport, TransportMakeRequestResponse } from '@sentry/core'; | ||
| import { | ||
| debug, | ||
| getEnvelopeEndpointWithUrlEncodedAuth, | ||
| getTunnelPath, | ||
| makeDsn, | ||
| _INTERNAL_normalizeTunnelBasePath as normalizeTunnelBasePath, | ||
| } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import type { BrowserOptions } from '../client'; | ||
| import type { BrowserTransportOptions } from '../transports/types'; | ||
|
|
||
| /** | ||
| * Resolves `tunnel: true` or {@link BrowserTunnelOptions} to the same-origin path the server derives from the DSN. | ||
| */ | ||
| export function resolveBrowserTunnelOption( | ||
| options: BrowserOptions, | ||
| ): Pick<BrowserOptions, 'transportOptions' | '_managedTunnel'> & { tunnel: string | undefined } { | ||
| const { tunnel, dsn, transportOptions } = options; | ||
| if (typeof tunnel === 'string' || !tunnel) { | ||
| return { tunnel, transportOptions }; | ||
| } | ||
|
|
||
| const tunnelOptions: BrowserTunnelOptions = tunnel === true ? {} : tunnel; | ||
| const tunnelPath = tunnelOptions.path ?? (dsn && getTunnelPath(dsn)); | ||
| if (!tunnelPath) { | ||
| return { tunnel: undefined, transportOptions }; | ||
| } | ||
|
|
||
| const basePath = normalizeTunnelBasePath(tunnelOptions.basePath); | ||
| const resolvedTunnel = `${basePath}${tunnelPath}${tunnelOptions.trailingSlash ? '/' : ''}`; | ||
|
|
||
| DEBUG_BUILD && debug.log(`Tunneling events to "${resolvedTunnel}"`); | ||
|
|
||
| return { | ||
| tunnel: resolvedTunnel, | ||
| _managedTunnel: tunnelOptions, | ||
| // CSRF protection (SvelteKit, Astro) rejects the default `text/plain` POST as a form submission. | ||
| transportOptions: { | ||
| ...transportOptions, | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope', ...transportOptions?.headers }, | ||
| }, | ||
| }; | ||
| } | ||
|
logaretm marked this conversation as resolved.
|
||
|
|
||
| let hasWarnedAboutRejectedDsn = false; | ||
|
|
||
| /** | ||
| * Falls back to sending directly when nothing on this origin serves the managed tunnel. An envelope is resent only | ||
| * when the response proves it never reached Sentry. | ||
| */ | ||
| export function makeManagedTunnelTransport( | ||
| makeTransport: (options: BrowserTransportOptions) => Transport, | ||
| directHeaders: BrowserTransportOptions['headers'], | ||
| ): (options: BrowserTransportOptions) => Transport { | ||
| return options => { | ||
| const tunnelTransport = makeTransport(options); | ||
| let directTransport: Transport | undefined; | ||
|
|
||
| return { | ||
| async send(envelope: Envelope): Promise<TransportMakeRequestResponse> { | ||
| if (directTransport) { | ||
| return directTransport.send(envelope); | ||
| } | ||
|
|
||
| const response = await tunnelTransport.send(envelope); | ||
| const statusCode = response.statusCode || 0; | ||
|
|
||
| if (response.headers?.['x-sentry-tunnel']) { | ||
| if (DEBUG_BUILD && statusCode === 403 && !hasWarnedAboutRejectedDsn) { | ||
| hasWarnedAboutRejectedDsn = true; | ||
| debug.warn( | ||
| `The managed tunnel at "${options.url}" rejected this DSN, so events sent through it are lost. Add it to the server's \`allowedDsns\`.`, | ||
| ); | ||
| } | ||
|
logaretm marked this conversation as resolved.
|
||
|
|
||
| return response; | ||
| } | ||
|
|
||
| // Not the tunnel: a 4xx never reached Sentry so it's resent, a 2xx may have, so only later envelopes go direct. | ||
| const rejected = statusCode >= 400 && statusCode < 500; | ||
| const { dsn, sdk } = envelope[0]; | ||
| const components = (rejected || (statusCode >= 200 && statusCode < 300)) && dsn && makeDsn(dsn); | ||
| if (!components) { | ||
| return response; | ||
| } | ||
|
|
||
| DEBUG_BUILD && | ||
| debug.warn(`Sending to Sentry directly: "${options.url}" answered ${statusCode} instead of the tunnel.`); | ||
| directTransport ??= makeTransport({ | ||
| ...options, | ||
| url: getEnvelopeEndpointWithUrlEncodedAuth(components, undefined, sdk), | ||
| headers: directHeaders, | ||
| }); | ||
|
|
||
| return rejected ? directTransport.send(envelope) : response; | ||
| }, | ||
| async flush(timeout?: number): Promise<boolean> { | ||
| const results = await Promise.all([tunnelTransport.flush(timeout), directTransport?.flush(timeout) ?? true]); | ||
|
|
||
| return results.every(Boolean); | ||
| }, | ||
| }; | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.