Merge branch 'master' of github.com:jochen-schweizer/express-prom-bundle

This commit is contained in:
Konstantin Pogorelov
2016-10-10 13:51:24 +02:00
4 changed files with 64 additions and 23 deletions

View File

@@ -46,6 +46,7 @@ See the example below.
* **whitelist**, **blacklist**: array of strings or regexp specifying which metrics to include/exclude
* **buckets**: buckets used for `http_request_seconds` histogram
* **excludeRoutes**: array of strings or regexp specifying which routes should be skipped for `http_request_seconds` metric. It uses `req.path` as subject when checking
* **autoregister**: boolean. If `/metrics` endpoint should be registered. It is **true** by default
## Example

View File

@@ -1,6 +1,6 @@
{
"name": "express-prom-bundle",
"version": "1.1.6",
"version": "1.1.7",
"description": "express middleware with popular prometheus metrics in one bundle",
"main": "src/index.js",
"keywords": [

View File

@@ -6,14 +6,16 @@ let express = require("express"),
bundle = require("../");
describe("index", () => {
it("/metrics returns up=1", done => {
it("metrics returns up=1", done => {
const app = express();
app.use(bundle({
const bundled = bundle({
prefix: "hello:",
whitelist: ["up"]
}));
});
app.use(bundled);
app.use("/test", (req, res) => res.send("it worked"));
const agent = supertest(app);
agent.get("/test").end(() => {
agent
@@ -25,6 +27,41 @@ describe("index", () => {
});
});
});
it("metrics should be attached to /metrics by default", done => {
const app = express();
const bundled = bundle({
prefix: "hello:",
whitelist: ["up"]
});
app.use(bundled);
const agent = supertest(app);
agent.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
it("metrics can be attached to /metrics programatically", done => {
const app = express();
const bundled = bundle({
autoregister: false
});
app.use(bundled.metricsMiddleware);
app.use(bundled);
app.use("/test", (req, res) => res.send("it worked"));
const agent = supertest(app);
agent.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
it("metrics can be filtered using exect match", () => {
const instance = bundle({blacklist: ["up"]});
expect(instance.metrics.up).not.toBeDefined();
@@ -79,7 +116,7 @@ describe("index", () => {
});
});
it("filters out the excludeRoutes", done => {
const app = express();
const app = express();
const instance = bundle({
excludeRoutes: ["/test"]
});
@@ -100,4 +137,4 @@ describe("index", () => {
});
});
});
});
});

View File

@@ -40,11 +40,11 @@ function prepareMetricNames(opts, metricTemplates) {
}
function main(opts) {
opts = opts === undefined ? {} : opts;
opts = Object.assign({ autoregister: true }, opts || {} );
if (arguments[2] && arguments[1] && arguments[1].send) {
arguments[1].status(500)
.send("<h1>500 Error</h1>\n"
+ "<p>Unexapected 3d param in express-prom-bundle.\n"
+ "<p>Unexpected 3rd param in express-prom-bundle.\n"
+ "<p>Did you just put express-prom-bundle into app.use "
+ "without calling it as a function first?");
return;
@@ -91,19 +91,22 @@ function main(opts) {
metrics.up.set(1);
}
const middleware = function (req, res, next) {
if (req.path == "/metrics") {
let memoryUsage = process.memoryUsage();
if (metrics["nodejs_memory_heap_total_bytes"]) {
metrics["nodejs_memory_heap_total_bytes"].set(memoryUsage.heapTotal);
}
if (metrics["nodejs_memory_heap_used_bytes"]) {
metrics["nodejs_memory_heap_used_bytes"].set(memoryUsage.heapUsed);
}
const metricsMiddleware = function(req,res) {
let memoryUsage = process.memoryUsage();
if (metrics["nodejs_memory_heap_total_bytes"]) {
metrics["nodejs_memory_heap_total_bytes"].set(memoryUsage.heapTotal);
}
if (metrics["nodejs_memory_heap_used_bytes"]) {
metrics["nodejs_memory_heap_used_bytes"].set(memoryUsage.heapUsed);
}
res.contentType("text/plain")
.send(factory.promClient.register.metrics());
return;
res.contentType("text/plain").send(factory.promClient.register.metrics());
return;
};
const middleware = function (req, res, next) {
if (opts.autoregister && req.path == "/metrics") {
return metricsMiddleware(req,res);
}
if (opts.excludeRoutes && matchVsRegExps(req.path, opts.excludeRoutes)) {
@@ -127,8 +130,8 @@ function main(opts) {
middleware.metricTemplates = metricTemplates;
middleware.metrics = metrics;
middleware.promClient = factory.promClient;
middleware.metricsMiddleware = metricsMiddleware;
return middleware;
}
module.exports = main;
module.exports = main;