@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.
- package/README.md +240 -107
- 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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
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.
|
|
169
|
+
version: ReactRouterVersion.V5, // or ReactRouterVersion.V4,
|
|
82
170
|
dependencies: {
|
|
83
|
-
|
|
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
|
-
|
|
91
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
]);
|
|
183
|
+
```tsx
|
|
184
|
+
import { FaroRoute } from '@grafana/faro-react';
|
|
96
185
|
|
|
97
|
-
|
|
186
|
+
// during render
|
|
187
|
+
<Switch>
|
|
188
|
+
<FaroRoute path="/">
|
|
189
|
+
<Home />
|
|
190
|
+
</FaroRoute>
|
|
191
|
+
{/* ... */}
|
|
192
|
+
</Switch>;
|
|
98
193
|
```
|
|
99
194
|
|
|
100
|
-
|
|
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
|
-
|
|
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: "
|
|
258
|
+
baz: "abc"
|
|
130
259
|
},
|
|
131
|
-
|
|
132
|
-
}
|
|
260
|
+
};
|
|
133
261
|
|
|
134
262
|
// during render
|
|
135
|
-
<FaroErrorBoundary
|
|
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
|
-
|
|
277
|
+
## React erver side rendering support
|
|
141
278
|
|
|
142
|
-
|
|
279
|
+
Follow this guide to learn how to initialize your Faro instrumentation to support React Server Side
|
|
280
|
+
Rendering (SSR) for:
|
|
143
281
|
|
|
144
|
-
|
|
145
|
-
import { FaroRoutes } from '@grafana/faro-react';
|
|
282
|
+
React Router v6 without a data router:
|
|
146
283
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
299
|
+
React Router v6 with a data router:
|
|
155
300
|
|
|
156
|
-
|
|
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
|
-
|
|
160
|
-
const reactBrowserRouter = createBrowserRouter([
|
|
161
|
-
//...
|
|
162
|
-
]);
|
|
303
|
+
React Router v5:
|
|
163
304
|
|
|
164
|
-
|
|
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
|
-
|
|
320
|
+
## React component profiling
|
|
168
321
|
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
56
|
-
"@grafana/faro-web-tracing": "^1.
|
|
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": "
|
|
74
|
+
"gitHead": "b06dd9c3b974d3d9103f7a045d1e86207ba007d0"
|
|
75
75
|
}
|