@grafana/faro-react 2.0.0-beta-2 → 2.1.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 (39) hide show
  1. package/README.md +7 -409
  2. package/dist/bundle/faro-react.iife.js +6 -6
  3. package/dist/bundle/types/index.d.ts +3 -3
  4. package/dist/bundle/types/router/types.d.ts +3 -1
  5. package/dist/bundle/types/router/v6/types.d.ts +1 -0
  6. package/dist/bundle/types/types.d.ts +9 -1
  7. package/dist/bundle/types/utils/index.d.ts +1 -1
  8. package/dist/bundle/types/utils/reactVersion.d.ts +1 -0
  9. package/dist/cjs/index.js +3 -2
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/router/initialize.js +4 -2
  12. package/dist/cjs/router/initialize.js.map +1 -1
  13. package/dist/cjs/router/types.js +2 -0
  14. package/dist/cjs/router/types.js.map +1 -1
  15. package/dist/cjs/router/v6/types.js.map +1 -1
  16. package/dist/cjs/types.js.map +1 -1
  17. package/dist/cjs/utils/index.js +2 -1
  18. package/dist/cjs/utils/index.js.map +1 -1
  19. package/dist/cjs/utils/reactVersion.js +3 -2
  20. package/dist/cjs/utils/reactVersion.js.map +1 -1
  21. package/dist/esm/index.js +1 -1
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/router/initialize.js +4 -2
  24. package/dist/esm/router/initialize.js.map +1 -1
  25. package/dist/esm/router/types.js +2 -0
  26. package/dist/esm/router/types.js.map +1 -1
  27. package/dist/esm/router/v6/types.js.map +1 -1
  28. package/dist/esm/types.js.map +1 -1
  29. package/dist/esm/utils/index.js +1 -1
  30. package/dist/esm/utils/index.js.map +1 -1
  31. package/dist/esm/utils/reactVersion.js +2 -1
  32. package/dist/esm/utils/reactVersion.js.map +1 -1
  33. package/dist/types/index.d.ts +3 -3
  34. package/dist/types/router/types.d.ts +3 -1
  35. package/dist/types/router/v6/types.d.ts +1 -0
  36. package/dist/types/types.d.ts +9 -1
  37. package/dist/types/utils/index.d.ts +1 -1
  38. package/dist/types/utils/reactVersion.d.ts +1 -0
  39. package/package.json +14 -10
package/README.md CHANGED
@@ -6,9 +6,8 @@ Out of the box, the package provides you the following features:
6
6
 
7
7
  - Error Boundary - Provides additional stacktrace for errors and configuration options for
8
8
  pushError behavior
9
- - Component Profiler - Capture every re-render of a component, the un/mounting time etc.
10
- - Router (v4-v6) integration - Send events for all route changes
11
- - SSR support
9
+ - Component Profiler - Capture every re-render of a component, the un/mounting time etc
10
+ - Router (v4–v7) integration - Send events for all route changes.
12
11
 
13
12
  The Faro Web SDK is a highly configurable open source real user monitoring (RUM) library built on
14
13
  OpenTelemetry and integrating seamlessly with Grafana Cloud and Grafana Frontend Observability.
@@ -16,413 +15,12 @@ OpenTelemetry and integrating seamlessly with Grafana Cloud and Grafana Frontend
16
15
  Faro-React is a distribution of the Faro Web SDK for project using React, which offers easier
17
16
  integrations and the following features:
18
17
 
19
- - **Support for React Router v6 or v4/v5.x**: send events for all route changes, including the data
18
+ - **Support for React Router v4–v7**: send events for all route changes, including the data
20
19
  router API
21
20
  - **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**
21
+ - **Component profiler**: to capture component renders, un/mounting time, and more.
24
22
 
25
- ## Install the Faro-React Web SDK
23
+ ## How to instrument with Faro React
26
24
 
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
- ```ts
71
- import {
72
- initializeFaro,
73
- createReactRouterV6DataOptions,
74
- ReactIntegration,
75
- getWebInstrumentations,
76
- } from '@grafana/faro-react';
77
-
78
- import { matchRoutes } from 'react-router-dom';
79
-
80
- initializeFaro({
81
- // ...
82
-
83
- instrumentations: [
84
- // Load the default Web instrumentations
85
- ...getWebInstrumentations(),
86
-
87
- new ReactIntegration({
88
- router: createReactRouterV6DataOptions({
89
- matchRoutes,
90
- }),
91
- }),
92
- ],
93
- });
94
- ```
95
-
96
- Instrument the routes from a React data router (`BrowserRouter`, `HashRouter`, or `MemoryRouter`).
97
-
98
- In the file you create your data router, often the App.\* file pass your data router to the Faro-React
99
- function `withFaroRouterInstrumentation` to wrap all your routes and apply Faro auto instrumentation:
100
-
101
- ```ts
102
- import { createBrowserRouter } from 'react-router-dom';
103
-
104
- const reactBrowserRouter = createBrowserRouter([
105
- // your routes...
106
- ]);
107
-
108
- const browserRouter = withFaroRouterInstrumentation(reactBrowserRouter);
109
- ```
110
-
111
- ### Router v6 (no Data Router)
112
-
113
- In the file you define your router, import `createRoutesFromChildren`, `matchRoutes`, `Routes`,
114
- `useLocation`, `useNavigationType` from `react-router-dom` and pass them to the dependencies object.
115
-
116
- ```ts
117
- import { createRoutesFromChildren, matchRoutes, Routes, useLocation, useNavigationType } from 'react-router-dom';
118
-
119
- import {
120
- createReactRouterV6Options,
121
- getWebInstrumentations,
122
- initializeFaro,
123
- ReactIntegration,
124
- ReactRouterVersion,
125
- } from '@grafana/faro-react';
126
-
127
- initializeFaro({
128
- // Mandatory, the URL of the Grafana collector
129
- url: 'my/collector/url',
130
-
131
- // Mandatory, the identification label of your application
132
- app: {
133
- name: 'my-react-app',
134
- },
135
-
136
- // ...
137
-
138
- instrumentations: [
139
- // Load the default Web instrumentations
140
- ...getWebInstrumentations(),
141
-
142
- new ReactIntegration({
143
- router: createReactRouterV6Options({
144
- createRoutesFromChildren,
145
- matchRoutes,
146
- Routes,
147
- useLocation,
148
- useNavigationType,
149
- }),
150
- }),
151
- ],
152
- });
153
- ```
154
-
155
- To instrument React Router v6 import the `<FaroRoutes/>` component and use it instead of the React
156
- router `<Routes />` component, for example:
157
-
158
- ```tsx
159
- import { FaroRoutes } from '@grafana/faro-react';
160
-
161
- // during render
162
- <FaroRoutes>
163
- <Route path="/" element={<Home />} />
164
- {/* ... */}
165
- </FaroRoutes>;
166
- ```
167
-
168
- ### Router v4/v5
169
-
170
- To instrument React Router v4 or v5, import the `Route` component from `react-router-dom` and the
171
- `history` object from the `history package` and pass them to the dependencies object:
172
-
173
- The final result should look similar like this example:
174
-
175
- ```ts
176
- import { createBrowserHistory } from 'history';
177
- import { Route } from 'react-router-dom';
178
-
179
- import {
180
- // or createReactRouterV4Options
181
- createReactRouterV5Options,
182
- getWebInstrumentations,
183
- initializeFaro,
184
- ReactIntegration,
185
- ReactRouterVersion,
186
- } from '@grafana/faro-react';
187
-
188
- const history = createBrowserHistory();
189
-
190
- initializeFaro({
191
- // Mandatory, the URL of the Grafana collector
192
- url: 'my/collector/url',
193
-
194
- // Mandatory, the identification label of your application
195
- app: {
196
- name: 'my-react-app',
197
- },
198
-
199
- // ...
200
-
201
- instrumentations: [
202
- // Load the default Web instrumentations
203
- ...getWebInstrumentations(),
204
-
205
- new ReactIntegration({
206
- // or createReactRouterV4Options
207
- router: createReactRouterV5Options({
208
- history, // the history object used by react-router
209
- Route, // Route component imported from react-router package
210
- }),
211
- }),
212
- ],
213
- });
214
- ```
215
-
216
- To instrument React Router v4, v5 import the `<FaroRoute/>` component and use it instead of the
217
- React router `<Route />` component, for example:
218
-
219
- ```tsx
220
- import { FaroRoute } from '@grafana/faro-react';
221
-
222
- // during render
223
- <Switch>
224
- <FaroRoute path="/">
225
- <Home />
226
- </FaroRoute>
227
- {/* ... */}
228
- </Switch>;
229
- ```
230
-
231
- ## React ErrorBoundary support
232
-
233
- React Error boundaries are components that allow you to render a fallback UI in case an error occurs
234
- in the respective React component tree.
235
-
236
- Faro provides its own error boundary component which enhances the standard React error boundary with
237
- Faro specific functionality.
238
-
239
- In case of an error it sends a Faro error event which contains the error message, the React component
240
- stack of the component which contains the exception, and the name of name of the error boundary
241
- if configured.
242
-
243
- ```ts
244
- import { initializeFaro } from '@grafana/faro-react';
245
-
246
- initializeFaro({
247
- // required: the URL of the Grafana collector
248
- url: 'my/collector/url',
249
-
250
- // required: the identification label of your application
251
- app: {
252
- name: 'my-react-app',
253
- },
254
-
255
- instrumentations: [
256
- // Load the default Web instrumentations
257
- ...getWebInstrumentations(),
258
-
259
- // Must be initialized to get FaroErrorBoundary support
260
- new ReactIntegration(),
261
- ],
262
- });
263
- ```
264
-
265
- ```tsx
266
- import { FaroErrorBoundary } from '@grafana/faro-react';
267
-
268
- // during render
269
- <FaroErrorBoundary>
270
- <App />
271
- </FaroErrorBoundary>;
272
- ```
273
-
274
- or
275
-
276
- ```tsx
277
- import { withErrorBoundary } from '@grafana/faro-react';
278
-
279
- export default withErrorBoundary(App);
280
- ```
281
-
282
- ### FaroErrorBoundary properties
283
-
284
- Configuration options:
285
-
286
- - `fallback`: The fallback UI to render instead, either:
287
- - a `ReactElement`
288
- - `FaroErrorBoundaryFallbackRender` function that passes the Error object and a callback function
289
- to reset the error boundary to it’s initial state when called
290
- - `pushErrorOptions`: Options passed to the pushError API, for example additional context to an error
291
-
292
- Lifecycle callbacks:
293
-
294
- - `beforeCapture`: Executed before the Faro pushError API call, with the Error object as a parameter
295
- - `onError`: Executed when an error occurs, with the Error object as a parameter
296
- - `onMount`: Executed when React calls the componentDidMount lifecycle hook
297
- - `onUnmount`: Executed when React calls the componentWillUnmount lifecycle hook, with the Error
298
- object as a parameter
299
- - `onReset`: Executed when React calls resetErrorBoundary, with the Error object as a parameter
300
-
301
- ```tsx
302
- import { FaroErrorBoundary, PushErrorOptions } from '@grafana/faro-react';
303
-
304
- const pushErrorOptions: PushErrorOptions = {
305
- type: "Custom Error Type"
306
- context: {
307
- foo: "bar",
308
- baz: "abc"
309
- },
310
- };
311
-
312
- // during render
313
- <FaroErrorBoundary
314
- beforeCapture={(error) => handleBeforeCapture(error)}
315
- onError={(error) => handleError(error)}
316
- onMount={() => handleOnMount()}
317
- onUnmount={(error) => handleUnmount(error)}
318
- onReset={(error) => handleReset(error)}
319
- pushErrorOptions={pushErrorOptions}
320
- fallback={(error, resetBoundary) => {
321
- return errorBoundaryFallbackRenderer(error, resetBoundary) }}
322
- >
323
- <App />
324
- </FaroErrorBoundary>;
325
- ```
326
-
327
- ## React server side rendering support
328
-
329
- Follow this guide to learn how to initialize your Faro instrumentation to support React Server Side
330
- Rendering (SSR) for:
331
-
332
- React Router v6 without a data router:
333
-
334
- ```tsx
335
- import { FaroErrorBoundary, setReactRouterV6SSRDependencies } from '@grafana/faro-react';
336
- setReactRouterV6SSRDependencies({ Routes });
337
-
338
- export function renderToString(...) {
339
- return reactRenderToString(
340
- <FaroErrorBoundary>
341
- <StaticRouter location={...}>
342
- <App />
343
- </StaticRouter>
344
- </FaroErrorBoundary>
345
- ),
346
- }
347
- ```
348
-
349
- React Router v6 with a data router:
350
-
351
- Wrap your data router with `withFaroRouterInstrumentation(dataRouter)` in your routes file.
352
-
353
- React Router v5:
354
-
355
- ```tsx
356
- import { FaroErrorBoundary, setReactRouterV4V5SSRDependencies } from '@grafana/faro-react';
357
- setReactRouterV4V5SSRDependencies({ Route, history });
358
-
359
- export function renderToString(...) {
360
- return reactRenderToString(
361
- <FaroErrorBoundary>
362
- <StaticRouter location={...}>
363
- <App />
364
- </StaticRouter>
365
- </FaroErrorBoundary>
366
- ),
367
- }
368
- ```
369
-
370
- ## React component profiling
371
-
372
- Follow this guide to setup and use the Faro Profiler to get insights into the render performance of
373
- a React components.
374
-
375
- > WARNING
376
- > Using the profiler has an impact on performance and should not be used in production.
377
-
378
- To use the Faro profiler, import the Faro web-tracing package and initialize Faro-React as follows:
379
-
380
- ```ts
381
- import { withFaroProfiler } from '@grafana/faro-react';
382
-
383
- export default withFaroProfiler(MyReactComponent);
384
- ```
385
-
386
- ## Upgrading from a v4, v5, v6 router to data router
387
-
388
- This section describes how to upgrade the Faro React router instrumentation if you already have a
389
- React app instrumented which doesn’t use data routers.
390
-
391
- In the `ReactIntegration` call, change the `version` property from `ReactRouterVersion.[V4|V5|V6]` to
392
- `ReactRouterVersion.V6_data_router`.
393
-
394
- If you use React Router v4 or v5 remove the `history` and `Route` dependencies and add the `matchRoutes`
395
- function exported by `react-router-dom`.
396
-
397
- If you use React Router v6 remove the following dependencies from the dependencies object:
398
-
399
- - `createRoutesFromChildren`
400
- - `Routes`
401
- - `useLocation`
402
- - `useNavigationType`
403
-
404
- The ReactIntegration call should look similar to:
405
-
406
- ```tsx
407
- import { matchRoutes } from 'react-router-dom';
408
-
409
- import { getWebInstrumentations, initializeFaro, ReactIntegration, ReactRouterVersion } from '@grafana/faro-react';
410
-
411
- initializeFaro({
412
- // ...
413
-
414
- instrumentations: [
415
- // Load the default Web instrumentations
416
- ...getWebInstrumentations(),
417
-
418
- new ReactIntegration({
419
- router: {
420
- version: ReactRouterVersion.V6_data_router,
421
- dependencies: {
422
- matchRoutes,
423
- },
424
- },
425
- }),
426
- ],
427
- });
428
- ```
25
+ For a complete guide to instrumenting web applications with the Faro-React Web SDK, refer to our
26
+ Grafana Cloud Frontend Observability [documentation](https://grafana.com/docs/grafana-cloud/monitor-applications/frontend-observability/instrument/faro-react/).