Skip to content

Commit

Permalink
Support gzipped pushgateway requests (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpalmisano authored Aug 23, 2022
1 parent 9e45dfc commit 9733ef9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
`processRequests` metrics along with information about any other types of
async resources that these metrics do not keep a track of (like timers).

- Support gzipped pushgateway requests

## [14.0.1] - 2021-11-02

### Changed
Expand Down
7 changes: 7 additions & 0 deletions lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const url = require('url');
const http = require('http');
const https = require('https');
const { gzipSync } = require('zlib');
const { globalRegistry } = require('./registry');

class Pushgateway {
Expand Down Expand Up @@ -79,6 +80,12 @@ async function useGateway(method, job, groupings) {
this.registry
.metrics()
.then(metrics => {
if (
options.headers &&
options.headers['Content-Encoding'] === 'gzip'
) {
metrics = gzipSync(metrics);
}
req.write(metrics);
req.end();
})
Expand Down
28 changes: 28 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const nock = require('nock');
const { gzipSync } = require('zlib');

describe('pushgateway', () => {
const Pushgateway = require('../index').Pushgateway;
Expand Down Expand Up @@ -177,6 +178,33 @@ describe('pushgateway', () => {
expect(mockHttp.isDone());
});
});

it('should use gzip request', () => {
const mockHttp = nock('http://192.168.99.100:9091', {
reqheaders: {
'Content-Encoding': 'gzip',
},
})
.post(
'/metrics/job/testJob',
gzipSync('# HELP test test\n# TYPE test counter\ntest 100\n'),
)
.reply(200);

instance = new Pushgateway(
'http://192.168.99.100:9091',
{
headers: {
'Content-Encoding': 'gzip',
},
},
registry,
);

return instance.pushAdd({ jobName: 'testJob' }).then(() => {
expect(mockHttp.isDone());
});
});
};

describe('global registry', () => {
Expand Down

0 comments on commit 9733ef9

Please sign in to comment.