Skip to content

feat: Add routing instrumentation for react router v4/v5 #2780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2020

Conversation

AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented Jul 28, 2020

So in React Router v4 and v5, there is no longer a static set of routes we can depend upon to change transaction name.

Take a look at their philosophy for more: https://reactrouter.com/core/guides/philosophy

This means it is hard for us to get all the transaction names without additional user configuration. I have set this up in two ways.

a) They can use a custom Route component to achieve this functionality. This is as every time a Route renders, we get access to the match params (what path they matched with). We can then grab update the name there.

Usage:

import {Route, Router, Switch } from 'react-router-dom';
import { createBrowserHistory } from 'history'; 
import { reactRouterV4Instrumentation, withSentryRouting } from '../src';

const SentryRoute = withSentryRouting(Route);
const history = createBrowserHistory();

Sentry.init({
  tracesSampleRate: 1,
  integrations: [
    new Integrations.BrowserTracing({
      routingInstrumentation: reactRouterV4Instrumentation(history),
    });
  ]
});

render() {
  return (
    <Router history={history}>
      <Switch>
         <SentryRoute path="/users/:userid" component={() => <div>UserId</div>} />
         <SentryRoute path="/users" component={() => <div>Users</div>} />
         <SentryRoute path="/" component={() => <div>Home</div>} />
      </Switch>
    </Router>
  );
}

b) They can pass us a route config. We accept a config object that is supported by https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config, so something that looks like:

const routes = [
  {
    component: Root,
    routes: [
      {
        path: "/",
        exact: true,
        component: Home
      },
      {
        path: "/child/:id",
        component: Child,
        routes: [
          {
            path: "/child/:id/grand-child",
            component: GrandChild
          }
        ]
      }
    ]
  }
];

Then usage:

import {Route, Router, Switch, matchPath } from 'react-router-dom';
import { createBrowserHistory } from 'history'; 
import { reactRouterV4Instrumentation, withSentryRouting } from '../src';

const history = createBrowserHistory();
const routes = [{ path: '/users/:userid' }, { path: '/users' }, { path: '/' }];

Sentry.init({
  tracesSampleRate: 1,
  integrations: [
    new Integrations.BrowserTracing({
      routingInstrumentation: reactRouterV4Instrumentation(history, routes, matchPath),
    });
  ]
});

render() {
  return (
    <Router history={history}>
      <Switch>
         <SentryRoute path="/users/:userid" component={() => <div>UserId</div>} />
         <SentryRoute path="/users" component={() => <div>Users</div>} />
         <SentryRoute path="/" component={() => <div>Home</div>} />
      </Switch>
    </Router>
  );
}

The Route Component matching trumps the config.

@AbhiPrasad AbhiPrasad requested a review from HazAT July 28, 2020 12:36
@AbhiPrasad AbhiPrasad requested a review from kamilogorek as a code owner July 28, 2020 12:36
@AbhiPrasad AbhiPrasad force-pushed the abhi/feat/react-router-v4-v5 branch from eb5ad74 to 8ae7fee Compare July 28, 2020 12:38
@github-actions
Copy link
Contributor

github-actions bot commented Jul 28, 2020

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 17.66 KB (+0.03% 🔺)
@sentry/browser - Webpack 18.42 KB (+0.06% 🔺)
@sentry/react - Webpack 18.42 KB (+0.06% 🔺)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 22.64 KB (+0.02% 🔺)

@AbhiPrasad AbhiPrasad force-pushed the abhi/feat/react-router-v4-v5 branch from 8ae7fee to f41e609 Compare July 29, 2020 14:51
@getsentry-bot
Copy link
Contributor

getsentry-bot commented Jul 29, 2020

Warnings
⚠️ Please add a changelog entry for your changes.
Messages
📖

@sentry/browser bundle gzip'ed minified size: (ES5: 17.251 kB) (ES6: 16.3525 kB)

📖 ✅ TSLint passed

Generated by 🚫 dangerJS against 9887f01

Copy link
Member

@HazAT HazAT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many tests ❤️

@AbhiPrasad AbhiPrasad force-pushed the abhi/feat/react-router-v4-v5 branch from d678e92 to 9887f01 Compare August 10, 2020 11:59
@AbhiPrasad AbhiPrasad merged commit ddf1062 into master Aug 10, 2020
@AbhiPrasad AbhiPrasad deleted the abhi/feat/react-router-v4-v5 branch August 10, 2020 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants