Implementing distributed logging
Once we have created a distributed system, everything gets more complicated. One of the things that becomes more complicated is logging and tracing a request across multiple microservices. In Chapter 11, we covered distributed logging – this is a technique that allows us to trace all the log lines that are relevant to a specific request flow via the use of correlation IDs (reqId
). In this section, we will put that into practice.
First, we modify our gateway’s server.js
file to generate a new UUID for the request chain, like so:
const crypto = require('crypto') const fastify = require('fastify')({ logger: true, genReqId (req) { const uuid = crypto.randomUUID() req.headers['x-request-id'] = uuid return uuid } })
Note that we generate a new UUID at every request and assign it back to the headers...