-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.js
154 lines (135 loc) · 3.16 KB
/
consumer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import AWS from 'aws-sdk';
const s3 = new AWS.S3();
export const handler = async (event) => {
const parsedRecords = parseRecords(event.Records);
const {
succesfullyProcessedRecords,
processingFailures,
} = filterParsedRecords(parsedRecords);
await updateS3Objects(
succesfullyProcessedRecords,
processingFailures,
);
const response = {
statusCode: 200,
body: `Succesfully processed ${succesfullyProcessedRecords.length} data entries`,
};
return response;
};
function parseRecords (records) {
return records.map(record => {
try {
const {
timestamp,
name,
value,
} = JSON.parse(record.body);
if (!timestamp || !name || !value) {
return {
failed: true,
body: record.body,
reason: "Incorrect metric format",
};
}
return {
timestamp,
name,
value,
};
} catch (error) {
return {
failed: true,
body: record.body,
reason: error.message,
};
}
});
}
function filterParsedRecords (records) {
return records.reduce((acc, cur) => {
if (cur.failed) {
acc.processingFailures.push(cur);
} else {
acc.succesfullyProcessedRecords.push(cur);
}
return acc;
}, {
succesfullyProcessedRecords: [],
processingFailures: [],
});
}
async function updateS3Objects(
succesfullyProcessedRecords,
processingFailures,
) {
if (succesfullyProcessedRecords.length > 0) {
// metrics other than memory usage are ignored for now
await appendS3Object(
process.env.BUCKET_NAME,
process.env.MEMORY_CONSUMPTION_METRICS_OBJECT_KEY,
succesfullyProcessedRecords
.filter(record => record.name === 'memory.usage')
.map(record => `${record.timestamp}: ${record.value}`)
.join('\n') + '\n',
);
}
if (processingFailures.length > 0) {
await appendS3Object(
process.env.BUCKET_NAME,
process.env.FAULTY_METRICS_OBJECT_KEY,
processingFailures
.map(failure => `${failure.reason}: ${failure.body}`)
.join('\n') + '\n',
);
}
}
async function appendS3Object(
bucketName,
objectKey,
dataString,
) {
const currentObjectData = await getObjectFromS3(
bucketName,
objectKey,
);
await putObjectToS3(
bucketName,
objectKey,
currentObjectData.Body.toString() + dataString,
);
}
async function getObjectFromS3(bucketName, key) {
return new Promise((resolve, reject) => {
const params = {
Bucket : bucketName,
Key : key,
}
s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
reject(err);
} else {
console.log(data);
resolve(data);
}
});
});
}
function putObjectToS3(bucketName, key, data){
return new Promise((resolve, reject) => {
const params = {
Bucket : bucketName,
Key : key,
Body : data,
}
s3.putObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
reject(err);
} else {
console.log(data);
resolve(data);
}
});
});
}