diff --git a/README.md b/README.md index 94685c9..89ef08c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ express middleware with popular prometheus metrics in one bundle. Internally it uses **prom-client**. See: https://github.com/siimon/prom-client Included metrics: - + * `up`: normally is just 1 * `nodejs_memory_heap_total_bytes` and `nodejs_memory_heap_used_bytes` * `http_request_seconds`: http latency histogram labeled with `status_code` @@ -22,13 +22,25 @@ npm install express-prom-bundle ```javascript const promBundle = require("express-prom-bundle"), -const metricsMiddleware = promBundle({/* options */ }); + metricsMiddleware = promBundle({/* options */ }); app.use(metricsMiddleware); app.use(/* your middleware */); app.listen(3000); ``` +**Usage Koa Framework (currently only v1)** + +```javascript +const promBundle = require("express-prom-bundle"), + c2k = require("koa-connect"), + metricsMiddleware = promBundle({/* options */ }); + +app.use(c2k(metricsMiddleware)); +app.use(/* your middleware */); +app.listen(3000); +``` + * call your endpoints * see your metrics here: [http://localhost:3000/metrics](http://localhost:3000/metrics) @@ -56,8 +68,8 @@ setup std. metrics but exclude `up`-metric: "use strict"; const express = require("express"), - app = express(), - promBundle = require("express-prom-bundle"); + app = express(), + promBundle = require("express-prom-bundle"); // calls to this route will not appear in metrics diff --git a/package.json b/package.json index 1ff01c7..b50b1c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-prom-bundle", - "version": "1.1.7", + "version": "1.1.8", "description": "express middleware with popular prometheus metrics in one bundle", "main": "src/index.js", "keywords": [ @@ -24,7 +24,10 @@ "express": "^4.13.4", "istanbul": "^0.4.4", "jasme": "^4.1.2", - "supertest": "^1.2.0" + "koa": "^1.2.4", + "koa-connect": "^1.0.0", + "supertest": "^1.2.0", + "supertest-koa-agent": "^0.3.0" }, "repository": { "type": "git", diff --git a/spec/indexSpec.js b/spec/indexSpec.js index f5f2e2f..e77a3a1 100644 --- a/spec/indexSpec.js +++ b/spec/indexSpec.js @@ -3,7 +3,10 @@ let express = require("express"), supertest = require("supertest"), - bundle = require("../"); + bundle = require("../"), + koa = require("koa"), + c2k = require("koa-connect"), + supertestKoa = require("supertest-koa-agent"); describe("index", () => { it("metrics returns up=1", done => { @@ -137,4 +140,31 @@ describe("index", () => { }); }); }); + + it("Koa: metrics returns up=1", done => { + const app = koa(); + const bundled = bundle({ + prefix: "hello:", + whitelist: ["up"] + }); + app.use(c2k(bundled)); + + app.use(function*(next) { + if (this.path !== "test") { + return yield next; + } + this.body = "it worked"; + }); + + const agent = supertestKoa(app); + agent.get("/test").end(() => { + agent + .get("/metrics") + .end((err, res) => { + expect(res.status).toBe(200); + expect(res.text).toMatch(/hello:up\s1/); + done(); + }); + }); + }); }); diff --git a/src/index.js b/src/index.js index 7bdac78..6a4d1d5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,8 @@ "use strict"; -const - PromFactory = require("./PromFactory"), - onFinished = require("on-finished"); +const PromFactory = require("./PromFactory"), + onFinished = require("on-finished"), + url = require("url"); function matchVsRegExps(element, regexps) { for (let regexp of regexps) { @@ -17,7 +17,6 @@ function matchVsRegExps(element, regexps) { return false; } - function filterArrayByRegExps(array, regexps) { return array.filter(element => { return matchVsRegExps(element, regexps); @@ -78,9 +77,8 @@ function main(opts) { } }; - const - metrics = {}, - names = prepareMetricNames(opts, metricTemplates); + const metrics = {}, + names = prepareMetricNames(opts, metricTemplates); for (let name of names) { @@ -100,20 +98,23 @@ function main(opts) { metrics["nodejs_memory_heap_used_bytes"].set(memoryUsage.heapUsed); } - res.contentType("text/plain").send(factory.promClient.register.metrics()); - return; + res.writeHead(200, {"Content-Type": "text/plain"}); + res.end(factory.promClient.register.metrics()); }; const middleware = function (req, res, next) { - if (opts.autoregister && req.path == "/metrics") { + + const path = req.path || url.parse(req.url).pathname; + let labels; + + if (opts.autoregister && path == "/metrics") { return metricsMiddleware(req,res); } - if (opts.excludeRoutes && matchVsRegExps(req.path, opts.excludeRoutes)) { + if (opts.excludeRoutes && matchVsRegExps(path, opts.excludeRoutes)) { return next(); } - let labels; if (metrics["http_request_seconds"]) { labels = {"status_code": 0}; let timer = metrics["http_request_seconds"].startTimer(labels); @@ -123,7 +124,8 @@ function main(opts) { }); } - next(); + next(); + }; middleware.factory = factory;