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

const logger = global.yoho.logger;

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

const server = {
    async handle(data) {
        try {

            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('[server] handle ERROR [%s]', JSON.stringify(m));
                        break;
                    }
                }
            });
            logger.info('[server] handle OK [%s]', data);
        } 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);
};