From bdfa53a603054cdfd1b3bc5cb5c4be1f29c842f5 Mon Sep 17 00:00:00 2001 From: Jace Date: Wed, 8 Jul 2026 15:59:33 -0700 Subject: [PATCH] Support registry as Pushgateway second argument Signed-off-by: Jace --- CHANGELOG.md | 1 + index.d.ts | 12 +++++++++++- lib/pushgateway.js | 7 ++++++- test/pushgatewayTest.js | 16 ++++++++++++++++ test/typescript.ts | 16 +++++++++++++++- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e89b4e..15422c9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This release marks our first release under the Prometheus umbrella. ### Changed +- Allow `Pushgateway` to accept a custom registry as the second constructor argument - Add `Registry#getMetricsAsString()` to the TypeScript definitions - Improve types for no labels - perf: Faster stats gathering with lower memory overhead diff --git a/index.d.ts b/index.d.ts index c79f49cf..41a55f2c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -773,7 +773,12 @@ export class Pushgateway { * @param options Options * @param registry Registry */ - constructor(url: string, options?: any, registry?: Registry); + constructor(url: string, registry: Registry); + constructor( + url: string, + options?: Pushgateway.Options | null, + registry?: Registry, + ); /** * Add metric and overwrite old ones @@ -801,6 +806,11 @@ export class Pushgateway { } export namespace Pushgateway { + interface Options { + requireJobName?: boolean; + [key: string]: unknown; + } + interface Parameters { /** * Jobname that is pushing the metric diff --git a/lib/pushgateway.js b/lib/pushgateway.js index 6ac7f495..ffcbd808 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -3,10 +3,15 @@ const http = require('http'); const https = require('https'); const { gzipSync } = require('zlib'); -const { globalRegistry } = require('./registry'); +const Registry = require('./registry'); +const { globalRegistry } = Registry; class Pushgateway { constructor(gatewayUrl, options, registry) { + if (options instanceof Registry) { + registry = options; + options = undefined; + } if (!registry) { registry = globalRegistry; } diff --git a/test/pushgatewayTest.js b/test/pushgatewayTest.js index f7a510e9..812b1199 100644 --- a/test/pushgatewayTest.js +++ b/test/pushgatewayTest.js @@ -326,6 +326,22 @@ describe.each([ cnt.inc(100); }); + it('should use a registry passed as the second constructor argument', () => { + const body = + regType === Registry.OPENMETRICS_CONTENT_TYPE + ? '# HELP test test\n# TYPE test counter\ntest_total 100\n# EOF\n' + : '# HELP test test\n# TYPE test counter\ntest 100\n'; + const mockHttp = nock('http://192.168.99.100:9091') + .put('/metrics/job/testJob', body) + .reply(200); + + instance = new Pushgateway('http://192.168.99.100:9091', registry); + + return instance.push({ jobName: 'testJob' }).then(() => { + expect(mockHttp.isDone()); + }); + }); + tests(); }); }); diff --git a/test/typescript.ts b/test/typescript.ts index f660e901..13840a38 100644 --- a/test/typescript.ts +++ b/test/typescript.ts @@ -1,4 +1,4 @@ -import { Counter, Registry } from '../index'; +import { Counter, Pushgateway, Registry } from '../index'; const registry = new Registry(); const counter = new Counter({ @@ -8,5 +8,19 @@ const counter = new Counter({ }); const metricsText: Promise = registry.getMetricsAsString(counter); +const gatewayWithRegistry = new Pushgateway('http://127.0.0.1:9091', registry); +const gatewayWithOptionsAndRegistry = new Pushgateway( + 'http://127.0.0.1:9091', + { timeout: 10, requireJobName: false }, + registry, +); +const gatewayWithNullOptionsAndRegistry = new Pushgateway( + 'http://127.0.0.1:9091', + null, + registry, +); void metricsText; +void gatewayWithRegistry; +void gatewayWithOptionsAndRegistry; +void gatewayWithNullOptionsAndRegistry;