-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-13119 Fix logging of correlation ID to log messages
- Remove references to `async local storage`. - No longer using this as that will be a big change. - Instead continue to save logging context fields to the request instead. - Remove all references to `async local storage` - including the `request-context` file. - Fix bug where the correlation id was not getting logged and not sent as a header to backend services. - When configuring a client that calls the backend - we now pass the correlation id and save it for each request. - Pass the correlation id to the backend as a header. - Log the correlation id as part of logging messages. - Remove logging messages that are no longer required as the `pay-js-commons/axios-base-client` will do the logging instead.
- Loading branch information
Showing
10 changed files
with
105 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
'use strict' | ||
|
||
const { CORRELATION_ID } = require('@govuk-pay/pay-js-commons').logging.keys | ||
const crypto = require('crypto') | ||
|
||
const { CORRELATION_HEADER } = require('../../config/correlation-header') | ||
const { setLoggingField } = require('../utils/logging-fields-helper') | ||
const { CORRELATION_ID } = require('@govuk-pay/pay-js-commons').logging.keys | ||
|
||
module.exports = (req, res, next) => { | ||
req.correlationId = req.headers[CORRELATION_HEADER] || '' | ||
if (!req.headers[CORRELATION_HEADER]) { | ||
req.headers[CORRELATION_HEADER] = crypto.randomBytes(16).toString('hex') | ||
} | ||
|
||
req.correlationId = req.headers[CORRELATION_HEADER] | ||
setLoggingField(req, CORRELATION_ID, req.correlationId) | ||
|
||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const proxyquire = require('proxyquire') | ||
const sinon = require('sinon') | ||
const { CORRELATION_HEADER } = require('../../config/correlation-header') | ||
|
||
const { expect } = require('chai') | ||
|
||
function getCorrelationHeader () { | ||
return proxyquire('./correlation-header', { | ||
crypto: { | ||
randomBytes: function () { | ||
return 'test-correlation-id' | ||
} | ||
} | ||
}) | ||
} | ||
|
||
describe('Correlation header', () => { | ||
it('sets the correlation id when there is no x_request_id header', () => { | ||
const correlationHeader = getCorrelationHeader() | ||
|
||
const req = { | ||
headers: {} | ||
} | ||
const res = null | ||
const next = sinon.spy() | ||
|
||
correlationHeader(req, res, next) | ||
expect(req.headers[CORRELATION_HEADER]).to.equal('test-correlation-id') | ||
expect(req.correlationId).to.equal('test-correlation-id') | ||
sinon.assert.calledWithExactly(next) | ||
}) | ||
|
||
it('sets the correlation id using the x-request-id header when it exists', () => { | ||
const correlationHeader = getCorrelationHeader() | ||
const xRequestIdHeaderValue = 'x-request-id-value' | ||
|
||
const req = { | ||
headers: {} | ||
} | ||
|
||
req.headers[CORRELATION_HEADER] = xRequestIdHeaderValue | ||
|
||
const res = null | ||
const next = sinon.spy() | ||
|
||
correlationHeader(req, res, next) | ||
expect(req.headers[CORRELATION_HEADER]).to.equal(xRequestIdHeaderValue) | ||
expect(req.correlationId).to.equal(xRequestIdHeaderValue) | ||
sinon.assert.calledWithExactly(next) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.