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

feat(middleware-correlationid): added ability for custom logger #231

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ Accepts a configuration object of the following shape:

```js
{
sampleDebugLogRate: double [between 0 and 1]
sampleDebugLogRate: double [between 0 and 1],
constructLoggerFn: (correlationIds) => Logger [optional]
}
```

```js
const middy = require('middy')
const correlationIds = require('@dazn/lambda-powertools-middleware-correlation-ids')
const Log = require('@dazn/lambda-powertools-middleware-correlation-ids')

const handler = async (event, context) => {
return 42
}

module.exports = middy(handler)
.use(correlationIds({ sampleDebugLogRate: 0.01 }))
.use(correlationIds({ sampleDebugLogRate: 0.01, constructLoggerFn: (correlationIds) => new Log({ correlationIds }) }))
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const captureCorrelationIds = require('../index')

global.console.log = jest.fn()

const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
const invokeDynamoHandler = (event, awsRequestId, captureArg, handlerF, recordF, done) => {
const handler = middy((event, context, cb) => {
// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())
Expand All @@ -21,7 +21,7 @@ const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF,

cb(null)
})
handler.use(captureCorrelationIds({ sampleDebugLogRate }))
handler.use(captureCorrelationIds(captureArg))

handler(event, { awsRequestId }, (err, result) => {
if (err) {
Expand Down Expand Up @@ -61,7 +61,7 @@ const dynamoTests = () => {
describe('when sampleDebugLogRate = 0', () => {
it('always sets debug-log-enabled to false', () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
invokeDynamoHandler(genDynamoEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -77,7 +77,7 @@ const dynamoTests = () => {
describe('when event lacks NewImage', () => {
it('should set default correlation id', () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, 0,
invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -94,7 +94,7 @@ const dynamoTests = () => {
describe('when sampleDebugLogRate = 1', () => {
it('always sets debug-log-enabled to true', () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 1,
invokeDynamoHandler(genDynamoEvent(), requestId, { sampleDebugLogRate: 1 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('true')
Expand All @@ -110,7 +110,7 @@ const dynamoTests = () => {
describe('when correlation ID is not provided in the event', () => {
it('sets it to the AWS Request ID', () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
invokeDynamoHandler(genDynamoEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
// correlation IDs at the handler level
expect(x['x-correlation-id']).toBe(requestId)
Expand All @@ -128,7 +128,7 @@ const dynamoTests = () => {
describe('when call-chain-length is not provided in the event', () => {
it('sets it to 1', () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
invokeDynamoHandler(genDynamoEvent(), requestId, { sampleDebugLogRate: 0 },
x => {}, // n/a
record => {
const x = record.correlationIds.get()
Expand Down Expand Up @@ -157,7 +157,7 @@ const dynamoTests = () => {

const event = genDynamoEvent(correlationIds)
requestId = uuid()
invokeDynamoHandler(event, requestId, 0, x => {
invokeDynamoHandler(event, requestId, { sampleDebugLogRate: 0 }, x => {
handlerCorrelationIds = x
}, aRecord => {
record = aRecord
Expand Down Expand Up @@ -204,7 +204,7 @@ const dynamoTests = () => {
}

const event = genDynamoEvent(correlationIds)
invokeDynamoHandler(event, uuid(), 0,
invokeDynamoHandler(event, uuid(), { sampleDebugLogRate: 0 },
() => {},
aRecord => { record = aRecord },
done)
Expand All @@ -215,6 +215,36 @@ const dynamoTests = () => {
expect(x['call-chain-length']).toBe(2)
})
})

describe('when constructLoggerFn provided in the args', () => {
let record
let id
let logger
let constructLoggerFn

beforeEach((done) => {
id = uuid()
logger = { name: 'newLogger' }
constructLoggerFn = jest.fn(id => logger)
const correlationIds = {
'x-correlation-id': id,
'call-chain-length': 1
}

const event = genDynamoEvent(correlationIds)
invokeDynamoHandler(event, uuid(), { sampleDebugLogRate: 0, constructLoggerFn },
() => {},
aRecord => { record = aRecord },
done)
})

it('sets logger as a non-enumerable property', () => {
expect(record).toHaveProperty('logger')
expect(record.propertyIsEnumerable('logger')).toBe(false)
expect(record.logger).toBe(logger)
expect(constructLoggerFn.mock.calls.length).toBe(1)
})
})
}

describe('Correlation IDs middleware (Dynamo)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const captureCorrelationIds = require('../index')

global.console.log = jest.fn()

const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
const invokeFirehoseHandler = (event, awsRequestId, captureArg, handlerF, recordF, done) => {
const handler = middy((event, context, cb) => {
// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())
Expand All @@ -20,7 +20,7 @@ const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF

cb(null)
})
handler.use(captureCorrelationIds({ sampleDebugLogRate }))
handler.use(captureCorrelationIds(captureArg))

handler(event, { awsRequestId }, (err, result) => {
if (err) {
Expand Down Expand Up @@ -54,7 +54,7 @@ const firehoseTests = () => {
describe('when sampleDebugLogRate = 0', () => {
it('always sets debug-log-enabled to false', () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
invokeFirehoseHandler(genFirehoseEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -70,7 +70,7 @@ const firehoseTests = () => {
describe('when event lacks JSON payload', () => {
it('should ignore the event', () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, 0,
invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand Down Expand Up @@ -101,7 +101,7 @@ const firehoseTests = () => {
describe('when correlation ID is not provided in the event', () => {
it('sets it to the AWS Request ID', () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
invokeFirehoseHandler(genFirehoseEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
// correlation IDs at the handler level
expect(x['x-correlation-id']).toBe(requestId)
Expand All @@ -119,7 +119,7 @@ const firehoseTests = () => {
describe('when call-chain-length is not provided in the event', () => {
it('sets it to 1', () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
invokeFirehoseHandler(genFirehoseEvent(), requestId, { sampleDebugLogRate: 0 },
x => {}, // n/a
record => {
const x = record.correlationIds.get()
Expand Down Expand Up @@ -148,7 +148,7 @@ const firehoseTests = () => {

const event = genFirehoseEvent(correlationIds)
requestId = uuid()
invokeFirehoseHandler(event, requestId, 0, x => {
invokeFirehoseHandler(event, requestId, { sampleDebugLogRate: 0 }, x => {
handlerCorrelationIds = x
}, aRecord => {
record = aRecord
Expand Down Expand Up @@ -195,7 +195,7 @@ const firehoseTests = () => {
}

const event = genFirehoseEvent(correlationIds)
invokeFirehoseHandler(event, uuid(), 0,
invokeFirehoseHandler(event, uuid(), { sampleDebugLogRate: 0 },
() => {},
aRecord => { record = aRecord },
done)
Expand All @@ -206,6 +206,36 @@ const firehoseTests = () => {
expect(x['call-chain-length']).toBe(2)
})
})

describe('when constructLoggerFn provided in the args', () => {
let record
let id
let logger
let constructLoggerFn

beforeEach((done) => {
id = uuid()
logger = { name: 'newLogger' }
constructLoggerFn = jest.fn(id => logger)
const correlationIds = {
'x-correlation-id': id,
'call-chain-length': 1
}

const event = genFirehoseEvent(correlationIds)
invokeFirehoseHandler(event, uuid(), { sampleDebugLogRate: 0, constructLoggerFn },
() => {},
aRecord => { record = aRecord },
done)
})

it('sets logger as a non-enumerable property', () => {
expect(record).toHaveProperty('logger')
expect(record.propertyIsEnumerable('logger')).toBe(false)
expect(record.logger).toBe(logger)
expect(constructLoggerFn.mock.calls.length).toBe(1)
})
})
}

describe('Correlation IDs middleware (Firehose)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const captureCorrelationIds = require('../index')

global.console.log = jest.fn()

const invokeKinesisHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
const invokeKinesisHandler = (event, awsRequestId, captureArg, handlerF, recordF, done) => {
const handler = middy((event, context, cb) => {
// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())
Expand All @@ -20,7 +20,7 @@ const invokeKinesisHandler = (event, awsRequestId, sampleDebugLogRate, handlerF,

cb(null)
})
handler.use(captureCorrelationIds({ sampleDebugLogRate }))
handler.use(captureCorrelationIds(captureArg))

handler(event, { awsRequestId }, (err, result) => {
if (err) {
Expand Down Expand Up @@ -54,7 +54,7 @@ const kinesisTests = () => {
describe('when sampleDebugLogRate = 0', () => {
it('always sets debug-log-enabled to false', () => {
const requestId = uuid()
invokeKinesisHandler(genKinesisEvent(), requestId, 0,
invokeKinesisHandler(genKinesisEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -70,7 +70,7 @@ const kinesisTests = () => {
describe('when event lacks JSON payload', () => {
it('should ignore the event', () => {
const requestId = uuid()
invokeKinesisHandler(genKinesisEventWithoutJSON(), requestId, 0,
invokeKinesisHandler(genKinesisEventWithoutJSON(), requestId, { sampleDebugLogRate: 0 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -85,7 +85,7 @@ const kinesisTests = () => {
describe('when sampleDebugLogRate = 1', () => {
it('always sets debug-log-enabled to true', () => {
const requestId = uuid()
invokeKinesisHandler(genKinesisEvent(), requestId, 1,
invokeKinesisHandler(genKinesisEvent(), requestId, { sampleDebugLogRate: 1 },
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('true')
Expand All @@ -101,7 +101,7 @@ const kinesisTests = () => {
describe('when correlation ID is not provided in the event', () => {
it('sets it to the AWS Request ID', () => {
const requestId = uuid()
invokeKinesisHandler(genKinesisEvent(), requestId, 0,
invokeKinesisHandler(genKinesisEvent(), requestId, { sampleDebugLogRate: 0 },
x => {
// correlation IDs at the handler level
expect(x['x-correlation-id']).toBe(requestId)
Expand All @@ -119,7 +119,7 @@ const kinesisTests = () => {
describe('when call-chain-length is not provided in the event', () => {
it('sets it to 1', () => {
const requestId = uuid()
invokeKinesisHandler(genKinesisEvent(), requestId, 0,
invokeKinesisHandler(genKinesisEvent(), requestId, { sampleDebugLogRate: 0 },
x => {}, // n/a
record => {
const x = record.correlationIds.get()
Expand Down Expand Up @@ -148,7 +148,7 @@ const kinesisTests = () => {

const event = genKinesisEvent(correlationIds)
requestId = uuid()
invokeKinesisHandler(event, requestId, 0, x => {
invokeKinesisHandler(event, requestId, { sampleDebugLogRate: 0 }, x => {
handlerCorrelationIds = x
}, aRecord => {
record = aRecord
Expand Down Expand Up @@ -195,7 +195,7 @@ const kinesisTests = () => {
}

const event = genKinesisEvent(correlationIds)
invokeKinesisHandler(event, uuid(), 0,
invokeKinesisHandler(event, uuid(), { sampleDebugLogRate: 0 },
() => {},
aRecord => { record = aRecord },
done)
Expand All @@ -206,6 +206,36 @@ const kinesisTests = () => {
expect(x['call-chain-length']).toBe(2)
})
})

describe('when constructLoggerFn provided in the args', () => {
let record
let id
let logger
let constructLoggerFn

beforeEach((done) => {
id = uuid()
logger = { name: 'newLogger' }
constructLoggerFn = jest.fn(id => logger)
const correlationIds = {
'x-correlation-id': id,
'call-chain-length': 1
}

const event = genKinesisEvent(correlationIds)
invokeKinesisHandler(event, uuid(), { sampleDebugLogRate: 0, constructLoggerFn },
() => {},
aRecord => { record = aRecord },
done)
})

it('sets logger as a non-enumerable property', () => {
expect(record).toHaveProperty('logger')
expect(record.propertyIsEnumerable('logger')).toBe(false)
expect(record.logger).toBe(logger)
expect(constructLoggerFn.mock.calls.length).toBe(1)
})
})
}

describe('Correlation IDs middleware (Kinesis)', () => {
Expand Down