cagov / ui-claim-tracker

This repo contains the Claim Status Tracker app, which helps Californians better understand what’s happening with their unemployment claim and benefits.
9 stars 4 forks source link

Log a UUID for all traces in a single request/response cycle using a pino child logger #523

Closed rocketnova closed 3 years ago

rocketnova commented 3 years ago

Ticket

Resolves #506

Changes

Context

Right now, there is no way to associate related log lines with one another. This PR creates a UUID for each request and then includes it in all logger calls made for that request. This will allow us to easily search and associate logger calls when analyzing logs.

In the request entrypoint in index.tsx:getServerSideProps():

      await asyncContext.run(childLogger, async () => {
        // Make the API request and return the data.
        const claimData = await queryApiGateway(req, uniqueNumber)
        logger.log(childLogger, 'info', claimData, 'ClaimData')

        // Run business logic to get content for the current scenario.
        scenarioContent = getScenarioContent(claimData)
        logger.log(childLogger, 'info', scenarioContent, 'ScenarioContent')
      })

In subsequent utility functions (e.g. queryApiGateway(), getClaimDetails.ts:getProgramExtensionPair(), etc), to make a logger call:

Testing

Regression Testing

Because these changes refactor the way we call the core functionality (i.e. by wrapping queryApiGateway() and getScenarioContent() inside an async callback function), we should thoroughly test for any regressions by:

Testing New Functionality

Secondly, we should make sure the changes actually give us the benefit we want (i.e. the UUID is accessible to associate log lines together).

Easy Tests

These tests are easy to trigger on the local environment connected to Dev API gateway:

Harder Tests

These tests are harder to trigger on a local environment because they cover edge cases that we don't have unique numbers for on the Dev API gateway.

To test these, use the mini API gateway mock I built for this purpose. Instructions:

  1. Clone https://github.com/rocketnova/ca-ui-mini-api-mock and use docker-compose up --build -d to build and run the container
  2. In the CA UI repo, in .env.local, change API_URL to API_URL=http://localhost:8888/GetClaimStatus
  3. [hack for local testing only!] In the CA UI repo, in queryApiGateway.ts, comment out line 209 (agent: sslConfiguredAgent)
  4. In the CA UI repo, run yarn dev and use modHeader to test the various "unique numbers" in https://github.com/rocketnova/ca-ui-mini-api-mock#usage
lomky commented 3 years ago

Tested on Internal - all looks good!