-
Notifications
You must be signed in to change notification settings - Fork 21
/
list-kinesis-streams.js
243 lines (211 loc) · 6.64 KB
/
list-kinesis-streams.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const _ = require("lodash");
const { getAWSSDK } = require("../lib/aws");
const Table = require("cli-table");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const Kinesis = require("../lib/kinesis");
const uuid = require("uuid");
const { getStreamsInRegion } = require("../lib/kinesis");
const { track } = require("../lib/analytics");
require("colors");
const FIVE_MIN_IN_SECONDS = 60 * 5;
const MAX_RECORDS_PER_SHARD_WRITE = 1000;
const MAX_RECORDS_PER_SHARD_READ = 2000;
const BYTES_INCOMING_PER_SHARD = 1024 * 1024;
const BYTES_OUTGOING_PER_SHARD = 2048 * 1024;
const formatFloat = num => {
return Number.parseFloat(num).toFixed(2);
};
class ListKinesisStreamsCommand extends Command {
async run() {
const { flags } = this.parse(ListKinesisStreamsCommand);
const { region, profile } = flags;
global.profile = profile;
checkVersion();
track("list-kinesis-streams", { region });
let streamsDescription = [];
if (region) {
this.log(`Checking Kinesis streams in [${region}]`);
streamsDescription = await this.getStreamsDescriptionFromRegion(region);
} else {
this.log("Checking Kinesis streams in all regions");
streamsDescription = await this.getStreamsDescriptionFromAllRegions();
}
this.show(streamsDescription);
}
async getStreamsDescriptionFromRegion(region) {
const streamNames = await getStreamsInRegion(region);
const usageStats = await this.getUsageMetrics(streamNames, region);
return this.describeStreams(streamNames, region, usageStats);
}
async getStreamsDescriptionFromAllRegions() {
const promises = Kinesis.regions.map(region =>
this.getStreamsDescriptionFromRegion(region)
);
const results = await Promise.all(promises);
return _.flatMap(results);
}
async describeStreams(streams, region, usageStats) {
const streamsDescription = [];
for (const stream of streams) {
streamsDescription.push(
this.describeStream(stream, region, usageStats[stream])
);
}
return Promise.all(streamsDescription);
}
async describeStream(streamName, region, streamStat) {
const AWS = getAWSSDK();
const Kinesis = new AWS.Kinesis({ region });
const resp = await Kinesis.describeStream({
StreamName: streamName
}).promise();
const numberOfShards = resp.StreamDescription.Shards.length;
const outgoingBytesForAllShardPerFiveMinute =
numberOfShards * BYTES_OUTGOING_PER_SHARD * FIVE_MIN_IN_SECONDS;
const outgoingRecordsForAllShardPerFiveMinute =
numberOfShards * MAX_RECORDS_PER_SHARD_READ * FIVE_MIN_IN_SECONDS;
const incomingBytesForAllShardsPerFiveMinute =
numberOfShards * BYTES_INCOMING_PER_SHARD * FIVE_MIN_IN_SECONDS;
const incomingRecordsForAllShardsPerFiveMinute =
numberOfShards * MAX_RECORDS_PER_SHARD_WRITE * FIVE_MIN_IN_SECONDS;
return {
streamName: streamName,
region: region,
status: resp.StreamDescription.StreamStatus,
shards: resp.StreamDescription.Shards,
outgoingMbPercentage: formatFloat(
(streamStat["GetRecords.BytesSum"] /
outgoingBytesForAllShardPerFiveMinute) *
100
),
outgoingRecordPercentage: formatFloat(
(streamStat["GetRecords.RecordsSum"] /
outgoingRecordsForAllShardPerFiveMinute) *
100
),
incomingMbPercentage: formatFloat(
(streamStat.IncomingBytesSum / incomingBytesForAllShardsPerFiveMinute) *
100
),
incomingRecordPercentage: formatFloat(
(streamStat.IncomingRecordsSum /
incomingRecordsForAllShardsPerFiveMinute) *
100
)
};
}
prepareMetricsQueries(streamNames) {
const metricNames = [
["IncomingBytes", "Sum"],
["IncomingRecords", "Sum"],
["GetRecords.Bytes", "Sum"],
["GetRecords.Records", "Sum"]
];
return _.flatMap(streamNames, streamName =>
metricNames.map(([metricName, stat]) => ({
Id:
metricName.replace(".", "").toLowerCase() +
uuid()
.replace(/-/g, "")
.substr(0, 5),
Label: `${streamName}:${metricName}:${stat}`,
MetricStat: {
Metric: {
Dimensions: [
{
Name: "StreamName",
Value: streamName
}
],
MetricName: metricName,
Namespace: "AWS/Kinesis"
},
Period: FIVE_MIN_IN_SECONDS,
Stat: stat
},
ReturnData: true
}))
);
}
async getUsageMetrics(streamNames, region) {
const AWS = getAWSSDK();
const CloudWatch = new AWS.CloudWatch({ region });
const fiveMinuteAgo = new Date();
fiveMinuteAgo.setMinutes(fiveMinuteAgo.getMinutes() - 5);
const queries = this.prepareMetricsQueries(streamNames);
// each GetMetricData request can send as many as 100 queries
const chunks = _.chunk(queries, 100);
const promises = chunks.map(async metricDataQueries => {
const resp = await CloudWatch.getMetricData({
StartTime: fiveMinuteAgo,
EndTime: new Date(),
MetricDataQueries: metricDataQueries
}).promise();
return resp.MetricDataResults.map(res => {
const [name, metricName] = res.Label.split(":");
const dataPoint = res.Values[0] || 0;
return {
name,
metricName: metricName + "Sum",
dataPoint
};
});
});
// array of {shardId, metricName, dataPoint}
const results = _.flatMap(await Promise.all(promises));
const byShardId = _.groupBy(results, res => res.name);
return _.mapValues(byShardId, shardMetrics => {
const kvp = shardMetrics.map(({ metricName, dataPoint }) => [
metricName,
dataPoint
]);
return _.fromPairs(kvp);
});
}
show(streamsDetails) {
const table = new Table({
head: [
"Stream name",
"Region",
"Shards",
"Read utilization",
"Write utilization",
"Status"
]
});
streamsDetails.forEach(x => {
table.push([
x.streamName,
x.region,
x.shards.length,
`${x.outgoingMbPercentage}% (MB)\n${x.outgoingRecordPercentage}% (Records)`,
`${x.incomingMbPercentage}% (MB)\n${x.incomingRecordPercentage}% (Records)`,
x.status
]);
});
this.log(`Total [${streamsDetails.length}] streams`);
this.log(table.toString());
this.log(`
Each shard can at most input ${BYTES_INCOMING_PER_SHARD /
1024} KB of data and up to ${MAX_RECORDS_PER_SHARD_WRITE} records per second.
Each shard can at most output ${BYTES_OUTGOING_PER_SHARD /
1024} KB of data and up to ${MAX_RECORDS_PER_SHARD_READ} records per second.
Read and write utilization are for the last ${FIVE_MIN_IN_SECONDS / 60} minutes.
`);
}
}
ListKinesisStreamsCommand.description = "Lists the Kinesis streams";
ListKinesisStreamsCommand.flags = {
region: flags.string({
char: "r",
description: "AWS region, e.g. us-east-1",
required: false
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
})
};
module.exports = ListKinesisStreamsCommand;