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

const logger = global.yoho.logger;

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

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

        msgs.forEach((m) => {
            if (!m.measurement) {
                return;
            }

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

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

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