@grafana/faro-react 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +240 -107
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -4,36 +4,108 @@ Faro package that enables easier integration in projects built with React.
4
4
 
5
5
  Out of the box, the package provides you the following features:
6
6
 
7
- - Error Boundary - Provides additional stacktrace for errors and configuration options for pushError behavior
7
+ - Error Boundary - Provides additional stacktrace for errors and configuration options for
8
+ pushError behavior
8
9
  - Component Profiler - Capture every re-render of a component, the un/mounting time etc.
9
10
  - Router (v4-v6) integration - Send events for all route changes
10
11
  - SSR support
11
12
 
12
- ## Installation
13
+ The Faro Web SDK is a highly configurable open source real user monitoring (RUM) library built on
14
+ OpenTelemetry and integrating seamlessly with Grafana Cloud and Grafana Frontend Observability.
13
15
 
14
- ### React Router without Data Routers
16
+ Faro-React is a distribution of the Faro Web SDK for project using React, which offers easier
17
+ integrations and the following features:
15
18
 
16
- To set up Faro-React with React Router V5 or V6 without Data routers, add the following code snippet
17
- to your project. If you use React Router V6 with Data Routers, refer to the React Router with Data
18
- Routers section.
19
+ - **Support for React Router v6 or v4/v5.x**: send events for all route changes, including the data
20
+ router API
21
+ - **Error boundary**: enhancements to stack traces and configuration options for pushError behavior
22
+ - **Component profiler**: to capture component renders, un/mounting time, and more
23
+ - **Server side rendering (SSR) support**
24
+
25
+ ## Install the Faro-React Web SDK
26
+
27
+ First add Faro-React to your project. Install the Faro-React package by running the following command
28
+ for NPM:
29
+
30
+ ```sh
31
+ npm i @grafana/faro-react
32
+ ```
33
+
34
+ Or with Yarn:
35
+
36
+ ```sh
37
+ yarn add @grafana/faro-react
38
+ ```
39
+
40
+ The Faro-React package offers all the functionality and behavior from the Faro Web SDK package plus
41
+ additional React specific functionality like router instrumentation, a custom ErrorBoundary, and more.
42
+
43
+ Add the following code snippet to your project to import Faro-React with the minimum setup needed to
44
+ get insights into the health and performance of your application or website:
45
+
46
+ ```ts
47
+ import { initializeFaro } from '@grafana/faro-react';
48
+
49
+ initializeFaro({
50
+ // required: the URL of the Grafana collector
51
+ url: 'my/collector/url',
52
+
53
+ // required: the identification label of your application
54
+ app: {
55
+ name: 'my-react-app',
56
+ },
57
+ });
58
+ ```
59
+
60
+ Faro-React captures data about your application’s health and performance and exports them to the
61
+ configured collector like Grafana Alloy.
62
+
63
+ ## Instrument your application
64
+
65
+ The Faro-React package offers all the functionality and behavior from the Faro Web SDK package plus
66
+ additional React specific functionality like router instrumentation, a custom ErrorBoundary, and more.
67
+
68
+ ### Router v6 with Data Router
69
+
70
+ Instrument the routes from a React data router (`BrowserRouter`, `HashRouter`, or `MemoryRouter`).
71
+
72
+ In the file you create your data router, often the App.\* file pass your data router to the Faro-React
73
+ function `withFaroRouterInstrumentation` to wrap all your routes and apply Faro auto instrumentation:
74
+
75
+ ```ts
76
+ const reactBrowserRouter = createBrowserRouter([
77
+ // your routes...
78
+ ]);
79
+
80
+ const browserRouter = withFaroRouterInstrumentation(reactBrowserRouter);
81
+ ```
82
+
83
+ ### Router v6 (no Data Router)
84
+
85
+ In the file you define your router, import `createRoutesFromChildren`, `matchRoutes`, `Routes`,
86
+ `useLocation`, `useNavigationType` from `react-router-dom` and pass them to the dependencies object.
19
87
 
20
88
  ```ts
21
89
  import { createRoutesFromChildren, matchRoutes, Routes, useLocation, useNavigationType } from 'react-router-dom';
22
90
 
23
91
  import { getWebInstrumentations, initializeFaro, ReactIntegration, ReactRouterVersion } from '@grafana/faro-react';
24
- import { TracingInstrumentation } from '@grafana/faro-web-tracing';
25
92
 
26
93
  initializeFaro({
94
+ // Mandatory, the URL of the Grafana collector
95
+ url: 'my/collector/url',
96
+
97
+ // Mandatory, the identification label of your application
98
+ app: {
99
+ name: 'my-react-app',
100
+ },
101
+
27
102
  // ...
103
+
28
104
  instrumentations: [
29
105
  // Load the default Web instrumentations
30
106
  ...getWebInstrumentations(),
31
107
 
32
- // Tracing Instrumentation is needed if you want to use the React Profiler
33
- new TracingInstrumentation(),
34
-
35
108
  new ReactIntegration({
36
- // Only needed if you want to use the React Router instrumentation
37
109
  router: {
38
110
  version: ReactRouterVersion.V6,
39
111
  dependencies: {
@@ -44,60 +116,93 @@ initializeFaro({
44
116
  useNavigationType,
45
117
  },
46
118
  },
47
-
48
- // Or if you use react-router v4/v5
49
- router2: {
50
- version: ReactRouterVersion.V5, // or ReactRouterVersion.V4,
51
- dependencies: {
52
- history, // the history object used by react-router
53
- Route, // Route component imported from react-router package
54
- },
55
- },
56
119
  }),
57
120
  ],
58
121
  });
59
122
  ```
60
123
 
61
- ### Use with React Router with Data Routers
124
+ To instrument React Router v6 import the `<FaroRoutes/>` component and use it instead of the React
125
+ router `<Routes />` component, for example:
126
+
127
+ ```tsx
128
+ import { FaroRoutes } from '@grafana/faro-react';
129
+
130
+ // during render
131
+ <FaroRoutes>
132
+ <Route path="/" element={<Home />} />
133
+ {/* ... */}
134
+ </FaroRoutes>;
135
+ ```
136
+
137
+ ### Router v4/v5
138
+
139
+ To instrument React Router v4 or v5, import the `Route` component from `react-router-dom` and the
140
+ `history` object from the `history package` and pass them to the dependencies object:
141
+
142
+ The final result should look similar like this example:
62
143
 
63
144
  ```ts
64
- import { matchRoutes } from 'react-router-dom';
145
+ import { createBrowserHistory } from 'history';
146
+ import { Route } from 'react-router-dom';
65
147
 
66
148
  import { getWebInstrumentations, initializeFaro, ReactIntegration, ReactRouterVersion } from '@grafana/faro-react';
67
- import { TracingInstrumentation } from '@grafana/faro-web-tracing';
149
+
150
+ const history = createBrowserHistory();
68
151
 
69
152
  initializeFaro({
153
+ // Mandatory, the URL of the Grafana collector
154
+ url: 'my/collector/url',
155
+
156
+ // Mandatory, the identification label of your application
157
+ app: {
158
+ name: 'my-react-app',
159
+ },
160
+
70
161
  // ...
162
+
71
163
  instrumentations: [
72
164
  // Load the default Web instrumentations
73
165
  ...getWebInstrumentations(),
74
166
 
75
- // Tracing Instrumentation is needed if you want to use the React Profiler
76
- new TracingInstrumentation(),
77
-
78
167
  new ReactIntegration({
79
- // Only needed if you want to use the React Router instrumentation
80
168
  router: {
81
- version: ReactRouterVersion.V6_data_router,
169
+ version: ReactRouterVersion.V5, // or ReactRouterVersion.V4,
82
170
  dependencies: {
83
- matchRoutes,
171
+ history, // the history object used by react-router
172
+ Route, // Route component imported from react-router package
84
173
  },
85
174
  },
86
175
  }),
87
176
  ],
88
177
  });
178
+ ```
89
179
 
90
- // To instrument the router you need to attach Faro instrumentations providing it to the withFaroRouterInstrumentation function
91
- // Do this in your App.js or other file where you create the router.
180
+ To instrument React Router v4, v5 import the `<FaroRoute/>` component and use it instead of the
181
+ React router `<Route />` component, for example:
92
182
 
93
- const reactBrowserRouter = createBrowserRouter([
94
- //...
95
- ]);
183
+ ```tsx
184
+ import { FaroRoute } from '@grafana/faro-react';
96
185
 
97
- const browserRouter = withFaroRouterInstrumentation(reactBrowserRouter);
186
+ // during render
187
+ <Switch>
188
+ <FaroRoute path="/">
189
+ <Home />
190
+ </FaroRoute>
191
+ {/* ... */}
192
+ </Switch>;
98
193
  ```
99
194
 
100
- ### Error Boundary
195
+ ## React ErrorBoundary support
196
+
197
+ React Error boundaries are components that allow you to render a fallback UI in case an error occurs
198
+ in the respective React component tree.
199
+
200
+ Faro provides its own error boundary component which enhances the standard React error boundary with
201
+ Faro specific functionality.
202
+
203
+ In case of an error it sends a Faro error event which contains the error message, the React component
204
+ stack of the component which contains the exception, and the name of name of the error boundary
205
+ if configured.
101
206
 
102
207
  ```tsx
103
208
  import { FaroErrorBoundary } from '@grafana/faro-react';
@@ -116,130 +221,158 @@ import { withErrorBoundary } from '@grafana/faro-react';
116
221
  export default withErrorBoundary(App);
117
222
  ```
118
223
 
119
- #### pushErrorOptions prop
224
+ ### FaroErrorBoundary properties
225
+
226
+ Configuration options:
227
+
228
+ - `fallback`: The fallback UI to render instead, either:
229
+ - a `ReactElement`
230
+ - `FaroErrorBoundaryFallbackRender` function that passes the Error object and a callback function
231
+ to reset the error boundary to it’s initial state when called
232
+ - `pushErrorOptions`: Options passed to the pushError API, for example additional context to an error
233
+
234
+ Lifecycle callbacks:
235
+
236
+ - `beforeCapture`: Executed before the Faro pushError API call, with the Error object as a parameter
237
+ - `onError`: Executed when an error occurs, with the Error object as a parameter
238
+ - `onMount`: Executed when React calls the componentDidMount lifecycle hook
239
+ - `onUnmount`: Executed when React calls the componentWillUnmount lifecycle hook, with the Error
240
+ object as a parameter
241
+ - `onReset`: Executed when React calls resetErrorBoundary, with the Error object as a parameter
120
242
 
121
243
  ```tsx
244
+ // ...
122
245
 
246
+ <FaroErrorBoundary>
247
+ <MyComponent />
248
+ </FaroErrorBoundary>
249
+ ```
250
+
251
+ ```tsx
123
252
  import { FaroErrorBoundary, PushErrorOptions } from '@grafana/faro-react';
124
253
 
125
254
  const pushErrorOptions: PushErrorOptions = {
126
255
  type: "Custom Error Type"
127
256
  context: {
128
257
  foo: "bar",
129
- baz: "qux"
258
+ baz: "abc"
130
259
  },
131
- // ...
132
- }
260
+ };
133
261
 
134
262
  // during render
135
- <FaroErrorBoundary pushErrorOptions={pushErrorOptions}>
263
+ <FaroErrorBoundary
264
+ beforeCapture={(error) => handleBeforeCapture(error)}
265
+ onError={(error) => handleError(error)}
266
+ onMount={() => handleOnMount()}
267
+ onUnmount={(error) => handleUnmount(error)}
268
+ onReset={(error) => handleReset(error)}
269
+ pushErrorOptions={pushErrorOptions}
270
+ fallback={(error, resetBoundary) => {
271
+ return errorBoundaryFallbackRenderer(error, resetBoundary) }}
272
+ >
136
273
  <App />
137
274
  </FaroErrorBoundary>;
138
275
  ```
139
276
 
140
- ### Router
277
+ ## React erver side rendering support
141
278
 
142
- #### V6
279
+ Follow this guide to learn how to initialize your Faro instrumentation to support React Server Side
280
+ Rendering (SSR) for:
143
281
 
144
- ```tsx
145
- import { FaroRoutes } from '@grafana/faro-react';
282
+ React Router v6 without a data router:
146
283
 
147
- // during render
148
- <FaroRoutes>
149
- <Route path="/" element={<Home />} />
150
- {/* ... */}
151
- </FaroRoutes>;
284
+ ```tsx
285
+ import { FaroErrorBoundary, setReactRouterV6SSRDependencies } from '@grafana/faro-react';
286
+ setReactRouterV6SSRDependencies({ Routes });
287
+
288
+ export function renderToString(...) {
289
+ return reactRenderToString(
290
+ <FaroErrorBoundary>
291
+ <StaticRouter location={...}>
292
+ <App />
293
+ </StaticRouter>
294
+ </FaroErrorBoundary>
295
+ ),
296
+ }
152
297
  ```
153
298
 
154
- #### V6 Data Router
299
+ React Router v6 with a data router:
155
300
 
156
- 1. Create a data router (createBrowserRouter, createHashRouter, createMemoryRouter)
157
- 2. Instrument the data router to receive route changes by wrapping it with `withFaroRouterInstrumentation()`
301
+ Wrap your data router with `withFaroRouterInstrumentation(dataRouter)` in your routes file.
158
302
 
159
- ```ts
160
- const reactBrowserRouter = createBrowserRouter([
161
- //...
162
- ]);
303
+ React Router v5:
163
304
 
164
- const browserRouter = withFaroRouterInstrumentation(reactBrowserRouter);
305
+ ```tsx
306
+ import { FaroErrorBoundary, setReactRouterV4V5SSRDependencies } from '@grafana/faro-react';
307
+ setReactRouterV4V5SSRDependencies({ Route, history });
308
+
309
+ export function renderToString(...) {
310
+ return reactRenderToString(
311
+ <FaroErrorBoundary>
312
+ <StaticRouter location={...}>
313
+ <App />
314
+ </StaticRouter>
315
+ </FaroErrorBoundary>
316
+ ),
317
+ }
165
318
  ```
166
319
 
167
- #### V4/v5
320
+ ## React component profiling
168
321
 
169
- ```tsx
170
- import { FaroRoute } from '@grafana/faro-react';
322
+ Follow this guide to setup and use the Faro Profiler to get insights into the render performance of
323
+ a React components.
171
324
 
172
- // during render
173
- <Switch>
174
- <FaroRoute path="/">
175
- <Home />
176
- </FaroRoute>
177
- {/* ... */}
178
- </Switch>;
325
+ > WARNING
326
+ > Using the profiler has an impact on performance and should not be used in production.
327
+
328
+ To use the Faro profiler, import the Faro web-tracing package and initialize Faro-React as follows:
329
+
330
+ ```ts
331
+ import { withFaroProfiler } from '@grafana/faro-react';
332
+
333
+ export default withFaroProfiler(MyReactComponent);
179
334
  ```
180
335
 
181
- #### Upgrading from instrumented V6 router to V6 data router
336
+ ## Upgrading from a v4, v5, v6 router to data router
337
+
338
+ This section describes how to upgrade the Faro React router instrumentation if you already have a
339
+ React app instrumented which doesn’t use data routers.
340
+
341
+ In the `ReactIntegration` call, change the `version` property from `ReactRouterVersion.[V4|V5|V6]` to
342
+ `ReactRouterVersion.V6_data_router`.
182
343
 
183
- ##### Change router config
344
+ If you use React Router v4 or v5 remove the `history` and `Route` dependencies and add the `matchRoutes`
345
+ function exported by `react-router-dom`.
184
346
 
185
- 1. Change `version` property from `ReactRouterVersion.V6` to `ReactRouterVersion.V6_data_router`.
186
- 2. Remove the following dependencies from the dependencies object
347
+ If you use React Router v6 remove the following dependencies from the dependencies object:
187
348
 
188
349
  - `createRoutesFromChildren`
189
350
  - `Routes`
190
351
  - `useLocation`
191
352
  - `useNavigationType`
192
353
 
193
- Example: updating dependencies
354
+ The ReactIntegration call should look similar to:
355
+
356
+ ```tsx
357
+ import { matchRoutes } from 'react-router-dom';
358
+
359
+ import { getWebInstrumentations, initializeFaro, ReactIntegration, ReactRouterVersion } from '@grafana/faro-react';
194
360
 
195
- ```ts
196
361
  initializeFaro({
197
362
  // ...
363
+
198
364
  instrumentations: [
199
365
  // Load the default Web instrumentations
200
366
  ...getWebInstrumentations(),
201
367
 
202
- // Tracing Instrumentation is needed if you want to use the React Profiler
203
- new TracingInstrumentation(),
204
-
205
368
  new ReactIntegration({
206
- // Only needed if you want to use the React Router instrumentation
207
369
  router: {
208
- // version: ReactRouterVersion.V6 // => change to .V6_data_router,
209
370
  version: ReactRouterVersion.V6_data_router,
210
371
  dependencies: {
211
372
  matchRoutes,
212
- // +++ remove the following dependencies +++
213
- // createRoutesFromChildren,
214
- // Routes,
215
- // useLocation,
216
- // useNavigationType,
217
373
  },
218
374
  },
219
375
  }),
220
376
  ],
221
377
  });
222
378
  ```
223
-
224
- ##### Change Router instrumentation
225
-
226
- 1. Remove `<FaroRoutes>` component. This will not work anymore with V6 data routers.
227
- 2. Create a data router and wrap it with `withFaroRouterInstrumentation(dataRouter)`
228
-
229
- Example: Instrument Router
230
-
231
- ```ts
232
- const reactBrowserRouter = createBrowserRouter([
233
- // your routes
234
- ]);
235
-
236
- const browserRouter = withFaroRouterInstrumentation(reactBrowserRouter);
237
- ```
238
-
239
- ### Profiler
240
-
241
- ```tsx
242
- import { withFaroProfiler } from '@grafana/faro-react';
243
-
244
- export default withFaroProfiler(App);
245
- ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/faro-react",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Faro package that enables easier integration in projects built with React.",
5
5
  "keywords": [
6
6
  "observability",
@@ -52,8 +52,8 @@
52
52
  "quality:circular-deps": "madge --circular ."
53
53
  },
54
54
  "dependencies": {
55
- "@grafana/faro-web-sdk": "^1.6.0",
56
- "@grafana/faro-web-tracing": "^1.6.0",
55
+ "@grafana/faro-web-sdk": "^1.7.0",
56
+ "@grafana/faro-web-tracing": "^1.7.0",
57
57
  "hoist-non-react-statics": "^3.3.2"
58
58
  },
59
59
  "devDependencies": {
@@ -71,5 +71,5 @@
71
71
  "publishConfig": {
72
72
  "access": "public"
73
73
  },
74
- "gitHead": "108fd2cbd0ff73398599bb87a020215c170e07b9"
74
+ "gitHead": "b06dd9c3b974d3d9103f7a045d1e86207ba007d0"
75
75
  }