From 4e6d0bf341d862457dd2ef2c4c78f16964930d14 Mon Sep 17 00:00:00 2001 From: Felippe Leitao Nacif Date: Tue, 6 Jun 2023 10:48:16 -0300 Subject: [PATCH 1/3] fix: pushgateway reject with timeout --- lib/pushgateway.js | 4 ++++ test/pushgatewayTest.js | 49 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/pushgateway.js b/lib/pushgateway.js index cbc22439..c87abdb5 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -79,6 +79,10 @@ async function useGateway(method, job, groupings) { reject(err); }); + req.on('timeout', () => { + reject(new Error('Pushgateway request timed out')); + }); + if (method !== 'DELETE') { this.registry .metrics() diff --git a/test/pushgatewayTest.js b/test/pushgatewayTest.js index f61f480e..8bfce05c 100644 --- a/test/pushgatewayTest.js +++ b/test/pushgatewayTest.js @@ -66,6 +66,23 @@ describe.each([ expect(mockHttp.isDone()); }); }); + + it('should timeout when taking too long', () => { + const mockHttp = nock('http://192.168.99.100:9091') + .post('/metrics/job/testJob/key/va%26lue', body) + .delay(100) + .reply(200); + + expect.assertions(1); + return instance + .pushAdd({ + jobName: 'testJob', + groupings: { key: 'va&lue' }, + }) + .catch(err => { + expect(err.message).toStrictEqual('Pushgateway request timed out'); + }); + }); }); describe('push', () => { @@ -88,6 +105,18 @@ describe.each([ expect(mockHttp.isDone()); }); }); + + it('should timeout when taking too long', () => { + const mockHttp = nock('http://192.168.99.100:9091') + .put('/metrics/job/test%26Job', body) + .delay(100) + .reply(200); + + expect.assertions(1); + return instance.push({ jobName: 'test&Job' }).catch(err => { + expect(err.message).toStrictEqual('Pushgateway request timed out'); + }); + }); }); describe('delete', () => { @@ -100,6 +129,18 @@ describe.each([ expect(mockHttp.isDone()); }); }); + + it('should timeout when taking too long', () => { + const mockHttp = nock('http://192.168.99.100:9091') + .delete('/metrics/job/testJob') + .delay(100) + .reply(200); + + expect.assertions(1); + return instance.delete({ jobName: 'testJob' }).catch(err => { + expect(err.message).toStrictEqual('Pushgateway request timed out'); + }); + }); }); describe('when using basic authentication', () => { @@ -202,7 +243,7 @@ describe.each([ beforeEach(() => { registry = undefined; - instance = new Pushgateway('http://192.168.99.100:9091'); + instance = new Pushgateway('http://192.168.99.100:9091', { timeout: 10 }); const promClient = require('../index'); const cnt = new promClient.Counter({ name: 'test', help: 'test' }); cnt.inc(100); @@ -218,7 +259,11 @@ describe.each([ beforeEach(() => { registry = new Registry(regType); - instance = new Pushgateway('http://192.168.99.100:9091', null, registry); + instance = new Pushgateway( + 'http://192.168.99.100:9091', + { timeout: 10 }, + registry, + ); const promeClient = require('../index'); const cnt = new promeClient.Counter({ name: 'test', From 5b25669a9bcc9da3f6a9d6445fefa7c587e981d5 Mon Sep 17 00:00:00 2001 From: Felippe Leitao Nacif Date: Tue, 6 Jun 2023 13:59:11 -0300 Subject: [PATCH 2/3] fix: destroying request on timeout --- lib/pushgateway.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pushgateway.js b/lib/pushgateway.js index c87abdb5..1b1b666d 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -80,7 +80,7 @@ async function useGateway(method, job, groupings) { }); req.on('timeout', () => { - reject(new Error('Pushgateway request timed out')); + req.destroy(new Error('Pushgateway request timed out')); }); if (method !== 'DELETE') { From 9bfed0de16c389424431039f54a061d384bec138 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 9 Oct 2023 10:36:49 +0200 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2137ef55..edf17eb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ project adheres to [Semantic Versioning](http://semver.org/). - Correct TS types for working with OpenMetrics - Updated Typescript and Readme docs for `setToCurrentTime()` to reflect units as seconds. - Do not ignore error if request to pushgateway fails +- Make sure to reject the request to pushgateway if it times out ### Added