Skip to content

Commit 9733ef9

Browse files
authored
Support gzipped pushgateway requests (#508)
1 parent 9e45dfc commit 9733ef9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
3636
`processRequests` metrics along with information about any other types of
3737
async resources that these metrics do not keep a track of (like timers).
3838

39+
- Support gzipped pushgateway requests
40+
3941
## [14.0.1] - 2021-11-02
4042

4143
### Changed

lib/pushgateway.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const url = require('url');
44
const http = require('http');
55
const https = require('https');
6+
const { gzipSync } = require('zlib');
67
const { globalRegistry } = require('./registry');
78

89
class Pushgateway {
@@ -79,6 +80,12 @@ async function useGateway(method, job, groupings) {
7980
this.registry
8081
.metrics()
8182
.then(metrics => {
83+
if (
84+
options.headers &&
85+
options.headers['Content-Encoding'] === 'gzip'
86+
) {
87+
metrics = gzipSync(metrics);
88+
}
8289
req.write(metrics);
8390
req.end();
8491
})

test/pushgatewayTest.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const nock = require('nock');
4+
const { gzipSync } = require('zlib');
45

56
describe('pushgateway', () => {
67
const Pushgateway = require('../index').Pushgateway;
@@ -177,6 +178,33 @@ describe('pushgateway', () => {
177178
expect(mockHttp.isDone());
178179
});
179180
});
181+
182+
it('should use gzip request', () => {
183+
const mockHttp = nock('http://192.168.99.100:9091', {
184+
reqheaders: {
185+
'Content-Encoding': 'gzip',
186+
},
187+
})
188+
.post(
189+
'/metrics/job/testJob',
190+
gzipSync('# HELP test test\n# TYPE test counter\ntest 100\n'),
191+
)
192+
.reply(200);
193+
194+
instance = new Pushgateway(
195+
'http://192.168.99.100:9091',
196+
{
197+
headers: {
198+
'Content-Encoding': 'gzip',
199+
},
200+
},
201+
registry,
202+
);
203+
204+
return instance.pushAdd({ jobName: 'testJob' }).then(() => {
205+
expect(mockHttp.isDone());
206+
});
207+
});
180208
};
181209

182210
describe('global registry', () => {

0 commit comments

Comments
 (0)