Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using Sentry Performance Monitoring with Inertia.js #8528

AbhiPrasad started this conversation in Show and tell
Discussion options

Errors instrumentation should work out of the box with Inertia.js, but you'll have to do some more work to get it working for performance monitoring. In particular, you'll need to give information to Sentry about the inertia router so that Sentry can generate pageload and navigations appropriately.

Adding Inertia support with Sentry performance monitoring depends on if you are using Sentry.BrowserTracing or `Sentry.browserTracingIntegration.

If you are using Sentry.browserTracingIntegration, first disable the automatic routing instrumentation, and then initialize pageloads/navigations yourself accordingly via the inertia router.

Sentry.init({
 integrations: [
 Sentry.browserTracingIntegration({
 // disable automatic span creation
 instrumentNavigation: false,
 instrumentPageLoad: false,
 }),
 ],
});
// We start the pageload span as early as possible!
let name = route().current();
let pageLoadSpan = Sentry.startBrowserTracingPageLoadSpan({ op: "pageload", name });
router.on("before", (route) => {
 const client = Sentry.getClient();
 const newName = route().current();
 if (newName !== name) {
 name = newName;
 Sentry.startBrowserTracingNavigationSpan(client, {
 op: "navigation",
 name,
 attributes: {
 [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "route",
 },
 });
 }
 }
});
router.on('finish', () => {
 name = route().current();
 // always make sure we are using the correct route name
 const span = Sentry.getActiveSpan();
 const op = span && Sentry.spanToJSON(span).op;
 if (op === 'pageload' || op === 'navigation) {
 span.setName(name);
 }
});

If you are using Sentry.BrowserTracing, create a routingInstrumentation that listens in on the inertia router.

// Uses https://inertiajs.com/events
function inertiaRoutingInstrumentation(
 customStartTransaction,
 startTransactionOnPageLoad = true,
 startTransactionOnLocationChange = true,
) {
 let activeTransaction;
 let name;
 if (startTransactionOnPageLoad) {
 name = route().current();
 activeTransaction = customStartTransaction({
 name,
 op: 'pageload',
 metadata: {
 source: 'route',
 },
 });
 }
 if (startTransactionOnLocationChange) {
 router.on('before', (_to, _from) => {
 if (activeTransaction) {
 activeTransaction.finish();
 }
 const newName = route().current();
 if (newName !== name) {
 activeTransaction = customStartTransaction({
 name: newName,
 op: 'navigation',
 metadata: {
 source: 'route',
 },
 });
 }
 });
 router.on('finish', () => {
 activeTransaction.setName(route().current(), 'route');
 });
 }
}
Sentry.init({
 dsn: '__PUBLIC_DSN__',
 tracesSampleRate: 1.0,
 integrations: [
 new Sentry.BrowserTracing({
 routingInstrumentation: inertiaRoutingInstrumentation,
 }),
 ],
});
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hello
Question about @sentry/vue ver. 1.0.15,

Sentry.init({
dsn: 'PUBLIC_DSN',
tracesSampleRate: 1.0,
integrations: [
new Sentry.BrowserTracing({
routingInstrumentation: inertiaRoutingInstrumentation,
}),
],
});

I got warning:
Deprecated symbol used, consult docs for better alternative
Method expression is not of Function type

Now, I should use Sentry.browserTracingIntegration
But in Sentry.browserTracingIntegration I can't find routingInstrumentation or alternatives for this option, do you have any advice?

You must be logged in to vote
1 reply
Comment options

AbhiPrasad Apr 2, 2024
Maintainer Author

@RChutchev with Sentry.browserTracingIntegration, custom routing has changed a bit. See the docs to see how to set it up. https://docs.sentry.io/platforms/javascript/performance/instrumentation/automatic-instrumentation/#custom-routing

I'll mark a TODO on the guide to update this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /