express-prom-bundle
Advanced tools
Comparing version 4.1.0 to 4.2.0
{ | ||
"name": "express-prom-bundle", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "express middleware with popular prometheus metrics in one bundle", | ||
@@ -14,3 +14,4 @@ "main": "src/index.js", | ||
"scripts": { | ||
"test": "node_modules/jasme/run.js" | ||
"test": "node_modules/jasme/run.js", | ||
"coverage": "make coverage" | ||
}, | ||
@@ -17,0 +18,0 @@ "author": "Konstantin Pogorelov <or@pluseq.com>", |
@@ -43,2 +43,24 @@ [![build status](https://travis-ci.org/jochen-schweizer/express-prom-bundle.png)](https://travis-ci.org/jochen-schweizer/express-prom-bundle) [![Coverage Status](https://coveralls.io/repos/github/jochen-schweizer/express-prom-bundle/badge.svg?branch=master)](https://coveralls.io/github/jochen-schweizer/express-prom-bundle?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://www.tldrlegal.com/l/mit) [![NPM version](https://badge.fury.io/js/express-prom-bundle.png)](http://badge.fury.io/js/express-prom-bundle) | ||
## Usage with Node Cluster | ||
``` javascript | ||
if (cluster.isMaster) { | ||
const numCPUs = Math.max(2, os.cpus().length); | ||
const workers: cluster.Worker[] = []; | ||
for (let i=1; i < numCPUs; i++) { | ||
const worker = forkWorker(); | ||
workers.push(worker); | ||
} | ||
const metricsApp = express(); | ||
metricsApp.use('/cluster_metrics', promBundle.clusterMetrics()); | ||
metricsApp.listen(9999); | ||
console.log('metrics listening on 9999'); // call localhost:9999/cluster_metrics for aggregated metrics | ||
} else { | ||
const app = express(); | ||
app.use(promBundle({includeMethod: true}); | ||
app.use('/api', require('./api')); | ||
app.listen(3000); | ||
} | ||
``` | ||
The code the master process runs will expose an API with a single endpoint `/cluster_metrics` which returns an aggregate of all metrics from all the workers. | ||
## Options | ||
@@ -174,2 +196,38 @@ | ||
## using with cluster | ||
You'll need to use an additional **clusterMetrics()** middleware. | ||
In the example below the master process will expose an API with a single endpoint `/metrics` | ||
which returns an aggregate of all metrics from all the workers. | ||
``` javascript | ||
const cluster = require('cluster'); | ||
const promBundle = require('./src/index'); | ||
const numCPUs = Math.max(2, require('os').cpus().length); | ||
const express = require('express'); | ||
if (cluster.isMaster) { | ||
for (let i = 1; i < numCPUs; i++) { | ||
cluster.fork(); | ||
} | ||
const metricsApp = express(); | ||
metricsApp.use('/metrics', promBundle.clusterMetrics()); | ||
metricsApp.listen(9999); | ||
console.log('cluster metrics listening on 9999'); | ||
console.log('call localhost:9999/metrics for aggregated metrics'); | ||
} else { | ||
const app = express(); | ||
app.use(promBundle({ | ||
autoregister: false, // disable /metrics for single workers | ||
includeMethod: true | ||
})); | ||
app.use((req, res) => res.send(`hello from pid ${process.pid}\n`)); | ||
app.listen(3000); | ||
console.log(`worker ${process.pid} listening on 3000`); | ||
} | ||
``` | ||
## using with kraken.js | ||
@@ -176,0 +234,0 @@ |
@@ -41,2 +41,19 @@ 'use strict'; | ||
function clusterMetrics() { | ||
const aggregatorRegistry = new promClient.AggregatorRegistry(); | ||
const metricsMiddleware = function(req, res, next) { | ||
aggregatorRegistry.clusterMetrics((err, clusterMetrics) => { | ||
if (err) { | ||
console.error(err); | ||
return res.sendStatus(500); | ||
} | ||
res.set('Content-Type', aggregatorRegistry.contentType); | ||
res.send(clusterMetrics); | ||
}); | ||
}; | ||
return metricsMiddleware; | ||
} | ||
function main(opts) { | ||
@@ -124,3 +141,3 @@ opts = Object.assign( | ||
if (opts.autoregister && path.match(/^\/metrics\/?$/)) { | ||
return metricsMiddleware(req, res); | ||
return metricsMiddleware(req, res); | ||
} | ||
@@ -170,2 +187,3 @@ | ||
main.normalizeStatusCode = normalizeStatusCode; | ||
main.clusterMetrics = clusterMetrics; | ||
module.exports = main; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18635
193
272