Skip to content

Commit

Permalink
PP-5762 Add correlation id to logging
Browse files Browse the repository at this point in the history
Add middleware to store the correlation on the commonLoggingFields
object on the request so that it will be included in logs.
  • Loading branch information
stephencdaly committed Dec 9, 2019
1 parent d49b4c0 commit 0540f97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/middleware/correlation_header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const { CORRELATION_ID } = require('@govuk-pay/pay-js-commons').logging.keys
const { CORRELATION_HEADER } = require('../../config/correlation_header')
const { setLoggingField } = require('../utils/logging_fields_helper')

module.exports = (req, res, next) => {
req.correlationId = req.headers[CORRELATION_HEADER] || ''
setLoggingField(req, CORRELATION_ID, req.correlationId)
next()
}
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const i18nConfig = require('./config/i18n')
const i18nPayTranslation = require('./config/pay-translation')
const Sentry = require('./app/utils/sentry.js').initialiseSentry()
const csp = require('./app/middleware/csp')
const correlationHeader = require('./app/middleware/correlation_header')

// Global constants
const { NODE_ENV, PORT, ANALYTICS_TRACKING_ID, GOOGLE_PAY_MERCHANT_ID, APPLE_PAY_MERCHANT_ID_CERTIFICATE } = process.env
Expand Down Expand Up @@ -83,6 +84,7 @@ function initialiseGlobalMiddleware (app) {
app.use(compression())

app.disable('x-powered-by')
app.use(correlationHeader)
}

function initialisei18n (app) {
Expand Down

0 comments on commit 0540f97

Please sign in to comment.