forked from getndazn/dazn-lambda-powertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (60 loc) · 1.74 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = '1'
const Kinesis = require('aws-sdk/clients/kinesis')
const client = new Kinesis()
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._putRecord = client.putRecord
client.putRecord = (...args) => {
return client.putRecordWithCorrelationIds(CorrelationIds, ...args)
}
client.putRecordWithCorrelationIds = (correlationIds, params, ...args) => {
const newData = addCorrelationIds(correlationIds, params.Data)
const extendedParams = {
...params,
Data: newData
}
return client._putRecord(extendedParams, ...args)
}
client._putRecords = client.putRecords
client.putRecords = (...args) => {
return client.putRecordsWithCorrelationIds(CorrelationIds, ...args)
}
client.putRecordsWithCorrelationIds = (correlationIds, params, ...args) => {
const newRecords = params.Records.map(record => {
const newData = addCorrelationIds(correlationIds, record.Data)
return {
...record,
Data: newData
}
})
const extendedParams = {
...params,
Records: newRecords
}
return client._putRecords(extendedParams, ...args)
}
module.exports = client