Skip to content

Commit cf299e1

Browse files
jack828mikehardy
authored andcommitted
docs(perf): Add docs for ky intergration & full example
1 parent 8d9e751 commit cf299e1

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

docs/perf/axios-integration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Performance Monitoring with Axios
33
description: Monitor HTTP request performance with Firebase Performance Monitoring and Axios.
4+
next: /perf/ky-integration
45
previous: /perf/usage
56
---
67

docs/perf/ky-integration.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Performance Monitoring with KY
3+
description: Monitor HTTP request performance with Firebase Performance Monitoring and KY.
4+
previous: /perf/axios-integration
5+
---
6+
7+
# KY
8+
9+
The [KY library](https://github.com/sindresorhus/ky) is a tiny wrapper over fetch providing a simpler API and some useful shortcuts.
10+
KY provides functionality allowing all requests & responses to be intercepted, exposing
11+
metadata which can be passed onto the Performance Monitoring library.
12+
13+
## Before Request Hook
14+
15+
Before HTTP requests are sent out of the React Native environment, a number of functions can be called to modify or do things based on the current request.
16+
We can use this `beforeRequest` hook to create our HTTP metric.
17+
18+
```js
19+
import perf from '@react-native-firebase/perf';
20+
import ky from 'ky';
21+
22+
const getContentLength = headers => {
23+
const length = getHeader('Content-Length', headers);
24+
return length ? Number(length) : null;
25+
};
26+
27+
const getHeader = (header, headers) => headers.get(header) || headers.get(header.toLowerCase());
28+
29+
const api = ky.create({
30+
hooks: {
31+
beforeRequest: [
32+
async (request, options) => {
33+
const { url, method } = request;
34+
options.metric = await perf().newHttpMetric(url, method);
35+
36+
// add any extra metric attributes, if required
37+
// options.metric.putAttribute('userId', '12345678');
38+
// options.metric.setRequestPayloadSize(1234)
39+
40+
await options.metric.start();
41+
},
42+
],
43+
},
44+
});
45+
46+
export default api;
47+
```
48+
49+
This callback attaches the HTTP metric returned onto the request metadata, which can later be used on an
50+
incoming response.
51+
52+
## After Request Hook
53+
54+
Similar to the before request hook, we can also hook into all responses from HTTP calls.
55+
56+
```js
57+
import ky from 'ky';
58+
59+
const getContentLength = headers => {
60+
const length = getHeader('Content-Length', headers);
61+
return length ? Number(length) : null;
62+
};
63+
64+
const getHeader = (header, headers) => headers.get(header) || headers.get(header.toLowerCase());
65+
66+
const api = ky.create({
67+
hooks: {
68+
afterResponse: [
69+
async (request, options, response) => {
70+
const { status, headers } = response;
71+
const { metric } = options;
72+
metric.setHttpResponseCode(status);
73+
metric.setResponseContentType(getHeader('Content-Type', headers));
74+
metric.setResponsePayloadSize(getContentLength(headers));
75+
await metric.stop();
76+
},
77+
],
78+
},
79+
});
80+
81+
export default api;
82+
```
83+
84+
All outbound requests sent from KY will appear in your Firebase console, detailing information such as
85+
how long the request took, response codes & more. Such data can give you greater insight into the performance of your application via any external APIs you may be using.
86+
87+
## Full Example
88+
89+
A working example is provided below. Please adjust and extend according to your use case.
90+
91+
```js
92+
import perf from '@react-native-firebase/perf';
93+
import ky from 'ky';
94+
95+
const getContentLength = headers => {
96+
const length = getHeader('Content-Length', headers);
97+
return length ? Number(length) : null;
98+
};
99+
100+
const getHeader = (header, headers) => headers.get(header) || headers.get(header.toLowerCase());
101+
102+
const api = ky.create({
103+
// prevent circular dependency warning
104+
fetch: fetch,
105+
hooks: {
106+
beforeRequest: [
107+
async (request, options) => {
108+
const { url, method } = request;
109+
options.metric = await perf().newHttpMetric(url, method);
110+
111+
// add any extra metric attributes, if required
112+
// options.metric.putAttribute('userId', '12345678');
113+
// options.metric.setRequestPayloadSize(1234)
114+
115+
await options.metric.start();
116+
},
117+
],
118+
119+
afterResponse: [
120+
async (request, options, response) => {
121+
const { status, headers } = response;
122+
const { metric } = options;
123+
metric.setHttpResponseCode(status);
124+
metric.setResponseContentType(getHeader('Content-Type', headers));
125+
metric.setResponsePayloadSize(getContentLength(headers));
126+
await metric.stop();
127+
},
128+
],
129+
},
130+
});
131+
132+
export default api;
133+
```

docs/sidebar.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,6 @@
118118
- '/perf/usage'
119119
- - Axios Integration
120120
- '/perf/axios-integration'
121+
- - KY Integration
122+
- '/perf/ky-integration'
121123
- '//static.invertase.io/assets/firebase/performance-monitoring.svg'

packages/perf/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ yarn add @react-native-firebase/perf
4141
### Additional Topics
4242

4343
- [Axios Integration](https://rnfirebase.io/perf/axios-integration)
44+
- [KY Integration](https://rnfirebase.io/perf/ky-integration)
4445

4546
## License
4647

0 commit comments

Comments
 (0)