diff --git a/spec/indexSpec.js b/spec/indexSpec.js index 1b468ec..f5f2e2f 100644 --- a/spec/indexSpec.js +++ b/spec/indexSpec.js @@ -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", () => { }); }); }); -}); \ No newline at end of file +}); diff --git a/src/index.js b/src/index.js index 1dcaadb..df8ffec 100644 --- a/src/index.js +++ b/src/index.js @@ -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("

500 Error

\n" - + "

Unexapected 3d param in express-prom-bundle.\n" + + "

Unexapected 3rd param in express-prom-bundle.\n" + "

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; \ No newline at end of file +module.exports = main;