mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-01-18 16:27:28 +01:00
Merge pull request #1 from nwtse/master
Add possibility to manipulate /metrics endpoint registration
This commit is contained in:
@@ -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", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
35
src/index.js
35
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("<h1>500 Error</h1>\n"
|
||||
+ "<p>Unexapected 3d param in express-prom-bundle.\n"
|
||||
+ "<p>Unexapected 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;
|
||||
|
||||
Reference in New Issue
Block a user