Skip to content

Commit 71e6891

Browse files
committed
adding seperate metrics server and use prometheus-gc-stats
1 parent d19df1d commit 71e6891

7 files changed

Lines changed: 15 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The first argument represents the server of the middleware.
4646
The second argument is optional, and allows some configuration of epimetheus
4747

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

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

lib/express.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function middleware (request, response, done) {
1212

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

lib/hapi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const metrics = require('./metrics')
33
function plugin (options) {
44
var plugin = {
55
register: (server, o, done) => {
6-
server.route({
6+
let mertricsEndpointServer = options.metricsServer || server;
7+
mertricsEndpointServer.route({
78
method: 'GET',
89
path: options.url,
910
handler: (req, reply) => {

lib/http.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function observeMetrics (request, response) {
1818
}
1919

2020
function instrument (server, options) {
21-
server.on('request', (request, response) => {
21+
let mertricsEndpointServer = options.metricsServer || server;
22+
mertricsEndpointServer.on('request', (request, response) => {
2223
if (request.url === options.url) {
2324
sendMetrics(request, response)
2425
} else {

lib/metrics.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const metric = {
1111
}
1212
}
1313

14+
const gcStats = require('prometheus-gc-stats');
15+
const startGcStats = gcStats(client.register);
16+
startGcStats();
17+
1418
function ms (start) {
1519
var diff = process.hrtime(start)
1620
return Math.round((diff[0] * 1e9 + diff[1]) / 1000000)

lib/restify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ function middleware (request, response, done) {
1414
};
1515

1616
function instrument (server, options) {
17+
let mertricsEndpointServer = options.metricsServer || server;
1718
server.use(middleware)
18-
server.get(options.url, (req, res) => {
19+
mertricsEndpointServer.get(options.url, (req, res) => {
1920
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
2021
/**
2122
* Using send uses the native Node handlers rather than the restify one

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"sinon": "^2.3.6"
4343
},
4444
"dependencies": {
45-
"prom-client": "^10.0.0"
45+
"prom-client": "^11.1.1",
46+
"prometheus-gc-stats": "^0.5.1"
4647
}
4748
}

0 commit comments

Comments
 (0)