CallLog.js 4.0 KB

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