(options)
| 13 | } |
| 14 | |
| 15 | function subscribeHttpServerRequestStart(options) { |
| 16 | const { setHttpConfig, addLiveRequest, addCloseRequest, addSentRequest, |
| 17 | addRequestTimeout, addHttpStatusCode, addHttpProfilingDetail, patch_http_timeout } = options; |
| 18 | |
| 19 | setHttpConfig(httpConfig); |
| 20 | |
| 21 | function onHttpServerRequestStart({ request, response }) { |
| 22 | addLiveRequest(); |
| 23 | |
| 24 | const start = Date.now(); |
| 25 | |
| 26 | const timer = setTimeout(() => { |
| 27 | addRequestTimeout(); |
| 28 | if (httpConfig.http_detail_profiling) { |
| 29 | const detail = getRequestDetail(request, response, start, 0); |
| 30 | addHttpProfilingDetail(detail); |
| 31 | } |
| 32 | }, patch_http_timeout * 1000); |
| 33 | timer.unref(); |
| 34 | |
| 35 | response.on('finish', () => { |
| 36 | addHttpStatusCode(response.statusCode); |
| 37 | addSentRequest(Date.now() - start); |
| 38 | clearTimeout(timer); |
| 39 | if (httpConfig.http_detail_profiling) { |
| 40 | const detail = getRequestDetail(request, response, start, 1); |
| 41 | addHttpProfilingDetail(detail); |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | response.on('close', () => { |
| 46 | addCloseRequest(); |
| 47 | clearTimeout(timer); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | // https://nodejs.org/docs/latest/api/diagnostics_channel.html#http |
| 52 | // use diagnostics_channel |
| 53 | const diagnosticsChannel = require('diagnostics_channel'); |
| 54 | diagnosticsChannel.subscribe('http.server.request.start', onHttpServerRequestStart); |
| 55 | } |
| 56 | |
| 57 | module.exports = { subscribeHttpServerRequestStart }; |
no test coverage detected