Skip to content

Commit

Permalink
fix: updating metrics to collect better things
Browse files Browse the repository at this point in the history
  • Loading branch information
leftieFriele committed Nov 8, 2024
1 parent 726e6ee commit efe65e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Generic http client built on [undici] with a circuit breaker using [opossum], er
[![GitHub Actions status](https://github.com/podium-lib/http-client/workflows/Run%20Lint%20and%20Tests/badge.svg)](https://github.com/podium-lib/layout/actions?query=workflow%3A%22Run+Lint+and+Tests%22)
[![Known Vulnerabilities](https://snyk.io/test/github/podium-lib/http-client/badge.svg)](https://snyk.io/test/github/podium-lib/http-client)

## Documentation
**Table of contents:**

- [Installing](#installation)
- [Usage](#usage)
- [API](#api)
Expand Down
12 changes: 11 additions & 1 deletion lib/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export default class HttpClient {
this.#requestDuration = this.#metrics.histogram({
name: 'http_client_request_duration',
description: 'Request duration',
labels: {
method: undefined,
status: undefined,
url: undefined,
},
buckets: [0.001, 0.01, 0.1, 0.5, 1, 2, 10],
});
this.#breakerCounter = this.#metrics.counter({
Expand Down Expand Up @@ -129,6 +134,11 @@ export default class HttpClient {
return this.#metrics;
}

/**
*
* @param {HttpClientRequestOptionsAdditions | Object} options
* @returns {Promise<{headers: IncomingHttpHeaders, body: BodyReadable & Dispatcher.BodyMixin, statusCode: number, trailers: Record<string, string>}>}
*/
async #request(options = {}) {
if (options.redirectable) {
const { redirect } = interceptors;
Expand All @@ -148,7 +158,7 @@ export default class HttpClient {
labels: {
method: options.method,
status: statusCode,
url: options.url,
url: `${options.origin}${options.path}`,
},
});

Expand Down
7 changes: 6 additions & 1 deletion tests/http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ await test('http-client - redirects', async (t) => {
});
// await t.test.skip('throw on max redirects', async () => {});
await t.test('does not follow redirects by default', async () => {
const client = new HttpClient({ threshold: 50 });
const client = new HttpClient();
const response = await client.request({
method: 'GET',
origin: `http://${host}:${port}`,
Expand Down Expand Up @@ -323,6 +323,11 @@ await test('http-client: metrics', async (t) => {
strictEqual(requestMetrics[0].labels[0].value, 'GET');
strictEqual(requestMetrics[0].labels[1].name, 'status');
strictEqual(requestMetrics[0].labels[1].value, 200);
strictEqual(requestMetrics[0].labels[2].name, 'url');
strictEqual(
requestMetrics[0].labels[2].value,
'http://localhost:3003/',
);
ok(requestMetrics[0].value > 0);

strictEqual(requestMetrics[2].type, 2);
Expand Down

0 comments on commit efe65e7

Please sign in to comment.