Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pushgateway: adds configuration for omitting job name #597

Merged
Merged
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
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
Loading