Skip to content

Commit

Permalink
feat: hapi v17 Support
Browse files Browse the repository at this point in the history
This commit adds hapi v17 support and changes the .instrument() method
for hapi to be async

Additionally some codestyle fixes included
  • Loading branch information
mkg20001 committed May 26, 2018
1 parent d19df1d commit d55d663
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 96 deletions.
84 changes: 41 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,82 +51,80 @@ See the following examples of use with [http](#http), [express](#express), [hapi

# <a name="http"></a> http
```
const http = require('http');
const epimetheus = require('../../index');
const http = require('http')
const epimetheus = require('../../index')
const server = http.createServer((req, res) => {
if(req.url !== '/metrics') {
res.statusCode = 200;
res.end();
res.statusCode = 200
res.end()
}
});
})
epimetheus.instrument(server);
epimetheus.instrument(server)
server.listen(8003, '127.0.0.1', () => {
console.log('http listening on 8003');
});
console.log('http listening on 8003')
})
```
# <a name="express"></a> Express
```
const express = require('express');
const epimetheus = require('epimetheus');
const express = require('express')
const epimetheus = require('epimetheus')
const app = express();
epimetheus.instrument(app);
const app = express()
epimetheus.instrument(app)
app.get('/', (req, res) => {
res.send();
});
res.send()
})
app.listen(3000, () => {
console.log('express server listening on port 3000');
});
console.log('express server listening on port 3000')
})
```
# <a name="hapi"></a> Hapi
```
const Hapi = require('hapi');
const epimetheus = require('epimetheus');
const Hapi = require('hapi')
const epimetheus = require('epimetheus')
const server = new Hapi.Server();
const server = new Hapi.Server()
server.connection({
port: 3000
});
epimetheus.instrument(this.server);
server.route({
method: 'GET',
path: '/',
handler: (req, resp) => {
resp();
}
});
server.start(() => {
console.log('hapi server listening on port 3000');
});
})
epimetheus.instrument(server).then(() => {
server.route({
method: 'GET',
path: '/',
handler: () => 'Hello World!'
})
server.start(() => {
console.log('hapi server listening on port 3000')
})
})
```
# <a name="restify"></a> Restify
```
const restify = require('restify');
const epimetheus = require('epimetheus');
const restify = require('restify')
const epimetheus = require('epimetheus')
const server = restify.createServer();
const server = restify.createServer()
epimetheus.instrument(this.server);
epimetheus.instrument(this.server)
server.get('/', (req, res, done) => {
res.send();
done();
});
res.send()
done()
})
server.listen(3000, () => {
console.log('restify server listening on port 3000');
});
console.log('restify server listening on port 3000')
})
```

Expand Down
3 changes: 2 additions & 1 deletion examples/express/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const app = express()
epithemeus.instrument(app)

app.get('/', function (req, res) {
var high = 500, low = 150
let high = 500
let low = 150

setTimeout(() => {
res.send()
Expand Down
3 changes: 2 additions & 1 deletion examples/restify/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const server = restify.createServer()
epithemeus.instrument(server)

server.get('/', function (req, res, done) {
var high = 500, low = 150
let high = 500
let low = 150

setTimeout(() => {
res.send()
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function instrument (app, options) {
options = defaults(options)

if (hapi.instrumentable(app)) {
hapi.instrument(app, options)
return hapi.instrument(app, options)
} else if (express.instrumentable(app)) {
express.instrument(app, options)
return express.instrument(app, options)
} else if (restify.instrumentable(app)) {
restify.instrument(app, options)
return restify.instrument(app, options)
} else if (http.instrumentable(app)) {
http.instrument(app, options)
return http.instrument(app, options)
}
}

Expand Down
27 changes: 13 additions & 14 deletions lib/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ const metrics = require('./metrics')

function plugin (options) {
var plugin = {
register: (server, o, done) => {
register: (server, o) => {
server.route({
method: 'GET',
path: options.url,
handler: (req, reply) => {
const response = reply(metrics.summary())
response.type('text/plain')
handler: (request, h) => {
return h
.response(metrics.summary())
.type('text/plain')
}
})

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

server.on('response', (response) => {
metrics.observe(response.method, response.path, response.response.statusCode, response.epimetheus.start)
server.ext('onPreResponse', (request, h) => {
metrics.observe(request.method, request.path, request.response.statusCode, request.epimetheus.start)
return h.continue
})

return done()
}
}

plugin.register.attributes = {
return Promise.resolve()
},
name: 'epimetheus',
version: '1.0.0'
}
Expand All @@ -36,7 +35,7 @@ function plugin (options) {
}

function instrument (server, options) {
server.register(plugin(options), () => {})
return server.register(plugin(options))
}

function instrumentable (server) {
Expand Down
4 changes: 2 additions & 2 deletions lib/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ function parse (path) {
cardinality: 'many'
}

if (path[path.length - 1] != '/') {
if (path[path.length - 1] !== '/') {
if (!path.includes('.')) {
ret.path = path.substr(0, path.lastIndexOf('/') + 1)
}
ret.cardinality = 'one'
};
}

return ret
}
Expand Down
2 changes: 1 addition & 1 deletion lib/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
"node": ">=4.0.0"
},
"devDependencies": {
"chai": "^4.0.2",
"coveralls": "^2.13.1",
"express": "^4.15.3",
"chai": "^4.1.2",
"coveralls": "^3.0.1",
"express": "^4.16.3",
"git-pre-hooks": "^1.2.0",
"hapi": "^16.4.3",
"hapi": "^17.5.0",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"request": "^2.81.0",
"restify": "^5.0.0",
"sinon": "^2.3.6"
"mocha": "^5.2.0",
"request": "^2.87.0",
"restify": "^7.2.0",
"sinon": "^5.0.10"
},
"dependencies": {
"prom-client": "^10.0.0"
"prom-client": "^11.0.0"
}
}
36 changes: 15 additions & 21 deletions test/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@ const assertExpectations = require('./assert-expectations')
function setup (options) {
return describe('hapi ' + options.url, () => {
before((done) => {
this.server = new Hapi.Server()
this.server.connection({
port: 3000
})
epithemeus.instrument(this.server, options)
this.server.route({
method: 'GET',
path: '/',
handler: (req, resp) => {
resp()
}
})
this.server.route({
method: 'GET',
path: '/resource/101',
handler: (req, resp) => {
resp()
}
})
this.server.start(done)
this.server = new Hapi.Server({ port: 3000 })
epithemeus.instrument(this.server, options).then(() => {
this.server.route({
method: 'GET',
path: '/',
handler: (req, h) => h.response()
})
this.server.route({
method: 'GET',
path: '/resource/101',
handler: (req, h) => h.response()
})
this.server.start().then(() => done(), done)
}, done)
})

after((done) => {
return this.server.stop(done)
this.server.stop().then(() => done(), done)
})

assertExpectations(options)
Expand Down

0 comments on commit d55d663

Please sign in to comment.