Skip to content

Commit

Permalink
Promisify pushgateway methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickP authored and zbjornson committed Sep 19, 2021
1 parent 6f1f3b2 commit f177b1f
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 151 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ project adheres to [Semantic Versioning](http://semver.org/).
Fewer bucket bounds will be affected by rounding errors. Histogram bucket
labels may change.

- changed: The push gateway methods `pushAdd()`, `push()` and `delete()` now
return Promises instead of accepting a callback:

```js
// Old:
gateway.pushAdd({ jobName: 'test' }, (err, resp, body) => {});
// New:
gateway
.pushAdd({ jobName: 'test' })
.then(({ resp, body }) => {})
.catch(err => {});
// or
const { resp, body } = await gateway.pushAdd({ jobName: 'test' });
```

### Changed

### Added
Expand Down
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,36 @@ It is possible to push metrics via a
const client = require('prom-client');
let gateway = new client.Pushgateway('http://127.0.0.1:9091');

gateway.pushAdd({ jobName: 'test' }, function (err, resp, body) {}); //Add metric and overwrite old ones
gateway.push({ jobName: 'test' }, function (err, resp, body) {}); //Overwrite all metrics (use PUT)
gateway.delete({ jobName: 'test' }, function (err, resp, body) {}); //Delete all metrics for jobName
gateway.pushAdd({ jobName: 'test' })
.then({resp, body} => {
/* ... */
})
.catch(err => {
/* ... */
})); //Add metric and overwrite old ones
gateway.push({ jobName: 'test' })
.then({resp, body} => {
/* ... */
})
.catch(err => {
/* ... */
})); //Overwrite all metrics (use PUT)
gateway.delete({ jobName: 'test' })
.then({resp, body} => {
/* ... */
})
.catch(err => {
/* ... */
})); //Delete all metrics for jobName

//All gateway requests can have groupings on it
gateway.pushAdd({ jobName: 'test', groupings: { key: 'value' } }, function (
err,
resp,
body,
) {});
gateway.pushAdd({ jobName: 'test', groupings: { key: 'value' } })
.then({resp, body} => {
/* ... */
})
.catch(err => {
/* ... */
}));

// It's possible to extend the Pushgateway with request options from nodes core
// http/https library. In particular, you might want to provide an agent so that
Expand Down
14 changes: 9 additions & 5 deletions example/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ function run() {
register.registerMetric(test);
test.inc(10);

gateway.push({ jobName: prefix }, (err, resp, body) => {
console.log(`Error: ${err}`);
console.log(`Body: ${body}`);
console.log(`Response status: ${resp.statusCode}`);
});
return gateway
.push({ jobName: prefix })
.then(({ resp, body }) => {
console.log(`Body: ${body}`);
console.log(`Response status: ${resp.statusCode}`);
})
.catch(err => {
console.log(`Error: ${err}`);
});
}

run();
65 changes: 33 additions & 32 deletions lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ class Pushgateway {
this.requestOptions = Object.assign({}, options);
}

pushAdd(params, callback) {
pushAdd(params) {
if (!params || !params.jobName) {
throw new Error('Missing jobName parameter');
}

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

push(params, callback) {
push(params) {
if (!params || !params.jobName) {
throw new Error('Missing jobName parameter');
}

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

delete(params, callback) {
delete(params) {
if (!params || !params.jobName) {
throw new Error('Missing jobName parameter');
}

useGateway.call(this, 'DELETE', params.jobName, params.groupings, callback);
return useGateway.call(this, 'DELETE', params.jobName, params.groupings);
}
}
function useGateway(method, job, groupings, callback) {
async function useGateway(method, job, groupings) {
// `URL` first added in v6.13.0
// eslint-disable-next-line node/no-deprecated-api
const gatewayUrlParsed = url.parse(this.gatewayUrl);
Expand All @@ -60,34 +60,35 @@ function useGateway(method, job, groupings, callback) {
method,
});

const req = httpModule.request(options, res => {
let body = '';
res.setEncoding('utf8');
res.on('data', chunk => {
body += chunk;
return new Promise((resolve, reject) => {
const req = httpModule.request(options, resp => {
let body = '';
resp.setEncoding('utf8');
resp.on('data', chunk => {
body += chunk;
});
resp.on('end', () => {
resolve({ resp, body });
});
});
res.on('end', () => {
callback(null, res, body);
req.on('error', err => {
reject(err);
});
});
req.on('error', err => {
callback(err);
});

if (method !== 'DELETE') {
this.registry
.metrics()
.then(metrics => {
req.write(metrics);
req.end();
})
.catch(err => {
req.end();
callback(err);
});
} else {
req.end();
}
if (method !== 'DELETE') {
this.registry
.metrics()
.then(metrics => {
req.write(metrics);
req.end();
})
.catch(err => {
reject(err);
});
} else {
req.end();
}
});
}

function generateGroupings(groupings) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"husky": "^4.2.1",
"jest": "^26.0.1",
"lint-staged": "^10.0.4",
"nock": "^13.0.5",
"prettier": "2.0.5",
"typescript": "^4.0.2"
},
Expand Down

0 comments on commit f177b1f

Please sign in to comment.