Skip to content

Commit

Permalink
fix: pushgateway reject with timeout (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
FLNacif authored Oct 9, 2023
1 parent 638aa94 commit 32690d0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
49 changes: 47 additions & 2 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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',
Expand Down

0 comments on commit 32690d0

Please sign in to comment.