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

adding seperate metrics server #54

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The first argument represents the server of the middleware.
The second argument is optional, and allows some configuration of epimetheus

- `url` - the url on which to serve metrics. Defaults to `/metrics`.
- `metricsServer` - which server to attach option.url to. (Can be really useful if you like to run metrics endpoint on seperate port);

See the following examples of use with [http](#http), [express](#express), [hapi](#hapi) and [restify](#restify).

Expand Down
9 changes: 5 additions & 4 deletions lib/express.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const metrics = require('./metrics')

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

response.on('finish', function () {
Expand All @@ -10,15 +10,16 @@ function middleware (request, response, done) {
return done()
};

function instrument (server, options) {
function instrument(server, options) {
server.use(middleware)
server.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
3 changes: 2 additions & 1 deletion lib/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const metrics = require('./metrics')
function plugin (options) {
var plugin = {
register: (server, o, done) => {
server.route({
let mertricsEndpointServer = options.metricsServer || server;
mertricsEndpointServer.route({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix spelling

method: 'GET',
path: options.url,
handler: (req, reply) => {
Expand Down
11 changes: 6 additions & 5 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,15 +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) {
server.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 @@ -27,7 +28,7 @@ function instrument (server, options) {
})
}

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

Expand Down
4 changes: 4 additions & 0 deletions lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const metric = {
}
}

const gcStats = require('prometheus-gc-stats');
const startGcStats = gcStats(client.register);
startGcStats();

function ms (start) {
var diff = process.hrtime(start)
return Math.round((diff[0] * 1e9 + diff[1]) / 1000000)
Expand Down
3 changes: 2 additions & 1 deletion lib/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ function middleware (request, response, done) {
};

function instrument (server, options) {
let mertricsEndpointServer = options.metricsServer || server;
server.use(middleware)
server.get(options.url, (req, res) => {
mertricsEndpointServer.get(options.url, (req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
/**
* Using send uses the native Node handlers rather than the restify one
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"sinon": "^2.3.6"
},
"dependencies": {
"prom-client": "^10.0.0"
"prom-client": "^11.1.1",
"prometheus-gc-stats": "^0.5.1"
}
}
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