Skip to content
Open
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 @@ -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
Expand Down
12 changes: 11 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,12 @@ export class Pushgateway<T extends RegistryContentType> {
* @param options Options
* @param registry Registry
*/
constructor(url: string, options?: any, registry?: Registry<T>);
constructor(url: string, registry: Registry<T>);
constructor(
url: string,
options?: Pushgateway.Options | null,
registry?: Registry<T>,
);

/**
* Add metric and overwrite old ones
Expand Down Expand Up @@ -801,6 +806,11 @@ export class Pushgateway<T extends RegistryContentType> {
}

export namespace Pushgateway {
interface Options {
requireJobName?: boolean;
[key: string]: unknown;
}

interface Parameters {
/**
* Jobname that is pushing the metric
Expand Down
7 changes: 6 additions & 1 deletion lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
16 changes: 15 additions & 1 deletion test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Counter, Registry } from '../index';
import { Counter, Pushgateway, Registry } from '../index';

const registry = new Registry();
const counter = new Counter({
Expand All @@ -8,5 +8,19 @@ const counter = new Counter({
});

const metricsText: Promise<string> = 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;
Loading