Есть ли способ получить тело запроса и ответа на запрос http для приложения микросервисов? - PullRequest
0 голосов
/ 06 июня 2018

У меня есть приложение, состоящее из 2 микросервисов.

Есть ли способ получить запрос и ответ для каждого запроса, полученного и отправленного каждым микросервисом, с минимальными изменениями в коде.

Приложение находится в файле node.js.

Я пробовал некоторые платформы трассировки, но они предоставляют только временную информацию.

1 Ответ

0 голосов
/ 06 июня 2018

Если вы используете Express для реализации своего бэкэнд-сервера API, вы можете использовать express-winston для перехвата запроса и ответа на каждый вызов.

В приведенном ниже примере используйте Express winston для выхода из системы.запрос и ответ в консоль.

const winston = require('winston');
const expressWinston = require('express-winston');

/**
 * Custom Wiston Express Middleware to log all requests and responses in the console.
 */
module.exports = async() => {
    // Creating middleware
    expressWinston.requestWhitelist.push('body');
    expressWinston.responseWhitelist.push('body');
    const wistonMiddleware = expressWinston.logger({
        transports: [
            new winston.transports.Console({
                json: true,
                colorize: true
            })
        ],
        // optional: control whether you want to log the meta data about the request (default to true)
        meta: true,
        // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
        msg: 'HTTP {{req.method}} {{req.url}}',
        // Use the default Express/morgan request formatting, with the same colors.
        // Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true
        expressFormat: true,
        // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true
        colorStatus: true,
        ignoreRoute: function (req, res) {
            return false;
        } // optional: allows to skip some log messages based on request and/or response
    });

    return wistonMiddleware;
};

Но вы можете использовать другие модули Winston для входа в систему, как в режиме упругого поиска: https://github.com/vanthome/winston-elasticsearch

...