minor dep. upgrade, do not remove initial metrics if they match the requested prefix (i.e. recognized as own metrics), bump 1.2.2

This commit is contained in:
Konstantin Pogorelov
2016-12-01 00:28:45 +01:00
parent b374e7f54c
commit c918b44c2b
3 changed files with 47 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "express-prom-bundle",
"version": "1.2.1",
"version": "1.2.2",
"description": "express middleware with popular prometheus metrics in one bundle",
"main": "src/index.js",
"keywords": [
@@ -20,10 +20,10 @@
"url-value-parser": "^1.0.0"
},
"devDependencies": {
"coveralls": "^2.11.12",
"eslint": "^3.11.0",
"express": "^4.13.4",
"istanbul": "^0.4.4",
"coveralls": "^2.11.15",
"eslint": "^3.11.1",
"express": "^4.14.0",
"istanbul": "^0.4.5",
"jasme": "^5.2.0",
"koa": "^1.2.4",
"koa-connect": "^1.0.0",

View File

@@ -9,6 +9,17 @@ 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();
@@ -146,6 +157,29 @@ describe("index", () => {
});
});
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({

View File

@@ -51,10 +51,16 @@ function main(opts) {
return;
}
// remove default metrics provided by prom-client
// this is a really messy hack but needed for compatibility with v1
// will be completely removed in v2
if (!opts.keepDefaultMetrics) {
const metrics = promClient.register.getMetricsAsJSON();
clearInterval(promClient.defaultMetrics());
promClient.register.clear();
metrics.forEach(metric => {
if (!opts.prefix || metric.name.substr(0, opts.prefix.length) != opts.prefix) {
promClient.register.removeSingleMetric(metric.name);
}
});
}
const factory = new PromFactory(opts);