Skip to content

Commit f177b1f

Browse files
PierrickPzbjornson
authored andcommitted
Promisify pushgateway methods
1 parent 6f1f3b2 commit f177b1f

File tree

6 files changed

+199
-151
lines changed

6 files changed

+199
-151
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ project adheres to [Semantic Versioning](http://semver.org/).
1414
Fewer bucket bounds will be affected by rounding errors. Histogram bucket
1515
labels may change.
1616

17+
- changed: The push gateway methods `pushAdd()`, `push()` and `delete()` now
18+
return Promises instead of accepting a callback:
19+
20+
```js
21+
// Old:
22+
gateway.pushAdd({ jobName: 'test' }, (err, resp, body) => {});
23+
// New:
24+
gateway
25+
.pushAdd({ jobName: 'test' })
26+
.then(({ resp, body }) => {})
27+
.catch(err => {});
28+
// or
29+
const { resp, body } = await gateway.pushAdd({ jobName: 'test' });
30+
```
31+
1732
### Changed
1833

1934
### Added

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,16 +490,36 @@ It is possible to push metrics via a
490490
const client = require('prom-client');
491491
let gateway = new client.Pushgateway('http://127.0.0.1:9091');
492492

493-
gateway.pushAdd({ jobName: 'test' }, function (err, resp, body) {}); //Add metric and overwrite old ones
494-
gateway.push({ jobName: 'test' }, function (err, resp, body) {}); //Overwrite all metrics (use PUT)
495-
gateway.delete({ jobName: 'test' }, function (err, resp, body) {}); //Delete all metrics for jobName
493+
gateway.pushAdd({ jobName: 'test' })
494+
.then({resp, body} => {
495+
/* ... */
496+
})
497+
.catch(err => {
498+
/* ... */
499+
})); //Add metric and overwrite old ones
500+
gateway.push({ jobName: 'test' })
501+
.then({resp, body} => {
502+
/* ... */
503+
})
504+
.catch(err => {
505+
/* ... */
506+
})); //Overwrite all metrics (use PUT)
507+
gateway.delete({ jobName: 'test' })
508+
.then({resp, body} => {
509+
/* ... */
510+
})
511+
.catch(err => {
512+
/* ... */
513+
})); //Delete all metrics for jobName
496514

497515
//All gateway requests can have groupings on it
498-
gateway.pushAdd({ jobName: 'test', groupings: { key: 'value' } }, function (
499-
err,
500-
resp,
501-
body,
502-
) {});
516+
gateway.pushAdd({ jobName: 'test', groupings: { key: 'value' } })
517+
.then({resp, body} => {
518+
/* ... */
519+
})
520+
.catch(err => {
521+
/* ... */
522+
}));
503523

504524
// It's possible to extend the Pushgateway with request options from nodes core
505525
// http/https library. In particular, you might want to provide an agent so that

example/pushgateway.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ function run() {
1616
register.registerMetric(test);
1717
test.inc(10);
1818

19-
gateway.push({ jobName: prefix }, (err, resp, body) => {
20-
console.log(`Error: ${err}`);
21-
console.log(`Body: ${body}`);
22-
console.log(`Response status: ${resp.statusCode}`);
23-
});
19+
return gateway
20+
.push({ jobName: prefix })
21+
.then(({ resp, body }) => {
22+
console.log(`Body: ${body}`);
23+
console.log(`Response status: ${resp.statusCode}`);
24+
})
25+
.catch(err => {
26+
console.log(`Error: ${err}`);
27+
});
2428
}
2529

2630
run();

lib/pushgateway.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ class Pushgateway {
1515
this.requestOptions = Object.assign({}, options);
1616
}
1717

18-
pushAdd(params, callback) {
18+
pushAdd(params) {
1919
if (!params || !params.jobName) {
2020
throw new Error('Missing jobName parameter');
2121
}
2222

23-
useGateway.call(this, 'POST', params.jobName, params.groupings, callback);
23+
return useGateway.call(this, 'POST', params.jobName, params.groupings);
2424
}
2525

26-
push(params, callback) {
26+
push(params) {
2727
if (!params || !params.jobName) {
2828
throw new Error('Missing jobName parameter');
2929
}
3030

31-
useGateway.call(this, 'PUT', params.jobName, params.groupings, callback);
31+
return useGateway.call(this, 'PUT', params.jobName, params.groupings);
3232
}
3333

34-
delete(params, callback) {
34+
delete(params) {
3535
if (!params || !params.jobName) {
3636
throw new Error('Missing jobName parameter');
3737
}
3838

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

63-
const req = httpModule.request(options, res => {
64-
let body = '';
65-
res.setEncoding('utf8');
66-
res.on('data', chunk => {
67-
body += chunk;
63+
return new Promise((resolve, reject) => {
64+
const req = httpModule.request(options, resp => {
65+
let body = '';
66+
resp.setEncoding('utf8');
67+
resp.on('data', chunk => {
68+
body += chunk;
69+
});
70+
resp.on('end', () => {
71+
resolve({ resp, body });
72+
});
6873
});
69-
res.on('end', () => {
70-
callback(null, res, body);
74+
req.on('error', err => {
75+
reject(err);
7176
});
72-
});
73-
req.on('error', err => {
74-
callback(err);
75-
});
7677

77-
if (method !== 'DELETE') {
78-
this.registry
79-
.metrics()
80-
.then(metrics => {
81-
req.write(metrics);
82-
req.end();
83-
})
84-
.catch(err => {
85-
req.end();
86-
callback(err);
87-
});
88-
} else {
89-
req.end();
90-
}
78+
if (method !== 'DELETE') {
79+
this.registry
80+
.metrics()
81+
.then(metrics => {
82+
req.write(metrics);
83+
req.end();
84+
})
85+
.catch(err => {
86+
reject(err);
87+
});
88+
} else {
89+
req.end();
90+
}
91+
});
9192
}
9293

9394
function generateGroupings(groupings) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"husky": "^4.2.1",
4141
"jest": "^26.0.1",
4242
"lint-staged": "^10.0.4",
43+
"nock": "^13.0.5",
4344
"prettier": "2.0.5",
4445
"typescript": "^4.0.2"
4546
},

0 commit comments

Comments
 (0)