Skip to content

Commit

Permalink
feat: Add docs for custom browser tracing routing instrumentation (#9113
Browse files Browse the repository at this point in the history
)



---------

Co-authored-by: Liza Mock <[email protected]>
  • Loading branch information
mydea and lizokm committed Feb 14, 2024
1 parent f0c184e commit 159202d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
36 changes: 0 additions & 36 deletions includes/performance/browser-custom-routing-instrumentation.mdx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### Custom Routing

By default, the `browserTracingIntegration()` will create a `pageload` span for when the page is initially loaded, as well as a `navigation` span for whenever the URL changes afterwards.

To make sure that spans are created correctly for a custom routing setup, you'll need to opt out of the default span creation by setting `instrumentNavigation: false` and `instrumentPageLoad: false` in the `browserTracingIntegration()` options. You can then manually create spans like this:

```javascript
Sentry.init({
integrations: [
Sentry.browserTracingIntegration({
// disable automatic span creation
instrumentNavigation: false,
instrumentPageLoad: false,
}),
],
});

// We start the pageload span as early as possible!
let pageLoadSpan = Sentry.startBrowserTracingPageLoadSpan({
name: window.location.pathname
attributes: {
[Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "url",
},
});

// Somewhere, instrument your router like this:
myRouter.on("routeChange", (route) => {
const client = Sentry.getClient();

// Make sure that the pageload span uses the route name
// After that, each route change should trigger a navigation span (which will automatically finish the previous one)
if (pageLoadSpan) {
pageloadSpan.updateName(route.name);
pageloadSpan.setAttribute(Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, "route");
pageLoadSpan = undefined;
} else {
Sentry.startBrowserTracingNavigationSpan(client, {
op: "navigation",
name: route.name, // or what the name of the span should be
attributes: {
[Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "route",
},
});
}
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ Sentry.init({
tracesSampleRate: 1.0,
});
```

<PlatformContent includePath="performance/automatic-instrumentation-custom-routing" />
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Sentry.init({
// * reactRouterV4BrowserTracingInstrumentation
// * reactRouterV6BrowserTracingInstrumentation
// or just browserTracingIntegration
routingInstrumentation: Sentry.reactRouterV5Instrumentation(history),
// ... other options
}),
],

Expand All @@ -34,3 +32,5 @@ Sentry.init({
```

Now you should be generating `pageload`/`navigation` transactions from the `BrowserTracing` integration, using Sentry's [`react-router` instrumentation](/platforms/javascript/guides/react/configuration/integrations/react-router/).

<PlatformContent includePath="performance/automatic-instrumentation-custom-routing" />

0 comments on commit 159202d

Please sign in to comment.