Skip to content
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 @@ -22,6 +22,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
- Cleanup code and refactor to be more efficient
- 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

### Added

Expand Down
8 changes: 7 additions & 1 deletion lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ async function useGateway(method, job, groupings) {
body += chunk;
});
resp.on('end', () => {
resolve({ resp, body });
if (resp.statusCode >= 400) {
reject(
new Error(`push failed with status ${resp.statusCode}, ${body}`),
);
} else {
resolve({ resp, body });
}
});
});
req.on('error', err => {
Expand Down
36 changes: 36 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.post('/metrics/job/testJob/key/value', body)
.reply(400);

return expect(
instance.pushAdd({
jobName: 'testJob',
groupings: { key: 'value' },
}),
).rejects.toThrow('push failed with status 400');
});
});

describe('push', () => {
Expand All @@ -88,6 +101,19 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.put('/metrics/job/testJob/key/value', body)
.reply(400);

return expect(
instance.push({
jobName: 'testJob',
groupings: { key: 'value' },
}),
).rejects.toThrow('push failed with status 400');
});
});

describe('delete', () => {
Expand All @@ -100,6 +126,16 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.delete('/metrics/job/testJob')
.reply(400);

return expect(instance.delete({ jobName: 'testJob' })).rejects.toThrow(
'push failed with status 400',
);
});
});

describe('when using basic authentication', () => {
Expand Down