Skip to content

Commit

Permalink
PP-11120 Add support for additionalLoggingFields
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencdaly committed Oct 12, 2023
1 parent 8788f01 commit f9af883
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
7 changes: 5 additions & 2 deletions app/services/clients/base-client/axios-base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Client {
method: config.method,
url: config.url,
description: config.description,
additionalLoggingFields: config.additionalLoggingFields,
...config.retryCount && { retryCount: config.retryCount }
}
if (options.onRequestStart) {
Expand All @@ -64,7 +65,8 @@ class Client {
params: response.config.params,
status: response.status,
url: response.config.url,
description: response.config.description
description: response.config.description,
additionalLoggingFields: response.config.additionalLoggingFields
}
if (options.onSuccessResponse) {
options.onSuccessResponse(responseContext)
Expand All @@ -87,7 +89,8 @@ class Client {
errorIdentifier: error.response && error.response.data && error.response.data.error_identifier,
reason: error.response && error.response.data && error.response.data.reason,
message: errors || error.response.data || 'Unknown error',
description: config.description
description: config.description,
additionalLoggingFields: config.additionalLoggingFields
}

// TODO: could use axios-retry to achieve this if desired
Expand Down
81 changes: 60 additions & 21 deletions app/services/clients/base-client/config.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const nock = require('nock')
const sinon = require('sinon')
const proxyquire = require('proxyquire')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
Expand All @@ -12,39 +13,77 @@ const { expect } = chai
const baseUrl = 'http://localhost:8000'
const app = 'an-app'

function getConfigWithMockedRequestContext (correlationId) {
const logInfoSpy = sinon.spy()

function getConfigWithMocks (correlationId) {
const config = proxyquire('./config.js', {
'../../../utils/request-context': {
getRequestCorrelationIDField: () => correlationId
}
},
'../../../utils/request-logger': proxyquire('../../../utils/request-logger', {
'./logger': () => ({
info: logInfoSpy
})
})
})
return config
}

describe('Client config', () => {
it('should add correlation ID as header when correlation ID exists on request context', () => {
const client = new Client(app)
const config = getConfigWithMockedRequestContext('abc123')
config.configureClient(client, baseUrl)

nock(baseUrl)
.get('/')
.reply(200)
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.have.property('x-request-id', 'abc123')
beforeEach(() => {
logInfoSpy.resetHistory()
})

describe('Headers', () => {
it('should add correlation ID as header when correlation ID exists on request context', () => {
const client = new Client(app)
const config = getConfigWithMocks('abc123')
config.configureClient(client, baseUrl)

nock(baseUrl)
.get('/')
.reply(200)
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.have.property('x-request-id', 'abc123')
})
})

it('should not add correlation ID as header when correlation ID does not exist on request context', () => {
const client = new Client(app)
const config = getConfigWithMocks()
config.configureClient(client, baseUrl)

nock(baseUrl)
.get('/')
.reply(200)
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.not.have.key('x-request-id')
})
})
})

it('should not add correlation ID as header when correlation ID does not exist on request context', () => {
const client = new Client(app)
const config = getConfigWithMockedRequestContext()
config.configureClient(client, baseUrl)
describe('Logging', () => {
it('should log request start', () => {
const client = new Client(app)
const config = getConfigWithMocks('abc123')
config.configureClient(client, baseUrl)

nock(baseUrl)
.get('/')
.reply(200)
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.not.have.key('x-request-id')
nock(baseUrl)
.get('/')
.reply(200)
return expect(client.get('/', 'do something', {
additionalLoggingFields: {
foo: 'bar'
}
})).to.be.fulfilled.then((response) => {
sinon.assert.calledWith(logInfoSpy, 'Calling an-app to do something', {
service: app,
method: 'get',
url: '/',
description: 'do something',
foo: 'bar'
})
})
})
})
})

0 comments on commit f9af883

Please sign in to comment.