Files
express-prom-bundle/spec/indexSpec.js

232 lines
7.3 KiB
JavaScript

"use strict";
/* eslint-env jasmine */
const express = require("express");
const supertest = require("supertest");
const bundle = require("../");
const koa = require("koa");
const c2k = require("koa-connect");
const supertestKoa = require("supertest-koa-agent");
const promClient = require("prom-client");
// had to reinvent, because getSingleMetric() is still not in npm
function myGetSingleMetric(name) {
let returnMetric;
promClient.register.getMetricsAsJSON().forEach(metric => {
if (metric.name === name) {
returnMetric = metric;
}
});
return returnMetric;
}
describe("index", () => {
beforeEach(() => {
promClient.register.clear();
});
it("metrics returns up=1", done => {
const app = express();
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
.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/hello:up\s1/);
done();
});
});
});
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();
expect(instance.metrics.nodejs_memory_heap_total_bytes).toBeDefined();
});
it("metrics can be filtered using regex", () => {
const instance = bundle({blacklist: [/memory/]});
expect(instance.metrics.up).toBeDefined();
expect(instance.metrics.nodejs_memory_heap_total_bytes).not.toBeDefined();
});
it("metrics can be whitelisted", () => {
const instance = bundle({whitelist: [/^up$/]});
expect(instance.metrics.up).toBeDefined();
expect(instance.metrics.nodejs_memory_heap_total_bytes).not.toBeDefined();
expect(instance.metrics.http_request_seconds).not.toBeDefined();
});
it("throws on both white and blacklist", () => {
expect(() => {
bundle({whitelist: [/up/], blacklist: [/up/]});
}).toThrow();
});
it("returns error 500 on incorrect middleware usage", done => {
const app = express();
app.use(bundle);
supertest(app)
.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(500);
done();
});
});
it("http latency gets counted", done => {
const app = express();
const instance = bundle();
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"]).toBeDefined();
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) => {
expect(res.status).toBe(200);
done();
});
});
});
describe("initial metrics removal", () => {
it("removes unexpected metrics on start with no prefix", () => {
new promClient.Counter("foo", "bar");
expect(myGetSingleMetric("foo")).toBeDefined();
bundle();
expect(myGetSingleMetric("foo")).not.toBeDefined();
});
it("removes unexpected metrics on start with a prefix", () => {
new promClient.Counter("foo", "bar");
expect(myGetSingleMetric("foo")).toBeDefined();
bundle({prefix: "some_test_"});
expect(myGetSingleMetric("foo")).not.toBeDefined();
});
it("doesnt remove metrics with matched prefix", () => {
new promClient.Counter("some_test_foo", "bar");
expect(myGetSingleMetric("some_test_foo")).toBeDefined();
bundle({prefix: "some_test_"});
expect(myGetSingleMetric("some_test_foo")).toBeDefined();
});
});
it("tolerates includePath, includeMethod and keepDefaultMetrics", done => {
const app = express();
const instance = bundle({
includePath: true,
includeMethod: true,
keepDefaultMetrics: true
});
app.use(instance);
app.use("/test", (req, res) => res.send("it worked"));
const agent = supertest(app);
agent
.get("/test")
.end(() => {
agent
.get("/metrics")
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
});
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();
});
});
});
});