Skip to content

Commit

Permalink
adding test + fix spelling mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
hsingh6764 committed Aug 21, 2018
1 parent 41da576 commit b08c313
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
12 changes: 6 additions & 6 deletions lib/express.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const metrics = require('./metrics')

function middleware (request, response, done) {
function middleware(request, response, done) {
var start = process.hrtime()

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

return done()
};

function instrument (server, options) {
function instrument(server, options) {
server.use(middleware)
let mertricsEndpointServer = options.metricsServer || server;
mertricsEndpointServer.get(options.url, (req, res) => {
let metricsEndpointServer = options.metricsServer || server;
metricsEndpointServer.get(options.url, (req, res) => {
res.header('content-type', 'text/plain; charset=utf-8')
return res.send(metrics.summary())
})
}

function instrumentable (server) {
function instrumentable(server) {
return server && server.defaultConfiguration && server.use
}

Expand Down
12 changes: 6 additions & 6 deletions lib/http.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const defaults = require('./defaults')
const metrics = require('./metrics')

function sendMetrics (request, response) {
function sendMetrics(request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
})
Expand All @@ -10,16 +10,16 @@ function sendMetrics (request, response) {
response.end()
}

function observeMetrics (request, response) {
function observeMetrics(request, response) {
var start = process.hrtime()
response.on('finish', () => {
metrics.observe(request.method, request.url, response.statusCode, start)
})
}

function instrument (server, options) {
let mertricsEndpointServer = options.metricsServer || server;
mertricsEndpointServer.on('request', (request, response) => {
function instrument(server, options) {
let metricsEndpointServer = options.metricsServer || server;
metricsEndpointServer.on('request', (request, response) => {
if (request.url === options.url) {
sendMetrics(request, response)
} else {
Expand All @@ -28,7 +28,7 @@ function instrument (server, options) {
})
}

function instrumentable (server) {
function instrumentable(server) {
return server
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epimetheus",
"version": "1.0.56",
"version": "0.0.1",
"description": "node middleware to automatically instrument node applications for consumption by prometheus",
"main": "index.js",
"scripts": {
Expand Down
14 changes: 12 additions & 2 deletions test/assert-expectations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('request')
const should = require('chai').should()

module.exports = function (options) {
module.exports = function(options) {
it('should return 200 for /', (done) => {
request('http://localhost:3000/', (e, r, b) => {
r.statusCode.should.equal(200)
Expand All @@ -17,7 +17,8 @@ module.exports = function (options) {
})

it('should return 200 for ' + options.url, (done) => {
request('http://localhost:3000' + options.url, (e, r, b) => {
let metricsPort = options.metricsServer ? 3001 : 3000;
request('http://localhost:' + metricsPort + options.url, (e, r, b) => {
r.statusCode.should.equal(200)
should.exist(r.headers['content-type'])
r.headers['content-type'].should.equal('text/plain; charset=utf-8')
Expand All @@ -29,4 +30,13 @@ module.exports = function (options) {
return done(e)
})
})

if (options.metricsServer) {
it('should return 404 for /metrics on 3000', (done) => {
request('http://localhost:3000/metrics', (e, r, b) => {
r.statusCode.should.equal(404)
return done(e)
});
});
}
}
25 changes: 21 additions & 4 deletions test/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const epithemeus = require('../index')
const assertExpectations = require('./assert-expectations')
const assert = require('chai').assert
const libExpress = require('../lib/express')
const request = require('request')
const should = require('chai').should()

function setup (options) {
function setup(options) {
return describe('express ' + options.url, () => {
before((done) => {
const app = express()
Expand All @@ -16,7 +18,13 @@ function setup (options) {
app.get('/resource/:id', (req, res) => {
res.send()
})
this.server = app.listen(3000, done)
this.server = app.listen(3000, () => {
if (options.metricsServer) {
options.metricsServer.listen(3001, done);
} else {
done()
}
});
})

after((done) => {
Expand All @@ -32,11 +40,18 @@ setup({
url: '/xxx'
})

setup({
url: '/metrics',
metricsServer: express()
})

describe('express should check "instrumentablity" properly', () => {
it('should return true when express conditions are correct', () => {
// Arrange
const server = () => {}
server.defaultConfiguration = { sample: true }
server.defaultConfiguration = {
sample: true
}
server.use = () => {}

// Act
Expand Down Expand Up @@ -69,7 +84,9 @@ describe('express should check "instrumentablity" properly', () => {
it('should return false when server does not have a use function', () => {
// Arrange
const server = () => {}
server.defaultConfiguration = { sample: true }
server.defaultConfiguration = {
sample: true
}

// Act
const actual = libExpress.instrumentable(server)
Expand Down

0 comments on commit b08c313

Please sign in to comment.