Skip to content

Looking for way to add correlation ids #1306

Answered by RobinTail
pupca asked this question in Q&A
Discussion options

You must be logged in to vote

@pupca , here is what I found out.
This solution works fine, when you apply the express-http-context middleware directly to express app (not by using .addExpressMiddleware).
Therefore, consider going with custom express app approach and attachRouting() method, described in the documentation, and applying the following lines to your own app:

import { v4 } from "uuid";
import * as httpContext from "express-http-context";

app.use(httpContext.middleware).use(({}, res, next) => {
    const requestId = v4();
    httpContext.set("requestId", requestId);
    console.log("the id", httpContext.get("requestId"));
    res.setHeader("x-request-id", requestId);
    next();
  });

I tested it and it works.

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@pupca
Comment options

Answer selected by RobinTail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants