Home  Tech   How to corr ...

How to correlation id to track the request flow in expressjs app

Using a correlation ID to track the flow in an application is a common technique for tracing and debugging. A correlation ID is a unique identifier assigned to each request that passes through your application, allowing you to track all actions and logs related to that request across different services and components. Here’s how you can implement this in a Node.js application using Express, Winston, and a middleware approach.

Steps to Implement Correlation ID

  1. Generate a Correlation ID for Each Request: Use middleware to generate and assign a correlation ID to each incoming request.
  2. Attach the Correlation ID to All Logs: Modify your logging setup to include the correlation ID in all log messages.
  3. Propagate the Correlation ID: Ensure the correlation ID is propagated across all services and components involved in handling the request.

Example Implementation

Step 1: Setup the Project

First, create a new Node.js project and install the necessary dependencies:

mkdir correlation-id-example
cd correlation-id-example
npm init -y
npm install express uuid morgan winston

Step 2: Create Middleware to Generate and Attach Correlation ID

Create a middleware function to generate a correlation ID and attach it to the request object:

// correlation-id-middleware.js
const { v4: uuidv4 } = require('uuid');

const correlationIdMiddleware = (req, res, next) => {
  const correlationId = uuidv4();
  req.correlationId = correlationId;
  res.set('X-Correlation-ID', correlationId);
  next();
};

module.exports = correlationIdMiddleware;

Step 3: Setup Logging with Winston

Configure Winston to include the correlation ID in log messages:

// logger.js
const winston = require('winston');

const correlationIdFormat = winston.format((info, opts) => {
  info.correlationId = opts.req.correlationId;
  return info;
});

const logger = (req) => winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    correlationIdFormat({ req }),
    winston.format.json(),
    winston.format.timestamp(),
    winston.format.printf(info => `${info.timestamp} [${info.correlationId}] ${info.level}: ${info.message}`)
  ),
  transports: [
    new winston.transports.Console()
  ]
});

module.exports = logger;

Step 4: Use Middleware and Logger in Express App

Integrate the correlation ID middleware and logging into your Express application:

// app.js
const express = require('express');
const morgan = require('morgan');
const correlationIdMiddleware = require('./correlation-id-middleware');
const createLogger = require('./logger');

const app = express();

// Use correlation ID middleware
app.use(correlationIdMiddleware);

// Use Morgan to log HTTP requests
app.use(morgan('combined'));

// Example route
app.get('/', (req, res) => {
  const logger = createLogger(req);
  logger.info('Handling request');
  res.send('Hello, world!');
});

// Example route with an async operation
app.get('/async', async (req, res) => {
  const logger = createLogger(req);
  logger.info('Handling async request');
  await new Promise(resolve => setTimeout(resolve, 1000));
  logger.info('Completed async operation');
  res.send('Async operation completed');
});

// Start the server
const port = 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

In this example, we:

  1. Generated a Correlation ID: Each request gets a unique correlation ID using the correlationIdMiddleware.
  2. Attached the Correlation ID to Logs: Winston was configured to include the correlation ID in each log message.
  3. Logged Requests and Responses: Using Morgan for HTTP request logging and Winston for application-level logging, all logs include the correlation ID.

This setup allows you to trace and debug the flow of a request through your application by searching for logs with the same correlation ID. This is especially useful in microservices architectures where a single request may traverse multiple services.

Published on: Jun 12, 2024, 06:25 AM  
 

Comments

Add your comment