add excludeRoutes option

This commit is contained in:
Konstantin Pogorelov
2016-08-17 14:29:26 +02:00
parent a191b08a55
commit f69502c2ba
4 changed files with 51 additions and 17 deletions

View File

@@ -45,6 +45,7 @@ See the example below.
* **prefix**: prefix added to every metric name
* **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
## Example
@@ -63,9 +64,13 @@ const express = require("express"),
app.get("/status", (req, res) => res.send("i am healthy"));
app.use(promBundle({
prefix: "demo_app:something"
prefix: "demo_app:something",
excludeRoutes: ["/foo"]
}));
// this call will NOT appear in metrics, because it matches excludeRoutes
app.get("/foo", (req, res) => res.send("bar"));
// calls to this route will appear in metrics
app.get("/hello", (req, res) => res.send("ok"));

View File

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

View File

@@ -70,6 +70,28 @@ describe("index", () => {
const labeled = metricHashMap["status_code:200"];
expect(labeled.count).toBe(1);
agent
.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
});
it("filters out the excludeRoutes", done => {
const app = express();
const instance = bundle({
excludeRoutes: ["/test"]
});
app.use(instance);
app.use("/test", (req, res) => res.send("it worked"));
const agent = supertest(app);
agent
.get("/test")
.end(() => {
const metricHashMap = instance.metrics.http_request_seconds.hashMap;
expect(metricHashMap["status_code:200"]).not.toBeDefined();
agent
.get("/metrics")
.end((err, res) => {

View File

@@ -4,18 +4,23 @@ const
PromFactory = require("./PromFactory"),
onFinished = require("on-finished");
function filterArrayByRegExps(array, regexps) {
return array.filter(element => {
for (let regexp of regexps) {
if (regexp instanceof RegExp) {
if (element.match(regexp)) {
return true;
}
} else if (element == regexp) {
function matchVsRegExps(element, regexps) {
for (let regexp of regexps) {
if (regexp instanceof RegExp) {
if (element.match(regexp)) {
return true;
}
} else if (element == regexp) {
return true;
}
return false;
}
return false;
}
function filterArrayByRegExps(array, regexps) {
return array.filter(element => {
return matchVsRegExps(element, regexps);
});
}
@@ -87,11 +92,6 @@ function main(opts) {
}
const middleware = function (req, res, next) {
let timer, labels;
if (metrics["http_request_seconds"]) {
labels = {"status_code": 0};
timer = metrics["http_request_seconds"].startTimer(labels);
}
if (req.path == "/metrics") {
let memoryUsage = process.memoryUsage();
if (metrics["nodejs_memory_heap_total_bytes"]) {
@@ -106,7 +106,14 @@ function main(opts) {
return;
}
if (timer) {
if (opts.excludeRoutes && matchVsRegExps(req.path, opts.excludeRoutes)) {
return next();
}
let labels;
if (metrics["http_request_seconds"]) {
labels = {"status_code": 0};
let timer = metrics["http_request_seconds"].startTimer(labels);
onFinished(res, () => {
labels["status_code"] = res.statusCode;
timer();