puzpuzpuz/cls-rtracer

cls-rtracer is Generating same request id for multiple requests even for different routes.

ashutosh4336 opened this issue · 4 comments

This might be the dumbest question you've seen in your life or might be I'm doing something wrong. In past I've used this package and it worked like charm. But recently I wanted to use it in out existing app but I'm facing problem with same request id being used for multiple request. (Different routes even).

I'm attaching the sample app for reference. Please let me know if I'm doing something wrong or missing something.

"cls-rtracer": "^2.6.3",
"express": "^4.18.3",
const express = require('express');
const rTracer = require('cls-rtracer');

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(rTracer.expressMiddleware());

// Middleware to set request ID in headers
app.use((req, res, next) => {
  const rTracerId = rTracer.id();
  req.headers['X-POC-Request-ID'] = rTracerId;
  res.header('X-POC-Request-ID', rTracerId);
  next();
});

app.get('/fast-route', (req, res) => {
  const requestId = rTracer.id();
  console.log('Request ID in fast-route:', requestId);

  logMe();

  res.status(200).json({ success: true, requestId });
});

app.get('/slow-route', async (req, res) => {
  const requestId = rTracer.id();
  console.log('Request ID in slow-route:', requestId);

  const response = await new Promise((resolve) => {
    setTimeout(() => {
      return resolve({ message: 'Timed Out....', requestId });
    }, 3000);
  });

  res.status(200).json({
    success: true,
    data: response,
  });
});

function logMe() {
  const requestId = rTracer.id();
  console.log('Request ID in logMe:', requestId);
}

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

Hi,

How do you test the application to face the duplicate ids?

As In the code there are console.logs inside the routes and in the response also I'm sending the generated requestID.

 const requestId = rTracer.id();
 console.log('Request ID in fast-route:', requestId);

Weird, since I'm observing different ids with Express 4.19.0 and Node v20.8.0.

$ node cls.js 
Server is running on port 3000
Request ID in fast-route: ca5bba60-e6f1-11ee-b86d-47e6cf73c01d
Request ID in logMe: ca5bba60-e6f1-11ee-b86d-47e6cf73c01d
Request ID in fast-route: cad55730-e6f1-11ee-b86d-47e6cf73c01d
Request ID in logMe: cad55730-e6f1-11ee-b86d-47e6cf73c01d
Request ID in slow-route: ce5d6c80-e6f1-11ee-b86d-47e6cf73c01d
Request ID in slow-route: d243ce70-e6f1-11ee-b86d-47e6cf73c01d
Request ID in slow-route: d7f616c0-e6f1-11ee-b86d-47e6cf73c01d

Note: id suffixes are identical, but prefixes are different, so these are different UUIDs.

Ahhh.... yes. I'm sorry to bug you.... Yes the suffixes are identical prefixes were alittle different.
Through I briefly looked at the prefixes as well. I guess I didn't pay enough attention. I'm so sorry.

Thank you so much. :)

I'm closing the issue. I'm so sorry.