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 committed Oct 26, 2023
1 parent 629cd5b commit d1637a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
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 = Object.assign({}, 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
13 changes: 13 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ 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.requireJobName = false;

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

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

0 comments on commit d1637a5

Please sign in to comment.