Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grab correlation ID and send as a response header #1787

Merged
merged 14 commits into from
Jun 11, 2024

Conversation

yunakim714
Copy link
Collaborator

@yunakim714 yunakim714 commented May 15, 2024

Description

As part of log forwarding, we are now generating a v4 UUID Correlation ID to attach to our MRT logs so that our logs can be searched for outside of MRT. In MRT, this is now added as a new header, x-correlation-id, to the event object, which is the json representation of the HTTP request. In pwa-kit, we then need to pull this new header, modify the response, and add this header to the response so that the correlation ID value is accessible and visible to the user.

GUS: W-15706159
Linked PR to generate Correlation ID and add to Log Center logs: https://git.soma.salesforce.com/cc-mobify/portal_app/pull/5770
Linked PR to add Correlation ID to Splunk logs and log tailing: https://git.soma.salesforce.com/cc-mobify/portal_app/pull/5806

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • In _setRequestId(), instead of the x-api-gateway-event header, the x-correlation-id header is now the value used to set the new requestId
  • In processLambdaResponse(), x-correlation-id is set as a response header if it exists

How to Test-Drive This PR

Screenshot 2024-06-03 at 4 08 21 PM

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@yunakim714 yunakim714 changed the title Grab correlation id header Grab correlation ID and send as a response header Jun 3, 2024
@yunakim714 yunakim714 marked this pull request as ready for review June 3, 2024 20:12
@yunakim714 yunakim714 requested a review from a team as a code owner June 3, 2024 20:12
@kevinxh
Copy link
Collaborator

kevinxh commented Jun 3, 2024

Before we merge this PR, is it possible to verify that the x-correlation-id from the incoming header, matches the SCAPI request headers, on both the server and the client side?

* @private
*/
_setRequestId(app) {
app.use((req, res, next) => {
if (!req.headers['x-apigateway-event']) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just double checking...

The header x-apigateway-event will still be available from MRT right? We need to make sure the old sdk versions continue to work on MRT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did we use the x-apigateway-event header for before?

Also, I see that we reference x-apigateway-event in a few other places, such as in tests. Do we need to replace those with x-correlation-id as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that APIG is actually expected to be removed next release. @drewzboto could you confirm this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do intend to remove APIG. We haven't firmly committed which release it'll be done in, but definitely over the next ~6 months

Copy link
Collaborator

@johnboxall johnboxall Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really important discussion.

We need to consider sites that are already live and that haven't been redeployed and sites running older versions of the PWA Kit.

In this case, we're in good shape because this code "guards" against the absence of the both the new and the old headers, but we might consider continuing to send the "old" header with our own generated value to "backport" the feature to folks using earlier versions of the PWA Kit.

This is especially important because if you look at the next line down, we log an error if we can't see this header – which means that any sites using an older version of the PWA Kit would fill up their logs with errors about this header being missing if we removed it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. @johnboxall Would it be okay to set res.locals.requestId to the x-apigateway-event header if x-correlation-id isn't available? Or is this undesirable as then we don't know which value res.locals.requestId is representing?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with the correlation Id we can now safely remove these lines:

// If the request has an X-Amz-Cf-Id header, log it now
// to make it easier to associated CloudFront requests
// with Lambda log entries. Generally we avoid logging
// because it increases the volume of log data, but this
// is important for log analysis.
const cloudfrontId = req.headers['x-amz-cf-id']
if (cloudfrontId) {
// Log the Express app request id plus the cloudfront
// x-edge-request-id value. The resulting line in the logs
// will automatically include the lambda RequestId, so
// one line links all ids.
console.log(`Req ${res.locals.requestId} for x-edge-request-id ${cloudfrontId}`)
}

kevinxh
kevinxh previously approved these changes Jun 5, 2024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@johnboxall johnboxall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What tests would we expect to alter/update or add with this change?

alexvuong
alexvuong previously approved these changes Jun 6, 2024
@yunakim714 yunakim714 merged commit c4a97cd into develop Jun 11, 2024
28 checks passed
@adamraya adamraya mentioned this pull request Jun 12, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants