The main goal of this spike is to have unique traceId for each request throughout the flow of that particular request

Eg: When requesting inventory information from IMS then the flow is something like this Controller > Handler > Repository

For the above flow many logs are generated by application, If all the logs for one particular request have same unique ID then it will help us to identify whole flow each request and pin point to issue if occurs.

There are few approaches to have unique traceId for each requests

1) Passing traceID in every function ****

In this approach we can pass unique traceId in each function where the functions are called.

Pros: This is the easiest and reliable approach

Cons: Lot of touch points, If any changes happens we have to make changes everywhere and when application grows it is very difficult to maintain

2) Global variable approach

To solve the problem in first approach we can introduce global variable where traceID will be stored and can used by all the functions in the application for flow of a particular request.

Pros: Global variable is reused everywhere and no need to pass argument in each function

Cons: Global variable can be easily polluted

Example:

  1. Req-1 to get “SKU-1” inventory info
    1. Req-1 set traceID to “id-1”
    2. started fetching data from database
  2. While for Req-1, application is fetching the data from database, new request Req-2 with sku “SKU-2” is received by the application
    1. Req-2 set the traceID to “id-2”
    2. start fetching the data from database
  3. Req-1 gets the inventory info from database and returns the data but, the traceID was changed by new Req-2 and because of that Req-1 will have the incorrect traceID value “id-2” instead of “id-1”

Since, The global variable can be easily polluted and resulting in incorrect traceID we cannot go ahead with this approach