Skip to content

feat: Add performance docs for react #1792

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 4 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

**React**

To get started with performance monitoring using Sentry's React SDK, first install the `@sentry/react` and `@sentry/apm` packages:

```bash
# Using yarn
$ yarn add @sentry/react @sentry/apm

# Using npm
$ npm install @sentry/react @sentry/apm
```

Next, initialize the integration in your call to `Sentry.init`. Make sure this happens before you mount your React component on the page.

```jsx
import * as Sentry from '@sentry/react';
import { Integrations } from "@sentry/apm";
Sentry.init({
dsn: '"___PUBLIC_DSN___"',
release: 'my-project-name@' + process.env.npm_package_version,
integrations: [
new Integrations.Tracing(),
],
tracesSampleRate: 0.25, // must be present and non-zero
});

// ...

ReactDOM.render(<App />, rootNode);

// Can also use with React Concurrent Mode
// ReactDOM.createRoot(rootNode).render(<App />);
```

**`withProfiler` Higher-Order Component**

`@sentry/react` exports a `withProfiler` higher-order component that leverages the `@sentry/apm` Tracing integration to add React related spans to transactions. If the Tracing integration is not enabled, the Profiler will not work.

In the example below, the `withProfiler` higher-order component is used to instrument an App component.

```jsx
import React from 'react';
import * as Sentry from '@sentry/react';

class App extends React.Component {
render() {
return (
<FancyComponent>
<NestedComponent someProp={2} />
<AnotherComponent />
</FancyComponent>
)
}
}

export default Sentry.withProfiler(App);
```

The React Profiler currently generates spans with three different kinds of op-codes: `react.mount`, `react.render`, and `react.update`.

`react.mount`

: The span that represents how long it took for the profiled component to mount.

`react.render`

: The span that represents how long the profiled component was on a page. This span is only generated if the profiled component mounts and unmounts while a transaction is occurring.

`react.update`

: The span that represents when the profiled component updated. This span is only generated if the profiled component has mounted.

{% capture __alert_content -%}
In [React Strict Mode](https://reactjs.org/docs/strict-mode.html), certain component methods will be [invoked twice](https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects). This may lead to duplicate `react.mount` spans appearing in a transaction. React Strict Mode only runs in development mode, so this will have no impact on your production traces.
{%- endcapture -%}
{%- include components/alert.html
title="Note"
content=__alert_content
level="warning"
%}

The `withProfiler` higher-order component has a variety of options for further customization. They can be passed in as the second argument to the `withProfiler` function.

```jsx
export default Sentry.withProfiler(App, { name: "CustomAppName" })
```

`name` (string)

: The name of the component being profiled. By default, the name is taken from the component `displayName` property or the component `name` property.

`includeRender` (boolean)

: If a `react.render` span should be created by the Profiler. Set as true by default.

`includeUpdates` (boolean)

: If `react.update` spans should be created by the Profiler. Set as true by default. We recommend setting this prop as false for components that will experience many rerenders, such as text input components, as the resulting spans can be very noisy.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Supported SDKs for Tracing
- JavaScript Browser SDK ≥ 5.16.0
- JavaScript Node SDK ≥ 5.16.0
- Python SDK version ≥ 0.11.2
- Javascript React SDK ≥ 5.18.1

{%- endcapture -%}
{%- include components/alert.html
Expand Down