-
Notifications
You must be signed in to change notification settings - Fork 45
/
restify.js
37 lines (31 loc) · 1010 Bytes
/
restify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const metrics = require('./metrics')
function middleware (request, response, done) {
var start = process.hrtime()
response.on('finish', function () {
if (typeof request.path === 'function') { // restify
request.path = request.path()
}
metrics.observe(request.method, request.path, response.statusCode, start)
})
return done()
};
function instrument (server, options) {
server.use(middleware)
server.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
* 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())
})
}
function instrumentable (server) {
return server && server.name && server.use
}
module.exports = {
instrumentable: instrumentable,
instrument: instrument
}