From b06e37159b447a7711c079b0e43a9d696b73e02a Mon Sep 17 00:00:00 2001 From: YvesPasteur Date: Tue, 16 May 2017 17:39:48 +0200 Subject: [PATCH] use route path instead of url in metric labels --- lib/express.js | 2 +- lib/hapi.js | 2 +- lib/labels.js | 7 ++----- lib/restify.js | 2 +- test/assert-expectations.js | 2 +- test/express.js | 4 +++- test/hapi.js | 4 +++- test/http.js | 2 ++ test/labels.js | 12 ++++++------ test/restify.js | 4 +++- 10 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/express.js b/lib/express.js index 4c2b18a..44eff26 100644 --- a/lib/express.js +++ b/lib/express.js @@ -4,7 +4,7 @@ function middleware(request, response, done) { var start = process.hrtime(); response.on('finish', function() { - metrics.observe(request.method, request.path, response.statusCode, start); + metrics.observe(request.method, request.route.path, response.statusCode, start); }); return done(); diff --git a/lib/hapi.js b/lib/hapi.js index 0f2e2a8..de1565f 100644 --- a/lib/hapi.js +++ b/lib/hapi.js @@ -20,7 +20,7 @@ function plugin(options) { }); server.on('response', (response) => { - metrics.observe(response.method, response.path, response.response.statusCode, response.epimetheus.start); + metrics.observe(response.method, response.route.path, response.response.statusCode, response.epimetheus.start); }); return done(); diff --git a/lib/labels.js b/lib/labels.js index ba88451..b2ad8ac 100644 --- a/lib/labels.js +++ b/lib/labels.js @@ -2,14 +2,11 @@ function parse(path) { var ret = { path: path, cardinality: 'many' - } + }; if (path[path.length - 1] != '/') { - if (!path.includes('.')) { - ret.path = path.substr(0, path.lastIndexOf('/') + 1); - } ret.cardinality = 'one'; - }; + } return ret; } diff --git a/lib/restify.js b/lib/restify.js index c6850be..281e51e 100644 --- a/lib/restify.js +++ b/lib/restify.js @@ -4,7 +4,7 @@ function middleware(request, response, done) { var start = process.hrtime(); response.on('finish', function() { - metrics.observe(request.method, request.path(), response.statusCode, start); + metrics.observe(request.method, request.route.path, response.statusCode, start); }); return done(); diff --git a/test/assert-expectations.js b/test/assert-expectations.js index 4c84e17..14a2986 100644 --- a/test/assert-expectations.js +++ b/test/assert-expectations.js @@ -23,7 +23,7 @@ module.exports = function(options) { should.exist(r.headers['content-type']); r.headers['content-type'].should.equal('text/plain; charset=utf-8'); b.should.have.string('# HELP '); - b.should.have.string('"/resource/"'); + b.should.have.string('"' + options.routePath + '"'); b.should.have.string('cardinality="one"'); b.should.have.string('cardinality="many"'); b.should.have.string('status="200"'); diff --git a/test/express.js b/test/express.js index 0bb7761..2a5dc41 100644 --- a/test/express.js +++ b/test/express.js @@ -4,6 +4,8 @@ const epithemeus = require('../index'); const assertExpectations = require('./assert-expectations'); function setup(options) { + options.routePath = '/resource/:id'; + return describe('express ' + options.url, () => { before((done) => { const app = express(); @@ -11,7 +13,7 @@ function setup(options) { app.get('/', (req, res) => { res.send(); }); - app.get('/resource/:id', (req, res) => { + app.get(options.routePath, (req, res) => { res.send(); }); this.server = app.listen(3000, done); diff --git a/test/hapi.js b/test/hapi.js index 37b35c2..a4395b2 100644 --- a/test/hapi.js +++ b/test/hapi.js @@ -4,6 +4,8 @@ const epithemeus = require('../index'); const assertExpectations = require('./assert-expectations'); function setup(options) { + options.routePath = '/resource/{id}'; + return describe('hapi ' + options.url, () => { before((done) => { this.server = new Hapi.Server(); @@ -20,7 +22,7 @@ function setup(options) { }); this.server.route({ method: 'GET', - path: '/resource/101', + path: options.routePath, handler: (req, resp) => { resp(); } diff --git a/test/http.js b/test/http.js index be06e55..5a6d83d 100644 --- a/test/http.js +++ b/test/http.js @@ -4,6 +4,8 @@ const epithemeus = require('../index'); const assertExpectations = require('./assert-expectations'); function setup(options) { + options.routePath = '/resource/101'; + return describe('native ' + options.url, () => { before((done) => { this.server = http.createServer((req, res) => { diff --git a/test/labels.js b/test/labels.js index cfdf8c9..4d7292c 100644 --- a/test/labels.js +++ b/test/labels.js @@ -6,17 +6,17 @@ describe('labels', () => { labels.parse('/users/').path.should.equal('/users/'); labels.parse('/users/').cardinality.should.equal('many'); }); - it('should parse /users/freddie', () => { - labels.parse('/users/freddie').path.should.equal('/users/'); - labels.parse('/users/freddie').cardinality.should.equal('one'); + it('should parse /users/:freddie', () => { + labels.parse('/users/:freddie').path.should.equal('/users/:freddie'); + labels.parse('/users/:freddie').cardinality.should.equal('one'); }); it('should parse /staff/users/', () => { labels.parse('/staff/users/').path.should.equal('/staff/users/'); labels.parse('/staff/users/').cardinality.should.equal('many'); }); - it('should parse /staff/users/freddie', () => { - labels.parse('/staff/users/freddie').path.should.equal('/staff/users/'); - labels.parse('/staff/users/freddie').cardinality.should.equal('one'); + it('should parse /staff/users/:freddie', () => { + labels.parse('/staff/users/:freddie').path.should.equal('/staff/users/:freddie'); + labels.parse('/staff/users/:freddie').cardinality.should.equal('one'); }); it('should parse /static/page.css', () => { labels.parse('/static/page.css').path.should.equal('/static/page.css'); diff --git a/test/restify.js b/test/restify.js index 9d5d027..774c92a 100644 --- a/test/restify.js +++ b/test/restify.js @@ -4,6 +4,8 @@ const epithemeus = require('../index'); const assertExpectations = require('./assert-expectations'); function setup(options) { + options.routePath = '/resource/:id'; + describe('restify ' + options.url, () => { before((done) => { this.server = restify.createServer(); @@ -12,7 +14,7 @@ function setup(options) { res.send(); done(); }); - this.server.get('/resource/:id', (req, res, done) => { + this.server.get(options.routePath, (req, res, done) => { res.send(); done(); });