server-apm.js 3.23 KB
const Sender = require('influx-batch-sender');
const MysqlSender = require('../lib/mysql-sender');
const config = require('../common/config');
const msg2row = require('./msg2row');
const lineparse = require('../lib/line-parse');

const errorSqlSender = new MysqlSender(config.table.error);
const slowRouterSqlSender = new MysqlSender(config.table.slow);

const routeInfluxSender = new Sender(config.reportRoute);
const apiInfluxSender = new Sender(config.reportApi);
const _ = require('lodash');

const API_BLACK_LIST = [
    'app.shop.banner'
];

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

        for (let m of msgs) {
            if (m.measurement === 'web-server-duration') {
                let duration = parseInt(m.fields.duration);

                if (duration > config.slowRoute.min / 10 && duration < config.slowRoute.max) {
                    const newData = _.merge({}, m, {
                        fields: {
                            duration
                        }
                    });

                    slowRouterSqlSender.addMessage(msg2row.slowRouter(newData));
                }

                if (m.tags.type.toLowerCase() === 'api') {
                    if (_.includes(API_BLACK_LIST, m.tags.api)) {
                        return;
                    }

                    apiInfluxSender.addMessage({
                        tags: {
                            app: m.tags.app,
                            host: m.tags.hostname,
                            api: m.tags.api
                        },
                        fields: {
                            duration: duration,
                            times: 1
                        }
                    });
                }

                if (m.tags.type.toLowerCase() === 'route') {
                    routeInfluxSender.addMessage({
                        tags: {
                            app: m.tags.app,
                            host: m.tags.hostname,
                            route: m.tags.route
                        },
                        fields: {
                            duration: duration
                        }
                    });
                }
            } else if (m.measurement === 'error-report') {
                routeInfluxSender.addMessage({
                    measurement: 'error-info',
                    tags: {
                        app: m.tags.app,
                        host: m.tags.hostname,
                        route: m.tags.route,
                        code: m.tags.code
                    },
                    fields: {
                        times: 1
                    }
                });

                errorSqlSender.addMessage(msg2row.errorRouter(m));
            } else if (m.measurement === 'process-info') {
                m.measurement = 'process-info';
                _.set(m, 'fields.memory', parseInt(_.get(m, 'fields.memory', '0')));
                _.set(m, 'fields.cpu', parseInt(_.get(m, 'fields.cpu', '0')));
                routeInfluxSender.addMessage(m);
            }
        }
    }
};

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

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