Title: Revamp the Transports API · Issue #4660 · getsentry/sentry-javascript · GitHub
Open Graph Title: Revamp the Transports API · Issue #4660 · getsentry/sentry-javascript
X Title: Revamp the Transports API · Issue #4660 · getsentry/sentry-javascript
Description: As part of #4240 (comment), we want to revamp the Transports API to make it easier to extend (adding support for attachments), consolidate the options, and reduce bundle size. This issue forms a living document on how we are going to tac...
Open Graph Description: As part of #4240 (comment), we want to revamp the Transports API to make it easier to extend (adding support for attachments), consolidate the options, and reduce bundle size. This issue forms a li...
X Description: As part of #4240 (comment), we want to revamp the Transports API to make it easier to extend (adding support for attachments), consolidate the options, and reduce bundle size. This issue forms a li...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/4660
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Revamp the Transports API","articleBody":"As part of https://github.com/getsentry/sentry-javascript/issues/4240#issuecomment-1035323682, we want to revamp the Transports API to make it easier to extend (adding support for attachments), consolidate the options, and reduce bundle size.\r\n\r\nThis issue forms a living document on how we are going to tackle this - first by setting up an API that can be (possibly) merged in before v7.\r\n\r\nMuch of this is inspired from the transport design of https://github.com/getsentry/sentry-javascript/blob/v7-dev/packages/transport-base/src/transport.ts\r\n\r\n### Options\r\n\r\nTo start, let's look at the current state of the options:\r\n\r\nhttps://github.com/getsentry/sentry-javascript/blob/b1009b5766b34b77781e28cebbf6cbde870e01a2/packages/types/src/transport.ts#L51-L74\r\n\r\nDoing a quick runthrough, here's what that looks like:\r\n\r\n```ts\r\nexport interface BaseTransportOptions {\r\n /** Sentry DSN */\r\n dsn: DsnLike;\r\n /** Define custom headers */\r\n headers?: { [key: string]: string };\r\n /** Set a HTTP proxy that should be used for outbound requests. */\r\n httpProxy?: string; // ONLY USED BY NODE SDK\r\n /** Set a HTTPS proxy that should be used for outbound requests. */\r\n httpsProxy?: string; // ONLY USED BY NODE SDK\r\n /** HTTPS proxy certificates path */\r\n caCerts?: string; // ONLY USED BY NODE SDK\r\n /** Fetch API init parameters */\r\n fetchParameters?: { [key: string]: string }; // ONLY USED BY BROWSER SDK\r\n /** The envelope tunnel to use. */\r\n tunnel?: string;\r\n /** Send SDK Client Reports. Enabled by default. */\r\n sendClientReports?: boolean; // ONLY USED BY BROWSER SDK ATM\r\n /**\r\n * Set of metadata about the SDK that can be internally used to enhance envelopes and events,\r\n * and provide additional data about every request.\r\n * */\r\n _metadata?: SdkMetadata;\r\n}\r\n```\r\n\r\nThis means we can probably reduce it down to:\r\n\r\n```ts\r\nexport interface BaseTransportOptions {\r\n // url to send the event\r\n // transport does not care about dsn specific - client should take care of\r\n // parsing and figuring that out\r\n url: string;\r\n headers?: Record\u003cstring, string>;\r\n bufferSize?: number; // make transport buffer size configurable\r\n};\r\n\r\nexport interface BrowserTransportOptions extends BaseTransportOptions {\r\n // options to pass into fetch request\r\n fetchParams: Record\u003cstring, string>;\r\n sendClientReports?: boolean;\r\n // TODO: Add custom fetch implementation?\r\n}\r\n\r\nexport interface NodeTransportOptions extends BaseTransportOptions {\r\n // Set a HTTP proxy that should be used for outbound requests.\r\n httpProxy?: string;\r\n // Set a HTTPS proxy that should be used for outbound requests.\r\n httpsProxy?: string;\r\n // HTTPS proxy certificates path\r\n caCerts?: string;\r\n}\r\n```\r\n\r\n### API\r\n\r\nThe transport does a couple of things in the SDK, but we can think about it mainly turning a `SentryRequest` into a `Response` of some kind. Due to rate-limiting, the transport is essentially a closed-loop control system, so the `Response` must be of some form to guide the transport (and the SDK in general) in making future decisions about how it should function.\r\n\r\n`SentryRequest\u003cT>` -> `SentryResponse`. `SentryRequest` becomes generic (defaulting to `T = string`), so transports can work with buffers/event-emitters/streams as the body if it so pleases. This also leaves us open to have non-http transports (although we'll never probably have that as a default).\r\n\r\nAssuming that in v7, the client is in charge of creating the envelope (because it has to be able to dynamically add items).\r\n\r\n```ts\r\ninterface INewTransport {\r\n // TODO: How to best attach the type?\r\n send(request: Envelope, type: SentryRequestType): PromiseLike\u003cTransportResponse>;\r\n flush(timeout: number): PromiseLike\u003cboolean>;\r\n}\r\n```\r\n\r\nWe can actually make this entirely functional\r\n\r\n```ts\r\nfunction createTransport(options: TransportOptions): INewTransport {\r\n // some rate limiting logic\r\n // some transport buffer logic\r\n\r\n // this is a huge breaking change though\r\n // makes it way harder for users to implement their own transports\r\n // but is that a case we really care about?\r\n return { send, flush };\r\n}\r\n```\r\n\r\nSticking with classes though\r\n\r\n```ts\r\n/**\r\n * Heavily based on Kamil's work in\r\n * https://github.com/getsentry/sentry-javascript/blob/v7-dev/packages/transport-base/src/transport.ts\r\n */\r\nexport abstract class BaseTransport implements INewTransport {\r\n protected readonly _buffer: PromiseBuffer\u003cTransportResponse>;\r\n private readonly _rateLimits: Record\u003cstring, number> = {};\r\n\r\n public constructor(protected readonly _options: BaseTransportOptions) {\r\n this._buffer = makePromiseBuffer(this._options.bufferSize || 30);\r\n }\r\n\r\n /** */\r\n public send(envelope: Envelope, type: SentryRequestType): PromiseLike\u003cTransportResponse> {\r\n const request: TransportRequest = {\r\n // I'm undecided if the type API should work like this\r\n // though we are a little stuck with this because of how\r\n // minimal the envelopes implementation is\r\n // perhaps there is a way we can expand it?\r\n type,\r\n body: serializeEnvelope(envelope),\r\n };\r\n\r\n if (isRateLimited(this._rateLimits, type)) {\r\n return rejectedSyncPromise(\r\n new SentryError(`oh no, disabled until: ${rateLimitDisableUntil(this._rateLimits, type)}`),\r\n );\r\n }\r\n\r\n const requestTask = (): PromiseLike\u003cTransportResponse> =>\r\n this._makeRequest(request).then(({ body, headers, reason, statusCode }): PromiseLike\u003cTransportResponse> => {\r\n if (headers) {\r\n updateRateLimits(this._rateLimits, headers);\r\n }\r\n\r\n // TODO: This is the happy path!\r\n const status = eventStatusFromHttpCode(statusCode);\r\n if (status === 'success') {\r\n return resolvedSyncPromise({ status });\r\n }\r\n\r\n return rejectedSyncPromise(new SentryError(body || reason || 'Unknown transport error'));\r\n });\r\n\r\n return this._buffer.add(requestTask);\r\n }\r\n\r\n /** */\r\n public flush(timeout?: number): PromiseLike\u003cboolean> {\r\n return this._buffer.drain(timeout);\r\n }\r\n\r\n // Each is up to each transport implementation to determine how to make a request -> return an according response\r\n // `TransportMakeRequestResponse` is different than `TransportResponse` because the client doesn't care about\r\n // these extra details\r\n protected abstract _makeRequest(request: TransportRequest): PromiseLike\u003cTransportMakeRequestResponse>;\r\n}\r\n```\r\n\r\nEdit: Have to incorporate client outcomes here - will figure that out in a bit.","author":{"url":"https://github.com/AbhiPrasad","@type":"Person","name":"AbhiPrasad"},"datePublished":"2022-03-01T19:48:26Z","url":"https://github.com/getsentry/sentry-javascript/issues/4660"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d75c3ae1-ab4c-b307-cb62-102c66ceb7fc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9750:1F2F:3A5744:4E945F:6B04312A |
| html-safe-nonce | 2b0b21d96a3b468e6bd0764e4f1acc279999b59ad575fa4582512bfdbe1ada92 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzUwOjFGMkY6M0E1NzQ0OjRFOTQ1Rjo2QjA0MzEyQSIsInZpc2l0b3JfaWQiOiIzNjAwODI1ODU4OTIwODIxMDM0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f07ce592a15ded9107a3fb910df6b0e3edac628def60e20abb4a66c858022a41 |
| hovercard-subject-tag | issue:1155745691 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/4660/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d4a4241d74f7a70ca70672d4bebf0ef48b2676af68ec3badc8f7fad20c7ea979/getsentry/sentry-javascript/issues/4660 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d4a4241d74f7a70ca70672d4bebf0ef48b2676af68ec3badc8f7fad20c7ea979/getsentry/sentry-javascript/issues/4660 |
| og:image:alt | As part of #4240 (comment), we want to revamp the Transports API to make it easier to extend (adding support for attachments), consolidate the options, and reduce bundle size. This issue forms a li... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AbhiPrasad |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width