http.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS103: Rewrite code to no longer use __guard__
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const os = require("os");
  9. const yn = require("yn");
  10. const STACKDRIVER_LOGGING = yn(process.env['STACKDRIVER_LOGGING']);
  11. module.exports.monitor = logger => (function(req, res, next) {
  12. const Metrics = require("./metrics");
  13. const startTime = process.hrtime();
  14. const {
  15. end
  16. } = res;
  17. res.end = function() {
  18. let info;
  19. end.apply(this, arguments);
  20. const responseTime = process.hrtime(startTime);
  21. const responseTimeMs = Math.round((responseTime[0] * 1000) + (responseTime[1] / 1000000));
  22. const requestSize = parseInt(req.headers["content-length"], 10);
  23. if ((req.route != null ? req.route.path : undefined) != null) {
  24. const routePath = req.route.path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1);
  25. Metrics.timing("http_request", responseTimeMs, null, {method:req.method, status_code: res.statusCode, path:routePath});
  26. if (requestSize) {
  27. Metrics.summary("http_request_size_bytes", requestSize, {method:req.method, status_code: res.statusCode, path:routePath});
  28. }
  29. }
  30. const remoteIp = req.ip || __guard__(req.socket != null ? req.socket.socket : undefined, x => x.remoteAddress) || (req.socket != null ? req.socket.remoteAddress : undefined);
  31. const reqUrl = req.originalUrl || req.url;
  32. const referrer = req.headers['referer'] || req.headers['referrer'];
  33. if (STACKDRIVER_LOGGING) {
  34. info = {
  35. httpRequest: {
  36. requestMethod: req.method,
  37. requestUrl: reqUrl,
  38. requestSize,
  39. status: res.statusCode,
  40. responseSize: (res._headers != null ? res._headers["content-length"] : undefined),
  41. userAgent: req.headers["user-agent"],
  42. remoteIp,
  43. referer: referrer,
  44. latency: {
  45. seconds: responseTime[0],
  46. nanos: responseTime[1]
  47. },
  48. protocol: req.protocol
  49. }
  50. };
  51. } else {
  52. info = {
  53. req: {
  54. url: reqUrl,
  55. method: req.method,
  56. referrer,
  57. "remote-addr": remoteIp,
  58. "user-agent": req.headers["user-agent"],
  59. "content-length": req.headers["content-length"]
  60. },
  61. res: {
  62. "content-length": (res._headers != null ? res._headers["content-length"] : undefined),
  63. statusCode: res.statusCode
  64. },
  65. "response-time": responseTimeMs
  66. };
  67. }
  68. return logger.info(info, "%s %s", req.method, reqUrl);
  69. };
  70. return next();
  71. });
  72. function __guard__(value, transform) {
  73. return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
  74. }