Skip to content

Commit f979104

Browse files
Lms24AbhiPrasad
andauthored
docs(transports): Update Migration document with custom Transport changes (#5012)
add the Transport changes to MIGRATION.md. show the difference between old and new transports and give example how to migrate a custom v6 transport to v7. Co-authored-by: Abhijeet Prasad <aprasad@sentry.io>
1 parent b3fddcd commit f979104

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

MIGRATION.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,110 @@ const storeEndpoint = api.getStoreEndpointWithUrlEncodedAuth();
102102
const envelopeEndpoint = api.getEnvelopeEndpointWithUrlEncodedAuth();
103103
```
104104

105-
## Enum changes
105+
## Transport Changes
106+
107+
The `Transport` API was simplified and some functionality (e.g. APIDetails and client reports) was refactored and moved
108+
to the Client. To send data to Sentry, we switched from the previously used [Store endpoint](https://develop.sentry.dev/sdk/store/) to the [Envelopes endpoint](https://develop.sentry.dev/sdk/envelopes/).
109+
110+
This example shows the new v7 and the v6 Transport API:
111+
112+
```js
113+
// New in v7:
114+
export interface Transport {
115+
/* Sends an envelope to the Envelope endpoint in Sentry */
116+
send(request: Envelope): PromiseLike<void>;
117+
/* Waits for all events to be sent or the timeout to expire, whichever comes first */
118+
flush(timeout?: number): PromiseLike<boolean>;
119+
}
120+
121+
// Before:
122+
export interface Transport {
123+
/* Sends the event to the Store endpoint in Sentry */
124+
sendEvent(event: Event): PromiseLike<Response>;
125+
/* Sends the session to the Envelope endpoint in Sentry */
126+
sendSession?(session: Session | SessionAggregates): PromiseLike<Response>;
127+
/* Waits for all events to be sent or the timeout to expire, whichever comes first */
128+
close(timeout?: number): PromiseLike<boolean>;
129+
/* Increment the counter for the specific client outcome */
130+
recordLostEvent?(type: Outcome, category: SentryRequestType): void;
131+
}
132+
```
133+
134+
### Custom Transports
135+
If you rely on a custom transport, you will need to make some adjustments to how it is created when migrating
136+
to v7. Note that we changed our transports from a class-based to a functional approach, meaning that
137+
the previously class-based transports are now created via functions. This also means that custom transports
138+
are now passed by specifying a factory function in the `Sentry.init` options object instead passing the custom
139+
transport's class.
140+
141+
The following example shows how to create a custom transport in v7 vs. how it was done in v6:
142+
143+
```js
144+
// New in v7:
145+
import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
146+
import { createTransport } from '@sentry/core';
147+
148+
export function makeMyCustomTransport(options: BaseTransportOptions): Transport {
149+
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
150+
// this is where your sending logic goes
151+
const myCustomRequest = {
152+
body: request.body,
153+
url: options.url
154+
};
155+
// you define how `sendMyCustomRequest` works
156+
return sendMyCustomRequest(myCustomRequest).then(response => ({
157+
headers: {
158+
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
159+
'retry-after': response.headers.get('Retry-After'),
160+
},
161+
}));
162+
}
163+
164+
// `createTransport` takes care of rate limiting and flushing
165+
return createTransport({ bufferSize: options.bufferSize }, makeRequest);
166+
}
167+
168+
Sentry.init({
169+
dsn: '...',
170+
transport: makeMyCustomTransport, // this function will be called when the client is initialized
171+
...
172+
})
173+
174+
175+
176+
// Before:
177+
class MyCustomTransport extends BaseTransport {
178+
constructor(options: TransportOptions) {
179+
// initialize your transport here
180+
super(options);
181+
}
182+
183+
public sendEvent(event: Event): PromiseLike<Response> {
184+
// this is where your sending logic goes
185+
// `url` is decoded from dsn in BaseTransport
186+
const myCustomRequest = createMyCustomRequestFromEvent(event, this.url);
187+
return sendMyCustomRequest(myCustomRequest).then(() => resolve({status: 'success'}));
188+
}
189+
190+
public sendSession(session: Session): PromiseLike<Response> {...}
191+
// ...
192+
}
193+
194+
Sentry.init({
195+
dsn: '...',
196+
transport: MyCustomTransport, // the constructor was called when the client was initialized
197+
...
198+
})
199+
```
200+
201+
Overall, the new way of transport creation allows you to create your custom sending implementation
202+
without having to deal with the conversion of events or sessions to envelopes.
203+
We recommend calling using the `createTransport` function from `@sentry/core` as demonstrated in the example above which, besides creating the `Transport`
204+
object with your custom logic, will also take care of rate limiting and flushing.
205+
206+
For a complete v7 transport implementation, take a look at our [browser fetch transport](https://github.com/getsentry/sentry-javascript/blob/ebc938a03d6efe7d0c4bbcb47714e84c9a566a9c/packages/browser/src/transports/fetch.ts#L1-L34).
207+
208+
## Enum Changes
106209

107210
Given that enums have a high bundle-size impact, our long term goal is to eventually remove all enums from the SDK in
108211
favor of string literals.

0 commit comments

Comments
 (0)