Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express route fix #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Epimetheus
[![CircleCI](https://img.shields.io/circleci/project/roylines/node-epimetheus.svg)]()
[![Coveralls](https://img.shields.io/coveralls/roylines/node-epimetheus.svg)]()
[![David](https://img.shields.io/david/roylines/node-epimetheus.svg)]()
[![CircleCI](https://img.shields.io/circleci/project/ashoksahoo/node-epimetheus.svg)]()
[![Coverage Status](https://coveralls.io/repos/github/ashoksahoo/node-epimetheus/badge.svg?branch=master)](https://coveralls.io/github/ashoksahoo/node-epimetheus?branch=master)
[![David](https://img.shields.io/david/ashoksahoo/node-epimetheus.svg)]()

[![NPM](https://nodei.co/npm/epimetheus.png)](https://nodei.co/npm/epimetheus/)
[![NPM](https://nodei.co/npm/node-epimetheus.png)](https://nodei.co/npm/node-epimetheus/)

Middleware to automatically instrument node applications for consumption by a [Prometheus](https://prometheus.io/) server.

Expand Down Expand Up @@ -35,7 +35,7 @@ These are metrics provided by [prom-client](https://github.com/siimon/prom-clien

# Installation
```
> npm install --save epimetheus
> npm install --save node-epimetheus
```

Epimetheus has only one method, instrument, and it has the following signature:
Expand Down Expand Up @@ -71,7 +71,7 @@ server.listen(8003, '127.0.0.1', () => {
# <a name="express"></a> Express
```
const express = require('express');
const epimetheus = require('epimetheus');
const epimetheus = require('node-epimetheus');

const app = express();
epimetheus.instrument(app);
Expand All @@ -88,7 +88,7 @@ app.listen(3000, () => {
# <a name="hapi"></a> Hapi
```
const Hapi = require('hapi');
const epimetheus = require('epimetheus');
const epimetheus = require('node-epimetheus');

const server = new Hapi.Server();

Expand All @@ -113,7 +113,7 @@ server.start(() => {
# <a name="restify"></a> Restify
```
const restify = require('restify');
const epimetheus = require('epimetheus');
const epimetheus = require('node-epimetheus');

const server = restify.createServer();

Expand Down
2 changes: 1 addition & 1 deletion examples/express/express.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express')
const epithemeus = require('epimetheus')
const epithemeus = require('node-epimetheus')

const app = express()
epithemeus.instrument(app)
Expand Down
2 changes: 1 addition & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"epimetheus": "*",
"node-epimetheus":"*"
"express": "*"
}
}
2 changes: 1 addition & 1 deletion examples/hapi/hapi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Hapi = require('hapi')
const epithemeus = require('epimetheus')
const epithemeus = require('node-epimetheus')

const server = new Hapi.Server()

Expand Down
2 changes: 1 addition & 1 deletion examples/http/http.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const http = require('http')
const epithemeus = require('epimetheus')
const epithemeus = require('node-epimetheus')

const server = http.createServer((req, res) => {
if (req.url !== '/metrics') {
Expand Down
2 changes: 1 addition & 1 deletion examples/restify/restify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const restify = require('restify')
const epithemeus = require('epimetheus')
const epithemeus = require('node-epimetheus')

const server = restify.createServer()
epithemeus.instrument(server)
Expand Down
9 changes: 7 additions & 2 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ function middleware (request, response, done) {
var start = process.hrtime()

response.on('finish', function () {
metrics.observe(request.method, request.path, response.statusCode, start)
})
if (request.route) {
metrics.observe(request.method, request.route.path, response.statusCode, start);
} else {
metrics.observe(request.method, '', response.statusCode, start);
}

});

return done()
};
Expand Down
14 changes: 7 additions & 7 deletions lib/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ function plugin (options) {
const response = reply(metrics.summary())
response.type('text/plain')
}
})
});

server.ext('onRequest', (request, reply) => {
request.epimetheus = {
start: process.hrtime()
}
};
return reply.continue()
})
});

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()
}
}
};

plugin.register.attributes = {
name: 'epimetheus',
version: '1.0.0'
}
};

return plugin
}
Expand Down
7 changes: 2 additions & 5 deletions lib/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function middleware (request, response, done) {
if (typeof request.path === 'function') { // restify
request.path = request.path()
}
metrics.observe(request.method, request.path, response.statusCode, start)
metrics.observe(request.method, request.route.path, response.statusCode, start);
})

return done()
Expand All @@ -20,7 +20,7 @@ function instrument (server, options) {
/**
* Using send uses the native Node handlers rather than the restify one
* changing send to end di the job
*
*
* see https://stackoverflow.com/questions/28680755/setting-content-type-header-with-restify-results-in-application-octet-stream
*/
return res.end(metrics.summary())
Expand Down
Loading