Skip to content

Commit 42f0465

Browse files
authored
feat(browser): Add simpler makeMultiplexedTransport docs for automatic matcher (#15692)
## DESCRIBE YOUR PR With [10.28.0](https://github.com/getsentry/sentry-javascript/releases/tag/10.28.0) we released a simpler way to work with micro-frontends by building an automatic base matcher into `makeMultiplexedTransport`. These docs reflect the change on top of showcasing how to set up a manual matcher. ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [X] None: Not urgent, can wait up to 1 week+
1 parent d167c05 commit 42f0465

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

docs/platforms/javascript/common/best-practices/micro-frontends.mdx

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,13 @@ import {
123123
makeFetchTransport,
124124
moduleMetadataIntegration,
125125
makeMultiplexedTransport,
126+
MULTIPLEXED_TRANSPORT_EXTRA_KEY,
126127
} from "@sentry/browser";
127128

128-
const EXTRA_KEY = "ROUTE_TO";
129-
130-
const transport = makeMultiplexedTransport(makeFetchTransport, (args) => {
131-
const event = args.getEvent();
132-
if (
133-
event &&
134-
event.extra &&
135-
EXTRA_KEY in event.extra &&
136-
Array.isArray(event.extra[EXTRA_KEY])
137-
) {
138-
return event.extra[EXTRA_KEY];
139-
}
140-
return [];
141-
});
142-
143129
init({
144130
dsn: "__DEFAULT_DSN__",
145131
integrations: [moduleMetadataIntegration()],
146-
transport,
132+
transport: makeMultiplexedTransport(makeFetchTransport),
147133
beforeSend: (event) => {
148134
if (event?.exception?.values?.[0].stacktrace.frames) {
149135
const frames = event.exception.values[0].stacktrace.frames;
@@ -156,7 +142,7 @@ init({
156142
if (routeTo.length) {
157143
event.extra = {
158144
...event.extra,
159-
[EXTRA_KEY]: routeTo,
145+
[MULTIPLEXED_TRANSPORT_EXTRA_KEY]: routeTo,
160146
};
161147
}
162148
}
@@ -186,28 +172,10 @@ init({
186172
></script>
187173

188174
<script>
189-
const EXTRA_KEY = "ROUTE_TO";
190-
191-
const transport = Sentry.makeMultiplexedTransport(
192-
Sentry.makeFetchTransport,
193-
(args) => {
194-
const event = args.getEvent();
195-
if (
196-
event &&
197-
event.extra &&
198-
EXTRA_KEY in event.extra &&
199-
Array.isArray(event.extra[EXTRA_KEY])
200-
) {
201-
return event.extra[EXTRA_KEY];
202-
}
203-
return [];
204-
}
205-
);
206-
207175
Sentry.init({
208176
dsn: "__DEFAULT_DSN__",
209177
integrations: [Sentry.moduleMetadataIntegration()],
210-
transport,
178+
transport: Sentry.makeMultiplexedTransport(Sentry.makeFetchTransport),
211179
beforeSend: (event) => {
212180
if (event?.exception?.values?.[0].stacktrace.frames) {
213181
const frames = event.exception.values[0].stacktrace.frames;
@@ -220,7 +188,7 @@ init({
220188
if (routeTo.length) {
221189
event.extra = {
222190
...event.extra,
223-
[EXTRA_KEY]: routeTo,
191+
[Sentry.MULTIPLEXED_TRANSPORT_EXTRA_KEY]: routeTo,
224192
};
225193
}
226194
}
@@ -249,11 +217,42 @@ will return matches for errors, transactions, and replays.
249217
## Manually Route Errors to Different Projects
250218
251219
If you want more control to be able to explicitly specify the destination for each individual `captureException`,
252-
you can use the more advanced interface multiplexed transport offers.
220+
you can use the multiplexed transport's API to route events to specific projects.
253221
254222
<Alert>Requires SDK version `7.59.0` or higher.</Alert>
255223
256-
The example below uses a `feature` tag to determine which Sentry project to
224+
### Using the Default Matcher
225+
226+
The simplest way to manually route events is by using the default matcher with `MULTIPLEXED_TRANSPORT_EXTRA_KEY`:
227+
228+
```js
229+
import {
230+
captureException,
231+
init,
232+
makeFetchTransport,
233+
makeMultiplexedTransport,
234+
MULTIPLEXED_TRANSPORT_EXTRA_KEY,
235+
} from "@sentry/browser";
236+
237+
init({
238+
dsn: "__FALLBACK_DSN__",
239+
transport: makeMultiplexedTransport(makeFetchTransport),
240+
});
241+
242+
// Route a specific error to different projects
243+
captureException(new Error("oh no!"), {
244+
extra: {
245+
[MULTIPLEXED_TRANSPORT_EXTRA_KEY]: [
246+
{ dsn: "__CART_DSN__", release: "cart@1.0.0" },
247+
{ dsn: "__GALLERY_DSN__", release: "gallery@1.2.0" },
248+
],
249+
},
250+
});
251+
```
252+
253+
### Using a Custom Matcher
254+
255+
For more advanced routing logic, you can provide a custom matcher function. The example below uses a `feature` tag to determine which Sentry project to
257256
send the event to. If the event doesn't have a `feature` tag, we send it to the
258257
fallback DSN defined in `Sentry.init`.
259258

0 commit comments

Comments
 (0)