Skip to content

Commit 2ffd316

Browse files
Geof Holbrookzbjornson
Geof Holbrook
authored andcommitted
pushgateway: adds configuration for omitting job name (for gravel gateway compatibility, etc.)
1 parent efdb283 commit 2ffd316

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
### Added
1818

19+
- Allow Pushgateway to now require job names for compatibility with Gravel Gateway.
20+
1921
## [15.0.0] - 2023-10-09
2022

2123
### Breaking

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,16 @@ gateway = new client.Pushgateway('http://127.0.0.1:9091', {
581581
});
582582
```
583583

584+
Some gateways such as [Gravel Gateway](https://github.com/sinkingpoint/prometheus-gravel-gateway) do not support grouping by job name, exposing a plain `/metrics` endpoint instead of `/metrics/job/<jobName>`. It's possible to configure a gateway instance to not require a jobName in the options argument.
585+
586+
```js
587+
gravelGateway = new client.Pushgateway('http://127.0.0.1:9091', {
588+
timeout: 5000,
589+
requireJobName: false,
590+
});
591+
gravelGateway.pushAdd();
592+
```
593+
584594
### Bucket Generators
585595

586596
For convenience, there are two bucket generator functions - linear and

lib/pushgateway.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ class Pushgateway {
1313
}
1414
this.registry = registry;
1515
this.gatewayUrl = gatewayUrl;
16-
this.requestOptions = Object.assign({}, options);
16+
const { requireJobName, ...requestOptions } = {
17+
requireJobName: true,
18+
...options,
19+
};
20+
this.requireJobName = requireJobName;
21+
this.requestOptions = requestOptions;
1722
}
1823

19-
pushAdd(params) {
20-
if (!params || !params.jobName) {
24+
pushAdd(params = {}) {
25+
if (this.requireJobName && !params.jobName) {
2126
throw new Error('Missing jobName parameter');
2227
}
2328

2429
return useGateway.call(this, 'POST', params.jobName, params.groupings);
2530
}
2631

27-
push(params) {
28-
if (!params || !params.jobName) {
32+
push(params = {}) {
33+
if (this.requireJobName && !params.jobName) {
2934
throw new Error('Missing jobName parameter');
3035
}
3136

3237
return useGateway.call(this, 'PUT', params.jobName, params.groupings);
3338
}
3439

35-
delete(params) {
36-
if (!params || !params.jobName) {
40+
delete(params = {}) {
41+
if (this.requireJobName && !params.jobName) {
3742
throw new Error('Missing jobName parameter');
3843
}
3944

@@ -48,9 +53,10 @@ async function useGateway(method, job, groupings) {
4853
gatewayUrlParsed.pathname && gatewayUrlParsed.pathname !== '/'
4954
? gatewayUrlParsed.pathname
5055
: '';
51-
const path = `${gatewayUrlPath}/metrics/job/${encodeURIComponent(
52-
job,
53-
)}${generateGroupings(groupings)}`;
56+
const jobPath = job
57+
? `/job/${encodeURIComponent(job)}${generateGroupings(groupings)}`
58+
: '';
59+
const path = `${gatewayUrlPath}/metrics${jobPath}`;
5460

5561
// eslint-disable-next-line node/no-deprecated-api
5662
const target = url.resolve(this.gatewayUrl, path);

test/pushgatewayTest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ describe.each([
9696
expect(err.message).toStrictEqual('Pushgateway request timed out');
9797
});
9898
});
99+
100+
it('should be possible to configure for gravel gateway integration (no job name required in path)', async () => {
101+
const mockHttp = nock('http://192.168.99.100:9091')
102+
.post('/metrics', body)
103+
.reply(200);
104+
105+
instance = new Pushgateway(
106+
'http://192.168.99.100:9091',
107+
{
108+
timeout: 10,
109+
requireJobName: false,
110+
},
111+
registry,
112+
);
113+
114+
return instance.pushAdd().then(() => expect(mockHttp.isDone()));
115+
});
99116
});
100117

101118
describe('push', () => {

0 commit comments

Comments
 (0)