https://github.com/congruencelabs/http-duration-client
Measure duration for Node HTTP lifecycle events
https://github.com/congruencelabs/http-duration-client
duration http nodejs timings
Last synced: 3 months ago
JSON representation
Measure duration for Node HTTP lifecycle events
- Host: GitHub
- URL: https://github.com/congruencelabs/http-duration-client
- Owner: congruencelabs
- License: mit
- Created: 2019-08-09T08:23:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T06:42:46.000Z (over 2 years ago)
- Last Synced: 2025-02-17T11:46:52.310Z (4 months ago)
- Topics: duration, http, nodejs, timings
- Language: TypeScript
- Homepage: https://medium.com/congruence-labs/improve-observability-of-nodejs-http-request-response-a12f8629ccd0
- Size: 938 KB
- Stars: 1
- Watchers: 5
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# http-duration-client
Measure duration (**in milliseconds**) for Node `HTTP` lifecycle events from various phases of HTTP:
* DNS lookup
* TCP/Socket connect
* TLS connect
* First byte
* Content transfer
* Total request[](https://travis-ci.org/congruencelabs/http-duration-client)
[](https://npmjs.org/package/http-duration-client?cacheSeconds=3600)
[](https://npmjs.org/package/http-duration-client?cacheSeconds=3600)## Development
1. To check the HTTP lifecycle events, clone this repository
```sh
git clone [email protected]:congruencelabs/http-duration-client.git
```2. Install the package dependencies using `yarn`
```javascript
yarn install
```3. Run the `examples/client-with-duration.js` file to see the lifecycle events being logged for a request to `https://api.github.com/users`
## Usage
1. Install the `http-duration-client` using `npm` or `yarn`
```
npm install http-duration-client
```
or
```
yarn add http-duration-client
```2. Make an http request to an endpoint as shown in example below
```javascript
const { requestWithDuration } = require('http-duration-client');requestWithDuration({
url: "https://api.github.com/users",
headers: {
'User-Agent': 'Example'
},
timeout: 500
}), (err, res) => {
if(err) {
console.error(err);
} else {
console.log(res.duration);
}
});
```The result of the above should look similar to
```json
{
"dnsLookup": 62.253469,
"tcpConnection": 20.589025,
"tlsHandshake": 44.643525,
"firstByte": 490.091997,
"contentTransfer": 15.480998,
"total": 633.059014
}
```You can also use it with `async` and `await requestWithDuration()` or `requestWithDuration().then()`
```javascript
(async () => {
try {
const resp = await requestWithDuration(
{
url: "https://api.github.com/users",
headers: {
"User-Agent": "Example"
},
timeout: 500
},
);
console.log(resp.duration)
} catch(err) {
console.error(err)
}
})()
```The result of the above should look similar to
```json
{
"dnsLookup": 62.253469,
"tcpConnection": 20.589025,
"tlsHandshake": 44.643525,
"firstByte": 490.091997,
"contentTransfer": 15.480998,
"total": 633.059014
}
```