CallLog.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var Q = require('q');
  2. var oujRedis = require('./oujRedis.js');
  3. var configs = require('../../conf/config.inc.js');
  4. var php = require('phpjs');
  5. var call_id = null;
  6. var objRedis = oujRedis.init('logstash_redis');
  7. function CallLog(objController) {
  8. /**
  9. * @type {Controller}
  10. */
  11. this.objController = objController;
  12. }
  13. function writeRedis(pushData) {
  14. try {
  15. var config = configs['redisInfo']['logstash_redis'];
  16. if (config) {
  17. objRedis.rpush('logstash:redis', JSON.stringify(pushData));
  18. }
  19. } catch (ex) {
  20. // 这里需要告警
  21. if (DEBUG) {
  22. throw ex;
  23. }
  24. }
  25. }
  26. CallLog.prototype.getCallId = function() {
  27. if (!call_id) {
  28. call_id = new Date().getTime() + "" + parseInt(Math.random() * 10000);
  29. }
  30. return call_id;
  31. }
  32. CallLog.prototype.logSelfCall = function(code, response) {
  33. var _req = this.objController && this.objController._req || {};
  34. var _startTime = this.objController && this.objController._startTime;
  35. var data = {};
  36. data['call_id'] = this.getCallId();
  37. data['url'] = '/' + CONTROLLER_NAME + '/' + ACTION_NAME;
  38. data['method'] = _req.method;
  39. var getParam = php.http_build_query(_req.query);
  40. var postParam = php.http_build_query(_req.body);
  41. var param = '';
  42. if (getParam && postParam) {
  43. param = getParam + '&' + postParam;
  44. } else if (getParam) {
  45. param = getParam;
  46. } else {
  47. param = postParam;
  48. }
  49. data['param'] = param;
  50. if (_req.headers) {
  51. data['cookie'] = _req.headers.cookie;
  52. data['useragent'] = _req.headers['user-agent'];
  53. }
  54. if (this.objController && this.objController.debugMsg) {
  55. response = '【debugMsg:' + this.objController.debugMsg + "】{response}";
  56. }
  57. data['response'] = response.substr(0, 3000);
  58. data['code'] = code;
  59. var curTime = new Date().getTime();
  60. data['delay'] = (curTime - _startTime) / 1000;
  61. data['server_ip'] = getServerIp();
  62. if (this.objController) {
  63. data['client_ip'] = getClientIp(this.objController._req);
  64. } else {
  65. data['client_ip'] = '0.0.0.0';
  66. }
  67. var pushData = {
  68. 'message' : data,
  69. 'type' : TYPE_SELF_CALL,
  70. 'time' : php.date('Y-m-d H:i:s', curTime / 1000)
  71. };
  72. writeRedis(pushData);
  73. }
  74. CallLog.prototype.logModuleCall = function(method, toUrl, postData, response, startTime) {
  75. var data = {};
  76. data['from_call_id'] = this.getCallId();
  77. data['from_url'] = '/' . CONTROLLER_NAME + '/' + ACTION_NAME;
  78. data['method'] = method;
  79. var parts = explode('?', toUrl);
  80. data['to_url'] = parts[0];
  81. var getParam = parts[1];
  82. var postParam = '';
  83. if (is_array(postData)) {
  84. postParam = http_build_query(postData);
  85. } else {
  86. postParam = postData;
  87. }
  88. var param = '';
  89. if (getParam && postParam) {
  90. param = getParam + '&' + postParam;
  91. } else if (getParam) {
  92. param = getParam;
  93. } else {
  94. param = postParam;
  95. }
  96. data['param'] = param;
  97. if (response !== false) {
  98. var objResult = JSON.parse(response);
  99. data['code'] = objResult['code'];
  100. data['response'] = response.substr(0, 3000);
  101. } else {
  102. data['code'] = CODE_REQUEST_TIMEOUT;
  103. }
  104. data['delay'] = new Date().getTime() - _startTime;
  105. data['server_ip'] = getServerIp();
  106. var pushData = {
  107. message : data,
  108. type : TYPE_MODULE_CALL,
  109. time : date('Y-m-d H:i:s', startTime)
  110. };
  111. writeRedis(pushData);
  112. }
  113. module.exports = CallLog;