From 9733ef9af42e234f0c9208eff4a51353568b0d91 Mon Sep 17 00:00:00 2001 From: Vittorio Palmisano Date: Tue, 23 Aug 2022 14:58:07 +0200 Subject: [PATCH] Support gzipped pushgateway requests (#508) --- CHANGELOG.md | 2 ++ lib/pushgateway.js | 7 +++++++ test/pushgatewayTest.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f2c68c0..72bd1737 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pushgateway.js b/lib/pushgateway.js index 8201ad5d..9e3dca26 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -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 { @@ -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(); }) diff --git a/test/pushgatewayTest.js b/test/pushgatewayTest.js index 8a20c968..6e84f8a2 100644 --- a/test/pushgatewayTest.js +++ b/test/pushgatewayTest.js @@ -1,6 +1,7 @@ 'use strict'; const nock = require('nock'); +const { gzipSync } = require('zlib'); describe('pushgateway', () => { const Pushgateway = require('../index').Pushgateway; @@ -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', () => {