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 diff --git a/lib/pushgateway.js b/lib/pushgateway.js index 0c916cd7..d2bc64d5 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -85,6 +85,10 @@ async function useGateway(method, job, groupings) { reject(err); }); + req.on('timeout', () => { + req.destroy(new Error('Pushgateway request timed out')); + }); + if (method !== 'DELETE') { this.registry .metrics() diff --git a/test/pushgatewayTest.js b/test/pushgatewayTest.js index 8b711efe..03ab68a3 100644 --- a/test/pushgatewayTest.js +++ b/test/pushgatewayTest.js @@ -79,6 +79,23 @@ describe.each([ }), ).rejects.toThrow('push failed with status 400'); }); + + 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', () => { @@ -114,6 +131,18 @@ describe.each([ }), ).rejects.toThrow('push failed with status 400'); }); + + 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', () => { @@ -136,6 +165,18 @@ describe.each([ 'push failed with status 400', ); }); + + 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', () => { @@ -238,7 +279,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); @@ -254,7 +295,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',