diff --git a/README.md b/README.md index fce07d5..1b2b821 100644 --- a/README.md +++ b/README.md @@ -51,82 +51,76 @@ See the following examples of use with [http](#http), [express](#express), [hapi # 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') +}) ``` # 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') +}) ``` # Hapi ``` -const Hapi = require('hapi'); -const epimetheus = require('epimetheus'); - -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'); -}); +const Hapi = require('hapi') +const epimetheus = require('epimetheus') + +const server = new Hapi.Server({ port: 3000 }) + +epimetheus.instrument(server).then(() => { + server.route({ + method: 'GET', + path: '/', + handler: () => 'Hello World!' + }) + + server.start(() => { + console.log('hapi server listening on port 3000') + }) +}) ``` # 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') +}) ``` diff --git a/examples/express/express.js b/examples/express/express.js index e97336e..22be82f 100644 --- a/examples/express/express.js +++ b/examples/express/express.js @@ -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() diff --git a/examples/restify/restify.js b/examples/restify/restify.js index 3e06bbe..b2b340a 100644 --- a/examples/restify/restify.js +++ b/examples/restify/restify.js @@ -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() diff --git a/index.js b/index.js index ea52b38..f2d7a9c 100644 --- a/index.js +++ b/index.js @@ -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) } } diff --git a/lib/hapi.js b/lib/hapi.js index bc3f3e5..cc69d74 100644 --- a/lib/hapi.js +++ b/lib/hapi.js @@ -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' } @@ -36,7 +35,7 @@ function plugin (options) { } function instrument (server, options) { - server.register(plugin(options), () => {}) + return server.register(plugin(options)) } function instrumentable (server) { diff --git a/lib/labels.js b/lib/labels.js index 8460b5f..6cf190a 100644 --- a/lib/labels.js +++ b/lib/labels.js @@ -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 } diff --git a/lib/restify.js b/lib/restify.js index ad76785..3837b3c 100644 --- a/lib/restify.js +++ b/lib/restify.js @@ -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()) diff --git a/package.json b/package.json index 71ddc94..cb292ed 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/hapi.js b/test/hapi.js index 04031ef..8e8b93f 100644 --- a/test/hapi.js +++ b/test/hapi.js @@ -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)