Skip to content

Commit 32690d0

Browse files
authored
fix: pushgateway reject with timeout (#574)
1 parent 638aa94 commit 32690d0

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
2323
- Correct TS types for working with OpenMetrics
2424
- Updated Typescript and Readme docs for `setToCurrentTime()` to reflect units as seconds.
2525
- Do not ignore error if request to pushgateway fails
26+
- Make sure to reject the request to pushgateway if it times out
2627

2728
### Added
2829

lib/pushgateway.js

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ async function useGateway(method, job, groupings) {
8585
reject(err);
8686
});
8787

88+
req.on('timeout', () => {
89+
req.destroy(new Error('Pushgateway request timed out'));
90+
});
91+
8892
if (method !== 'DELETE') {
8993
this.registry
9094
.metrics()

test/pushgatewayTest.js

+47-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ describe.each([
7979
}),
8080
).rejects.toThrow('push failed with status 400');
8181
});
82+
83+
it('should timeout when taking too long', () => {
84+
const mockHttp = nock('http://192.168.99.100:9091')
85+
.post('/metrics/job/testJob/key/va%26lue', body)
86+
.delay(100)
87+
.reply(200);
88+
89+
expect.assertions(1);
90+
return instance
91+
.pushAdd({
92+
jobName: 'testJob',
93+
groupings: { key: 'va&lue' },
94+
})
95+
.catch(err => {
96+
expect(err.message).toStrictEqual('Pushgateway request timed out');
97+
});
98+
});
8299
});
83100

84101
describe('push', () => {
@@ -114,6 +131,18 @@ describe.each([
114131
}),
115132
).rejects.toThrow('push failed with status 400');
116133
});
134+
135+
it('should timeout when taking too long', () => {
136+
const mockHttp = nock('http://192.168.99.100:9091')
137+
.put('/metrics/job/test%26Job', body)
138+
.delay(100)
139+
.reply(200);
140+
141+
expect.assertions(1);
142+
return instance.push({ jobName: 'test&Job' }).catch(err => {
143+
expect(err.message).toStrictEqual('Pushgateway request timed out');
144+
});
145+
});
117146
});
118147

119148
describe('delete', () => {
@@ -136,6 +165,18 @@ describe.each([
136165
'push failed with status 400',
137166
);
138167
});
168+
169+
it('should timeout when taking too long', () => {
170+
const mockHttp = nock('http://192.168.99.100:9091')
171+
.delete('/metrics/job/testJob')
172+
.delay(100)
173+
.reply(200);
174+
175+
expect.assertions(1);
176+
return instance.delete({ jobName: 'testJob' }).catch(err => {
177+
expect(err.message).toStrictEqual('Pushgateway request timed out');
178+
});
179+
});
139180
});
140181

141182
describe('when using basic authentication', () => {
@@ -238,7 +279,7 @@ describe.each([
238279

239280
beforeEach(() => {
240281
registry = undefined;
241-
instance = new Pushgateway('http://192.168.99.100:9091');
282+
instance = new Pushgateway('http://192.168.99.100:9091', { timeout: 10 });
242283
const promClient = require('../index');
243284
const cnt = new promClient.Counter({ name: 'test', help: 'test' });
244285
cnt.inc(100);
@@ -254,7 +295,11 @@ describe.each([
254295

255296
beforeEach(() => {
256297
registry = new Registry(regType);
257-
instance = new Pushgateway('http://192.168.99.100:9091', null, registry);
298+
instance = new Pushgateway(
299+
'http://192.168.99.100:9091',
300+
{ timeout: 10 },
301+
registry,
302+
);
258303
const promeClient = require('../index');
259304
const cnt = new promeClient.Counter({
260305
name: 'test',

0 commit comments

Comments
 (0)