Skip to content

Commit

Permalink
pushgateway: adds configuration for omitting job name (for gravel gat…
Browse files Browse the repository at this point in the history
…eway compatibility, etc.)
  • Loading branch information
Geof Holbrook authored and zbjornson committed Nov 25, 2023
1 parent efdb283 commit 2ffd316
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Allow Pushgateway to now require job names for compatibility with Gravel Gateway.

## [15.0.0] - 2023-10-09

### Breaking
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,16 @@ gateway = new client.Pushgateway('http://127.0.0.1:9091', {
});
```

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.

```js
gravelGateway = new client.Pushgateway('http://127.0.0.1:9091', {
timeout: 5000,
requireJobName: false,
});
gravelGateway.pushAdd();
```

### Bucket Generators

For convenience, there are two bucket generator functions - linear and
Expand Down
26 changes: 16 additions & 10 deletions lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ class Pushgateway {
}
this.registry = registry;
this.gatewayUrl = gatewayUrl;
this.requestOptions = Object.assign({}, options);
const { requireJobName, ...requestOptions } = {
requireJobName: true,
...options,
};
this.requireJobName = requireJobName;
this.requestOptions = requestOptions;
}

pushAdd(params) {
if (!params || !params.jobName) {
pushAdd(params = {}) {
if (this.requireJobName && !params.jobName) {
throw new Error('Missing jobName parameter');
}

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

push(params) {
if (!params || !params.jobName) {
push(params = {}) {
if (this.requireJobName && !params.jobName) {
throw new Error('Missing jobName parameter');
}

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

delete(params) {
if (!params || !params.jobName) {
delete(params = {}) {
if (this.requireJobName && !params.jobName) {
throw new Error('Missing jobName parameter');
}

Expand All @@ -48,9 +53,10 @@ async function useGateway(method, job, groupings) {
gatewayUrlParsed.pathname && gatewayUrlParsed.pathname !== '/'
? gatewayUrlParsed.pathname
: '';
const path = `${gatewayUrlPath}/metrics/job/${encodeURIComponent(
job,
)}${generateGroupings(groupings)}`;
const jobPath = job
? `/job/${encodeURIComponent(job)}${generateGroupings(groupings)}`
: '';
const path = `${gatewayUrlPath}/metrics${jobPath}`;

// eslint-disable-next-line node/no-deprecated-api
const target = url.resolve(this.gatewayUrl, path);
Expand Down
17 changes: 17 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ describe.each([
expect(err.message).toStrictEqual('Pushgateway request timed out');
});
});

it('should be possible to configure for gravel gateway integration (no job name required in path)', async () => {
const mockHttp = nock('http://192.168.99.100:9091')
.post('/metrics', body)
.reply(200);

instance = new Pushgateway(
'http://192.168.99.100:9091',
{
timeout: 10,
requireJobName: false,
},
registry,
);

return instance.pushAdd().then(() => expect(mockHttp.isDone()));
});
});

describe('push', () => {
Expand Down

0 comments on commit 2ffd316

Please sign in to comment.