Authored by 周奇琪

在错误处理里加上报APM

@@ -193,6 +193,11 @@ if (isProduction) { @@ -193,6 +193,11 @@ if (isProduction) {
193 } 193 }
194 return Math.min(options.attempt * 100, 1000); 194 return Math.min(options.attempt * 100, 1000);
195 } 195 }
  196 + },
  197 + report: {
  198 + host: 'influxd.yoho.cn',
  199 + port: 80,
  200 + db: 'web-apm'
196 } 201 }
197 }); 202 });
198 } else if (isTest) { 203 } else if (isTest) {
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 const headerModel = require('../models/header'); 5 const headerModel = require('../models/header');
6 const logger = global.yoho.logger; 6 const logger = global.yoho.logger;
7 const helpers = global.yoho.helpers; 7 const helpers = global.yoho.helpers;
  8 +const sender = global.yoho.apmSender;
8 9
9 const forceNoCache = (res) => { 10 const forceNoCache = (res) => {
10 if (res && !res.finished) { 11 if (res && !res.finished) {
@@ -49,6 +50,24 @@ exports.notFound = () => { @@ -49,6 +50,24 @@ exports.notFound = () => {
49 exports.serverError = () => { 50 exports.serverError = () => {
50 return (err, req, res, next) => { 51 return (err, req, res, next) => {
51 forceNoCache(res); 52 forceNoCache(res);
  53 + const uid = this.ctx.req.user.uid || 0;
  54 + const udid = this.ctx.req.query.udid || this.ctx.req.cookies._yasvd;
  55 +
  56 + //上报服务端错误
  57 + sender.addMessage({
  58 + measurement: 'error-report',
  59 + tags: {
  60 + type: 'server',
  61 + reqID: req.reqID,
  62 + uid,
  63 + udid,
  64 + code: err.code || 500
  65 + },
  66 + fields: {
  67 + message: err.message,
  68 + stack: err.stack
  69 + }
  70 + });
52 71
53 if (err && err.code === 401) { 72 if (err && err.code === 401) {
54 logger.error(`401 error info:client_type=${req.query.client_type},req.user=${JSON.stringify(req.user)},req.query=${JSON.stringify(req.query)},cookies=${JSON.stringify(req.cookies)}`); // eslint-disable-line 73 logger.error(`401 error info:client_type=${req.query.client_type},req.user=${JSON.stringify(req.user)},req.query=${JSON.stringify(req.query)},cookies=${JSON.stringify(req.cookies)}`); // eslint-disable-line