Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js express request Id

I'm trying to create some sort of a request Id for logging purposes which will be available to me through every function in the request flow. I want to log every step of the request flow with an Id stating which log line is for which request.

I've looked over some ideas and ran into 2 main suggestions:

The first is creating a middleware that will add a field in the 'req' object like so(as suggested here):

var logIdIterator = 0;

app.all('*', function(req, res, next) {
  req.log = {
    id: ++logIdIterator
  }
  return next();
});

And the second is using continuation-local-storage

The problems are:

For the first approach - it means I'll have to pass an extra argument to each function in the flow and this is not an easy solution to do over a mature application with countless APIs and flows.

The second one looks promising but unfortunately it has some issues where the state gets lost(see here for example). Also, it happened a few times when we used our redis library - which is bad because redis requests happen on each of our flows.

I guess if I don't find another solution, I'll have to use the first approach, it's just that I want to avoid passing an extra parameter to thousands of existing functions.

My question is - how do you suggest to maintain a request Id through the request flow?

like image 613
gibson Avatar asked May 31 '16 15:05

gibson


People also ask

How to add a request ID in a Node JS app?

As you already know, if you want to have request ids in your Node.js app, you may use cls-hooked and integrate it with the web framework that you are using. But probably you would want to use a library that would be doing this stuff. Recently I was in search of such library and failed to find a good match for the task.

How to import the express-request-ID instead of the required approach?

If you are using express and wanted to import the express-request-id instead of the required approach. You may try this. import expressRequestId from 'express-request-id' import express from 'express' const framework = express () framework.use (expressRequestId ())

How to add request ID to X-request-ID header?

Generate UUID for request and add it to X-Request-Id header. In case request contains X-Request-Id header, uses its value instead. Returns middleware function, that appends request id to req object. uuidVersion - version of uuid to use (defaults to v4 ).

How to implement an API in Node JS?

In the case of implementing an API in Node.js, the most common is to use the Express framework. Espress.js allows us to implement APIs in a very simple way. Some of the most powerful features it offers are: Implementation of functions to be executed in each request that arrives at the API


1 Answers

You can use this package: https://www.npmjs.com/package/express-request-id

This is the middleware that will append uuid for every request

var app = require('express')();
var addRequestId = require('express-request-id')();
 
app.use(addRequestId);
 
app.get('/', function (req, res, next) {
    res.send(req.id);
    next();
});
 
app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});
 
// curl localhost:3000 
// d7c32387-3feb-452b-8df1-2d8338b3ea22 
like image 80
vun Avatar answered Oct 13 '22 07:10

vun