Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pushgateway reject with timeout #574

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading