Skip to content

Commit

Permalink
feat: support eventbridge in correlation ids middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
theburningmonk committed Jan 1, 2020
1 parent 4dcffad commit 936f3ee
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "0",
"id": "2641b6fa-bb28-5a3a-3478-843d57e92d24",
"detail-type": "Step Functions Execution Status Change",
"source": "aws.states",
"account": "123456789",
"time": "2020-01-01T00:58:02Z",
"region": "us-east-1",
"resources": [
"arn:aws:states:us-east-1:123456789:execution:wait-and-error:41d41285-dbef-a1e9-4295-debe4ae29244"
],
"detail": {
"startDate": 1577840282671,
"name": "41d41285-dbef-a1e9-4295-debe4ae29244",
"executionArn": "arn:aws:states:us-east-1:123456789:execution:wait-and-error:41d41285-dbef-a1e9-4295-debe4ae29244",
"input": "{\n \"Comment\": \"Insert your JSON here\"\n}",
"status": "RUNNING",
"stateMachineArn": "arn:aws:states:us-east-1:123456789:stateMachine:wait-and-error",
"stopDate": null,
"output": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const _ = require('lodash')
const { standardTests } = require('./lib')

global.console.log = jest.fn()

const eventbridge = require('./event-templates/eventbridge.json')
const genEventBridgeEvent = (correlationIDs = {}) => {
const event = _.cloneDeep(eventbridge)
event.detail['__context__'] = correlationIDs

return event
}

describe('Correlation IDs middleware (EventBridge)', () => {
standardTests(genEventBridgeEvent)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')
const consts = require('../consts')

function isMatch (event) {
if (!event.hasOwnProperty('detail') ||
!event.hasOwnProperty('detail-type') ||
!event.hasOwnProperty('source')) {
return false
}

return true
}

function captureCorrelationIds ({ detail }, { awsRequestId }, sampleDebugLogRate) {
const correlationIds = detail['__context__'] || {}
correlationIds.awsRequestId = awsRequestId
if (!correlationIds[consts.X_CORRELATION_ID]) {
correlationIds[consts.X_CORRELATION_ID] = awsRequestId
}

if (!correlationIds[consts.DEBUG_LOG_ENABLED]) {
correlationIds[consts.DEBUG_LOG_ENABLED] =
Math.random() < sampleDebugLogRate ? 'true' : 'false'
}

correlationIds[consts.CALL_CHAIN_LENGTH] =
(correlationIds[consts.CALL_CHAIN_LENGTH] || 0) + 1

CorrelationIds.replaceAllWith(correlationIds)
}

module.exports = {
isMatch,
captureCorrelationIds
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const eventSources = [
require('./event-sources/kinesis'),
require('./event-sources/dynamodb'),
require('./event-sources/firehose'),
require('./event-sources/eventbridge'),
require('./event-sources/direct-invoke')
]

Expand Down

0 comments on commit 936f3ee

Please sign in to comment.