-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathindex.js
58 lines (48 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = '1'
const EventBridge = require('aws-sdk/clients/eventbridge')
const client = new EventBridge()
const Log = require('@dazn/lambda-powertools-logger')
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')
function tryJsonParse (data) {
if (typeof data !== 'string') {
return null
}
try {
return JSON.parse(data)
} catch (err) {
Log.warn('only JSON string data can be modified to insert correlation IDs', null, err)
return null
}
}
function addCorrelationIds (correlationIds, data) {
// only do this with JSON string data
const payload = tryJsonParse(data)
if (!payload) {
return data
}
const ids = correlationIds.get()
const newData = {
__context__: ids,
...payload
}
return JSON.stringify(newData)
}
client._putEvents = client.putEvents
client.putEvents = (...args) => {
return client.putEventsWithCorrelationIds(CorrelationIds, ...args)
}
client.putEventsWithCorrelationIds = (correlationIds, params, ...args) => {
const newEntries = params.Entries.map(entry => {
const newDetail = addCorrelationIds(correlationIds, entry.Detail)
return {
...entry,
Detail: newDetail
}
})
const extendedParams = {
...params,
Entries: newEntries
}
return client._putEvents(extendedParams, ...args)
}
module.exports = client