Skip to content

Commit

Permalink
adding seperate metrics server and use prometheus-gc-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
hsingh6764 committed Aug 4, 2018
1 parent d19df1d commit 71e6891
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 5 deletions.
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
3 changes: 2 additions & 1 deletion lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function middleware (request, response, done) {

function instrument (server, options) {
server.use(middleware)
server.get(options.url, (req, res) => {
let mertricsEndpointServer = options.metricsServer || server;
mertricsEndpointServer.get(options.url, (req, res) => {
res.header('content-type', 'text/plain; charset=utf-8')
return res.send(metrics.summary())
})
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({
method: 'GET',
path: options.url,
handler: (req, reply) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function observeMetrics (request, response) {
}

function instrument (server, options) {
server.on('request', (request, response) => {
let mertricsEndpointServer = options.metricsServer || server;
mertricsEndpointServer.on('request', (request, response) => {
if (request.url === options.url) {
sendMetrics(request, response)
} else {
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"
}
}

0 comments on commit 71e6891

Please sign in to comment.