serverapm.js 1.6 KB
const lineparse = require('../lib/line-parse');
const bb = require('bluebird');

const logger = global.yoho.logger;

const {
    handleErrorReport,
    handleProcessInfo,
    handleWebServerDuration
} = require('./serverapm-service');

//const riskService = require('./risk-service');

//const handleRisk = riskService();

const server = {
    async handle(data) {
        try {
            let msgs = lineparse.parse(data);

            await bb.map(msgs, async(m) => {
                if (!m.measurement) {
                    return;
                }

                switch (m.measurement) {
                    case 'web-server-duration': {
                        handleWebServerDuration(m);
                        //await handleRisk(m);
                        break;
                    }
                    case 'error-report': {
                        handleErrorReport(m);
                        break;
                    }
                    case 'process-info': {
                        handleProcessInfo(m);
                        break;
                    }
                    default: {
                        logger.error('[server] handle ERROR [%s]', JSON.stringify(m));
                        break;
                    }
                }

                logger.info('[server] handle OK [%s]', data);
            }, {concurrency: 2});
        } catch (e) {
            logger.error('[server] handle ERROR [%s]', e);
        }
    }
};

module.exports = function(req, res, next) {
    let data = req.body || ' ';

    server.handle(data).then(() => {
        res.status(204).json();
    }).catch(next);
};