Skip to content

Commit

Permalink
Lazy-load cluster module to fix Passenger
Browse files Browse the repository at this point in the history
lint fixes

Explicitely cache cluster module

Fix worker listener

Rebase & append CHANGELOG
  • Loading branch information
Laurent Goudet authored and zbjornson committed Nov 12, 2019
1 parent 83e393b commit 29b6d86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Changed `Metric` labelNames & labelValues in TypeScript declaration to a generic type `T extends string`, instead of `string`
- Lazy-load Node.js Cluster module to fix Passenger support (#293)

### Added

Expand Down
40 changes: 22 additions & 18 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
* cluster master.
*/

const cluster = require('cluster');
const Registry = require('./registry');
const { Grouper } = require('./util');
const { aggregators } = require('./metricAggregators');
// We need to lazy-load the 'cluster' module as some application servers -
// namely Passenger - crash when it is imported.
let cluster = () => {
const data = require('cluster');
cluster = () => data;
return data;
};

const GET_METRICS_REQ = 'prom-client:getMetricsReq';
const GET_METRICS_RES = 'prom-client:getMetricsRes';
Expand Down Expand Up @@ -66,11 +72,11 @@ class AggregatorRegistry extends Registry {
requestId
};

for (const id in cluster.workers) {
for (const id in cluster().workers) {
// If the worker exits abruptly, it may still be in the workers
// list but not able to communicate.
if (cluster.workers[id].isConnected()) {
cluster.workers[id].send(message);
if (cluster().workers[id].isConnected()) {
cluster().workers[id].send(message);
request.pending++;
}
}
Expand Down Expand Up @@ -153,9 +159,9 @@ function addListeners() {
if (listenersAdded) return;
listenersAdded = true;

if (cluster.isMaster) {
if (cluster().isMaster) {
// Listen for worker responses to requests for local metrics
cluster.on('message', (worker, message) => {
cluster().on('message', (worker, message) => {
if (message.type === GET_METRICS_RES) {
const request = requests.get(message.requestId);
message.metrics.forEach(registry => request.responses.push(registry));
Expand All @@ -177,17 +183,15 @@ function addListeners() {
}
}

if (cluster.isWorker) {
// Respond to master's requests for worker's local metrics.
process.on('message', message => {
if (message.type === GET_METRICS_REQ) {
process.send({
type: GET_METRICS_RES,
requestId: message.requestId,
metrics: registries.map(r => r.getMetricsAsJSON())
});
}
});
}
// Respond to master's requests for worker's local metrics.
process.on('message', message => {
if (cluster().isWorker && message.type === GET_METRICS_REQ) {
process.send({
type: GET_METRICS_RES,
requestId: message.requestId,
metrics: registries.map(r => r.getMetricsAsJSON())
});
}
});

module.exports = AggregatorRegistry;

0 comments on commit 29b6d86

Please sign in to comment.