Skip to content

Commit 17d120c

Browse files
YvesPasteurnevezide
authored andcommitted
use route path instead of url in metric labels
1 parent d19df1d commit 17d120c

10 files changed

Lines changed: 26 additions & 20 deletions

File tree

lib/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function middleware (request, response, done) {
44
var start = process.hrtime()
55

66
response.on('finish', function () {
7-
metrics.observe(request.method, request.path, response.statusCode, start)
7+
metrics.observe(request.method, request.route.path, response.statusCode, start)
88
})
99

1010
return done()

lib/hapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function plugin (options) {
2020
})
2121

2222
server.on('response', (response) => {
23-
metrics.observe(response.method, response.path, response.response.statusCode, response.epimetheus.start)
23+
metrics.observe(response.method, response.route.path, response.response.statusCode, response.epimetheus.start)
2424
})
2525

2626
return done()

lib/labels.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ function parse (path) {
22
var ret = {
33
path: path,
44
cardinality: 'many'
5-
}
5+
};
66

77
if (path[path.length - 1] != '/') {
8-
if (!path.includes('.')) {
9-
ret.path = path.substr(0, path.lastIndexOf('/') + 1)
10-
}
11-
ret.cardinality = 'one'
12-
};
8+
ret.cardinality = 'one';
9+
}
1310

1411
return ret
1512
}

lib/restify.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ function middleware (request, response, done) {
44
var start = process.hrtime()
55

66
response.on('finish', function () {
7+
let path = request.route.path;
78
if (typeof request.path === 'function') { // restify
8-
request.path = request.path()
9+
path = request.path()
910
}
10-
metrics.observe(request.method, request.path, response.statusCode, start)
11+
metrics.observe(request.method, path, response.statusCode, start)
1112
})
1213

1314
return done()

test/assert-expectations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (options) {
2222
should.exist(r.headers['content-type'])
2323
r.headers['content-type'].should.equal('text/plain; charset=utf-8')
2424
b.should.have.string('# HELP ')
25-
b.should.have.string('"/resource/"')
25+
b.should.have.string('"' + options.routePath + '"')
2626
b.should.have.string('cardinality="one"')
2727
b.should.have.string('cardinality="many"')
2828
b.should.have.string('status="200"')

test/express.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const assert = require('chai').assert
66
const libExpress = require('../lib/express')
77

88
function setup (options) {
9+
options.routePath = '/resource/:id';
10+
911
return describe('express ' + options.url, () => {
1012
before((done) => {
1113
const app = express()
1214
epithemeus.instrument(app, options)
13-
app.get('/', (req, res) => {
15+
app.get(options.routePath, (req, res) => {
1416
res.send()
1517
})
1618
app.get('/resource/:id', (req, res) => {

test/hapi.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const epithemeus = require('../index')
66
const assertExpectations = require('./assert-expectations')
77

88
function setup (options) {
9+
options.routePath = '/resource/{id}';
10+
911
return describe('hapi ' + options.url, () => {
1012
before((done) => {
1113
this.server = new Hapi.Server()
@@ -22,7 +24,7 @@ function setup (options) {
2224
})
2325
this.server.route({
2426
method: 'GET',
25-
path: '/resource/101',
27+
path: options.routePath,
2628
handler: (req, resp) => {
2729
resp()
2830
}

test/http.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const epithemeus = require('../index')
44
const assertExpectations = require('./assert-expectations')
55

66
function setup (options) {
7+
options.routePath = '/resource/101';
8+
79
return describe('native ' + options.url, () => {
810
before((done) => {
911
this.server = http.createServer((req, res) => {

test/labels.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ describe('labels', () => {
66
labels.parse('/users/').path.should.equal('/users/')
77
labels.parse('/users/').cardinality.should.equal('many')
88
})
9-
it('should parse /users/freddie', () => {
10-
labels.parse('/users/freddie').path.should.equal('/users/')
11-
labels.parse('/users/freddie').cardinality.should.equal('one')
9+
it('should parse /users/:freddie', () => {
10+
labels.parse('/users/:freddie').path.should.equal('/users/:freddie')
11+
labels.parse('/users/:freddie').cardinality.should.equal('one')
1212
})
1313
it('should parse /staff/users/', () => {
1414
labels.parse('/staff/users/').path.should.equal('/staff/users/')
1515
labels.parse('/staff/users/').cardinality.should.equal('many')
1616
})
17-
it('should parse /staff/users/freddie', () => {
18-
labels.parse('/staff/users/freddie').path.should.equal('/staff/users/')
19-
labels.parse('/staff/users/freddie').cardinality.should.equal('one')
17+
it('should parse /staff/users/:freddie', () => {
18+
labels.parse('/staff/users/:freddie').path.should.equal('/staff/users/:freddie')
19+
labels.parse('/staff/users/:freddie').cardinality.should.equal('one')
2020
})
2121
it('should parse /static/page.css', () => {
2222
labels.parse('/static/page.css').path.should.equal('/static/page.css')

test/restify.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const epithemeus = require('../index')
44
const assertExpectations = require('./assert-expectations')
55

66
function setup (options) {
7+
options.routePath = '/resource/:id';
8+
79
describe('restify ' + options.url, () => {
810
before((done) => {
911
this.server = restify.createServer()
@@ -12,7 +14,7 @@ function setup (options) {
1214
res.send()
1315
done()
1416
})
15-
this.server.get('/resource/:id', (req, res, done) => {
17+
this.server.get(options.routePath, (req, res, done) => {
1618
res.send()
1719
done()
1820
})

0 commit comments

Comments
 (0)